diff --git a/lib/iso9660/iso9660_fs.c b/lib/iso9660/iso9660_fs.c index d6f457c6..38490051 100644 --- a/lib/iso9660/iso9660_fs.c +++ b/lib/iso9660/iso9660_fs.c @@ -591,11 +591,18 @@ iso9660_ifs_fuzzy_read_superblock (iso9660_t *p_iso, return false; } + /* The frame isn't NUL-terminated when the whole of it is read, + so look for the identifier only within the frame. */ q = memchr(frame, 'C', p_iso->i_framesize); for ( p=q; p && p < frame + p_iso->i_framesize ; p=q+1 ) { q = memchr(p, 'C', p_iso->i_framesize - (p - frame)); - if ( !q || (pvd = strstr(q, ISO_STANDARD_ID)) ) + if (!q) break; + if (q + strlen(ISO_STANDARD_ID) <= frame + p_iso->i_framesize + && 0 == memcmp(q, ISO_STANDARD_ID, strlen(ISO_STANDARD_ID))) { + pvd = q; + break; + } } if (pvd) { @@ -752,6 +759,8 @@ iso9660_iso_seek_read (const iso9660_t *p_iso, void *ptr, lsn_t start, static bool iso9660_check_dir_block_end(iso9660_dir_t *p_iso9660_dir, unsigned *offset) { + unsigned int i_len; + if (!iso9660_get_dir_len(p_iso9660_dir)) { /* @@ -766,8 +775,14 @@ iso9660_check_dir_block_end(iso9660_dir_t *p_iso9660_dir, unsigned *offset) return true; } - if ((*offset + iso9660_get_dir_len(p_iso9660_dir) - 1) / ISO_BLOCKSIZE - != *offset / ISO_BLOCKSIZE) + /* A record shorter than its fixed part is malformed, and callers + still read the fixed part: check that it fits in the block too, + or reading it at the end of the last block goes past the buffer. */ + i_len = iso9660_get_dir_len(p_iso9660_dir); + if (i_len < sizeof(iso9660_dir_t)) + i_len = sizeof(iso9660_dir_t); + + if ((*offset + i_len - 1) / ISO_BLOCKSIZE != *offset / ISO_BLOCKSIZE) { /* Directory record spans over block limit. @@ -1174,10 +1189,12 @@ _fs_stat_traverse (const CdIo_t *p_cdio, const iso9660_stat_t *_root, p_stat = calloc(1, len); cdio_assert (p_stat != NULL); memcpy(p_stat, _root, len); - p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max); - cdio_assert (p_stat->rr.psz_symlink != NULL); - memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink, - p_stat->rr.i_symlink_max); + if (_root->rr.psz_symlink != NULL) { + p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max); + cdio_assert (p_stat->rr.psz_symlink != NULL); + memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink, + p_stat->rr.i_symlink_max); + } return p_stat; } @@ -1289,10 +1306,12 @@ _fs_iso_stat_traverse (iso9660_t *p_iso, const iso9660_stat_t *_root, p_stat = calloc(1, len); cdio_assert (p_stat != NULL); memcpy(p_stat, _root, len); - p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max); - cdio_assert (p_stat->rr.psz_symlink != NULL); - memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink, - p_stat->rr.i_symlink_max); + if (_root->rr.psz_symlink != NULL) { + p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max); + cdio_assert (p_stat->rr.psz_symlink != NULL); + memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink, + p_stat->rr.i_symlink_max); + } return p_stat; } @@ -1772,10 +1791,16 @@ find_lsn_recurse (void *p_image, iso9660_readdir_t iso9660_readdir, CdioList_t *visited) { CdioISO9660FileList_t *entlist = iso9660_readdir (p_image, psz_path); - CdioISO9660DirList_t *dirlist = iso9660_filelist_new(); + CdioISO9660DirList_t *dirlist; CdioListNode_t *entnode; - cdio_assert (entlist != NULL); + /* A directory that can't be read doesn't hold the LSN: go on with + the others. */ + if (entlist == NULL) { + cdio_warn("Can't read directory %s", psz_path); + return NULL; + } + dirlist = iso9660_filelist_new(); /* iterate over each entry in the directory */ @@ -1802,6 +1827,8 @@ find_lsn_recurse (void *p_image, iso9660_readdir_t iso9660_readdir, return NULL; } memcpy(ret_stat, statbuf, len2); + /* ret_stat now owns the symlink: don't free it with the list. */ + statbuf->rr.psz_symlink = NULL; iso9660_filelist_free (entlist); iso9660_dirlist_free(dirlist); return ret_stat; diff --git a/lib/iso9660/rock.c b/lib/iso9660/rock.c index 6b265168..3952033c 100644 --- a/lib/iso9660/rock.c +++ b/lib/iso9660/rock.c @@ -62,25 +62,30 @@ _iso9660_dd_find_lsn(void* p_image, lsn_t i_lsn); static bool realloc_symlink(/*in/out*/ iso9660_stat_t *p_stat, uint8_t i_grow) { - if (!p_stat->rr.i_symlink) { - const uint16_t i_max = 2*i_grow+1; - p_stat->rr.psz_symlink = (char *) calloc(1, i_max); - p_stat->rr.i_symlink_max = i_max; - return (NULL != p_stat->rr.psz_symlink); - } else { - unsigned int i_needed = p_stat->rr.i_symlink + i_grow * 2; // Allocate enough space for growth and additional buffer - if ( i_needed <= p_stat->rr.i_symlink_max) - return true; - else { - char * psz_newsymlink = (char *) calloc(1, 2*i_needed); - if (!psz_newsymlink) return false; - p_stat->rr.i_symlink_max = 2*i_needed; - memcpy(psz_newsymlink, p_stat->rr.psz_symlink, p_stat->rr.i_symlink); - free(p_stat->rr.psz_symlink); - p_stat->rr.psz_symlink = psz_newsymlink; - return true; - } + /* Allocate enough space for growth, an additional buffer and the + final NUL. */ + const unsigned int i_needed = p_stat->rr.i_symlink + i_grow * 2 + 1; + char *psz_newsymlink; + + if (p_stat->rr.psz_symlink != NULL + && i_needed <= (unsigned int) p_stat->rr.i_symlink_max) + return true; + + psz_newsymlink = (char *) calloc(1, 2*i_needed); + if (!psz_newsymlink) return false; + /* The length can be ahead of the buffer: an SL record that is + continued counts the separator before it allocates it, and the + buffer can be missing altogether when no component was stored. */ + if (p_stat->rr.psz_symlink != NULL) { + const int i_copy = p_stat->rr.i_symlink < p_stat->rr.i_symlink_max + ? p_stat->rr.i_symlink : p_stat->rr.i_symlink_max; + if (i_copy > 0) + memcpy(psz_newsymlink, p_stat->rr.psz_symlink, i_copy); + free(p_stat->rr.psz_symlink); } + p_stat->rr.psz_symlink = psz_newsymlink; + p_stat->rr.i_symlink_max = 2*i_needed; + return true; } /* These functions are designed to read the system areas of a directory record @@ -397,6 +402,8 @@ get_rock_ridge_filename(iso9660_dir_t * p_iso9660_dir, cdio_warn("Could not get Rock Ridge deep directory child"); break; } + /* p_stat takes the target's symlink below: free its own. */ + free(p_stat->rr.psz_symlink); memcpy(p_stat, target, sizeof(iso9660_stat_t)); /* Prevent the symlink from being freed on the duplicated struct */ target->rr.psz_symlink = NULL; diff --git a/test/testisorr.c b/test/testisorr.c index 6d16a40e..ddeed9ff 100644 --- a/test/testisorr.c +++ b/test/testisorr.c @@ -337,6 +337,325 @@ check_malformed_record(void) return result; } +/* Opens a new temporary file for writing. psz_tmp receives its name. */ +static FILE * +open_temp(char *psz_tmp, const char *psz_template) +{ +#ifdef HAVE_MKSTEMP + int fd; + FILE *fp; + + strcpy(psz_tmp, psz_template); + fd = mkstemp(psz_tmp); + if (fd < 0) + return NULL; + fp = fdopen(fd, "wb"); + if (!fp) + close(fd); + return fp; +#else + (void) psz_template; + if (!tmpnam(psz_tmp)) + return NULL; + return fopen(psz_tmp, "wb"); +#endif +} + +#define TEST_IMAGE_BLOCKS 40 +#define TEST_ROOT_LSN 20 + +/* Writes a 40-block image whose root directory, at block 20, is dir. */ +static int +write_test_image(char *psz_tmp, const char *psz_template, + const uint8_t dir[ISO_BLOCKSIZE]) +{ + uint8_t pvd[ISO_BLOCKSIZE] = {0}; + FILE *fp = open_temp(psz_tmp, psz_template); + + if (!fp) + return 1; + + pvd[0] = 1; + memcpy(&pvd[1], "CD001", 5); + pvd[6] = 1; + put_733(&pvd[80], TEST_IMAGE_BLOCKS); + pvd[128] = 0; + pvd[129] = 8; + pvd[156] = 34; + put_733(&pvd[158], TEST_ROOT_LSN); + put_733(&pvd[166], ISO_BLOCKSIZE); + pvd[181] = ISO_DIRECTORY; + pvd[188] = 1; + + if (fseek(fp, TEST_IMAGE_BLOCKS * ISO_BLOCKSIZE - 1, SEEK_SET) != 0 + || fputc(0, fp) == EOF + || fseek(fp, 16 * ISO_BLOCKSIZE, SEEK_SET) != 0 + || fwrite(pvd, 1, sizeof(pvd), fp) != sizeof(pvd)) + goto fail; + memset(pvd, 0, sizeof(pvd)); + pvd[0] = 255; + memcpy(&pvd[1], "CD001", 5); + pvd[6] = 1; + if (fwrite(pvd, 1, sizeof(pvd), fp) != sizeof(pvd) + || fseek(fp, TEST_ROOT_LSN * ISO_BLOCKSIZE, SEEK_SET) != 0 + || fwrite(dir, 1, ISO_BLOCKSIZE, fp) != ISO_BLOCKSIZE) + goto fail; + return fclose(fp) != 0; + +fail: + fclose(fp); + return 1; +} + +/* Adds a directory record with an optional System Use area. */ +static unsigned +add_su_record(uint8_t *dir, unsigned offset, uint32_t extent, uint32_t size, + uint8_t flags, const char *name, unsigned name_len, + const uint8_t *su, unsigned su_len) +{ + uint8_t *record = dir + offset; + unsigned length = 33 + name_len; + + if (length % 2) + length++; + memset(record, 0, length + su_len); + record[0] = length + su_len; + put_733(record + 2, extent); + put_733(record + 10, size); + record[25] = flags; + record[28] = 1; + record[32] = name_len; + memcpy(record + 33, name, name_len); + if (su_len) + memcpy(record + length, su, su_len); + return offset + length + su_len; +} + +/* Adds the "." and ".." records of the root directory. */ +static unsigned +add_root_dots(uint8_t *dir) +{ + unsigned offset = add_su_record(dir, 0, TEST_ROOT_LSN, ISO_BLOCKSIZE, + ISO_DIRECTORY, "\0", 1, NULL, 0); + return add_su_record(dir, offset, TEST_ROOT_LSN, ISO_BLOCKSIZE, + ISO_DIRECTORY, "\1", 1, NULL, 0); +} + +/* A Rock Ridge SL entry for the one-component link "x". */ +static const uint8_t sl_x[] = { 'S', 'L', 8, 1, 0, 0, 1, 'x' }; + +/* The stat that iso9660_ifs_find_lsn() returns owns its symlink. */ +static int +check_find_lsn_symlink(void) +{ + uint8_t dir[ISO_BLOCKSIZE] = {0}; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + iso9660_stat_t *p_stat = NULL; + int ret = 1; + + add_su_record(dir, add_root_dots(dir), 31, 0, 0, "L;1", 3, + sl_x, sizeof(sl_x)); + if (write_test_image(psz_tmp, "libcdio-rr-lsn-XXXXXX", dir) != 0) + goto out; + + p_iso = iso9660_open_ext(psz_tmp, ISO_EXTENSION_ALL); + if (!p_iso) + goto out; + p_stat = iso9660_ifs_find_lsn(p_iso, 31); + if (p_stat && p_stat->rr.psz_symlink + && 0 == strcmp(p_stat->rr.psz_symlink, "x")) + ret = 0; + +out: + if (p_stat) + iso9660_stat_free(p_stat); + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + +/* A Rock Ridge CL entry takes the symlink of the entry it points to. */ +static int +check_child_link_symlink(void) +{ + uint8_t dir[ISO_BLOCKSIZE] = {0}; + uint8_t cl[12] = { 'C', 'L', 12, 1 }; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + CdioISO9660FileList_t *entries = NULL; + CdioListNode_t *node; + unsigned offset; + int links = 0; + int ret = 1; + + put_733(cl + 4, 31); + offset = add_su_record(dir, add_root_dots(dir), 30, 0, 0, "C;1", 3, + cl, sizeof(cl)); + add_su_record(dir, offset, 31, 0, 0, "L;1", 3, sl_x, sizeof(sl_x)); + if (write_test_image(psz_tmp, "libcdio-rr-cl-XXXXXX", dir) != 0) + goto out; + + p_iso = iso9660_open_ext(psz_tmp, ISO_EXTENSION_ALL); + if (!p_iso) + goto out; + entries = iso9660_ifs_readdir(p_iso, "/"); + if (!entries) + goto out; + _CDIO_LIST_FOREACH (node, entries) { + iso9660_stat_t *p_stat = _cdio_list_node_data(node); + if (p_stat->rr.psz_symlink && 0 == strcmp(p_stat->rr.psz_symlink, "x")) + links++; + } + /* Both the link and the entry that points to it. */ + if (links == 2) + ret = 0; + +out: + if (entries) + iso9660_filelist_free(entries); + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + +/* An SL entry whose only component has an unknown flag stores nothing. */ +static int +check_unknown_symlink_component(void) +{ + static const uint8_t sl[] = { 'S', 'L', 7, 1, 1, 0x10, 0 }; + uint8_t dir[ISO_BLOCKSIZE] = {0}; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + CdioISO9660FileList_t *entries = NULL; + int ret = 1; + + add_su_record(dir, add_root_dots(dir), 30, 0, 0, "A;1", 3, sl, sizeof(sl)); + if (write_test_image(psz_tmp, "libcdio-rr-sl-XXXXXX", dir) != 0) + goto out; + + p_iso = iso9660_open_ext(psz_tmp, ISO_EXTENSION_ALL); + if (!p_iso) + goto out; + entries = iso9660_ifs_readdir(p_iso, "/"); + if (entries) + ret = 0; + +out: + if (entries) + iso9660_filelist_free(entries); + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + +/* Looking for an LSN goes past a directory that can't be read. */ +static int +check_find_lsn_unreadable_directory(void) +{ + uint8_t dir[ISO_BLOCKSIZE] = {0}; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + iso9660_stat_t *p_stat = NULL; + unsigned offset; + int ret = 1; + + /* A directory of length 0, then a file after it. */ + offset = add_su_record(dir, add_root_dots(dir), 19, 0, ISO_DIRECTORY, + "D", 1, NULL, 0); + add_su_record(dir, offset, 32, 0, 0, "F;1", 3, NULL, 0); + if (write_test_image(psz_tmp, "libcdio-rr-dir-XXXXXX", dir) != 0) + goto out; + + p_iso = iso9660_open_ext(psz_tmp, ISO_EXTENSION_ALL); + if (!p_iso) + goto out; + /* Not on the image: every directory is searched. */ + p_stat = iso9660_ifs_find_lsn(p_iso, 35); + if (p_stat) + goto out; + p_stat = iso9660_ifs_find_lsn(p_iso, 32); + if (p_stat) + ret = 0; + +out: + if (p_stat) + iso9660_stat_free(p_stat); + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + +/* Records shorter than their fixed part at the end of the last block of + a directory aren't read past the block. Only a memory checker sees + the difference. */ +static int +check_short_record_at_block_end(void) +{ + uint8_t dir[ISO_BLOCKSIZE] = {0}; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + CdioISO9660FileList_t *entries = NULL; + unsigned offset = add_root_dots(dir); + int ret = 1; + + memset(dir + offset, 1, ISO_BLOCKSIZE - offset); + if (write_test_image(psz_tmp, "libcdio-rr-short-XXXXXX", dir) != 0) + goto out; + + p_iso = iso9660_open_ext(psz_tmp, ISO_EXTENSION_ALL); + if (!p_iso) + goto out; + entries = iso9660_ifs_readdir(p_iso, "/"); + ret = 0; + +out: + if (entries) + iso9660_filelist_free(entries); + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + +/* The fuzzy search for a PVD doesn't read past a frame that holds no + NUL. Only a memory checker sees the difference. */ +static int +check_fuzzy_frame_without_nul(void) +{ + uint8_t frame[CDIO_CD_FRAMESIZE_RAW]; + char psz_tmp[64 + L_tmpnam]; + iso9660_t *p_iso = NULL; + FILE *fp = open_temp(psz_tmp, "libcdio-rr-fuzzy-XXXXXX"); + int i; + int ret = 1; + + if (!fp) + return 1; + memset(frame, 'C', sizeof(frame)); + for (i = 0; i < 24; i++) + if (fwrite(frame, 1, sizeof(frame), fp) != sizeof(frame)) { + fclose(fp); + goto out; + } + if (fclose(fp) != 0) + goto out; + + p_iso = iso9660_open_fuzzy_ext(psz_tmp, ISO_EXTENSION_ALL, 20); + if (!p_iso) + ret = 0; + +out: + if (p_iso) + iso9660_close(p_iso); + remove(psz_tmp); + return ret; +} + int main(int argc, const char *argv[]) { @@ -401,5 +720,35 @@ main(int argc, const char *argv[]) return 6; } + if (check_find_lsn_symlink() != 0) { + fprintf(stderr, "Symlink of a stat found by LSN is wrong\n"); + return 7; + } + + if (check_child_link_symlink() != 0) { + fprintf(stderr, "Symlink of a Rock Ridge child link is wrong\n"); + return 8; + } + + if (check_unknown_symlink_component() != 0) { + fprintf(stderr, "Unknown symlink component was not handled\n"); + return 9; + } + + if (check_find_lsn_unreadable_directory() != 0) { + fprintf(stderr, "Unreadable directory stopped the LSN search\n"); + return 10; + } + + if (check_short_record_at_block_end() != 0) { + fprintf(stderr, "Short records at the end of a block were not read\n"); + return 11; + } + + if (check_fuzzy_frame_without_nul() != 0) { + fprintf(stderr, "Fuzzy open found a PVD in a frame without one\n"); + return 12; + } + return 0; }