Every test passed, then a file broke it
A college app reads the class register as rows such as ann,12B,71. Its tests pass. Then somebody exports a row with one comma too many.
The app does not crash. It quietly writes past the end of an array. Nobody wrote a test for that row, because nobody thought of it.
The one rule
Warning
Fuzz only your own code, on your own lab machine. Fuzzing somebody else's program or service needs their written permission. Without it, in the UK, it can break the Computer Misuse Act 1990.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can use a fuzzer to find an input that breaks my own code.
Key words
| Word | What it means | Example |
|---|---|---|
| Fuzzing | feeding a program many changed inputs to find failures | |
| Fuzzer | the tool that makes and runs those inputs | afl-fuzz |
| Seed | a good input the fuzzer starts from | |
| Crash | an input that stops the program with an error | |
| Off-by-one | a limit that is one too high or too low | |
| Regression input | a saved input that once broke the code |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsTests check the inputs you thought of
Install the two tools first: clang, a C compiler, and AFL++, a fuzzer. Neither is in the lab's image, so the Run takes them from Ubuntu's archive, as the lab's own setup does.
Then write split_row. It notes where each field of a row starts, at most four fields. Two ordinary rows work.
Install clang and AFL++
sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq clang afl++ > /dev/null && clang --version | head -n 1 && command -v afl-fuzz afl-clang-fastShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the row splitter
mkdir -p ~/fuzz/rows && cd ~/fuzz/rows && cat > row.c <<'END'
#define MAX_FIELDS 4
int split_row(const char *row, int starts[MAX_FIELDS]) {
int count = 0;
starts[count++] = 0;
for (int i = 0; row[i]; i++) {
if (row[i] == ',') {
if (count > MAX_FIELDS) return -1;
starts[count++] = i + 1;
}
}
return count;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a program that reads rows
cd ~/fuzz/rows && cat > main.c <<'END'
#include <stdio.h>
#include <string.h>
int split_row(const char *row, int starts[4]);
int main(void) {
char row[256];
int starts[4];
while (fgets(row, sizeof row, stdin)) {
row[strcspn(row, "\n")] = 0;
int n = split_row(row, starts);
if (n > 0) printf("%d fields, the last starts at %d\n", n, starts[n - 1]);
else printf("refused\n");
}
return 0;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build it and try two rows
cd ~/fuzz/rows && gcc -Wall -g -o rows row.c main.c && printf 'ann,12B,71\nben,12B,64,late\n' | ./rowsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try a row with five fields
cd ~/fuzz/rows && printf 'cat,12B,80,late,again\n' | ./rowsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA fuzzer tries the inputs you did not
A fuzzer starts from a seed, changes it a little, runs the program and keeps any change that reaches new code. It does this thousands of times a second.
AFL++ also needs a way to see memory mistakes. AFL_USE_ASAN=1 builds in a checker that stops the program at the first one. Lesson 3 explains it.
Try it: a tiny fuzzer in Python
import random
# The same splitter in Python. A list of four places stands in for the C array.
def split_row(row):
starts = [0, 0, 0, 0]
count = 1
for i, ch in enumerate(row):
if ch == ",":
if count > 4:
return -1
starts[count] = i + 1
count += 1
return count
def mutate(row, rng):
at = rng.randrange(len(row) + 1)
return row[:at] + rng.choice(",a1") + row[at:]
rng = random.Random(1)
row = "ann,12B,71"
for attempt in range(1, 501):
row = mutate(row, rng) if len(row) < 20 else "ann,12B,71"
try:
split_row(row)
except IndexError:
print("attempt", attempt, "breaks on", row)
break
# Change Random(1) to Random(2). Does it break on a different row?
Make a free account to save your work and track your progress.
Build it for AFL++, with the checker
cd ~/fuzz/rows && AFL_USE_ASAN=1 afl-clang-fast -g -o rows-afl row.c main.c && file rows-afl | cut -d, -f1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Fuzz it for 30 seconds
cd ~/fuzz/rows && mkdir -p seeds && printf 'ann,12B,71\n' > seeds/row.txt && rm -rf out && AFL_NO_UI=1 AFL_SKIP_CPUFREQ=1 AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 afl-fuzz -i seeds -o out -V 30 -- ./rows-afl > afl.log 2>&1; grep -a Statistics afl.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the crashes it saved
cd ~/fuzz/rows && ls out/default/crashesShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the first crash again
cd ~/fuzz/rows && DEBUGINFOD_URLS= ./rows-afl < out/default/crashes/id:000000* 2>&1 | grep -E 'of size|SUMMARY'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a fuzzer only finds programs that crash anyway. It finds the quiet bugs too, but only if the build has a checker. Without one, this row looked like a normal answer.
Fix the limit, then check every crash
The array has four places, so the fifth field must be refused before it is written. The comparison should be >=, not >.
Fix it, rebuild, and run the row again. Then run every saved crash, not just the first.
Fix the comparison and try again
cd ~/fuzz/rows && sed -i 's/count > MAX_FIELDS/count >= MAX_FIELDS/' row.c && grep -n MAX_FIELDS row.c && AFL_USE_ASAN=1 afl-clang-fast -g -o rows-afl row.c main.c && printf 'cat,12B,80,late,again\n' | ./rows-aflShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check every saved crash
cd ~/fuzz/rows && for f in out/default/crashes/id*; do ./rows-afl < "$f" > /dev/null 2>&1 || echo "still crashes: $f"; done; echo "every saved crash checked"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Keep the crashing row as a test
A fixed bug can come back when somebody edits the code later. So the team keeps the input that broke it in a regress folder and runs every file there on every build.
Lesson 6 puts that folder into a pipeline.
Save the row and run the folder
cd ~/fuzz/rows && mkdir -p regress && printf 'cat,12B,80,late,again\n' > regress/five-fields.txt && for f in regress/*; do echo "$f: $(./rows-afl < "$f")"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: a tiny mutation fuzzer
Write a tiny fuzzer that changes one character at a time. Write two functions.
mutations(text) returns a list of changed copies of text. For each position i from the start, add first the copy with character i removed, then the copy with character i written twice. So mutations("ab") is ["b", "aab", "a", "abb"].
first_crash(target, seeds) tries every mutation of each seed in turn, in order. It calls target(m) on each one. Return the first mutation for which target raises any exception. Return None if none does.
def mutations(text):
return []
def first_crash(target, seeds):
return None
Make a free account to save your work and track your progress.
Exit quiz
Have a go