Section 10.11

Generation-based fuzzing

Definition

Generation-based fuzzing is fuzzing §10.9, the automated testing of a program on very many inputs, in which each input is built from scratch from a model of the input format, such as a grammar: rules that say how valid inputs are composed. Inputs that follow the format pass the target’s format checks and reach the code behind them, and the generator can compute lengths, counts and checksums §4.32 as it writes them. The risk is that the model leaves out the malformed inputs that the target must refuse.

The idea

Consider a binary message format with a header, a count field and that many records, where each record can contain further records. A mutation-based fuzzer, which edits example inputs, rarely turns a message with two records into a valid message with five nested levels: it would have to insert whole records and fix every count and length on the way. A generator builds such messages directly. It chooses the structure first and writes the bytes afterwards, so every derived field is consistent. Generation-based fuzzing therefore reaches deep, valid structures that mutation reaches only by luck. Csmith, which builds random C programs to test compilers, is a well-known generation-based fuzzer.

The model is also the blind spot, because a generator produces only what its model allows. If the model always computes the count field from the actual number of records, it never produces a message whose count disagrees with its content, and that is exactly the input a parser must refuse without reading past the end of the message. If the model caps nesting at three levels, it never tests the fourth. A generator built only from the description of valid inputs tests how the target handles valid inputs and says nothing about how it handles invalid ones.

How it works

A generation-based fuzzer works in four stages.

  1. Model. Describe the format: a grammar, a schema, or code that makes structured choices. For a binary format the model names each field, its width, its byte order §4.21, the values it may take, and which fields are derived from others, such as a length computed from the data that follows.

  2. Choice. Build a tree of structured values by making random choices: which kind of record, how many items, what value in each field. A size budget and a depth limit stop the tree from growing without end.

  3. Fault operators. With a chosen probability, apply one deliberate fault to the finished tree at a chosen place, and record which fault was applied.

  4. Serialization §4.28. Write the tree out as bytes, computing the derived fields as the last step, except where a fault operator has set a wrong value on purpose.

Each typical fault operator breaks one rule of the format.

Applying one fault at a time tells the test which check must reject the input; a smaller share of inputs combines faults, because some bugs need two at once. Every input then has an expected outcome. A valid input must be accepted with the correct result. An input with a fault must be refused cleanly: an error, no change of stored state, no crash, and time and memory within stated bounds. The fuzzer also counts the inputs of each class it produced and reports every cap it applies, such as the depth limit.

Worked example

A toy format encodes a list as a 1-byte count nn followed by nn items of 1 byte each, so a valid input is 1+n1 + n bytes long. The specification requires a parser to refuse an input whose length is not 1+n1 + n. A valid generated case with n=2n = 2 and the items A and B is the 3 bytes 02 41 42, written in hexadecimal, since the characters A and B have the ASCII codes 0x41 and 0x42.

A generator that always sets the count to the number of items it wrote cannot produce 03 41 42: count 3, but only 3−1=23 - 1 = 2 item bytes present. A parser that trusts the count reads a third item from the byte after the end of the message, an out-of-bounds read §4.5. The off-by-one fault operator on the count produces exactly this input, together with 01 41 42 (count too small: 1 item declared, 2 present), and the maximum-value operator produces ff 41 42 (count 255, the largest value of the field). All three inputs must be refused.

Now let an item be a list itself, so that inputs nest. Suppose a parser handles each nesting level with one recursive call that uses 128 bytes of stack, and that 1 KiB of stack is available for parsing. Then 1024/128=81024 / 128 = 8 levels fit, and a ninth level would need 9×128=11529 \times 128 = 1152 bytes, a stack overflow §4.10, unless the parser refuses depths beyond 8. A generator with a depth cap of 3 never produces depth 4 or more, so it can never find this stack overflow. Reporting the cap, and adding fixed cases at depths 8 and 9, or at the specified depth limit and one level beyond it, closes the gap.

Stated precisely

Let VV be the set of byte strings that the specification defines as valid, AA the set that the implementation accepts, and GG the set that the generator can produce. The implementation is correct about acceptance when A=VA = V. Two kinds of error are possible: rejecting a valid input, which is an element of VV outside AA, and accepting an invalid input, which is an element of AA outside VV. Testing can expose an error only at an input in GG. A generator with G⊆VG \subseteq V, meaning that every input it can produce is valid, can therefore find valid inputs that are rejected or handled wrongly, but never an invalid input that is accepted, and accepting invalid input is the error that attackers exploit. To test refusal, GG must contain invalid inputs, above all those one fault away from a valid input, because those pass the most checks before reaching the one that must reject them.

A cap removes a region from GG. With a depth cap cc, GG contains no input deeper than cc, whatever the specification says about those depths. A campaign gives evidence only about inputs in GG, and only about the inputs actually drawn; a report states GG by naming the model, the caps and the fault operators. A model that matches the specification is still no evidence about AA: the generator produces what its author believes the format is, and the parser accepts what its own author coded.

Limits and common mistakes

A generator and a parser written from the same wrong reading of the specification agree with each other and are both wrong. Check the model against independent encodings, such as example bytes printed in the specification or produced by another implementation.

The model costs effort and must change with every version of the format; a stale model tests an old format.

Generated inputs vary only as much as the model’s choices do. A bug that depends on a detail the model always fixes, such as one particular byte order or the alignment §4.22 of a field, is never reached. Combining generation with mutation and with coverage feedback covers part of this.

Protocols have state. A generator of single messages does not test sequences: a message that is valid only after a handshake, the same message twice, or messages in the wrong order. Generate sequences as well.

Caps keep runs short, but every cap excludes inputs. Name the excluded region and test its boundary with fixed cases.

Generating an invalid input is half the test. The other half is an oracle that checks the refusal: an error result, no change of state, no crash, and bounded time and memory.

Questions answered

How are intentionally invalid cases generated without excluding dangerous edge cases?

Intentionally invalid cases are generated by first building a valid input from the format model and then applying one named fault operator, a deliberate violation of one rule of the format, at a chosen place, while keeping track of every class of input the generator can and cannot produce. In generation-based fuzzing, a fuzzer builds every test input from a model of the input format, such as a grammar, and a model that describes only valid inputs never produces the malformed inputs that a parser must refuse.

The practice has five parts.

  1. Fault operators for every rule. Counts and lengths off by one, zero and at the maximum of the field; truncation at every field boundary; forbidden values; offsets outside the message or overlapping; parts in the wrong order, missing or repeated; nesting one level beyond the limit; wrong checksums.

  2. One fault at a time, plus some combinations. A single fault shows which check must reject the input; combinations catch bugs that need two faults.

  3. Derived fields left breakable. Lengths, counts and checksums are computed automatically by default, but each one also has an operator that sets it wrong, so that the most dangerous mismatch, a count larger than the data, is produced.

  4. Caps reported and their boundaries tested. A depth or size cap keeps runs short but excludes everything beyond it; inputs at the cap and just beyond it, and at the limit the specification sets and just beyond it, are added as fixed cases.

  5. An oracle for refusal. Every invalid input must be refused with an error, with no change of stored state, no crash, and time and memory within stated bounds.

Counting the inputs of each class in every run shows whether each class is actually reached.

Related entries: Property-based testing (§10.7), Structure-aware mutation (§10.17), Test oracle (§10.5), Serialization (§4.28), Mutation-based fuzzing (§10.10), Out-of-bounds read (§4.5), Stack overflow (§4.10). Further reading: [231].