Repository navigation
iso9660: fix memory errors and an abort on crafted Rock Ridge images - #120
Merged
Merged
Conversation
find_lsn_recurse() copied the iso9660_stat_t of the entry it found, pointer to the Rock Ridge symlink included, then freed the directory list and with it the symlink. The stat it returned held a freed pointer: - iso9660_ifs_find_lsn() and iso9660_fs_find_lsn() returned it, and iso9660_stat_free() freed the symlink a second time. Any image whose file at that LSN is a symbolic link does it. - A Rock Ridge CL entry looks up the directory it points to with the same function while the directory holding the CL entry is read. The entry took the freed symlink, so reading a directory whose CL entry points to a symbolic link read freed memory, and freeing the list freed it again. Hand the symlink over to the returned stat instead. The CL entry also lost its own symlink when the target's stat was copied over it: free it first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
realloc_symlink() allocated a new buffer only while the symlink was empty, and otherwise copied the symlink from the existing one. An SL entry that is continued counts the separator before the next record stores it, so the length can be 1 with no buffer at all: an SL entry with the continue flag whose only component has a flag that isn't implemented made realloc_symlink() copy a byte from NULL. And with an empty symlink and a buffer, it allocated another buffer and leaked the first one. Decide on the buffer itself, copy no more than it holds, and keep room for the final NUL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
find_lsn_recurse() asserted that every directory can be read, so iso9660_ifs_find_lsn() and iso9660_fs_find_lsn() aborted the program on an image with a directory of length 0, an extent past the end, a path deeper than MAX_ISO9660_PATH_DEPTH or any other directory that iso9660_ifs_readdir() rejects. Because a Rock Ridge CL entry looks up its target in the same way, reading a directory with a CL entry was enough. Warn and go on with the other directories. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The loops over directory records read the fixed part of each record, 33 bytes, even when _iso9660_dir_to_statbuf() rejects it for a length below that: file_flags decides whether a multi-extent file goes on. iso9660_check_dir_block_end() only checked that the record's own length stays within the block, so a record with a length from 1 to 32 in the last bytes of a directory's last block made them read up to 24 bytes past the directory buffer. Treat a record as long as its fixed part at least when checking for the end of the block. A short record elsewhere is still read and rejected as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
iso9660_ifs_fuzzy_read_superblock() looked for "CD001" with strstr() from each 'C' in a frame. A frame of 2352 bytes fills the buffer, so when it held no NUL, as on an image of any other data, strstr() read past the buffer on the stack. Compare the 5 bytes at each 'C' that has room for them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_fs_stat_traverse() and _fs_iso_stat_traverse() copied the root's symlink buffer into the stat they return for the root even when it had none, passing NULL to memcpy() with a length of 0, which is undefined, and asserting on calloc() of 0 bytes, which may return NULL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
testisorr builds small images to check that: - iso9660_ifs_find_lsn() returns a stat whose symlink can be read and freed; - a Rock Ridge CL entry and the symbolic link it points to both read the link; - an SL entry whose only component has an unknown flag can be read; - looking for an LSN goes past a directory of length 0, both for an LSN that isn't on the image and for one in a later file; - short records at the end of a directory's block, and the fuzzy search on frames full of 'C', can be read. The last two only show a difference under a memory checker such as valgrind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
LGTM. @pbatard I think this falls into an area you are interested in. Do you have any concerns before merging? |
Contributor
|
No concerns for me. These look okay at a cursory glance. |
Contributor
Thanks, @pbatard. We can always come back and change things if something comes up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes for memory errors and a crash when reading crafted ISO 9660 images, most of them in the Rock Ridge code. Each one was found with a small crafted image under AddressSanitizer or valgrind, and each one has a check in
testisorr.What went wrong
find_lsn_recurse()copied theiso9660_stat_tof the entry it found, including the pointer to its Rock Ridge symlink, and then freed the directory list, which freed that symlink too. As a result:iso9660_ifs_find_lsn()andiso9660_fs_find_lsn()returned a stat holding a freed pointer, andiso9660_stat_free()freed it a second time. Any image whose file at that LSN is a symbolic link does this.iso9660_ifs_readdir()returns an entry that points to freed memory, and freeing the list frees it again.realloc_symlink(). An SL entry with the continue flag whose only component has a flag that isn't implemented leaves the symlink length at 1 with no buffer.realloc_symlink()then copied a byte from NULL. And when the symlink was empty but a buffer already existed, it allocated another one and leaked the first.find_lsn_recurse()asserted that every directory can be read, so the find-by-LSN functions aborted on a directory of length 0, an extent past the end, a path that is too deep, and so on. Because CL entries use the same lookup, reading a directory that contains a CL entry was enough to trigger it.iso9660_open_fuzzy*(). The search forCD001usedstrstr()on a frame of 2352 bytes, which fills the buffer, so a frame with no NUL in it was read past its end on the stack.memcpy()from NULL for the root._fs_stat_traverse()and_fs_iso_stat_traverse()copied the root's symlink even when it had none. That passed NULL tomemcpy(), which is undefined behaviour and is flagged by UBSan on every image.Changes
find_lsn_recurse()hands the symlink over to the stat it returns. The CL handling frees the entry's own symlink before taking the target's.realloc_symlink()decides whether to allocate from the buffer itself rather than from the length. It copies no more than the buffer holds and keeps room for the final NUL.find_lsn_recurse()warns about a directory it can't read and goes on with the others.iso9660_check_dir_block_end()counts a record as at least as long as its fixed part when checking for the end of the block. A short record elsewhere in a block is still read and rejected as before, so the malformed-record check intestisorr(affd544) still passes.Cthat has room for them, instead of callingstrstr().Tests
testisorrbuilds small images at run time and checks that:iso9660_ifs_find_lsn()returns a stat whose symlink can be read and freed;C, can be read.The last two only show a difference under a memory checker.
make -C test checkandmake -C test/driver checkpass, andtestisorris clean under valgrind. Without the library changes,testisorraborts with a double free.🤖 Generated with Claude Code