Eleven crash files on Monday morning
The weekend fuzz job left eleven crash files. Is that eleven bugs, or one bug found eleven times?
Before anybody fixes anything, somebody has to sort them: which are the same, which is worst, and what the smallest input is.
The one rule
Warning
Triage only crashes from your own code, fuzzed on your own lab machine. Crash files from somebody else's product belong to them, under their permission. In the UK, testing without 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 sort a pile of crash files into distinct bugs and fix each one.
Key words
| Word | What it means | Example |
|---|---|---|
| Triage | sorting crashes before fixing them | |
| Bucket | crashes that stop at the same place | |
| Minimise | shrink a crashing input and keep it crashing | afl-tmin |
| Root cause | the mistake in the code behind a crash | |
| Severity | how much harm a bug could do |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA small reader with three mistakes
average_mark reads a line such as ann:71,64,80 and returns the average. It assumes at most four marks, small numbers, and at least one mark. Each assumption is a bug.
Write it, build it for AFL++ with both sanitizers, and build a second copy that reports clearly.
Write the marks reader
mkdir -p ~/fuzz/marks && cd ~/fuzz/marks && cat > marks.c <<'END'
#include <stdlib.h>
#include <string.h>
int average_mark(const char *line) {
int marks[4];
int count = 0, total = 0;
const char *p = strchr(line, ':');
if (p == NULL) return -1;
for (p = p + 1; *p; p++) {
if (*p >= '0' && *p <= '9') {
marks[count++] = atoi(p);
while (p[1] >= '0' && p[1] <= '9') p++;
}
}
for (int i = 0; i < count; i++) total += marks[i];
return total / count;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the program and try two lines
cd ~/fuzz/marks && cat > main.c <<'END'
#include <stdio.h>
#include <string.h>
int average_mark(const char *line);
int main(void) {
char line[256];
while (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\n")] = 0;
printf("%d\n", average_mark(line));
}
return 0;
}
END
gcc -Wall -g -o marks marks.c main.c && printf 'ann:71,64,80\nben:50\n' | ./marksShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build a fuzzing copy and a checking copy
cd ~/fuzz/marks && AFL_USE_ASAN=1 AFL_USE_UBSAN=1 afl-clang-fast -g -o marks-afl marks.c main.c && clang -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -o marks-check marks.c main.c && ls marks-afl marks-checkShown here, not run. Press Start lab to type it into your own lab’s terminal.
Fuzz it for a minute and count
cd ~/fuzz/marks && mkdir -p seeds && printf 'ann:71,64,80\n' > seeds/one.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 60 -- ./marks-afl > afl.log 2>&1; ls out/default/crashes | grep -c '^id'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsSort each crash by where it stops
AFL++'s UBSan build stops with a bare trap and no words. The checking copy prints a SUMMARY line naming the file and line, and that line is the bucket.
Run every crash file through the checking copy and count the distinct lines. Then ask what each bucket means.
See the fuzzing copy stop without words
cd ~/fuzz/marks && ./marks-afl < out/default/crashes/id:000000* > /dev/null 2>&1; echo "exit $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the crashes in each bucket
cd ~/fuzz/marks && for f in out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 SUMMARY; done | sort | uniq -cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Name what each bucket is
cd ~/fuzz/marks && for f in out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 'runtime error' | cut -d: -f2,4,5; done | sort -uShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: put reports into buckets
# Crash files and the SUMMARY line each one printed.
reports = {
"id:000000": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:16:16",
"id:000001": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:16:16",
"id:000002": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:11:7",
"id:000003": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:15:41",
}
buckets = {}
for name, line in sorted(reports.items()):
place = line.split()[-1]
buckets.setdefault(place, []).append(name)
for place, names in sorted(buckets.items()):
print(place, len(names), names)
# Change the place line so marks.c:16:16 becomes marks.c:16, the bucket without its column.
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsShrink each input, then check its bucket
afl-tmin removes and simplifies bytes while the program still crashes. A one-character input tells you more than a screen of noise.
It keeps any crash, not the same crash, so a file can slip into another bucket. Count the buckets again afterwards.
Shrink every crash file
cd ~/fuzz/marks && rm -rf min && mkdir min && for f in out/default/crashes/id*; do afl-tmin -i "$f" -o "min/$(basename "$f" | cut -c1-9)" -- ./marks-afl > /dev/null 2>&1; done; for f in min/*; do echo "$f: $(cat -v "$f")"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the buckets of the small files
cd ~/fuzz/marks && for f in min/*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 SUMMARY; done | sort | uniq -cShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think eleven crash files mean eleven bugs. Here they were three. Count buckets, not files, and fix the worst bucket first.
Worked example: the three buckets
| Bucket | What happens | Worst case | Fix |
|---|---|---|---|
| marks.c:11 | A fifth mark is written past the array | Memory corrupted | Refuse a fifth mark |
| marks.c:15 | Huge marks overflow the total | A wrong average | Refuse marks over 100 |
| marks.c:16 | A line with no marks divides by zero | A crash or a wrong answer | Refuse a line with none |
The cells students fill in are left blank in this preview.
One rewrite, three refusals
Fix the root causes, not the inputs: refuse a fifth mark, refuse any mark over 100, refuse a line with none. strtol reads a long number without wrapping.
Then keep the small files as regression inputs and run every crash and every one of them again.
Rewrite the marks reader
cd ~/fuzz/marks && cat > marks.c <<'END'
#include <stdlib.h>
#include <string.h>
int average_mark(const char *line) {
int marks[4], count = 0, total = 0;
const char *p = strchr(line, ':');
if (p == NULL) return -1;
for (p = p + 1; *p; p++) {
if (*p >= '0' && *p <= '9') {
long mark = strtol(p, NULL, 10);
if (count == 4 || mark > 100) return -1;
marks[count++] = (int)mark;
while (p[1] >= '0' && p[1] <= '9') p++;
}
}
if (count == 0) return -1;
for (int i = 0; i < count; i++) total += marks[i];
return total / count;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Recheck every crash and keep the small ones
cd ~/fuzz/marks && clang -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -o marks-check marks.c main.c && mkdir -p regress && cp min/* regress/ && for f in regress/* out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" > /dev/null 2>&1 || echo "still crashes: $f"; done; echo "every crash and every regression input checked"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: bucket and shrink crashes
Write the two steps of triage.
buckets(reports) takes a dictionary from a crash file's name to its SUMMARY line. The place is the last word of the line that has two colons in it, such as marks.c:16:16; the bucket is that place without the column, marks.c:16. Return a dictionary from each bucket to the sorted list of names in it.
shrink(data, crashes) makes a crashing string smaller. Starting at position 0, try removing the character there: if crashes is still true for the shorter string, keep it and try the same position again; if not, move on. Return the string when no single removal still crashes.
def buckets(reports):
return {}
def shrink(data, crashes):
return data
Make a free account to save your work and track your progress.
Exit quiz
Have a go