Port 8080, or port 4294975376?
A firewall tool reads a port from a settings file and refuses anything above 65535. Somebody types 4294975376. The tool says: port 8080, accepted.
Nothing crashed. The number wrapped round inside the program, and only a checker would have said so.
The one rule
Warning
Every program here is your own, built and run on your own lab machine. Probing another person's software or service needs their written permission. In the UK, doing it without that 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 choose a sanitizer that reports the kind of bug I am hunting.
Key words
| Word | What it means | Example |
|---|---|---|
| Sanitizer | a checker built into a program by the compiler | |
| AddressSanitizer | reports bad memory reads, writes and leaks | -fsanitize=address |
| UndefinedBehaviorSanitizer | reports operations C leaves undefined | -fsanitize=undefined |
| Signed overflow | an int result too big for its type | |
| Memory leak | memory taken and never given back |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsOne number, two different answers
read_port builds the number digit by digit in an int. A long enough number goes past the largest int, and C does not say what happens then.
Write it with a small program that prints its answer for each argument, then build it twice: once plainly, once with UBSan.
Write the port reader
mkdir -p ~/fuzz/san && cd ~/fuzz/san && cat > port.c <<'END'
int read_port(const char *text) {
int port = 0;
for (int i = 0; text[i] >= '0' && text[i] <= '9'; i++) {
port = port * 10 + (text[i] - '0');
}
if (port < 1 || port > 65535) return -1;
return port;
}
END
cat > show_port.c <<'END'
#include <stdio.h>
int read_port(const char *text);
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) printf("%s -> %d\n", argv[i], read_port(argv[i]));
return 0;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build it plainly and try three numbers
cd ~/fuzz/san && gcc -Wall -g -o show_port port.c show_port.c && ./show_port 8080 99999 4294975376Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Build it with UBSan and try again
cd ~/fuzz/san && clang -g -fsanitize=undefined -o show_port_ub port.c show_port.c && DEBUGINFOD_URLS= ./show_port_ub 8080 4294975376Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA fuzzer needs UBSan to stop
Carrying on is polite in a test, but a fuzzer only saves an input when the program stops. So a fuzz build adds -fno-sanitize-recover=undefined.
The Run writes a harness for read_port and fuzzes it with both sanitizers for up to a minute.
Fuzz the port reader with both checkers
cd ~/fuzz/san && cat > fuzz_port.c <<'END'
#include <stddef.h>
#include <stdint.h>
#include <string.h>
int read_port(const char *text);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
char text[32];
if (size >= sizeof text) return 0;
memcpy(text, data, size);
text[size] = 0;
read_port(text);
return 0;
}
END
clang -g -fsanitize=fuzzer,address,undefined -fno-sanitize-recover=undefined -o fuzz-port port.c fuzz_port.c && rm -f crash-* && DEBUGINFOD_URLS= ./fuzz-port -seed=1 -max_total_time=60 > fuzz.log 2>&1; grep -E 'runtime error|SUMMARY|Test unit' fuzz.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
A leak never crashes at all
read_name takes 32 bytes with malloc, then refuses a line with no =. On that path it returns without freeing them.
One leak is small. A server that reads a million bad lines runs out of memory. AddressSanitizer checks for leaks when the program ends.
Write the name reader
cd ~/fuzz/san && cat > name.c <<'END'
#include <stdlib.h>
#include <string.h>
char *read_name(const char *line) {
char *name = malloc(32);
if (name == NULL) return NULL;
const char *eq = strchr(line, '=');
if (eq == NULL || eq - line > 31) return NULL;
memcpy(name, line, (size_t)(eq - line));
name[eq - line] = 0;
return name;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a program that tries names
cd ~/fuzz/san && cat > show_name.c <<'END'
#include <stdio.h>
#include <stdlib.h>
char *read_name(const char *line);
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
char *name = read_name(argv[i]);
printf("%s -> %s\n", argv[i], name ? name : "(refused)");
free(name);
}
return 0;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build it plainly and try two lines
cd ~/fuzz/san && gcc -Wall -g -o show_name name.c show_name.c && ./show_name colour=red 'no equals here'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Build it with ASan and try again
cd ~/fuzz/san && clang -g -fsanitize=address -o show_name_asan name.c show_name.c && DEBUGINFOD_URLS= ./show_name_asan colour=red 'no equals here' 2>&1 | grep -E 'refused|leak'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 one sanitizer catches everything. AddressSanitizer saw the leak but says nothing about the overflow, and UBSan the other way round. Build fuzz targets with both, and MemorySanitizer in a build of its own.
Worked example: which checker for which bug
| Sanitizer | Reports | Flag | Cost |
|---|---|---|---|
| AddressSanitizer | Reads and writes out of bounds, use after free, leaks | -fsanitize=address | About twice as slow |
| UndefinedBehaviorSanitizer | Signed overflow, division by zero, bad shifts | -fsanitize=undefined | Small |
| MemorySanitizer | Reading memory never set | -fsanitize=memory (clang only) | About three times as slow |
| ThreadSanitizer | Two threads racing on one value | -fsanitize=thread | Several times slower |
The cells students fill in are left blank in this preview.
Check before the number grows
The port fix refuses as soon as the number passes 65535, so it can never reach the edge of an int. The name fix checks the line before taking any memory.
Rewrite both, then build them with both sanitizers and run the same inputs.
Rewrite the port reader
cd ~/fuzz/san && cat > port.c <<'END'
int read_port(const char *text) {
int port = 0;
for (int i = 0; text[i] >= '0' && text[i] <= '9'; i++) {
port = port * 10 + (text[i] - '0');
if (port > 65535) return -1;
}
if (port < 1) return -1;
return port;
}
END
gcc -Wall -g -o show_port port.c show_port.c && ./show_port 8080 99999 4294975376Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Rewrite the name reader
cd ~/fuzz/san && cat > name.c <<'END'
#include <stdlib.h>
#include <string.h>
char *read_name(const char *line) {
const char *eq = strchr(line, '=');
if (eq == NULL || eq - line > 31) return NULL;
char *name = malloc(32);
if (name == NULL) return NULL;
memcpy(name, line, (size_t)(eq - line));
name[eq - line] = 0;
return name;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check both with both sanitizers
cd ~/fuzz/san && clang -g -fsanitize=address,undefined -o port_check port.c show_port.c && clang -g -fsanitize=address,undefined -o name_check name.c show_name.c && DEBUGINFOD_URLS= ./port_check 8080 4294975376 && DEBUGINFOD_URLS= ./name_check colour=red 'no equals here'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Sanitizers belong in every test build
The team keeps both fixed by building the unit tests with -fsanitize=address,undefined in every pipeline run, and by keeping 4294975376 as a regression input.
A sanitizer build is for testing only. It is slower and is never what you ship.
Assignment: read a sanitizer report
A sanitizer report is long, but two lines say most of it. Write two functions.
summary(line) takes a line that starts SUMMARY: . Return a dictionary with tool (the word after SUMMARY: , without its colon), kind (the next word), file (the file name without its folders) and line (the line number, an int). For a leak summary such as SUMMARY: AddressSanitizer: 32 byte(s) leaked in 1 allocation(s)., kind is "leak", file is "" and line is 0.
access(lines) takes all the report's lines. Return "READ" or "WRITE" from the first line that starts with one of them, or "" if none does.
def summary(line):
return {}
def access(lines):
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go