EXT4: guard extent-tree parsing against out-of-bounds reads on malformed images - #849
Open
iabdullah215 wants to merge 1 commit into
Open
EXT4: guard extent-tree parsing against out-of-bounds reads on malformed images#849iabdullah215 wants to merge 1 commit into
iabdullah215 wants to merge 1 commit into
Conversation
…med images EXT4.EXT4Reader.getExtents decodes the extent tree from an inode's block region, which is untrusted input. Both the depth-0 and depth-1 loops iterated 0..<entries reading fixed-size records via subdata without checking the attacker-controlled entry count against the bytes available: - the inode block is only 60 bytes, so a header claiming more than four depth-0 leaves (or depth-1 indices) reads past it; and - a depth-1 leaf block trusts its own header's entries the same way. A malformed inode therefore traps the reader (Data.subdata precondition failure, SIGTRAP), a denial of service. Move the decode into an internal decodeExtents(inodeBlock:) helper, bounds-check every fixed-size read, and fix a latent reversed range when reading a leaf block's header (blockOffset..<extentHeaderSize -> blockOffset..<blockOffset + extentHeaderSize). Add regression tests for the depth-0 overflow and the valid/short/non-extent cases (the overflow crashes the reader without the fix; existing EXT4 reader tests still pass).
iabdullah215
force-pushed
the
harden-ext4-extent-bounds
branch
from
August 24, 2026 07:57
214abeb to
2e2a771
Compare
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.
What
Bounds-check the extent-tree decode in
EXT4.EXT4Readerso a malformed inode can no longer driveData.subdatapast the inode's block region. The decode is extracted into an internaldecodeExtents(inodeBlock:)helper so it can be unit-tested directly.Why
An ext4 image is untrusted input.
getExtentsiterated0..<entriesreading fixed-size records (12 bytes each) withsubdata, trusting the entry counts from the on-disk extent headers without checking them against the bytes available:entriesthe same way, past the end of the block buffer.Because
Data.subdata(in:)has a precondition on its range, either case traps the process (SIGTRAP) instead of returning an error, a denial of service for any caller that reads a file backed by a crafted inode.Fix
offset + size <= buffer.count(throwingEXT4.Error.invalidExtentswhen a header claims more records than fit, i.e. surfacing the corruption rather than silently truncating).blockOffset..<extentHeaderSize→blockOffset..<blockOffset + extentHeaderSize(previously benign only becauseblockOffset == 0there).Well-formed images are unaffected: their headers never claim more records than the block holds.
Testing
New
Tests/ContainerizationEXT4Tests/TestEXT4Reader+ExtentBounds.swift:wellFormedDepth0ExtentsParse— regression: valid depth-0 extents still parse.tooManyDepth0EntriesDoesNotTrap— a header claiming 5 leaves in a 60-byte block is rejected (crashes the reader without this change).shortInodeBlockReturnsEmpty/nonExtentInodeReturnsEmpty, no-extent cases.All existing
EXT4PathIOTests(which read real files throughgetExtents) still pass.Related
throw([Request]: Implement deep extent tree support (depth >= 2) in ContainerizationEXT4 #645 / Add deep extent tree support (depth >= 2) in EXT4 reader #649 / Add deep extent tree support (depth >= 2) in EXT4 formatter #650 track it as a feature); this PR doesn't change that.