fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers

Four pure parsers eat untrusted peer input and had no structured fuzzing:

- ratchet_frame  -> RatchetState::from_bytes + RatchetState::decrypt
                    (the 'rat ' frame parser: prefix, version, length,
                    N/PN bounds, DH step, AEAD open)
- qr_decode      -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
                    including the 'rat ' -> Kind::Ratchet routing arm
- file_decode    -> file::decode on 'fil ' chunk frames

No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.

Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).

Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
This commit is contained in:
rust-dev 2026-09-17 20:52:44 -04:00
commit bcee6a72a6
No known key found for this signature in database
30 changed files with 6645 additions and 0 deletions

View file

@ -0,0 +1,26 @@
//! Untrusted decrypted plaintext against the application dispatch classifier.
//!
//! `dispatch::classify` runs on every inbound message before anything else
//! looks at it, and its `Ratchet` arm is what routes a `rat `-prefixed message
//! into the ratchet parser (`src/ratchet.rs` `Frame::parse`). It takes raw
//! bytes — never a `&str` — so all 255 byte values are in scope, and it must
//! classify (or `Drop`) every input without panicking.
#![no_main]
use libfuzzer_sys::fuzz_target;
use onionwire::dispatch::{self, Kind};
fuzz_target!(|data: &[u8]| {
let kind = dispatch::classify(data);
// Total and deterministic: every input maps to exactly one variant, and the
// answer is stable across calls (no interior state, no ordering dependence).
assert_eq!(kind, dispatch::classify(data));
// The routing invariant that matters: the `rat ` prefix is never shadowed by
// the `[a-z]{3} ` catch-all, so a ratchet frame always reaches the ratchet
// parser rather than being dropped as an unknown prefix.
if data.starts_with(b"rat ") {
assert_eq!(kind, Kind::Ratchet);
}
});

View file

@ -0,0 +1,26 @@
//! Untrusted file-chunk bytes against the chunk decoder.
//!
//! A peer can send a `fil ` chunk at any time; `file::decode` header-parses it
//! (hex transfer id, filename, hex sha256, `idx/total`, then the payload) before
//! anything is written to disk. It must return `None` for every malformed
//! chunk and never panic — including on absurd `idx`/`total` and on filenames
//! that fail `safe_name`.
#![no_main]
use libfuzzer_sys::fuzz_target;
use onionwire::file;
fuzz_target!(|data: &[u8]| {
if let Some(chunk) = file::decode(data) {
// A chunk it accepted must survive an encode round-trip and decode
// back to the same header, byte for byte.
let re = file::encode(&chunk);
let again = file::decode(&re).expect("encoded chunk must decode");
assert_eq!(again.xfer_id, chunk.xfer_id);
assert_eq!(again.filename, chunk.filename);
assert_eq!(again.sha256, chunk.sha256);
assert_eq!(again.idx, chunk.idx);
assert_eq!(again.total, chunk.total);
assert_eq!(again.data, chunk.data);
}
});

View file

@ -0,0 +1,28 @@
//! Untrusted invite strings against the QR/invite decoder.
//!
//! `qr::decode` takes a `&str` (the invite is pasted or scanned as text), so the
//! fuzzer input is read lossily as UTF-8. It must return `Err` for every
//! malformed invite — bad hex, wrong lengths, a non-v3 onion, a foreign
//! signature over a well-formed body — and never panic on any of them. The
//! `onionwire:v1:k=…` string is a QR code's payload, so the byte budget is
//! attacker-sized and the parse must stay bounded.
#![no_main]
use libfuzzer_sys::fuzz_target;
use onionwire::qr;
fuzz_target!(|data: &[u8]| {
let raw = String::from_utf8_lossy(data);
// The decoder is total over `&str`: no panic, no hang, no runaway allocation.
if let Ok(payload) = qr::decode(&raw) {
// Anything it accepted must be shaped as the payload promises; a decode
// that returns a malformed payload is as bad as a panic downstream.
assert_eq!(payload.pubkey.len(), 32);
assert_eq!(payload.signed_prekey.len(), 32);
assert!(payload.onion.ends_with(".onion"));
}
// Determinism: the same invite must decode to the same answer.
assert_eq!(qr::decode(&raw).is_ok(), qr::decode(&raw).is_ok());
});

View file

@ -0,0 +1,48 @@
//! Untrusted peer bytes against the Double Ratchet frame parser.
//!
//! Two entry points, both reachable from the network:
//!
//! - `RatchetState::from_bytes` — persisted-state restore (a tampered or
//! truncated blob from the store must be an `Err`, never a panic).
//! - `RatchetState::decrypt` — the wire path (`Frame::parse` + `plan_*` +
//! `open`); every field in the header is attacker-controlled.
//!
//! Neither call may panic, and neither may leave the state mutated on a frame
//! it rejects (asserted in `tests/ratchet.rs`, re-checked cheaply here by
//! re-serializing after every decrypt).
#![no_main]
use libfuzzer_sys::fuzz_target;
use onionwire::ratchet::{RatchetKeyPair, RatchetState};
fuzz_target!(|data: &[u8]| {
// Restore path: arbitrary bytes as a persisted ratchet state.
if let Ok(state) = RatchetState::from_bytes(data) {
// A state that parsed must re-serialize without panicking.
let _ = state.to_bytes();
}
// Wire path: a fresh bootstrap epoch for every input, so one iteration can
// never influence the next. The keys are fixed (not fuzzed) on purpose:
// the interesting surface is `data` as the frame.
let mut state = RatchetState::bootstrap(
[0x42u8; 32],
RatchetKeyPair::from_secret([0x11u8; 32]),
[0x33u8; 32],
);
let before = state.to_bytes();
match state.decrypt(data) {
Ok(_) => {
// A frame this state accepts must have advanced the receiving chain.
let _ = state.recv_index();
}
Err(_) => {
// Refuse-and-roll-back: a rejected frame leaves the state untouched.
assert_eq!(state.to_bytes(), before, "rejected frame mutated the state");
}
}
// Re-encrypting a byte-identical state must stay a pure function.
let _ = state.encrypt(b"fuzz");
});