Skip to content

fix(fdt): check that memreserve entries end before dt_struct - #130

Open
darrellchee wants to merge 1 commit into
google:mainfrom
darrellchee:fix/issue-117
Open

darrellchee wants to merge 1 commit into
google:mainfrom
darrellchee:fix/issue-117

Conversation

@darrellchee

@darrellchee darrellchee commented Nov 16, 2026 •

Copy link
Copy Markdown

validate_mem_reservations checked that each memory reservation entry started before off_dt_struct, but not that it ended before it. Since off_dt_struct only 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 misleading BadToken(0) at off_dt_struct.

  • The loop now checks that the whole 16-byte entry fits: offset + size_of::<MemoryReservation>() > off_dt_struct. This returns MemReserveNotTerminated, as suggested in the issue. It can't overflow, since offset <= off_dt_struct <= data.len().
  • The error offset points at the start of the entry that does not fit. When a non-terminator entry spills over, the offset is now that entry's start rather than its end (e.g. 40 instead of 56).
  • The set of accepted blobs is unchanged; only the error kind and offset differ for these inputs.
  • After this change, the MemReserveInvalid mapping in this loop can no longer trigger (16 bytes are always available and MemoryReservation has alignment 1). I left it in to keep the change small. Happy to remove it if you prefer.
  • 8 unit tests in 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-features on 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) where MemReserveNotTerminated is expected. Right: the same tests with this fix, all 8 pass.

compare.mp4

`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

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.29%. Comparing base (b2c3a79) to head (0a590bc).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

validate_mem_reservations allow spilling into the next block

1 participant