Repository navigation
feat(ext4): image reader with hostile-input bounds - #12
Merged
Merged
Conversation
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>
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.
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).