Repository navigation
fix(fdt): check that memreserve entries end before dt_struct - #130
Open
darrellchee wants to merge 1 commit into
Open
darrellchee wants to merge 1 commit into
darrellchee wants to merge 1 commit into
Conversation
`validate_mem_reservations` checked that each 16-byte entry started before `off_dt_struct`, but not that it ended before it. Because `off_dt_struct` only has to be 4-byte aligned, the last entry, including the terminator, could spill up to 12 bytes into the structure block. Such blobs were still rejected later, but with a misleading `BadToken(0)` error. Check the end of the entry instead, so this case returns `MemReserveNotTerminated` at the offset of the entry that does not fit. Fixes google#117
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #130 +/- ##
==========================================
+ Coverage 93.91% 94.29% +0.37%
==========================================
Files 27 27
Lines 3503 3664 +161
==========================================
+ Hits 3290 3455 +165
+ Misses 213 209 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
validate_mem_reservationschecked that each memory reservation entry started beforeoff_dt_struct, but not that it ended before it. Sinceoff_dt_structonly needs 4-byte alignment, the last entry (including the terminator) could spill up to 12 bytes into the structure block. Such blobs were already rejected, but later and with a misleadingBadToken(0)atoff_dt_struct.offset + size_of::<MemoryReservation>() > off_dt_struct. This returnsMemReserveNotTerminated, as suggested in the issue. It can't overflow, sinceoffset <= off_dt_struct <= data.len().MemReserveInvalidmapping in this loop can no longer trigger (16 bytes are always available andMemoryReservationhas alignment 1). I left it in to keep the change small. Happy to remove it if you prefer.src/fdt.rs: a terminator spilling 4, 8 and 12 bytes, a spill after a real entry, a non-terminator entry spilling over, an empty block, an exact fit, and padding before the structure block.Tested locally with
cargo test --all-featureson 1.88, stable and nightly, plus clippy, rustfmt, cargo-machete, minimal versions, the feature powerset and Miri. I used Claude Code to help find and test this fix.Fixes #117
Before / After
Left: the new tests against the original check. 5 fail with
BadToken(0)(or the wrong offset) whereMemReserveNotTerminatedis expected. Right: the same tests with this fix, all 8 pass.compare.mp4