Section 10.27

AFL++

Full name: AFL plus plus, where AFL stands for American Fuzzy Lop, the breed of rabbit after which Michał Zalewski named the original fuzzer.

Definition

AFL++ is a coverage-guided fuzzer: a tool that runs a target program on very many generated inputs, keeps each input that reaches code no earlier input reached, mutates kept inputs into new ones, and saves the inputs that crash the program or make it hang. It continues the fuzzer AFL (American Fuzzy Lop) and adds many instrumentation §14.24 and mutation methods. To cut the cost of each run, AFL++ runs each input in a child process copied from a prepared process, or runs many inputs in one process, called persistent mode.

The idea

Running a program afresh for each input repeats all the work done before the program reads its input: the operating system creates the process, the dynamic linker §12.49 connects the shared libraries, the C library initializes, and the program reads its configuration. For a small parser this can take milliseconds while the parse takes a tenth of a millisecond, so most of the fuzzer’s time goes to start-up.

AFL++ skips the repeated work, and each way of skipping it also skips some cleaning. A fresh process starts clean because the operating system builds it from nothing. A forked child starts from a copy of a prepared process: its memory is clean, but files, sockets and everything else outside its memory are shared with earlier children. A persistent loop reuses one process, so nothing is cleaned unless the harness cleans it. Rinsing laboratory glassware instead of taking a new beaker is a fair picture: faster, and valid only if the rinse removes every residue that could affect the next measurement. The picture stops in one respect: a fuzzer reports the effects of residue as findings, under the name of an innocent input.

How it works

  1. Instrument. Compile the target with an AFL++ compiler wrapper such as afl-clang-fast. Each edge of the control flow, a possible jump from one block of straight-line code to another, gets a counter in a shared memory area, the coverage map, which the fuzzer reads after every run. Counts are sorted into the ranges 1, 2, 3, 4–7, 8–15, 16–31, 32–127 and 128 or more, and an input is new when some edge reaches a new range. A program without source code can run under an emulator instead, for example in QEMU (Quick Emulator) mode.

  2. Fuzz. The program afl-fuzz takes an input from its queue, mutates it, runs the target, and queues the mutated input if the coverage map shows something new. Crashing inputs go to the output directory crashes, inputs over the time limit to hangs.

  3. Run each input, in one of three ways.

The documentation warns against placing a deferred __AFL_INIT() after the creation of threads or child processes, which fork does not copy; after timers are set; after temporary files, network sockets or offset-sensitive file descriptors §15.21 are created, if their state affects later behavior (a file descriptor is the number by which a process refers to an open file, and its offset is the current read or write position); or after any access to the input. afl-fuzz also shows a value called stability: the percentage of coverage-map entries that stay the same when one input is rerun. The documentation names a drop in stability under persistent mode as a sign that the target keeps state between iterations.

Worked example

Assume these invented costs: start-up S=9S = 9 ms, covering process creation, loading and the program’s own initialization; one fork of the initialized program F=0.4F = 0.4 ms; processing one input P=0.1P = 0.1 ms.

Hygiene has its own costs. If the parser leaks a 1 KiB buffer per input, a fork server discards the leak with each child, while in persistent mode one child leaks 1000×1=10001000 \times 1 = 1000 KiB, about 0.98 MiB, before it is replaced. If the parser sets a global flag on accepting a header and never clears it, input 501 in a persistent loop can crash only because input 500 set the flag. afl-fuzz saves input 501, which does not crash when run alone. Under a fork server, each child starts with the flag clear and this misleading crash cannot occur.

A fork server still shares what lies outside process memory. A log file opened before __AFL_INIT() is shared by all children, with one write position. At 2000 inputs per second and 100 bytes per input, it grows by 2000×100=200,0002000 \times 100 = 200{,}000 bytes per second, which is 200,000×3600=720,000,000200{,}000 \times 3600 = 720{,}000{,}000 bytes per hour; if the program reads that file back, earlier inputs influence later ones.

Stated precisely

Split the state that can influence one run into three parts: process memory MM (global variables, heap, stacks); kernel resources KK held by the process (file descriptors with their read and write positions, sockets, timers, child processes); and external state EE (files on disk, devices, other processes, network peers). Then:

A saved input is guaranteed to reproduce its result alone when every part of MM, KK and EE that influences its run has, at the start of that run in the campaign, the same value as at the start of a fresh run. Persistent mode moves the duty of meeting this condition for MM and KK from the operating system to the harness. No mode meets the condition for EE by itself.

Limits and common mistakes

Speed figures belong to one target. The factor of 10 to 20 in the documentation and the factor of 90 in the example are not bounds for another program.

A high execution rate with state carried between inputs buys crashes that do not reproduce, corpus entries credited with coverage they did not cause, and coverage noise that misleads the search. Rerun every saved crash alone in a fresh process before trusting it; the AFL++ tool afl-tmin can then shrink it.

Stability below 100% has other causes too, such as timers, random numbers and threads, so a low value calls for investigation, not a conclusion. A high value does not prove that no state is carried over: carried state that changes no edge count is invisible to it.

A forked child that writes a file, sends a network message or changes a device changes it for all later children. Isolating those effects needs a fresh temporary directory, a fake network or a reset of the device, not a different fork point.

Firmware for a microcontroller §6.1 can be fuzzed with AFL++ only through a host build or an emulator; peripheral timing, interrupts §6.11 and power failures then lie outside what the campaign tests.

Questions answered

How do persistent execution and fork-based isolation trade speed against state hygiene?

Fork-based isolation gives each input a fresh copy of a prepared process, so the operating system cleans process memory between inputs at the cost of one fork per input; persistent execution runs many inputs in one process, which removes that cost but leaves all cleaning to the harness. In AFL++ (a fuzzer descended from AFL, American Fuzzy Lop) the trade works out as follows.

Use persistent mode when the code under test can be reset cheaply and completely, such as a parser without global state; use a fork server with deferred initialization when the state is hard to reset. In both cases, watch the stability value and rerun every saved crash alone in a fresh process.

Related entries: Coverage-guided fuzzing (§10.12), Fuzz harness (§10.13), Deterministic replay (§10.37), libFuzzer (§10.26), Copy-on-write (§8.11). Further reading: [107, 183].