Section 10.16

Input dictionary

Definition

An input dictionary is a list of byte strings, called tokens, that a fuzzer (a tool that runs a program on very many automatically changed inputs) inserts into its inputs or writes over parts of them. The tokens are keywords, magic numbers (fixed values that identify a format), delimiters and special values that the program compares its input against and that random byte changes would almost never produce. A dictionary helps inputs get past those comparisons. Unlike a grammar, it does not describe how a valid input is built.

The idea

Many parsers compare a fixed multi-byte value before they look at anything else. A file format may require its first 4 bytes to be a particular signature, and a text format may accept only the keywords true, false and null at a given place. A fuzzer that changes bytes at random almost never writes all of a 4-byte value correctly at once. Coverage feedback, which keeps inputs that execute new code, does not help when the 4 bytes are compared in one instruction: that comparison is one branch, so an input with 3 of the 4 bytes right executes the same code as an input with none right.

A dictionary hands the fuzzer the value whole: one mutation that copies the token into the input replaces billions of random tries. A phrase book is a fair picture. With one, a traveler can say the words the listener is waiting for without building them letter by letter. The picture stops at grammar. Knowing the words is not knowing how to arrange them, and an input made of correct tokens with a wrong length field, a wrong checksum §4.32 or a wrong order is still rejected.

How it works

  1. Collect tokens from three places: the format’s specification (signatures, keywords, field names, type and command codes, version numbers, and delimiters such as quotation marks, brackets and line ends); the program’s source code (constants that input bytes are compared against); and the fuzzer itself. libFuzzer §10.26, and AFL++ §10.27, the successor of the fuzzer AFL (American fuzzy lop, named after a breed of rabbit), can observe comparison instructions while the program runs and use their operands as tokens automatically.

  2. Add values just outside the valid set for each field the dictionary covers: an unassigned type code, a version number one above the highest supported, length bytes such as 00, 7F, 80 and FF (hexadecimal), the maximum length and the maximum plus 1, and characters that need escaping.

  3. Write the file. libFuzzer reads the same syntax that AFL reads with its -x option: one token per line, written as a name, an equals sign and the token in double quotes, with \x followed by two hexadecimal digits for any byte. The name may be omitted, and lines that start with # are comments. libFuzzer takes the file with the option -dict=FILE.

  4. Run. The mutator, the part of the fuzzer that changes inputs, mixes dictionary operations with its ordinary random changes. A dictionary operation inserts a token at a position, or writes the token over the bytes at that position.

  5. Measure. Compare the coverage reached with and without the dictionary, and remove tokens that open no new code.

A small dictionary file:

# signature of the format
magic="FUZZ"
kw_true="true"
version_too_high="\x03"
"\xff\xff"

The last line has no name and stands for the two bytes FF FF.

Worked example

A parser accepts an input only if its first 4 bytes are 46 55 5A 5A, the ASCII text FUZZ, and it compares the 4 bytes in one 32-bit comparison. Inputs are 8 bytes long.

Without a dictionary. If the 4 bytes were uniformly random, they would match with probability (1/256)4=1/4,294,967,296(1/256)^4 = 1/4{,}294{,}967{,}296, about one input in 4.3 billion. Coverage feedback gives no credit for a partial match.

With a dictionary. The dictionary holds 8 tokens, one of them FUZZ. Assume, for this example only, three things: the fuzzer chooses the operation “write a dictionary token over the input” for 1 mutation in 16; it chooses the token uniformly from the 8; and it chooses the start position uniformly from the 5 positions, 0 to 4, at which a 4-byte token fits inside 8 bytes. The token lands at position 0 with probability 116×18×15=1640.\frac{1}{16} \times \frac{1}{8} \times \frac{1}{5} = \frac{1}{640}. The fuzzer needs about 640 mutations on average instead of about 4.3 billion inputs, a factor of 4,294,967,296/640≈6,710,8864{,}294{,}967{,}296 / 640 \approx 6{,}710{,}886, about 6.7 million.

Dilution. With 16 tokens instead of 8, the same calculation gives 1/16×1/16×1/5=1/1,2801/16 \times 1/16 \times 1/5 = 1/1{,}280: doubling the dictionary doubles the expected wait for the one useful token.

What is still missing. Suppose byte 4 is a length and byte 7 a checksum. The token sets neither. Inputs that start with FUZZ reach the next test and mostly fail there. From that point ordinary mutation and coverage feedback take over, and the checksum needs a different technique.

Stated precisely

Let DD be the dictionary, a finite set of byte strings, and let |D||D| be its number of tokens. A dictionary adds two kinds of mutation: write a token d∈Dd \in D over the bytes that start at position pp, and insert dd at position pp. Consider the model of the example: the fuzzer picks a dictionary operation with probability qq, picks the token uniformly from DD, and picks the position uniformly from PP possible positions. Under that model, one mutation places a given token at a given position with probability q|D|⋅P.\frac{q}{|D| \cdot P}. Writing the same nn bytes as uniformly random bytes succeeds with probability 256−n256^{-n}. Real fuzzers choose their operations adaptively, so the formula is a model, not a prediction of their exact time to success.

The contract of a dictionary is exactly this: inputs that contain a token at a chosen place become reachable in one mutation. A dictionary promises nothing about whether the rest of the input is valid, which code will be covered, or which tokens matter.

Limits and common mistakes

A dictionary is not a grammar. Inserting a token does not fix a length field, a checksum, nesting or protocol state. For those, use structure-aware mutation §10.17, which changes a parsed form of the input and encodes it again, or generation-based fuzzing §10.11, which builds inputs from a description of the format.

A token helps only if the program compares it with bytes taken directly from the input. If the input is decoded, decompressed or decrypted first, the token must appear in its encoded form, or the fuzz harness §10.13, the code between the fuzzer and the program, must hand the decoded data to the code under test.

Large dictionaries dilute: the chance of picking the useful token falls in proportion to the number of tokens, so irrelevant tokens slow the search.

A dictionary of valid, common values overfits to normal cases. The fuzzer then produces mostly well-formed, common messages and spends little time on unknown versions, unassigned types and limit values, where parsers often fail. Add the invalid and boundary tokens deliberately, and keep ordinary random mutation running.

Comparison feedback in modern fuzzers finds many tokens automatically, but only for comparisons in instrumented code that the fuzzer’s inputs already reach. A written dictionary still helps for the rest.

Questions answered

Which constants improve reachability §3.21 without overfitting only normal cases?

The constants that improve reachability are those the program compares input bytes against at points where random mutation alone would stall: format signatures and other magic numbers, keywords and field names, type and command codes, version numbers, and delimiters and escape characters. Put them in the input dictionary, the list of byte strings that the fuzzer inserts into its inputs. To avoid overfitting to normal cases, add for each such field the values just outside the valid set: the next unassigned type code, a version one above the highest supported, the version 0, lengths 0, the maximum and the maximum plus 1, and bytes such as 00, 7F, 80 and FF (hexadecimal). Keep ordinary random mutation running alongside the dictionary.

Check the effect by fuzzing §10.9 with and without the dictionary and by removing tokens that open no new code. A dictionary never makes inputs valid: length fields, checksums and protocol state still need structure-aware mutation, an input generator, or a fuzzing build that skips the check.

Related entries: Mutation-based fuzzing (§10.10), Structure-aware mutation (§10.17), Coverage-guided fuzzing (§10.12), Generation-based fuzzing (§10.11), Seed corpus (§10.14), Boundary-value testing (§10.31).