Section 10.17

Structure-aware mutation

Definition

Structure-aware mutation is a way of making new test inputs for fuzzing §10.9, the testing of a program with large numbers of generated inputs. Instead of changing raw bytes, the mutator decodes an existing input into the fields and records of its format, changes them, and encodes the result again. It recomputes fields derived from other fields, such as lengths and checksums §4.32, so that the new input passes the format checks and reaches deeper code. The tester decides which checks the mutator keeps valid and which it breaks on purpose.

The idea

Take a message format in which every record states its own length, the message states its total length, and a checksum, a short value computed from all the other bytes, closes the message. In mutation-based fuzzing §10.10, the fuzzer changes random bytes of a valid input. Almost every such change breaks the total length or the checksum, so the parser rejects the input in its first few lines, and the code that handles the records almost never runs. A random value in a one-byte checksum is right with probability 1 in 256. In a 4-byte check value, such as the common 32-bit cyclic redundancy check (CRC-32), it is right with probability 1 in 2322^{32}, about 1 in 4.3 billion.

Structure-aware mutation makes the change where the format’s meaning lives. It adds, say, one byte to a record’s value in the decoded form, and the encoder writes lengths and a checksum that agree, so the record handler receives a value it has not seen before. Generation-based fuzzing §10.11, which builds every input from a format description, also reaches deep code; structure-aware mutation differs in starting from existing inputs, so it keeps whatever made them interesting. The same machinery can also break one check on purpose, which matters because the checks are code too: an input that satisfies every check never runs the code that rejects bad input.

How it works

A structure-aware mutator works in four steps.

  1. Decode. It takes an input from the fuzzer’s corpus, the collection of inputs the fuzzer keeps, and decodes it into a structured form: a tree of messages, records and fields. Decoding needs a description of the format, such as a hand-written decoder, a grammar or a schema.

  2. Mutate. It changes the structured form at field level. It replaces a value, sets an integer to a boundary value such as 0 or the largest value the field can hold, inserts, deletes, duplicates or reorders a record, changes a record’s type to another type, or copies a record from another input.

  3. Encode under a repair policy. It writes the structured form back to bytes, a step also called serialization §4.28. A repair policy, chosen by the tester, says for each check of the format whether the encoder makes it pass (the check is repaired), makes it fail (the check is violated on purpose) or leaves whatever the mutation produced. Repairing usually means recomputing a derived field, one computed from other fields: a length, count, offset, checksum or CRC, or, in a test build, a message authentication code §5.7 (MAC), a tag computed from a secret key and the message.

  4. Deliver and judge. The fuzz harness §10.13, the small program that passes each input to the code under test, runs the target. A test oracle §10.5, the rule that decides whether the observed behavior is correct, judges the result. In coverage-guided fuzzing §10.12, the fuzzer also keeps every input that executed code no earlier input reached.

Tools offer hooks for this. libFuzzer §10.26 lets a fuzz harness replace libFuzzer’s built-in byte changes with a mutation function of its own, and the library libprotobuf-mutator mutates inputs described as Protocol Buffers messages, Google’s schema language for structured data. Fuzzing that uses such a mutator is often called structure-aware fuzzing.

Worked example

The format is invented for this example, and byte values are written in hexadecimal. A message is laid out as follows.

The parser applies four checks in this order: (1) the message is exactly B+3B + 3 bytes long; (2) the checksum matches; (3) the version is 01; (4) the records exactly fill the body. A message that passes all four goes to the handler for each record’s type.

The seed, a valid input from the corpus, holds one record of type 07 with the 2-byte value 10 20:

01 04 07 02 10 20 3e

The body is 07 02 10 20, so B=4B = 4 and the message is 4+3=74 + 3 = 7 bytes long. The checksum is the sum of the first six bytes, in decimal 1+4+7+2+16+32=621 + 4 + 7 + 2 + 16 + 32 = 62, which is 3e.

The goal is to run the type-07 handler on a 3-byte value. A byte-level mutation inserts 30 after 20, giving 01 04 07 02 10 20 30 3e: 8 bytes, while B+3=7B + 3 = 7, so check 1 rejects it. To pass, the mutation would also have to set the body length, the record length and the checksum correctly; three random bytes are all right with probability 1/2563=1/16,777,2161 / 256^3 = 1 / 16{,}777{,}216.

The structure-aware mutator decodes the seed into version 01 and one record (type 07, value 10 20), appends 30 to the value and encodes with every check repaired: L=3L = 3, B=2+3=5B = 2 + 3 = 5, and checksum 1+5+7+3+16+32+48=1121 + 5 + 7 + 3 + 16 + 32 + 48 = 112, which is 70. Each variant below breaks one check on purpose and keeps every other check repaired.

Case Bytes Required result
All repaired 01 05 07 03 10 20 30 70 handler runs on 3 bytes
Check 4 violated 01 05 07 02 10 20 30 6f rejected at check 4
Check 2 violated 01 05 07 03 10 20 30 71 rejected at check 2
Check 1 violated 01 06 07 03 10 20 30 71 rejected at check 1

In the check-4 row the record claims L=2L = 2, so it ends after 2+2=42 + 2 = 4 body bytes, and the one byte left in the body, 30, cannot hold a record, which needs at least a type byte and a length byte. Its checksum, 1+5+7+2+16+32+48=1111 + 5 + 7 + 2 + 16 + 32 + 48 = 111, which is 6f, is correct, so the input passes checks 1 to 3 and reaches the record-length check, the code under test. In the check-2 row, 71 replaces the correct checksum 70. In the check-1 row, B=6B = 6 promises 6+3=96 + 3 = 9 bytes but 8 arrive, while the checksum 1+6+7+3+16+32+48=1131 + 6 + 7 + 3 + 16 + 32 + 48 = 113, which is 71, is correct. For each rejected row, the test oracle checks that the input is rejected at the named check, that the handler does not run and that nothing is stored.

Stated precisely

Let the format have the checks c1,…,ckc_1, \dots, c_k, numbered in the order in which the target applies them. For one test case, the repair policy puts each check into exactly one of three sets: RR, the checks the encoder repairs; VV, the checks it violates on purpose; and UU, the checks it leaves to chance. The required result follows from the sets.

A set of test cases tests the rejection code of cjc_j only if some case violates cjc_j while every earlier check passes; a check repaired in every case is never tested. The repaired cases assume that the mutator computes each repaired field exactly as the format’s specification requires. If the mutator computes a field wrongly, every input fails that check and nothing behind it runs. If the mutator and the target share the same wrong computation, every input passes and the error in the check stays hidden.

Limits and common mistakes

The mutator’s decoder and encoder are a second implementation of the format, and they must be updated whenever the format changes. A misunderstanding of the format that the mutator shares with the target causes no test failure, because both sides agree.

An encoder that always writes the standard, canonical form never produces unusual encodings that a lenient target might accept: a duplicated record, bytes after the last record, or a length written in a longer form than needed. Keeping some byte-level mutation without repair covers part of this gap.

Repairing a message authentication code (MAC) needs the key. Tests in which a mutator holding a test key repairs the MAC show what the code behind the MAC check does with content from a key holder. They are not evidence that an attacker without the key can reach that code. The test key must never be a key that real devices use.

The test oracle must match the repair policy. An oracle that expects acceptance of an input with a violated check reports a false failure; an oracle that checks only for crashes misses a target that silently accepts such an input.

Questions answered

Which checks are repaired and which are intentionally violated?

The checks that guard the code under test are repaired, and a check is violated on purpose when its own rejection code is what a test is for. The tester declares this choice for each set of tests. Repairing means that the mutator, the program that changes inputs, recomputes a field so that its check passes. The usual candidates are derived fields that random byte changes almost never get right: lengths, counts, offsets, checksums and CRCs (cyclic redundancy checks), and, in a test build with a test key, a message authentication code (MAC), a tag computed from a secret key and the message.

Each check must also be violated in test cases of its own, one check at a time with every earlier check repaired, so that the input reaches that check and fails it; the test oracle then expects rejection there and no change of state. Rules about meaning, such as value ranges and allowed record types, are usually left to the mutation, because the handlers must survive inputs that break them. A check repaired in every case is never tested itself, and passing a MAC check with a test key says nothing about an attacker without the key.

Related entries: Mutation-based fuzzing (§10.10), Generation-based fuzzing (§10.11), Fuzz harness (§10.13), Message authentication code (MAC) (§5.7), Coverage-guided fuzzing (§10.12), Test oracle (§10.5), Checksum versus CRC (§4.32).