Skip to content

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

Open
calef wants to merge 1 commit into
google:mainfrom
calef:fix-struct-size-check
Open

calef wants to merge 1 commit 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

`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

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@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 93.66%. Comparing base (7662cd1) to head (147e908).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #112      +/-   ##
==========================================
+ Coverage   93.61%   93.66%   +0.05%     
==========================================
  Files          26       26              
  Lines        3428     3459      +31     
==========================================
+ Hits         3209     3240      +31     
  Misses        219      219              

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

/// A root node with one child, `a`. The structure block is 28 bytes.
const FDT_CHILD_OK: &[u8] = &[

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.

Would it make sense to use the helpers added in #108 to construct this?

Comment thread src/fdt.rs
Comment on lines +525 to +528
// The walk above is bounded by the blob, not by `size_dt_struct`.
// Check that FDT_END lies within the structure block declared by the
// header, as `FdtMut` relies on `size_dt_struct` and `off_dt_strings`
// to bound the structure block.

@m4tx m4tx Nov 11, 2026 •

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.

This code comment is overly verbose IMO; it's fine to just write something like "validate that the entire dt_struct actually fits within what's declared in the header"

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