Examples — the ACFG parser, its bug, its fix, and its harness¶
Four C files and a build script. Together they are a complete, self-contained target you own, so the first thing you ever fuzz is something nobody else has a claim on.
| File | What it is |
|---|---|
acfg.h |
The wire format and the parser interface, documented. Read this first |
acfg-vulnerable.c |
The parser with one deliberately planted memory-safety defect |
acfg-fixed.c |
The same parser with that defect remediated, and the reasoning |
fuzz-acfg.c |
The libFuzzer harness, plus a standalone driver for toolchains without libFuzzer |
build.sh |
Builds every variant this machine can produce and reports the ones it cannot |
Quick start¶
# host, from this directory
./build.sh
OUT=${TMPDIR:-/tmp}/acfg-build
# a campaign against the vulnerable parser
$OUT/fuzz_vulnerable_standalone -m 200000 1234 $OUT/corpus/seed-one-record.bin $OUT/findings
# the same input against the fixed parser
$OUT/fuzz_fixed_standalone $OUT/findings/current.bin
With the PRNG seed 1234 and the 20-byte seed blob, the campaign produces a UBSan line before
execution 20,000 and an ASan heap-buffer-overflow at execution 143,121. Those numbers are
reproducible on the same build; a different seed gives different ones, which is exactly why the seed
is a parameter.
./build.sh replay builds everything and then replays a set of generated reproducers against both
parsers, side by side.
Nothing here is built into the repository¶
build.sh writes to ${TMPDIR:-/tmp}/acfg-build by default — outside this tree. Binaries,
object files, corpora and crash artefacts are all on the repository's artefact blocklist. If you
change OUT, keep it out of the tree.
The bug, and why it is that bug¶
The planted defect is a missing destination-size check on a memcpy. The parser correctly
verifies that the name bytes are present in the input, three lines above the copy, and then copies
up to 255 of them into a 32-byte field without asking whether they fit.
That shape was chosen because it is the one that survives code review. A reviewer skimming for "is there a bounds check?" finds one immediately above the copy and moves on. Chapter 70 §5.2 and §6.2 are built on it.
What the same defect does, measured¶
Built with gcc 11.4.0 -fsanitize=address,undefined -O1 and run:
count |
nlen |
Sanitizer output | Exit code |
|---|---|---|---|
| 1 | 58 | AddressSanitizer: heap-buffer-overflow ... WRITE of size 58 |
1 |
| 1 | 32 | runtime error: index 32 out of bounds for type 'char [32]' — UBSan only |
0 |
| 4 | 40 | runtime error: index 40 out of bounds for type 'char [32]' — UBSan only, ASan silent |
0 |
One defect, three faces. The second and third are invisible to a campaign graded on exit codes, and
the third is invisible to ASan entirely, because ASan places no redzone between two structs inside a
single allocation. sizeof(acfg_record) is 56 bytes, so at count = 1 the overflow leaves the
allocation and at count = 4 it does not.
This is the material behind chapter 70 §3.7, and it is the reason that chapter insists on building with ASan and UBSan and on grading campaigns by log content rather than exit status.
Severity of this defect: none¶
Nothing on any device parses ACFG. It is a bug in a teaching artefact. The full technical analysis in §7.8 is identical in form to what a real finding would carry, and the verdict is still nothing — because reachability, not mechanism, is what makes a bug a vulnerability.
Building for Android¶
export NDK=$HOME/Android/Sdk/ndk/<version>/toolchains/llvm/prebuilt/<host>
ABI=x86_64 ./build.sh
Two things that will bite you, both measured:
-lmis required.libclang_rt.fuzzer-<abi>-android.ais C++ and referencesceilfandsqrt; the C driver does not link libm on Android, and the link fails withundefined symbol: ceilfpointing at a file inside the sanitizer runtime. Linking withclang++works too.- Check the target's page size first.
adb shell getconf PAGE_SIZE. NDK r25 and earlier produce 4 KB-aligned binaries that segfault on a 16 KB-page system image — including a three-linemainthat does nothing. NDK r27 or newer is required for those images.
Extending this¶
Good exercises, in order of difficulty:
- Add a second record type with its own sub-format, and see what it does to the corpus you need.
- Add a CRC over the record area, and solve the checksum problem from §3.4 in the harness.
- Plant a different bug — an integer overflow in an allocation size, or a use-after-free on an error path — and see whether your triage reaches the right conclusion without looking at the source.
- Make the parser multi-threaded and try to find a race. Notice how much less help the tools are.
Lab ../labs/lab-02-harness-and-triage.md walks the whole
loop end to end, and lab 03 does the triage at scale.