Skip to content

fix(fdt): check that the structure block contains FDT_END - #112

Merged
m4tx merged 3 commits into
google:mainfrom
calef:fix-struct-size-check
Nov 23, 2026
Merged

m4tx merged 3 commits into
google:mainfrom
calef:fix-struct-size-check

Conversation

@calef

@calef calef commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

validate now returns InvalidHeader, reported at size_dt_struct, when FDT_END ends past off_dt_struct + size_dt_struct. With the existing check that the strings block starts at or after the end of the structure block, this also refuses a strings block that begins inside the structure.

The tests cover the three inputs from the issue, an exactly sized structure block, and FDT_NOPs or padding after FDT_END inside size_dt_struct, which are still accepted. A test on FdtMut::from_slice covers the mutable path.

Fixes #110

@google-cla

google-cla Bot commented Nov 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.39%. Comparing base (c79d506) to head (acb9725).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #112   +/-   ##
=======================================
  Coverage   94.38%   94.39%           
=======================================
  Files          27       27           
  Lines        3652     3658    +6     
=======================================
+ Hits         3447     3453    +6     
  Misses        205      205           

☔ 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.

@qwandor
qwandor requested a review from m4tx November 11, 2026 11:53
Comment thread src/fdt.rs Resolved
Comment thread src/fdt.rs Resolved
`Fdt::new` walks the structure tokens up to `FDT_END` bounded only by
the length of the blob, and never checks that `FDT_END` lies within
`off_dt_struct + size_dt_struct`. `FdtMut` then takes `size_dt_struct`
and `off_dt_strings` as the bounds of the structure block, so a blob
whose `size_dt_struct` is shorter than the structure that was walked
can make `shift_dt_struct`, `compact` and `add_property` panic, or
write a property name over a structure token.

Return `InvalidHeader` from `validate` when `FDT_END` ends past
`off_dt_struct + size_dt_struct`. Together with the existing check that
the strings block starts at or after the end of the structure block,
this also refuses a strings block that begins inside the structure.

Fixes google#110
The two tests for the `size_dt_struct` check spelled out an 84-byte
blob by hand as a unit test in `src/fdt.rs`. The helpers that google#108
added in `tests/common/mod.rs` build the same blob (`build_chain_dtb(1)`
is a root node with one child `a`), so the tests move to `tests/fdt.rs`
and use them. The padding case now builds the trailing tokens into the
structure block instead of patching header fields after the fact.

The integration tests can only see `FdtParseError::kind`, so the
assertion on the error offset goes away; the kind still carries the
`InvalidHeader` message.

Also shorten the comment on the check in `validate`, as asked in
review.
@calef
calef force-pushed the fix-struct-size-check branch from 73d15b7 to be9e76e Compare November 16, 2026 19:38

@m4tx m4tx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@m4tx
m4tx merged commit 1c67cfe into google:main Nov 23, 2026
26 checks passed
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.

Fdt::new does not check that the structure block ends within size_dt_struct

2 participants