Algorithm Search⌘K

Techniqueintro

Fuzz testing

Also called fuzzing, random testing for robustness.

Runs a program on many generated or mutated inputs and watches for crashes, hangs and failed checks.

Fuzz testing, or fuzzing, runs a program on a large number of inputs that are made automatically, by generating them at random, from a model of the input format, or by changing stored inputs, and it watches each run for a failure: a crash, a hang, a failed assertion, a report of a memory error checker, or a difference from a reference program. A failure is a bug in the program's handling of input, and in programs that read untrusted data many such bugs are vulnerabilities, such as a buffer overflow. Fuzzing needs no specification of the right output, only a test for an obviously wrong behavior, so it can be applied to large programs with little work. In return, it can show that a bug exists but never that none is left. The name comes from the tool fuzz of Miller, Fredriksen and So (1990), who fed random characters to UNIX utilities and measured that between a quarter and a third of them crashed or hung.

In plain words

Type nonsense into a program and see if it breaks. Do it again and again, very fast, with a machine instead of a person. Most of the time nothing happens. Now and then the program crashes or stops responding, and you keep the input that did it, so that a developer can run it again and find the cause.

Pure nonsense is often rejected at the first check, so better fuzzers make inputs that look almost right: they start from real files and change a few bytes, or they build inputs from a description of the format. Some fuzzers also watch which parts of the program each input runs and keep the inputs that reach new parts.

A long run with no crash is good news, but it does not prove that the program has no bugs.

When it applies

  • The program reads data that can be produced automatically: files, messages, command lines or the arguments of a library function.
  • A failure can be seen without knowing the right answer: the program crashes, hangs, triggers an assertion or a checker such as AddressSanitizer, or disagrees with another program that should give the same output.
  • Each run is short, so that many runs fit in the time budget. Thousands of runs per second are common for small parsers.
  • It does not apply well when a wrong output is the only symptom and there is no oracle, or when each run needs a slow or stateful environment that cannot be reset.

The template

  1. FUZZ(program P, budget B)
  2. 1findings <- empty
  3. 2state <- <initial state of the input source: seeds, a grammar, or nothing>
  4. 3for i <- 1 to B do
  5. 4x <- <make an input from state: generate it, or mutate a stored input>
  6. 5outcome <- <run P on x under a monitor: a time limit, a memory error checker, assertions>
  7. 6if <outcome is a failure> then
  8. 7add (x, outcome) to findings
  9. 8state <- <update state from x and outcome, for example keep x if it ran new code>
  10. 9return <group findings into likely bugs and shrink each kept input>

The holes are the design choices. Line L4 is the input source: random bytes as in Miller's study, mutations of seeds, or inputs from a grammar. Line L5 is the oracle: without a checker, a memory error that does not crash is missed, so fuzzers build the target with sanitizers. Line L8 is the feedback: a blackbox fuzzer leaves the state unchanged, a greybox fuzzer keeps inputs that reach new code, and a whitebox fuzzer solves path constraints. Line L9 is triage: crash bucketing groups failures, and delta debugging shrinks each input.

Classic examples

  • Random input to UNIX utilities. Miller, Fredriksen and So (1990) piped random characters into utilities and drove interactive ones through a pseudo terminal. The causes they report include errors with pointers and arrays and return codes that were not checked.
  • Mutation of sample files. Mutation-based fuzzing flips, sets, inserts and deletes bytes of valid files, so that the parser accepts most of the input.
  • Coverage feedback. Coverage-guided fuzzing keeps the mutated inputs that reach new edges of an edge coverage bitmap and chooses among them with a power schedule.
  • Whitebox fuzzing. Generational search runs an input symbolically, negates each branch condition on its path, and solves for new inputs.
  • Generation from a grammar. Grammar-based fuzzing produces inputs that pass the parser and test the stages behind it, such as the type checker of a compiler.
  • Stateful protocols. Protocol state fuzzing sends sequences of messages to find wrong transitions of a protocol state machine.

Variations

  • Oracle by comparison. Differential testing runs two implementations of one specification on the same input and reports a difference.
  • Oracle by property. Property-based testing states a property of a function, generates typed inputs, and shrinks a failing one to a small case.
  • Directed fuzzing. Directed greybox fuzzing spends the effort on inputs close to chosen targets, such as the lines of a patch.
  • Hybrid fuzzing. Hybrid fuzzing runs a constraint solver when the fuzzer stops making progress.
  • Corpus management. Seed corpus minimization keeps a small set of seeds with the same coverage.
  • Statistics of a campaign. Software testing as species discovery estimates from the counts of rare behaviors how likely the next input is to find something new.

Pitfalls

  • No oracle for silent errors. A read past the end of a buffer often does not crash. Without a memory error checker at line L5, the fuzzer runs over the bug and reports nothing.
  • Inputs rejected at the first check. Random bytes rarely pass a magic number, a length field or a checksum, so the fuzzer tests only the first lines of the parser. Use seeds, a grammar, or a build with the checksum test removed.
  • Counting crashes as bugs. A campaign can produce thousands of crashing inputs for a few bugs. Klees et al. (2018) measured that both the coverage rule and stack hashes count one bug many times, and recommend counting bugs by ground truth, such as distinct fixes.
  • Comparing fuzzers on one run. The result of a campaign is random. Klees et al. recommend many trials of each fuzzer, a statistical test such as the Mann-Whitney U test, long enough campaigns, and stating the seeds used.
  • Reading no failure as no bug. A campaign with no failure gives only a statistical statement about the inputs the fuzzer tends to make, not about all inputs.
  • Non-reproducible failures. A failure that depends on time, threads or the environment cannot be replayed from the input alone. Make the target deterministic, and store the full environment with each finding.

Exercises

  1. A parser fails only when the first two bytes of its input equal a fixed pair. The fuzzer makes inputs of uniform random bytes. Find the probability that one run fails, and the expected number of runs until the first failure. (Answer: 2−162^{-16} and 216=655362^{16} = 65536 runs, by the geometric distribution.)
  2. Each run fails independently with probability p=10−4p = 10^{-4}. Find the least number of runs nn for which the chance of at least one failure is at least 0.95. (Answer: n≥ln⁡20/(−ln⁡(1−p))n \ge \ln 20 / (-\ln(1 - p)), so n=29956n = 29956.)
  3. A campaign of nn independent runs from a fixed distribution has no failure. Show that every failure probability p>3/np > 3/n makes this outcome less likely than 0.05, which is the rule of three for a 95 percent upper confidence bound. Why does this say nothing about inputs that the fuzzer does not make?
  4. Write an oracle for a function that is meant to sort a list of integers. It must catch a result that is not in order and a result that is not a permutation of the input. Which of the two checks would a crash-only monitor miss?

References

  1. Barton P. Miller, Lars Fredriksen and Bryan So, An empirical study of the reliability of UNIX utilities, Communications of the ACM 33(12), 1990, pp. 32 to 44, the fuzz and ptyjig tools and the share of utilities that crashed or hung on random input
  2. Andreas Zeller, Rahul Gopinath, Marcel Böhme, Gordon Fraser and Christian Holler, The Fuzzing Book, chapter Fuzzing: Breaking Things with Random Inputs, the random fuzzer, the run of a program on its output, and the checkers that turn silent errors into failures
  3. Valentin J. M. Manès, HyungSeok Han, Choongwoo Han, Sang Kil Cha, Manuel Egele, Edward J. Schwartz and Maverick Woo, The art, science, and engineering of fuzzing, a survey, IEEE Transactions on Software Engineering 47(11), 2021, pp. 2312 to 2331, the generic fuzzing algorithm and the division into blackbox, greybox and whitebox fuzzers
  4. George Klees, Andrew Ruef, Benji Cooper, Shiyi Wei and Michael Hicks, Evaluating fuzz testing, Proceedings of CCS 2018, pp. 2123 to 2138, the recommendations on repeated trials, statistical tests, timeouts, seeds and counting bugs by ground truth

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises