Skip to content

iso9660: fix memory errors and an abort on crafted Rock Ridge images - #120

Merged
rocky merged 7 commits into
libcdio:mainfrom
nicosp:iso9660-rr-fixes
Nov 19, 2026
Merged

rocky merged 7 commits into
libcdio:mainfrom
nicosp:iso9660-rr-fixes

Conversation

@nicosp

@nicosp nicosp commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

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

  • Symlink freed while still in use. find_lsn_recurse() copied the iso9660_stat_t of 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() and iso9660_fs_find_lsn() returned a stat holding a freed pointer, and iso9660_stat_free() freed it a second time. Any image whose file at that LSN is a symbolic link does this.
    • A Rock Ridge CL entry looks up its target with the same function while its directory is being read. When the target is a symbolic link, iso9660_ifs_readdir() returns an entry that points to freed memory, and freeing the list frees it again.
    • Copying the target's stat over the CL entry also leaked the entry's own symlink.
  • Copy from NULL in 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.
  • Abort while looking for an LSN. 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.
  • Short record at the end of a block. The loops over directory records read the 33-byte fixed part of a record even when it is rejected for being shorter than that. A record of 1 to 32 bytes at the end of a directory's last block made them read up to 24 bytes past the buffer.
  • Search past the frame in iso9660_open_fuzzy*(). The search for CD001 used strstr() 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 to memcpy(), 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 in testisorr (affd544) still passes.
  • The fuzzy search compares the 5 bytes at each C that has room for them, instead of calling strstr().
  • The root's symlink is copied only when the root has one.

Tests

testisorr builds small images at run time and checks that:

  • iso9660_ifs_find_lsn() returns a stat whose symlink can be read and freed;
  • a 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 block, and the fuzzy search on frames full of C, can be read.

The last two only show a difference under a memory checker.

make -C test check and make -C test/driver check pass, and testisorr is clean under valgrind. Without the library changes, testisorr aborts with a double free.

🤖 Generated with Claude Code

nicosp and others added 7 commits November 17, 2026 08:19
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>
@nicosp
nicosp requested a review from rocky November 17, 2026 06:31
@rocky

rocky commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

LGTM. @pbatard I think this falls into an area you are interested in. Do you have any concerns before merging?

@pbatard

pbatard commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

No concerns for me. These look okay at a cursory glance.

@rocky

rocky commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

No concerns for me. These look okay at a cursory glance.

Thanks, @pbatard. We can always come back and change things if something comes up.

@rocky
rocky merged commit 21942de into libcdio:main Nov 19, 2026
9 checks passed
@nicosp
nicosp deleted the iso9660-rr-fixes branch November 19, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants