Why Fuzzing Works
In 1988 a class project at the University of Wisconsin, set by Barton Miller and published in 1990, fed random characters to standard Unix utilities and crashed a sizeable fraction of them. Random input is a surprisingly good test, because programmers write code for the inputs they imagine, and random bytes are nothing like those. But pure randomness hits a wall the moment a program checks something specific, such as a magic number, a checksum or a keyword. Modern fuzzers get through that wall with one idea: keep the inputs that made the program do something new. This lesson shows, with numbers, why that idea turns an impossible search into a short one.
The wall: one specific value
Take a toy parser that crashes when the first four bytes are T, A, D, !. A random input of length 1 to 8 must be at least 4 bytes long (5 of the 8 lengths) and match four bytes exactly, so the chance per run is
At a million runs per second that is about two hours on average for one four-byte check, and every further byte multiplies the time by 256. Real formats have dozens of such checks in a row.
The idea: coverage as a stepping stone
A coverage-guided fuzzer instruments the target so that every run reports which branches it took. When a mutated input takes a branch no earlier input took, the fuzzer adds it to its corpus and mutates it further. The four-byte check is written as four nested comparisons (in source, or after the compiler and the fuzzer split a memcmp), so matching T alone is already new coverage. The search becomes four searches of one byte each, done one after the other.
Suppose each mutation overwrites one random byte of a 4-byte input with a random value. Matching the next byte needs the right position and the right value:
so each step takes about 1024 runs and the whole check about runs, if the fuzzer always mutates its newest input. A real fuzzer picks the parent at random from the corpus, and at step only the newest of its inputs can make progress, so step costs about runs:
That is nearly six orders of magnitude fewer than . The example checks both numbers, the exact random probability and a simulation of the guided fuzzer over ten seeds. It runs in your browser:
import random
from fractions import Fraction
MAGIC = b"TAD!"
def depth(b):
"""How many magic bytes in a row the input matches (4 = the crash)."""
d = 0
while d < 4 and len(b) >= 4 and b[d] == MAGIC[d]:
d += 1
return d
# 1. Pure random inputs, length 1-8: P(crash) per run, exactly.
p_random = Fraction(5, 8) * Fraction(1, 256) ** 4
print(f"random: P(crash) per run = 1 in {float(1 / p_random):.3g}")
# 2. Model of a guided fuzzer: overwrite one random byte of a 4-byte input, keep progress.
# Each step needs the right position (1/4) and the right value (1/256).
per_step = 4 * 256
print(f"guided model, always mutating the newest input: 4 x {per_step} = {4 * per_step} runs")
print(f"guided model, parent picked at random from the corpus: {per_step} x (1+2+3+4) = {per_step * 10} runs")
# 3. Simulate that guided fuzzer: corpus = inputs that reached a new depth.
def guided(seed, limit=100_000):
rng = random.Random(seed)
corpus, best = [b"abcd"], 0
for run in range(1, limit + 1):
child = bytearray(rng.choice(corpus))
child[rng.randrange(4)] = rng.randrange(256)
d = depth(child)
if d == 4:
return run
if d > best:
best = d
corpus.append(bytes(child))
return None
runs = [guided(s) for s in range(1, 11)]
print("guided, 10 seeds:", runs)
print("mean:", sum(runs) // len(runs))random: P(crash) per run = 1 in 6.87e+09
guided model, always mutating the newest input: 4 x 1024 = 4096 runs
guided model, parent picked at random from the corpus: 1024 x (1+2+3+4) = 10240 runs
guided, 10 seeds: [5017, 20707, 13008, 12889, 3835, 10081, 8439, 14475, 14726, 5898]
mean: 10907
The ten seeds scatter widely (from 3,835 to 20,707 runs, because each step is a geometric waiting time), but the mean of 10,907 is close to the model's 10,240. A random fuzzer would need billions.
The lab: random vs coverage-guided
The lab runs both fuzzers on the same kind of target, deterministically from a seed, with twelve branch edges and a crash behind T, A, D, !. The random fuzzer draws fresh bytes every time; the guided one starts from abcd and keeps every input that reaches a new edge. Switch the mutation strategy and watch the edge counts:
With seed 1 and 50,000 executions:
- Random reaches 7 of the 12 edges (9 in the dictionary run): the easy ones, such as a short input or a last byte that is or isn't a newline. It never reaches the crash.
- Random byte (one overwritten byte per run) gets the guided fuzzer to 11 edges and the crash at execution 24,713.
- Havoc (one to four stacked byte overwrites, insertions and deletions) covers all 12 edges and crashes at execution 14,416, the same order as the simulation above.
- Havoc + dictionary finds the crash at 828: it also writes the bytes the target compares against (
T,A,D,!), the idea behind AFL++'s CmpLog and libFuzzer's comparison tracing. The Corpus, Dictionaries and Minimisation lesson uses it on a real build. - Bit flip stays at 3 edges: one flipped bit cannot turn
aintoT, and since only inputs with new coverage are kept, it never gets a stepping stone. A mutation strategy must be able to reach the values the target checks.
Why it finds security bugs: the oracle
Coverage guidance explains how a fuzzer reaches deep code. Finding a bug there also needs an oracle, something that says "this run was wrong". A crash is the simplest oracle, but most memory errors don't crash: a one-byte over-read returns a plausible value and the program carries on. That is why fuzzing and sanitizers belong together. AddressSanitizer turns every out-of-bounds access into an immediate, reported crash; UBSan does the same for undefined behaviour (libFuzzer Harnesses and Sanitizers as Oracles).
How fuzzing is used for detection, and its limits
Projects run fuzzers continuously against their own parsers (OSS-Fuzz has done so for open-source projects since 2016), so most bugs a fuzzer can find are found by the project first. The limits are real:
- Checksums, compression and cryptography stop mutation: a flipped byte fails the CRC and the parser rejects the input on the first check. Harnesses skip or fix up the checksum, or a structure-aware fuzzer generates valid ones.
- State: a protocol bug that needs five messages in the right order is far from a single mutated input (Protocol Fuzzing).
- Logic bugs with no oracle: if the program returns a wrong answer politely, no sanitizer fires. Differential testing and properties help (Property-Based Testing).
Pitfalls
- Judging a fuzzer by executions per second. A slow fuzzer that keeps the right inputs beats a fast blind one by orders of magnitude.
- Coverage is not correctness. 100% edge coverage says every branch ran, not that every value that matters was tried.
- Short campaigns. Each step is a random waiting time; one seed and one minute prove little. Run long, run several seeds.
Practice
1. The magic grows to 8 bytes. Estimate the random and the guided costs.
Random: about per run of the right length, hopeless. Guided with one-byte overwrites of an 8-byte input: each step costs about runs times the corpus factor, runs. Still minutes.
2. In the lab, why does havoc + dictionary find the crash about 17 times sooner than havoc?
Writing a dictionary byte hits the right value with probability about instead of , so each step needs mainly the right position. 14,416 / 828 is about 17.
3. A parser checks a CRC-32 over the whole file before parsing anything. What happens to a mutation fuzzer, and what do you do?
Almost every mutant fails the CRC, so coverage never grows past the check. Build the harness with the CRC check disabled (a fuzzing build flag) or recompute the CRC in the harness before calling the parser.
Test yourself
What does a coverage-guided fuzzer keep in its corpus?
- Every input it generates
- Inputs that reached code (edges) no earlier input reached
- Only crashing inputs
- Inputs chosen by the user
Why does coverage guidance help so much against a 4-byte magic check written as nested comparisons?
- It reads the magic from the source
- Matching each byte is new coverage, so four 1-in-256 searches replace one 1-in- search
- It runs on a GPU
- It skips the check
In the lab, why does the bit-flip strategy stay stuck at 3 edges?
- It is too slow
- One bit flip cannot turn 'a' into 'T', and without new coverage no intermediate input is kept
- The dictionary is missing
- The seed is wrong
Why are sanitizers used with fuzzers?
- They make the target faster
- They are the oracle: they turn silent memory errors such as small over-reads into reported crashes
- They generate inputs
- They measure coverage