Skip to content

fix(fdt_mut): do not panic on unreferenced bytes in the strings block - #111

Open
calef wants to merge 1 commit into
google:mainfrom
calef:fix-add-property-unreferenced-strings
Open

calef wants to merge 1 commit into
google:mainfrom
calef:fix-add-property-unreferenced-strings

Conversation

@calef

@calef calef commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

find_existing_string now compares raw bytes, as libfdt's fdt_find_string_ does: it splits the strings block at each null terminator, compares each terminated piece with the name, and skips a trailing piece with no terminator. Bytes that are not a valid string can then never match and never panic.

The new test covers an unterminated byte, a terminated byte that is not UTF-8, and a name that already exists before the invalid bytes, which is reused.

Fixes #109

`Fdt::new` validates only the strings that a property's `nameoff` points
to, so the `dt_strings` block of an accepted tree may contain bytes that
are not a null-terminated UTF-8 string. `find_existing_string` walked the
whole block and expected every string to be valid, which made
`FdtNodeMut::add_property` panic whenever the new name was not found
before such bytes.

Compare raw bytes instead: split the block at each null terminator,
compare every terminated piece with the name, and skip a trailing piece
without a terminator. A piece that is not a valid string can never equal
a `&str`, so it is passed over and the name is appended as before.

Fixes google#109

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.61%. Comparing base (7662cd1) to head (ade324d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #111   +/-   ##
=======================================
  Coverage   93.61%   93.61%           
=======================================
  Files          26       26           
  Lines        3428     3428           
=======================================
  Hits         3209     3209           
  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 12:00
Comment thread tests/fdt_mut.rs
/// Builds a version 17 DTB consisting of the header, one empty memory
/// reservation entry, `structure` and `strings`. The buffer is padded with
/// `spare` zero bytes so the tree can grow in place.
fn build_dtb(structure: &[u32], strings: &[u8], spare: usize) -> Vec<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.

Can we use the helper that was added in #108 here instead?

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

Looks good to me except some minor issues, thanks a lot!

A nice side effect of this should be slightly improved performance as we won't decode UTF-8 in find_existing_string anymore.

Comment thread src/fdt_mut.rs
Comment on lines +172 to +173
for s in str_block.split_inclusive(|&b| b == 0) {
if s.strip_suffix(&[0]) == Some(name.as_bytes()) {

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.

nit: because we operate on strings here, it's a bit more natural in my opinion to use char/string literals, i.e.:

Suggested change
for s in str_block.split_inclusive(|&b| b == 0) {
if s.strip_suffix(&[0]) == Some(name.as_bytes()) {
for s in str_block.split_inclusive(|c| c == b'\0') {
if s.strip_suffix(b"\0") == Some(name.as_bytes()) {

Comment thread src/fdt_mut.rs
// Validation only covers the strings that properties reference, so the
// block may contain bytes that are not a valid string. Comparing raw
// bytes means such bytes never match `name` and never panic. A trailing
// piece without a null terminator is skipped.

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 comment refers to the reason for the change and not so much to the code below. Suggest moving this to the commit message and/or rewriting to something like "comparing raw bytes to avoid decoding UTF-8 (which may not be validated)"

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.

FdtNodeMut::add_property panics when the strings block contains an invalid string that no property uses

2 participants