limine icon indicating copy to clipboard operation
limine copied to clipboard

host/limine: GPT conversion does not require a primary volume descriptor

Open pro-utkarshM opened this issue 2 months ago • 1 comments

On trunk 95c91326ad8b9f698f2c7b7ae8b65bdb1437207a, bios_install() decides that a GPT device is an ISO hybrid by checking only the ISO descriptor identifier. It does not require a primary volume descriptor (PVD), validate its version, or stop at the volume descriptor terminator before selecting GPT-to-MBR conversion.

That can select a destructive partition-table conversion for non-ISO media. The conversion's partition checks still apply, but they do not establish that the medium contains an ISO filesystem.

The existing iso9660_find_PVD() filesystem recogniser already has the appropriate recognition policy: scan at most 256 descriptors, require a type-1/version-1 PVD, and stop on an invalid identifier, a terminator, or read failure. The host probe should follow that policy. Requiring the PVD at sector 16 alone would reject valid boot-record-first media. Versions must be checked per descriptor type; an enhanced descriptor can have version 2.

Expected: failed ISO probing leaves the ordinary GPT installation path available. Valid ISO hybrids continue to convert as before.

Prepared fix: 14bea115. I am opening the corresponding PR immediately.

Validation of the prepared fix: 45 read-only probe checks (15 cases across 512/2048/4096-byte cache blocks), including valid descriptor ordering, invalid primary versions, terminators, truncation, scan limits, and reads after failed probing. A genuine xorriso hybrid still converts and installs; an ordinary BIOS/GPT fixture installs while preserving its partition table.

Read-only probe regression harness for the prepared fix

Save as /tmp/probe-test.c. From a BIOS-enabled built source tree:

cc -std=c99 -D_FILE_OFFSET_BITS=64 -O2 -Wall -Wextra -Werror \
  -I. -Ibin /tmp/probe-test.c -o /tmp/probe-test
/tmp/probe-test

The harness calls only the ISO recogniser and raw reads on temporary files. It does not call the installer or construct partition tables.

#define main limine_main
#include "host/limine.c"
#undef main
#include <assert.h>

static uint8_t image[274 * 2048];
static unsigned int checks;

static void descriptor(size_t lba, uint8_t type, uint8_t version) {
    uint8_t *desc = image + lba * 2048;
    desc[0] = type;
    memcpy(desc + 1, "CD001", 5);
    desc[6] = version;
}

static void check(const char *name, size_t bytes, bool expected) {
    for (size_t size = 512; size <= 4096; size *= 2) {
        if (size == 1024) {
            continue;
        }
        device = tmpfile();
        assert(device != NULL);
        assert(fwrite(image, 1, bytes, device) == bytes);
        assert(fflush(device) == 0);
        cache = malloc(size);
        assert(cache != NULL);
        block_size = size;
        cache_state = CACHE_CLEAN;
        cached_block = UINT64_MAX;
        assert(iso9660_has_pvd() == expected);
        /* A failed speculative read must not prevent later GPT reads. */
        uint8_t byte;
        assert(device_read_raw(&byte, 0, 1));
        assert(byte == image[0]);
        free(cache);
        cache = NULL;
        assert(fclose(device) == 0);
        device = NULL;
        checks++;
    }
    printf("PASS %s\n", name);
}

int main(void) {
    program_name = "probe-test";
    quiet = true;
    check("ordinary non-ISO", sizeof(image), false);
    memcpy(image + 16 * 2048 + 1, "CD001", 5);
    check("marker without primary", sizeof(image), false);
    descriptor(16, 1, 1);
    check("primary v1", sizeof(image), true);
    descriptor(16, 1, 0);
    check("primary v0", sizeof(image), false);
    descriptor(16, 1, 2);
    check("primary v2", sizeof(image), false);
    descriptor(16, 255, 1);
    descriptor(17, 1, 1);
    check("terminator before primary", sizeof(image), false);
    descriptor(16, 0, 1);
    check("boot record before primary", sizeof(image), true);
    descriptor(16, 2, 2);
    check("enhanced descriptor before primary", sizeof(image), true);
    image[16 * 2048 + 1] = 'X';
    check("bad identifier before primary", sizeof(image), false);
    memset(image, 0, sizeof(image));
    check("EOF before first descriptor", 16 * 2048, false);
    descriptor(16, 1, 1);
    check("truncated primary", 16 * 2048 + 512, false);
    descriptor(16, 0, 1);
    check("EOF after boot record", 17 * 2048, false);
    for (size_t lba = 16; lba < 274; lba++) {
        descriptor(lba, 0, 1);
    }
    check("bounded scan without primary", sizeof(image), false);
    descriptor(272, 1, 1);
    check("primary beyond scan bound", sizeof(image), false);
    descriptor(271, 1, 1);
    check("primary at scan bound", sizeof(image), true);
    printf("%u checks passed\n", checks);
    return 0;
}

pro-utkarshM avatar Oct 01 '26 03:10 pro-utkarshM

Fixed by #677

Mintsuki avatar Oct 03 '26 22:10 Mintsuki