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 • committed by agent
commit 2cf9e8c7c6
No known key found for this signature in database
30 changed files with 6645 additions and 0 deletions

61
scripts/run-fuzz.sh Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env sh
# Run an OnionWire fuzz target. Needs a nightly toolchain and cargo-fuzz:
#
# rustup toolchain install nightly --profile minimal
# cargo install cargo-fuzz
#
# `cargo` must be rustup's shim, not a distro package: the `+nightly` directive
# only works through rustup. `export PATH="$HOME/.cargo/bin:$PATH"` if in doubt.
#
# Usage:
# scripts/run-fuzz.sh # all four targets, 60s each
# scripts/run-fuzz.sh qr_decode # one target
# FUZZ_SECS=300 scripts/run-fuzz.sh ratchet_frame
# FUZZ_JOBS=4 scripts/run-fuzz.sh # fork N workers per target
#
# Every target is a pure parser/decryptor over untrusted peer input, so a
# non-zero exit means libFuzzer found a crash: the reproducer lands in
# fuzz/artifacts/<target>/ and is named crash-<hash>.
#
# Reproduce a crash without fuzzing:
# cargo +nightly fuzz run <target> fuzz/artifacts/<target>/crash-<hash>
#
# Fold a fixed crash into the regression seeds (committed, so it runs every time
# instead of being rediscovered):
# cp fuzz/artifacts/<target>/crash-<hash> fuzz/seeds/<target>/
set -eu
cd "$(dirname "$0")/.."
TARGETS="ratchet_frame qr_decode dispatch_classify file_decode"
SECS="${FUZZ_SECS:-60}"
JOBS="${FUZZ_JOBS:-1}"
# A build tree per machine, outside the repo: the fuzz crate links the whole
# onionwire graph (Arti, SQLCipher, vendored OpenSSL) and must not share a
# target dir with a stable build.
: "${CARGO_TARGET_DIR:=$HOME/.cargo/fuzz-target}"
export CARGO_TARGET_DIR
: "${TMPDIR:=/var/tmp/onionwire-fuzz-tmp}"
export TMPDIR
mkdir -p "$TMPDIR"
if [ "$#" -gt 0 ]; then
TARGETS="$*"
fi
for t in $TARGETS; do
if [ ! -d "fuzz/corpus/$t" ]; then
mkdir -p "fuzz/corpus/$t"
fi
# Seed the working corpus from fuzz/seeds (hand-built regression inputs).
# fuzz/corpus itself is gitignored: libFuzzer grows it, and that growth is
# machine-local noise, not source.
cp -n fuzz/seeds/"$t"/* "fuzz/corpus/$t"/ 2>/dev/null || true
printf '\n=== fuzz %s (%ss, %s jobs) ===\n' "$t" "$SECS" "$JOBS"
cargo +nightly fuzz run "$t" -j "$JOBS" -- -max_total_time="$SECS"
done
printf '\nall targets survived %ss each\n' "$SECS"