The bug in the room nobody entered
A settings reader has a bug, but only inside a [limits] section. Every test file the team has is about names and colours.
A fuzzer starting from those files has to stumble into [limits] by luck, unless you show it the way.
The one rule
Warning
The reader you fuzz is your own code on your own lab machine. Fuzzing a system you do not own needs its owner's written permission. In the UK the Computer Misuse Act 1990 makes that a legal matter.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can use coverage to judge whether a fuzzer is reaching all of my code.
Key words
| Word | What it means | Example |
|---|---|---|
| Corpus | the set of inputs a fuzzer keeps and builds on | |
| Coverage | which lines of code the inputs have run | |
| Dictionary | words of the input format given to the fuzzer | "[limits]" |
| Merge | keeping only the inputs that add coverage | -merge=1 |
| Global buffer overflow | a write past an array outside any function |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA reader with a section to find
read_conf reads [section] lines and key=value lines. Inside [limits], a line class=N counts a class in an array of eight. The check lets N be 8, one past the end.
Write it in two parts, then the harness and two seed files.
Write the first half of the reader
mkdir -p ~/fuzz/conf && cd ~/fuzz/conf && cat > conf.c <<'END'
#include <stddef.h>
#include <string.h>
static int per_class[8];
int read_conf(const char *text, size_t len) {
int count = 0, in_limits = 0;
size_t i = 0;
while (i < len) {
size_t end = i;
while (end < len && text[end] != '\n') end++;
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the second half
cd ~/fuzz/conf && cat >> conf.c <<'END'
const char *line = text + i;
size_t n = end - i;
if (n == 8 && memcmp(line, "[limits]", 8) == 0) in_limits = 1;
else if (n > 0 && line[0] == '[') in_limits = 0;
else if (in_limits && n == 7 && memcmp(line, "class=", 6) == 0) {
int c = line[6] - '0';
if (c < 0 || c > 8) return -1;
per_class[c]++;
count++;
} else if (memchr(line, '=', n)) count++;
i = end + 1;
}
return count;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the harness and two seeds
cd ~/fuzz/conf && cat > fuzz_conf.c <<'END'
#include <stddef.h>
#include <stdint.h>
int read_conf(const char *text, size_t len);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
read_conf((const char *)data, size);
return 0;
}
END
mkdir -p seeds && printf 'name=ann\ncolour=red\n' > seeds/basic.conf && printf '[people]\nname=ben\n' > seeds/section.conf && ls seedsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsCoverage shows the lines never run
Build the reader twice. One build is for fuzzing. The other records which lines run, with -fprofile-instr-generate -fcoverage-mapping.
Run the seeds once through the second build and ask llvm-cov for a report. Lines with a count of 0 were never reached.
Build the fuzzer and the coverage build
cd ~/fuzz/conf && clang -g -fsanitize=fuzzer,address -o fuzz-conf conf.c fuzz_conf.c && clang -g -fsanitize=fuzzer -fprofile-instr-generate -fcoverage-mapping -o cov-conf conf.c fuzz_conf.c && ls fuzz-conf cov-confShown here, not run. Press Start lab to type it into your own lab’s terminal.
Measure what the seeds reach
cd ~/fuzz/conf && LLVM_PROFILE_FILE=seeds.profraw ./cov-conf -runs=0 seeds > /dev/null 2>&1 && llvm-profdata-21 merge -o seeds.profdata seeds.profraw && llvm-cov-21 report ./cov-conf -instr-profile=seeds.profdata conf.c | grep TOTALShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the lines never run
cd ~/fuzz/conf && llvm-cov-21 show ./cov-conf -instr-profile=seeds.profdata conf.c | grep -E '^ +[0-9]+\| +0\|'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: keep only inputs that add lines
# Each input of a corpus, and the lines of the reader it reached.
corpus = {
"basic.conf": {1, 2, 3, 8},
"section.conf": {1, 2, 3, 4, 8},
"limits.conf": {1, 2, 3, 4, 5, 6, 7},
"copy.conf": {1, 2, 3, 8},
}
covered = set()
kept = []
for name in sorted(corpus, key=lambda one: -len(corpus[one])):
if corpus[name] - covered:
kept.append(name)
covered |= corpus[name]
print("keep", kept, "covering", len(covered), "lines")
# Add "empty.conf": {1} to the corpus. Is it kept? Why not?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsA dictionary hands over the format's words
A dictionary file lists words the format uses, one in quotes per line. The fuzzer drops them into inputs as it changes them.
Give it [limits] and class=, then fuzz for up to a minute from a copy of the seeds.
Write a two-word dictionary
cd ~/fuzz/conf && printf '"[limits]"\n"class="\n' > conf.dict && cat conf.dictShown here, not run. Press Start lab to type it into your own lab’s terminal.
Fuzz with the seeds and the dictionary
cd ~/fuzz/conf && rm -rf corpus crash-* && mkdir corpus && cp seeds/* corpus/ && DEBUGINFOD_URLS= ./fuzz-conf -seed=1 -dict=conf.dict -max_total_time=60 corpus > fuzz.log 2>&1; grep -E 'SUMMARY|Test unit' fuzz.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a bigger corpus is a better one. A corpus of hundreds of files that reach the same lines only slows every run. What matters is the coverage, and a merge keeps the few files that give it.
Fix the limit, then watch the corpus grow
The array has eight places, numbered 0 to 7, so the check must refuse c >= 8. Fix it, keep the crash as a regression input, and fuzz for another minute.
Then measure coverage again, over the corpus the fuzzer built.
Fix the check and keep the input
cd ~/fuzz/conf && sed -i 's/c > 8/c >= 8/' conf.c && grep -n 'c >= 8' conf.c && clang -g -fsanitize=fuzzer,address -o fuzz-conf conf.c fuzz_conf.c && mkdir -p regress && mv crash-* regress/class-eight && DEBUGINFOD_URLS= ./fuzz-conf regress/* 2>&1 | grep -E 'Running|NOTE'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Fuzz the fixed reader for a minute
cd ~/fuzz/conf && DEBUGINFOD_URLS= ./fuzz-conf -seed=2 -dict=conf.dict -max_total_time=60 corpus > grow.log 2>&1; echo "exit $?"; ls corpus | wc -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the lines still never run
cd ~/fuzz/conf && clang -g -fsanitize=fuzzer -fprofile-instr-generate -fcoverage-mapping -o cov-conf conf.c fuzz_conf.c && LLVM_PROFILE_FILE=corpus.profraw ./cov-conf -runs=0 corpus > /dev/null 2>&1 && llvm-profdata-21 merge -o corpus.profdata corpus.profraw && llvm-cov-21 show ./cov-conf -instr-profile=corpus.profdata conf.c | grep -cE '^ +[0-9]+\| +0\|'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Merge the corpus down to what matters
cd ~/fuzz/conf && rm -rf small && mkdir small && ./fuzz-conf -merge=1 small corpus > merge.log 2>&1; grep MERGE-OUTER merge.log | tail -n 1; ls small | wc -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: shrink a corpus
A corpus maps each input's name to the set of edges (numbered places in the code) it reached. Write two functions.
new_edges(seen, edges) returns the sorted list of edges in edges that are not in the set seen.
minimise(corpus) keeps a small set of inputs that reach every edge the whole corpus reaches. Repeat: pick the input that adds the most edges not yet covered (on a tie, the name that sorts first), until nothing is left to add. Return the sorted list of names kept.
def new_edges(seen, edges):
return []
def minimise(corpus):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go