Skip to content

feat(ext4): image reader with hostile-input bounds - #12

Merged
lstocchi merged 1 commit into
redhat-developer:mainfrom
lstocchi:reader
Nov 13, 2026
Merged

lstocchi merged 1 commit into
redhat-developer:mainfrom
lstocchi:reader

Conversation

@lstocchi

Copy link
Copy Markdown
Collaborator

Add ext4::reader::ImageReader, the read-only, positional-I/O access the upcoming snapshot feature needs. It exists separately from the builder because a snapshot must read an image the VM still has mounted: mmap of a concurrently written file is undefined behavior, so the reader uses seek_read/read_exact_at, never loads bitmaps and never writes.

The reader is the first consumer of the agent-modified-image threat model, so every on-disk index it follows is bounds-checked through one accessor (read_bytes), extent depth is capped at 5 with strictly decreasing depth, and directory walks are capped at the inode's own i_blocks count. A parity test compares its entries, file bytes and symlink targets against the builder on a rich fixture, crafted-image tests cover out-of-range extents, excessive depth, oversized directory mappings, bad records, container magics and truncation, and a concurrent-rewrite test proves reads are memory-safe and observe whole blocks while a second handle rewrites them.

It also decodes journal state: the jsb (s_start at 0x1C, big-endian, 12-byte journal_header_t) and INCOMPAT_RECOVER, which is the mounted/frozen bit on journaled images (a journaled ext4 keeps VALID_FS set while mounted; fsfreeze clears RECOVER). The jsb decoder is cross-checked against dumpe2fs on an image mke2fs made, catching offset and endianness mistakes an encode-what-we-decode test cannot. The strict builder open now decodes RECOVER too, so edit on a mounted or crashed journaled image fails with the mounted message instead of a foreign-feature error, and refuses non-empty journals (replay by booting once or e2fsck).

Add ext4::reader::ImageReader, the read-only, positional-I/O access the
upcoming snapshot feature needs. It exists separately from the builder
because a snapshot must read an image the VM still has mounted: mmap of
a concurrently written file is undefined behavior, so the reader uses
seek_read/read_exact_at, never loads bitmaps and never writes.

The reader is the first consumer of the agent-modified-image threat
model, so every on-disk index it follows is bounds-checked through one
accessor (read_bytes), extent depth is capped at 5 with strictly
decreasing depth, and directory walks are capped at the inode's own
i_blocks count. A parity test compares its entries, file bytes and
symlink targets against the builder on a rich fixture, crafted-image
tests cover out-of-range extents, excessive depth, oversized directory
mappings, bad records, container magics and truncation, and a
concurrent-rewrite test proves reads are memory-safe and observe whole
blocks while a second handle rewrites them.

It also decodes journal state: the jsb (s_start at 0x1C, big-endian,
12-byte journal_header_t) and INCOMPAT_RECOVER, which is the
mounted/frozen bit on journaled images (a journaled ext4 keeps
VALID_FS set while mounted; fsfreeze clears RECOVER). The jsb
decoder is cross-checked against dumpe2fs on an image mke2fs made,
catching offset and endianness mistakes an encode-what-we-decode test
cannot. The strict builder open now decodes RECOVER too, so edit on a
mounted or crashed journaled image fails with the mounted message
instead of a foreign-feature error, and refuses non-empty journals
(replay by booting once or e2fsck).

The fuzz/ target for the reader is deferred to its own commit: it
needs nightly plus cargo-fuzz, which only run in CI on Linux.

Assisted-by: Claude Code: GLM 5.3
Assisted-by: Claude Code: Opus 5.5
Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
@lstocchi
lstocchi merged commit 13f8e86 into redhat-developer:main Nov 13, 2026
4 checks passed
@lstocchi
lstocchi deleted the reader branch November 13, 2026 18:47
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.

1 participant