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

Merged
sirius merged 1 commit from wt/t_1265efb2 into main 2026-09-18 02:00:34 +00:00
Owner

OnionWire had no fuzz testing. The four parsers that eat untrusted peer input now have a cargo-fuzz harness, and all four survived a 60s/4-job run with zero crashes.

Targets (fuzz/fuzz_targets/)

target entry points why
ratchet_frame ratchet::RatchetState::from_bytes, RatchetState::decrypt the rat frame parser: prefix, version, MIN_FRAME_LEN, N/PN bounds, DH step, AEAD open. Every header field is attacker-controlled.
qr_decode qr::decode pasted/scanned invite strings: bad hex, wrong field lengths, non-v3 onion, foreign signature over a well-formed body.
dispatch_classify dispatch::classify runs on every inbound plaintext; its Ratchet arm is what routes rat into the parser. All 255 byte values in scope.
file_decode file::decode fil chunk headers: hex ids, idx/total, filename through safe_name.

Each target asserts properties a panic-free decoder must hold, so it is not just "no signal": a rejected ratchet frame must leave the state byte-identical (to_bytes() compared before/after), a decoded invite must be 32/32/*.onion-shaped, a decoded chunk must re-encode and decode back to the same header, and classify must be total and deterministic with rat never shadowed by the [a-z]{3} catch-all.

Card naming note: the brief said ratchet::decode() and Ratchet::process_frame(). Those names do not exist in src/ratchet.rs (whose parser is the private Frame::parse behind RatchetState::decrypt); the target calls the public entry point that reaches that shape instead.

Scope

  • NEW fuzz/ directory only; no src/ file touched, no root Cargo.toml dependency added (fuzz/Cargo.toml carries an empty [workspace] table, so it is a detached workspace: cargo metadata at the root still reports exactly ['onionwire'], and a normal root build never compiles it).
  • fuzz/Cargo.lock is committed on purpose, like crates/onionwire-sdk's, so a fuzzer's dependency drift shows up in review.
  • fuzz/seeds/<target>/ is committed and copied into the gitignored working corpus by the script; a fixed crash gets folded back there so it regresses on every run. fuzz/corpus/ is ignored because libFuzzer grows it locally (184 files after one 60s run) and that growth is noise, not source.

Verification (real output)

$ cargo +nightly fuzz list
dispatch_classify
file_decode
qr_decode
ratchet_frame

$ FUZZ_SECS=60 FUZZ_JOBS=4 scripts/run-fuzz.sh
ratchet_frame      seeds=6 jobs=4 exit=0 final cov/ft=1277/2499 crashes=0/0/0
qr_decode          seeds=5 jobs=4 exit=0 final cov/ft=843/1046  crashes=0/0/0
dispatch_classify  seeds=4 jobs=4 exit=0 final cov/ft=56/58    crashes=0/0/0
file_decode        seeds=4 jobs=4 exit=0 final cov/ft=382/906   crashes=0/0/0
all targets survived 60s each

fuzz/artifacts/* is empty for all four targets (a crash would leave a crash-<hash> reproducer there). Script exit code 0.

Environment notes

  • Nightly and cargo-fuzz were not installed on this box; the run needed rustup toolchain install nightly --profile minimal and cargo install cargo-fuzz, plus rustup's cargo ahead of the distro one on PATH (a distro cargo cannot resolve +nightly).
  • The nightly build prints one inherited warning from onionwire itself (recursion_depth_exceeding_limit, future-incompatible, attached to the crate as a whole — TorClient::connect_with_prefs recursion depth), on the src/outbox.rs boxed future. It is unrelated to this change and does not appear on the repo's pinned stable 1.98; src/ is untouched here, so it is left alone.

Not done (deliberately)

No CI job. The card scoped this to fuzz/ plus scripts/run-fuzz.sh, and a fuzz job in .forgejo/workflows/ci.yml needs a nightly runner plus a target-dir volume decision that belongs in its own card.

OnionWire had no fuzz testing. The four parsers that eat untrusted peer input now have a cargo-fuzz harness, and all four survived a 60s/4-job run with zero crashes. ## Targets (`fuzz/fuzz_targets/`) | target | entry points | why | |---|---|---| | `ratchet_frame` | `ratchet::RatchetState::from_bytes`, `RatchetState::decrypt` | the `rat ` frame parser: prefix, version, `MIN_FRAME_LEN`, `N`/`PN` bounds, DH step, AEAD open. Every header field is attacker-controlled. | | `qr_decode` | `qr::decode` | pasted/scanned invite strings: bad hex, wrong field lengths, non-v3 onion, foreign signature over a well-formed body. | | `dispatch_classify` | `dispatch::classify` | runs on every inbound plaintext; its `Ratchet` arm is what routes `rat ` into the parser. All 255 byte values in scope. | | `file_decode` | `file::decode` | `fil ` chunk headers: hex ids, `idx/total`, filename through `safe_name`. | Each target asserts properties a panic-free decoder must hold, so it is not just "no signal": a rejected ratchet frame must leave the state byte-identical (`to_bytes()` compared before/after), a decoded invite must be 32/32/`*.onion`-shaped, a decoded chunk must re-encode and decode back to the same header, and `classify` must be total and deterministic with `rat ` never shadowed by the `[a-z]{3} ` catch-all. Card naming note: the brief said `ratchet::decode()` and `Ratchet::process_frame()`. Those names do not exist in `src/ratchet.rs` (whose parser is the private `Frame::parse` behind `RatchetState::decrypt`); the target calls the public entry point that reaches that shape instead. ## Scope - NEW `fuzz/` directory only; no `src/` file touched, no root `Cargo.toml` dependency added (`fuzz/Cargo.toml` carries an empty `[workspace]` table, so it is a detached workspace: `cargo metadata` at the root still reports exactly `['onionwire']`, and a normal root build never compiles it). - `fuzz/Cargo.lock` is committed on purpose, like `crates/onionwire-sdk`'s, so a fuzzer's dependency drift shows up in review. - `fuzz/seeds/<target>/` is committed and copied into the gitignored working corpus by the script; a fixed crash gets folded back there so it regresses on every run. `fuzz/corpus/` is ignored because libFuzzer grows it locally (184 files after one 60s run) and that growth is noise, not source. ## Verification (real output) ``` $ cargo +nightly fuzz list dispatch_classify file_decode qr_decode ratchet_frame $ FUZZ_SECS=60 FUZZ_JOBS=4 scripts/run-fuzz.sh ratchet_frame seeds=6 jobs=4 exit=0 final cov/ft=1277/2499 crashes=0/0/0 qr_decode seeds=5 jobs=4 exit=0 final cov/ft=843/1046 crashes=0/0/0 dispatch_classify seeds=4 jobs=4 exit=0 final cov/ft=56/58 crashes=0/0/0 file_decode seeds=4 jobs=4 exit=0 final cov/ft=382/906 crashes=0/0/0 all targets survived 60s each ``` `fuzz/artifacts/*` is empty for all four targets (a crash would leave a `crash-<hash>` reproducer there). Script exit code 0. ## Environment notes - Nightly and cargo-fuzz were not installed on this box; the run needed `rustup toolchain install nightly --profile minimal` and `cargo install cargo-fuzz`, plus rustup's `cargo` ahead of the distro one on `PATH` (a distro `cargo` cannot resolve `+nightly`). - The nightly build prints one inherited warning from `onionwire` itself (`recursion_depth_exceeding_limit`, future-incompatible, attached to the crate as a whole — `TorClient::connect_with_prefs` recursion depth), on the `src/outbox.rs` boxed future. It is unrelated to this change and does not appear on the repo's pinned stable 1.98; `src/` is untouched here, so it is left alone. ## Not done (deliberately) No CI job. The card scoped this to `fuzz/` plus `scripts/run-fuzz.sh`, and a fuzz job in `.forgejo/workflows/ci.yml` needs a nightly runner plus a target-dir volume decision that belongs in its own card.
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.
sirius merged commit 2f6f737106 into main 2026-09-18 02:00:34 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!103
No description provided.