Skip to content

fuzz with libFuzzer - #85

Draft
yuval-block wants to merge 6 commits into
fuzz-targetsfrom
fuzz-libfuzzer
Draft

yuval-block wants to merge 6 commits into
fuzz-targetsfrom
fuzz-libfuzzer

Conversation

@yuval-block

@yuval-block yuval-block commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a cargo-fuzz frontend over the shared targets and runs it through nix:

nix run .#fuzz-libfuzzer -- fuzz_lattice_laws -max_total_time=600

The targets are built in a derivation and the app only runs them against
a corpus in the checkout.

There is a binary per target, fuzz_lattice_laws and fuzz_scrub, and the
workflow fuzzes each in a job of its own.

Fuzzing is randomized, so it runs from a daily workflow rather than as a
flake check. That workflow first runs fuzz-libfuzzer-verify, which
confirms libFuzzer finds the crash_test panic.

🤖 Generated with Claude Code

@yuval-block yuval-block mentioned this pull request Nov 12, 2026
@github-code-quality

github-code-quality Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

fuzz-targets 227c7b5 fuzz-libfuzzer 3d6bfa8 +/-
100% 100% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 13, 2026 06:52 UTC

Comment thread nix/fuzz/libfuzzer.nix
}:
let
# Every target in fuzz/libfuzzer, instrumented by cargo-fuzz and built
# in the sandbox, so that the app and the check run the same binaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check runs different binaries than the app

The comment says the app and the check run the same binaries, but the check builds verifyTargets with RUSTFLAGS = "--cfg fuzzer_verify_crash" while the app uses targets without it; the app's fuzz_crash_test exits cleanly on the verification input while the check's panics, so the check's crash cannot be reproduced with nix run .#fuzz-libfuzzer.

RUSTFLAGS = "--cfg fuzzer_verify_crash";

— fungi-protocol/concurrent-psbt@f2419ee · nix/fuzz/libfuzzer.nix#L69

oak · automated · 1a059c9

Comment thread nix/fuzz/libfuzzer.nix
# The same layout cargo fuzz run uses, relative to the checkout.
corpus="fuzz/libfuzzer/corpus/$target"
artifacts="fuzz/libfuzzer/artifacts/$target"
mkdir -p "$corpus" "$artifacts"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzzer app wrapper depends on caller's mkdir

The wrapper calls mkdir but declares no runtimeInputs, so mkdir is resolved from the caller's PATH; with a PATH that lacks mkdir, nix run .#fuzz-libfuzzer fails with mkdir: command not found before the fuzzer starts. Add pkgs.coreutils to runtimeInputs.

oak · automated · 1a059c9

Comment thread nix/fuzz/libfuzzer.nix
text = ''
target="''${1:?usage: nix run .#fuzz-libfuzzer -- <target> [libFuzzer args...]}"
shift
# The same layout cargo fuzz run uses, relative to the checkout.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corpus paths are relative to CWD, not checkout

The script creates fuzz/libfuzzer/{corpus,artifacts} relative to the current directory, so running nix run /path/to/repo#fuzz-libfuzzer from a directory with no corpus silently starts a fresh corpus there; the comment says the layout is relative to the checkout.

oak · automated · 1a059c9

Comment thread nix/fuzz/libfuzzer.nix Resolved
@github-actions

Copy link
Copy Markdown
range-diff for force push 1a059c9103 → 9f7f89b182 November 13, 2026 02:23 UTC: 2 unchanged, 1 dropped, 3 added; 3 → 5 commits; same base 3e7c10e5fa

reproduce: git range-diff 3e7c10e..1a059c9 3e7c10e..9f7f89b

1:  9157d546e1 +354  -0 5f = 1:  9157d546e1 +354  -0 5f fuzz: libFuzzer frontend via cargo-fuzz
2:  f2419eef88  +83  -0 1f = 2:  f2419eef88  +83  -0 1f nix: fuzz with libFuzzer
3:  1a059c9103   +0  -0 0f < -:  ----------             fuzz with libFuzzer
-:  ----------             > 3:  dbb952f17f  +28 -13 1f nix: verify libFuzzer from an app, not a check
-:  ----------             > 4:  cf2032860f  +35  -0 1f ci: fuzz with libFuzzer on a schedule
-:  ----------             > 5:  9f7f89b182   +0  -0 0f fuzz with libFuzzer

nothingmuch and others added 6 commits November 13, 2026 06:50
cargo-fuzz is the frontend most Rust fuzzing starts from, and the one
OSS-Fuzz builds. The targets are thin wrappers over the shared library.
Like cargo-fuzz's own template, the crate is a workspace of its own.

    cargo fuzz run --fuzz-dir fuzz/libfuzzer fuzz_lattice_laws

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Build the libFuzzer targets with cargo-fuzz in a derivation, and add an
app that runs one of them against a corpus in the checkout, in the
layout cargo fuzz run uses:

    nix run .#fuzz-libfuzzer -- fuzz_lattice_laws -max_total_time=600

Only the corpus lives outside the store, so a run does not depend on
whatever compiler and libraries the calling shell has.

The check, in the nightly aggregate, builds the targets with crash_test's
panic compiled in and confirms libFuzzer finds it within a minute. A
fuzzer with broken coverage instrumentation runs just as cleanly as one
with nothing to find, so the check asserts that a crash is found, not
that none is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Whether libFuzzer solves crash_test within its time limit depends on its
random choices, so fuzz-libfuzzer-verify could pass or fail with no change
to its inputs. A check that flakes like that blames whichever commit
happens to be built, and a hard-to-find regression would point at the
wrong one.

Run it as an app instead, so CI can run it on a schedule:

    nix run .#fuzz-libfuzzer-verify

The time limit goes from 60s to 600s, since an app is not bounded by a
check's budget, and libFuzzer arguments after -- override it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Once a day, confirm libFuzzer still finds the planted crash_test panic,
then fuzz the lattice laws for half an hour. If either step fails, upload
the crash artifacts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a fuzz_scrub bin for the shared scrub target, and fuzz it on the
schedule too. The workflow runs each target as its own matrix job, so a
crash in one does not stop the other, and their artifacts stay apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds a cargo-fuzz frontend over the shared targets and runs it through nix:

    nix run .#fuzz-libfuzzer -- fuzz_lattice_laws -max_total_time=600

The targets are built in a derivation and the app only runs them against
a corpus in the checkout.

Fuzzing is randomized, so it runs from a daily workflow rather than as a
flake check. That workflow first runs fuzz-libfuzzer-verify, which
confirms libFuzzer finds the crash_test panic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
range-diff for force push 9f7f89b182 → 3d6bfa890a November 13, 2026 06:29 UTC: 5 unchanged, 1 added; 5 → 6 commits; base 3e7c10e5fa → f23a1484f3 (diverged, 2 ahead and 1 behind)

reproduce: git range-diff 3e7c10e..9f7f89b f23a148..3d6bfa8

1:  9157d546e1 +354  -0 5f = 1:  79d8c7e578 +354  -0 5f fuzz: libFuzzer frontend via cargo-fuzz
2:  f2419eef88  +83  -0 1f = 2:  f302246a0c  +83  -0 1f nix: fuzz with libFuzzer
3:  dbb952f17f  +28 -13 1f = 3:  1c67148366  +28 -13 1f nix: verify libFuzzer from an app, not a check
4:  cf2032860f  +35  -0 1f = 4:  7cc1641f1a  +35  -0 1f ci: fuzz with libFuzzer on a schedule
-:  ----------             > 5:  743cac1bbd  +28  -4 4f fuzz: scrub with libFuzzer
5:  9f7f89b182   +0  -0 0f = 6:  3d6bfa890a   +0  -0 0f fuzz with libFuzzer

@github-actions

Copy link
Copy Markdown
range-diff for force push 9f7f89b182 → 3d6bfa890a November 13, 2026 06:29 UTC: 5 unchanged, 1 added; 5 → 6 commits; base 3e7c10e5fa → f23a1484f3 (diverged, 2 ahead and 1 behind)

reproduce: git range-diff 3e7c10e..9f7f89b f23a148..3d6bfa8

1:  9157d546e1 +354  -0 5f = 1:  79d8c7e578 +354  -0 5f fuzz: libFuzzer frontend via cargo-fuzz
2:  f2419eef88  +83  -0 1f = 2:  f302246a0c  +83  -0 1f nix: fuzz with libFuzzer
3:  dbb952f17f  +28 -13 1f = 3:  1c67148366  +28 -13 1f nix: verify libFuzzer from an app, not a check
4:  cf2032860f  +35  -0 1f = 4:  7cc1641f1a  +35  -0 1f ci: fuzz with libFuzzer on a schedule
-:  ----------             > 5:  743cac1bbd  +28  -4 4f fuzz: scrub with libFuzzer
5:  9f7f89b182   +0  -0 0f = 6:  3d6bfa890a   +0  -0 0f fuzz with libFuzzer

@yuval-block

Copy link
Copy Markdown
Collaborator Author

🤖 The first libFuzzer run of fuzz_scrub rediscovered rust-psbt#280, which bitcoinfuzz had found with differential fuzzing. #280 is fixed on rust-psbt master but not in psbt-v2 0.3.0, the version we depend on. We found a different symptom of the same root cause.

How it surfaced. One of the target's properties (6e6998a) is that whenever psbt-v2 accepts a PSBT, it also accepts the scrubbed form of that PSBT. Within seconds, libFuzzer produced a 46-byte input that breaks this. It decodes to a v2 PSBT with no inputs and one output, whose output map has these pairs, in this order:

key value
PSBT_OUT_SCRIPT with 17 bytes of key data empty
PSBT_OUT_AMOUNT 8 bytes
PSBT_OUT_SCRIPT d1f10d

v2::Psbt::deserialize accepts this as one output. The scrubber keeps all three pairs and writes them in key order, which puts the plain PSBT_OUT_SCRIPT before the one with key data. psbt-v2 then rejects the scrubbed PSBT with DuplicateKey.

Why. This is #280's mechanism. psbt-v2 0.3.0 treats an empty ScriptBuf as "no script yet" and doesn't check key data on PSBT_OUT_SCRIPT:

  • Read first, the keyed pair stores an empty script. That leaves the field looking unset, so the real script that follows isn't flagged as a duplicate.
  • Read in the other order, the real script is stored first, so the keyed pair is reported as a duplicate.

So psbt-v2's verdict depends on the order of the pairs. The same pattern applies to PSBT_OUT_AMOUNT (zero), PSBT_IN_PREVIOUS_TXID (all zeros) and PSBT_IN_OUTPUT_INDEX (u32::MAX). For example, it also rejects zero-value OP_RETURN outputs.

What changed here. This wasn't a scrubber bug. The target now gives psbt-v2 the input with each map sorted and deduplicated, the way the scrubber writes it, so the reference parser sees the same pair order on both sides. After that change, a 10-minute libFuzzer run (4.4M executions) and a 5-minute AFL++ run (3.4M executions) found nothing. The sorting can stay even after we move to a psbt-v2 release that includes the fix.

Comment thread nix/fuzz/libfuzzer.nix
# This is an app rather than a check: whether the fuzzer finds the crash
# within the time limit depends on its random choices, and a check that
# can fail without a change to its inputs would blame the wrong commit.
apps.fuzz-libfuzzer-verify =

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR CI does not compile the libfuzzer fuzz targets

No checks.* output builds fuzz/libfuzzer, so nix flake check and the per-commit validate-commits run never compile the new workspace; a type or API break that rustfmt does not catch can pass PR CI.

oak · automated · 3d6bfa8

@bc1cindy bc1cindy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at 3d6bfa8 a 1 h fuzz_scrub run (71M execs) still breaks the accepts-implies-scrub-accepts property: 93 inputs have trailing bytes after the declared maps (psbt-v2 ignores them, scrub rejects), 28 have a 0-input tx, see #84 comment

fuzz_lattice_laws: 539M execs, nothing

minimal trailing-bytes case and repro

a valid v0 PSBT plus one 00:

70736274ff010033020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0000000000000000

nix run .#fuzz-libfuzzer -- fuzz_scrub -fork=6 -ignore_crashes=1 -max_total_time=3600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants