Section 10.10

Mutation-based fuzzing

Definition

Mutation-based fuzzing is a kind of fuzzing §10.9, the automated testing of a program on very many inputs, in which each new input is made by changing an existing input, called a seed, with small random edits: flipping bits, replacing, inserting or deleting bytes, or splicing two inputs together. A valid seed already has the right structure, so most of its mutants, the inputs made from it, keep that structure and reach code that random bytes would not. Unlike generation-based fuzzing §10.11, mutation-based fuzzing needs no model of the input format.

The idea

Random bytes almost never form a valid input. A file format typically opens with a fixed signature of several bytes, then has length fields that must match the data, and ends with a check value computed over the data. A random input fails the first check, and the fuzzer spends its time in the same few lines of rejection code. A real file, by contrast, passes every check. Changing one byte of it produces an input that is still almost valid and that reaches the code behind the checks, where the interesting behavior is. Mutation-based fuzzing explores the neighborhood of known good inputs.

The weakness comes from the same structure. Nested checks reject most mutants at the first check that a mutation breaks. A checksum §4.32 or CRC (cyclic redundancy check), a short value computed from the data that the receiver recomputes and compares, is a typical such check: a CRC-32 detects every change confined to 32 consecutive bits, so every single-byte mutation of the data it covers is rejected. Length fields, fixed signatures and syntax rules act the same way. Getting past them takes either help for the fuzzer or a mutant whose dependent fields have been repaired.

How it works

A mutation-based fuzzer repeats one loop.

  1. Select an input from the corpus, the current set of inputs worth mutating; at the start this is the seed corpus §10.14.

  2. Mutate it with one or several edits chosen at random: flip a bit; replace a byte with a random value or a boundary value such as 0x00, 0x7f, 0x80 or 0xff; insert or delete bytes; copy a block within the input; insert a token from an input dictionary §10.16, a list of byte strings that matter to the format, such as keywords and signatures; or splice two corpus inputs together.

  3. Run the target on the mutant through the fuzz harness §10.13, the code that hands one input to the target, and apply the test oracle §10.5, the check that decides whether the run failed.

  4. Keep the mutant in the corpus if the feedback says it is new, for example because it reached code that no earlier input reached.

Four techniques get mutants past nested checks.

An authentication §1.6 tag is not a checksum. Anyone can recompute a checksum; a message authentication code §5.7 (MAC), a tag computed with a secret key, can be recomputed only by a holder of the key. No mutation produces a valid MAC by chance: a random 128-bit tag is right with probability 2−1282^{-128}. To fuzz the parser behind a MAC check, the harness either holds a test key and computes the correct tag for every mutant, or calls the parser directly, below the check. Either way, the campaign tests how the parser handles authenticated input, which matters if a key holder sends bad data or a key leaks. It does not show that an attacker without the key can reach the parser, and it does not test the MAC check itself. A separate campaign sends mutants with wrong tags and requires that every one is rejected with no change of state.

Worked example

A toy format has three parts: a 1-byte length LL, then LL payload bytes, then a 1-byte checksum equal to the sum of all earlier bytes modulo 256. The parser rejects an input whose total size is not L+2L + 2 bytes or whose checksum does not match. The seed carries the payload 0x10 0x20 0x30 0x40, so L=4L = 4. Its checksum is 4+16+32+48+64=1644 + 16 + 32 + 48 + 64 = 164, which is 0xa4. The seed is the 6 bytes 04 10 20 30 40 a4, written in hexadecimal.

Consider every single-byte mutation of the seed: 6 positions, each with 255 other values, 6×255=15306 \times 255 = 1530 mutants.

All 1530 mutants stop at the two checks, and the payload parser never sees a changed payload.

A repairing mutator changes a payload byte and then recomputes the checksum. Changing 0x20 to 0x21 raises the sum by 1, to 165, so the repaired checksum is 0xa5. All 4×255=10204 \times 255 = 1020 payload mutants now pass both checks and reach the payload parser. The fuzzer also keeps a second stream of unrepaired mutants, and of length mutants such as L=5L = 5 with only 4 payload bytes, so that the rejection paths stay tested. If the format ended with a 16-byte MAC instead of the checksum, the repair would need the key, as described above.

Stated precisely

Model the target as a chain of checks G1,G2,…,GmG_1, G_2, \dots, G_m that an input must pass, followed by the code PP behind them. For each check, a mutation strategy either preserves it (the check’s condition stays true, by repair or by the choice of edits), violates it on purpose, or ignores it.

For a check value of bb bits that anyone can compute, a mutant that changes the covered bytes at random without repairing the check value passes with probability about 2−b2^{-b}; a small change that the check is built to detect, such as any single changed byte under a CRC-32, never passes; a repaired mutant always passes. A MAC of bb bits differs in one point: without the key no repair is possible, and each guessed tag passes with probability about 2−b2^{-b}.

Limits and common mistakes

Over-repair hides bugs. A mutator that always repairs lengths and checksums never sends the inconsistent inputs that the parser must reject. Keep unrepaired and deliberately inconsistent mutants as well.

Mutants stay near their seeds. A message type, an option or a nesting depth that appears in no seed is reached rarely or never. Review the seed corpus against the format specification, or combine mutation with generation-based fuzzing.

A finding from a build with checks disabled may be unreachable in the real program. Reproduce it with every check enabled and a correct checksum before reporting it; a campaign behind a test key is reported as testing of the parser under the assumption that the input was authenticated.

Mutation-based fuzzing is not mutation testing §10.45. Mutation testing changes the program, not the input, to measure whether a test suite notices the change.

The limits of fuzzing in general apply: an input that reaches new code shows where the search went, not that the result there was correct, and the oracle sees only the failures it checks for.

Questions answered

Can mutations get past nested checksums and syntax constraints?

Random byte-level mutations alone mostly cannot get past nested checksums and syntax constraints, but mutations combined with repair, feedback and good seeds can. In mutation-based fuzzing, each new test input is made by editing an existing valid input, called a seed. Every check in the target, such as a length field that must match the data, a fixed signature, a syntax rule, or a checksum (a value computed from the data that the target recomputes and compares), rejects the edits that break it, and each later check sees only what the earlier checks let through. A checksum is a hard case for blind edits: under a CRC-32 (32-bit cyclic redundancy check), every single-byte change of the covered data is rejected.

Four techniques get past such checks.

  1. Seeds that contain every part of the format, so that deep structures are a small edit away.

  2. Dictionaries of the format’s keywords and signatures, and comparison feedback, in which the fuzzer copies the values that the target compares against into new inputs.

  3. Structure-aware mutators that edit the content and then recompute lengths, counts and checksums.

  4. A fuzzing build that disables a checksum comparison, with every finding confirmed afterwards on an input with a correct checksum.

The campaign must still send unrepaired and inconsistent mutants, because the rejection paths are code that must be tested too.

A cryptographic authentication tag, a message authentication code (MAC) computed with a secret key, cannot be passed by mutation, because nobody without the key can compute it. A harness that holds a test key, or that calls the code below the tag check directly, tests the parser behind the check; it does not show that an attacker can get past the check.

Related entries: Structure-aware mutation (§10.17), Seed corpus (§10.14), Message authentication code (MAC) (§5.7), Checksum versus CRC (§4.32), Input dictionary (§10.16), Mutation testing (§10.45), Generation-based fuzzing (§10.11). Further reading: [24, 107].