Repository navigation
Description
Version: dtoolkit 0.5.0, which is also current main (7662cd1). Rust 1.99.1.
What happens. validate_header checks that off_dt_struct + size_dt_struct lies within the blob and at or before off_dt_strings. validate then walks the tokens from off_dt_struct to FDT_END (src/fdt.rs#L474-L526), bounded only by the length of the blob. Nothing checks that FDT_END lies inside size_dt_struct. So Fdt::new accepts a blob whose size_dt_struct is shorter than the structure block it walked, including one whose strings block begins inside that structure.
FdtMut takes size_dt_struct and off_dt_strings as the bounds of the structure block: shift_dt_struct checks with debug_assert! that the insertion point lies inside it, compact stops its walk at off_dt_struct + size_dt_struct and computes size_dt_struct - new_size_dt_struct, and append_raw_string writes at the end of the strings block. On such a tree the tests below give:
| test | debug build | release build |
|---|---|---|
add_property_past_size_dt_struct |
panics at src/fdt_mut.rs:208:9: "offset must be within the structure block" |
succeeds; the output is accepted by Fdt::new |
compact_past_size_dt_struct |
panics at src/fdt_mut.rs:322:27: "attempt to subtract with overflow" (also in release with overflow-checks = true) |
no panic; the tree is cut to 64 bytes (the header, the reservation entry, and the root's FDT_BEGIN_NODE with its empty name), and Fdt::new refuses it with InvalidLength at offset 64 |
add_property_with_strings_block_inside_structure |
panics at src/fdt_mut/node.rs:102:14: "Fdt should be valid: FdtParseError { offset: 64, kind: BadToken(1879048193) }" |
the same |
In the third case the property name p is appended to the strings block, which overwrites the child node's FDT_BEGIN_NODE token (0x00000001 becomes 0x70000001).
Expected. Fdt::new (and therefore FdtMut::from_slice) refuses a blob whose structure walk ends past off_dt_struct + size_dt_struct, so that no edit can start from one.
Reproduction.
use dtoolkit::fdt::Fdt;
use dtoolkit::fdt_mut::FdtMut;
/// Builds a version-17 blob: the header, one empty reservation entry, then the
/// structure block and the strings block.
fn blob(structure: &[u32], strings: &[u8]) -> Vec<u8> {
let structure: Vec<u8> = structure.iter().flat_map(|w| w.to_be_bytes()).collect();
let off_struct = 40 + 16;
let off_strings = off_struct + structure.len();
let total = off_strings + strings.len();
let mut b = Vec::new();
for v in [0xd00dfeed, total, off_struct, off_strings, 40, 17, 16, 0, strings.len(), structure.len()] {
b.extend_from_slice(&(v as u32).to_be_bytes());
}
b.extend_from_slice(&[0; 16]);
b.extend_from_slice(&structure);
b.extend_from_slice(strings);
b
}
/// The root node with one child, `a`: 28 bytes of structure. The header's
/// `size_dt_struct` and `off_dt_strings` are then overwritten.
fn tree(size_dt_struct: u32, off_dt_strings: u32) -> Vec<u8> {
let a = u32::from_be_bytes(*b"a\0\0\0");
let mut dtb = blob(&[1, 0, 1, a, 2, 2, 9], &[]);
dtb[12..16].copy_from_slice(&off_dt_strings.to_be_bytes());
dtb[36..40].copy_from_slice(&size_dt_struct.to_be_bytes());
assert!(Fdt::new(&dtb).is_ok());
dtb.resize(dtb.len() + 64, 0); // spare capacity for the edit
dtb
}
/// `size_dt_struct` is 8 (the root's FDT_BEGIN_NODE and name only); the
/// strings block is where it would be for the full 28 bytes.
#[test]
fn add_property_past_size_dt_struct() {
let mut dtb = tree(8, 56 + 28);
let mut fdt = FdtMut::from_slice(&mut dtb).unwrap();
fdt.find_node_mut("/a").unwrap().add_property("p", 1u32).unwrap();
}
/// `size_dt_struct` is 4 (the root's FDT_BEGIN_NODE token without its name).
#[test]
fn compact_past_size_dt_struct() {
let mut dtb = tree(4, 56 + 28);
let mut fdt = FdtMut::from_slice(&mut dtb).unwrap();
fdt.compact();
assert!(Fdt::new(fdt.as_read_only().data()).is_ok());
}
/// The (empty) strings block starts 8 bytes into the structure that the walk
/// covers, which the header check allows because `size_dt_struct` is 8.
#[test]
fn add_property_with_strings_block_inside_structure() {
let mut dtb = tree(8, 56 + 8);
let mut fdt = FdtMut::from_slice(&mut dtb).unwrap();
fdt.root_mut().add_property("p", 1u32).unwrap();
}Suggestion. In validate, after the FDT_END check, return an error when end_offset + FDT_TAGSIZE > off_dt_struct + size_dt_struct. Together with the existing check that the strings block starts at or after off_dt_struct + size_dt_struct, this also refuses the overlapping case. A local change along these lines passes the existing test suite (cargo test --all-features), and Fdt::new then refuses all three inputs above.
Activity
Daiyuanranfeng commentedon Nov 16, 2026
I would like to take this issue. I will validate that the structure walk ends within size_dt_struct, reject overlapping or truncated structure blocks during parsing, and add regression coverage for valid and invalid boundary cases.
fix(fdt): check that the structure block contains FDT_END