crates

device_tree_blob

check proofs license minimum Rust version

A flattened device tree parser: bytes in, memory regions, register regions and property values out. The device tree blob is the machine describing itself, handed over by firmware (a bootloader, a Unified Extensible Firmware Interface (UEFI) loader or QEMU). The crate reads it as untrusted input. It checks every offset and bounds every walk, and a malformed blob is an Error, never a panic. It is no_std, has no allocator and no dependencies, and reads the flattened format of chapter 5 of the Devicetree Specification (release 0.4), versions up to 17.

EXAMPLES

use device_tree_blob::{DeviceTreeBlob, Region};

// A tree dumped from QEMU's riscv64 `virt` machine, kept in this repository.
let blob = include_bytes!("tests/fixtures/qemu-riscv64-virt.dtb");
let tree = DeviceTreeBlob::from_bytes(blob)?;

let mut ram = [Region { start: 0, size_bytes: 0 }; 4];
let banks = tree.memory_regions(&mut ram)?;
assert_eq!(&ram[..banks], [Region { start: 0x8000_0000, size_bytes: 0x800_0000 }]);

let mut rtc = [Region { start: 0, size_bytes: 0 }; 1];
let found = tree.node_register_regions_compatible(b"google,goldfish-rtc", &mut rtc)?;
assert_eq!(&rtc[..found], [Region { start: 0x10_1000, size_bytes: 0x1000 }]);
# Ok::<(), device_tree_blob::Error>(())

The same code runs as a doctest on memory_regions and node_register_regions_compatible.

What it reads

To read Call
The memory banks memory_regions
The memory reservation block memory_reservations
The children of /reserved-memory reserved_memory_regions
The register regions of a node, by name prefix or by compatible node_register_regions, node_register_regions_compatible
One property of a node node_property, node_property_inherited, node_property_compatible, parent_property_compatible, pointer_handle_property, node_properties
The /chosen node initial_ramdisk, boot_arguments

Build a tree with DeviceTreeBlob::from_bytes, or with the crate's one unsafe function, DeviceTreeBlob::from_pointer, when firmware hands over only an address.

A few behaviors a stranger may not expect:

What the proofs cover

Kani harnesses in src/lib.rs prove the leaf readers and the token step:

Each harness has a falsification patch in falsifications/ that breaks the code under proof and turns the harness red. The walkers are not proved. The hostile tests in tests/hostile.rs pin the behavior of each lookup on hand-built blobs, and a fuzz target covers the walkers.

Building and testing

cargo test
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo build --lib --target x86_64-unknown-none
cargo build --lib --target aarch64-unknown-none-softfloat
cargo build --lib --target riscv64imac-unknown-none-elf

Running the proofs

Install Kani first: run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup. Setup downloads a model checker and the Rust toolchain Kani pins, so it needs network access. Then run every harness:

cargo kani

Measured on an x86_64 Linux machine with Kani 0.68.0, the run of all nine harnesses took 9 minutes of wall time and peaked at 7.5 GiB of resident memory. The token-step harness takes 426 seconds of that, and the other eight take 92 seconds together.

A passing run prints warnings that mean nothing, among them use of an unstable feature for register_tool, Found the following unsupported constructs, and lines about unreachable code. The answer is the last line, Complete - 9 successfully verified harnesses, 0 failures, 9 total. To run one harness, give its full path:

cargo kani --harness verification::be32_is_total --exact

Running one falsification patch

Apply the patch, confirm the proof fails, then undo it with the same patch reversed:

patch -p1 --no-backup-if-mismatch < falsifications/verification.be32_is_total.patch
cargo kani --harness verification::be32_is_total --exact
patch -R -p1 --no-backup-if-mismatch < falsifications/verification.be32_is_total.patch

The failing run prints VERIFICATION:- FAILED and cargo kani exits 1.

Using it

The project has not published this crate, and publishing is on hold. No release tag exists yet. Depend on it by git, pinned to a commit of this repository's main and never to a branch:

[dependencies]
device_tree_blob = { git = "https://github.com/nifeos/device_tree_blob", rev = "<full commit hash>" }

Once a pre-release tag exists, use tag = "<that tag>" in place of rev.

Fuzzing

Weekly fuzzing of the crate's public inputs is off, because DeviceTreeBlob::from_pointer is unsafe. The repository holds one fuzz target, fuzz/fuzz_targets/device_tree_blob_walk.rs, which feeds arbitrary bytes through every lookup.

BUGS

Minimum Rust version

Rust 1.88. To check it, install the toolchain and run the tests with +1.88:

rustup toolchain install 1.88 --profile minimal
cargo +1.88 test

License

Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.