limine icon indicating copy to clipboard operation
limine copied to clipboard

fs/iso9660: Incomplete multi-extent files are accepted as truncated data

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

On trunk 6f618ed039a749538220ee0b3dc0ab98ea6be773, iso9660_open() accepts a multi-extent file whose last directory record still has the multi-extent flag set even though no continuation record exists.

The extent-counting loop breaks when iso9660_next_entry() returns NULL, but the code then constructs a file handle from the collected prefix. A malformed KERNEL;1 record with a four-byte first extent and no required continuation is therefore returned as a valid four-byte file. A continuation with a different file identifier has the same result.

Expected behavior

If the final collected record still promises another extent, reject the file instead of exposing a silently truncated prefix. Valid single-extent and completed multi-extent files must remain readable.

Reproduction

Save the following as /tmp/iso9660-regression.c, then compile it from a bootstrapped Limine source tree. It includes the actual ISO9660 implementation and supplies only allocation, volume-read, and panic stubs. The in-memory ISO fixture has a primary volume descriptor and root directory; no disk or firmware is required.

Regression harness
#include <stdlib.h>

#include "common/fs/iso9660.s2.c"

bool case_insensitive_fopen = false;
static uint8_t image[64 * ISO9660_SECTOR_SIZE];

void *ext_mem_alloc(uint64_t count) {
    size_t size = (count + ISO9660_SECTOR_SIZE - 1)
                & ~(ISO9660_SECTOR_SIZE - 1);
    void *ret = aligned_alloc(ISO9660_SECTOR_SIZE, size);
    if (ret == NULL) {
        abort();
    }
    return ret;
}

void *ext_mem_alloc_counted(uint64_t count, uint64_t elem_size) {
    return ext_mem_alloc(count * elem_size);
}

void pmm_free(void *ptr, uint64_t size) {
    (void)size;
    free(ptr);
}

bool volume_read(struct volume *vol, void *buffer, uint64_t loc,
                 uint64_t count) {
    (void)vol;
    if (loc > sizeof(image) || count > sizeof(image) - loc) {
        return false;
    }
    memcpy(buffer, image + loc, count);
    return true;
}

noreturn void panic(bool allow_menu, const char *format, ...) {
    (void)allow_menu;
    (void)format;
    abort();
}

static struct iso9660_directory_entry *make_entry(uint8_t *at,
                                                   uint32_t lba,
                                                   uint8_t flags) {
    struct iso9660_directory_entry *entry = (void *)at;
    entry->length = sizeof(*entry) + 9;
    entry->extent.little = lba;
    entry->extent_size.little = 4;
    entry->flags = flags;
    entry->filename_size = 8;
    memcpy(entry->name, "KERNEL;1", 8);
    return entry;
}

int main(int argc, char **argv) {
    bool complete = argc == 2 && strcmp(argv[1], "complete") == 0;
    bool mismatch = argc == 2 && strcmp(argv[1], "mismatch") == 0;
    bool single = argc == 2 && strcmp(argv[1], "single") == 0;
    struct iso9660_primary_volume *pvd =
        (void *)(image + ISO9660_FIRST_VOLUME_DESCRIPTOR
                         * ISO9660_SECTOR_SIZE);
    pvd->volume_descriptor.type = ISO9660_VDT_PRIMARY;
    memcpy(pvd->volume_descriptor.identifier, "CD001", 5);
    pvd->volume_descriptor.version = 1;
    pvd->root.extent.little = 20;
    pvd->root.extent_size.little = ISO9660_SECTOR_SIZE;

    uint8_t *root = image + 20 * ISO9660_SECTOR_SIZE;
    struct iso9660_directory_entry *first =
        make_entry(root, 30, single ? 0 : ISO9660_FLAG_MULTI_EXTENT);
    if (complete || mismatch) {
        struct iso9660_directory_entry *next =
            make_entry(root + first->length, 31, 0);
        if (mismatch) {
            next->name[0] = 'X';
        }
    }
    memcpy(image + 30 * ISO9660_SECTOR_SIZE, "ABCD", 4);
    memcpy(image + 31 * ISO9660_SECTOR_SIZE, "EFGH", 4);

    struct volume volume = {0};
    struct file_handle *file = iso9660_open(&volume, "KERNEL");
    if (!complete && !single) {
        return file == NULL ? 0 : 1;
    }
    uint64_t expected_size = complete ? 8 : 4;
    if (file == NULL || file->size != expected_size) {
        return 2;
    }
    char data[8];
    if (file->read(file, data, 0, expected_size) != expected_size
     || memcmp(data, "ABCDEFGH", expected_size) != 0) {
        return 3;
    }
    file->close(file);
    pmm_free(file, sizeof(*file));
    return 0;
}
cc -std=gnu11 -O0 -Wall -Wextra -Werror -fno-builtin \
  -ffunction-sections -fdata-sections -DBIOS -I. -Icommon \
  /tmp/iso9660-regression.c -Wl,--gc-sections -o /tmp/iso9660-regression

/tmp/iso9660-regression             # incomplete continuation
/tmp/iso9660-regression mismatch    # wrong continuation identifier
/tmp/iso9660-regression complete    # valid two-extent control
/tmp/iso9660-regression single      # valid single-extent control

On the affected tree, the first two commands exit 1 because the malformed files are accepted. Both controls exit 0. With the prepared check, all four commands exit 0; the malformed files are rejected and both valid files remain readable. The same four cases also pass with AddressSanitizer and UndefinedBehaviorSanitizer enabled.

The prepared fix is pushed; I will open the corresponding PR immediately.

pro-utkarshM avatar Sep 15 '26 17:09 pro-utkarshM

Fixed by #666

Mintsuki avatar Sep 26 '26 21:09 Mintsuki