A seatbelt, not safe driving
A seatbelt does not stop a crash. It makes a crash far less harmful.
Mitigations are the seatbelts of compiled code. The bug is still there, but the program stops safely, or the memory it hits is not where anybody expected.
By the end of this lesson
Note
I can explain what each standard mitigation does when a memory bug happens.
Key words
| Word | What it means | Example |
|---|---|---|
| Mitigation | a protection that limits the harm a bug can do | |
| Stack canary | a secret value checked before a function returns | |
| NX | memory that holds data may not run as code | |
| ASLR | placing memory regions at random addresses each run | |
| PIE | a program that can be loaded at any address | -fPIE -pie |
| RELRO | making start-up tables read-only once loaded |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA canary checks the stack before returning
The compiler puts a secret value beside a function's local buffers. Before the function returns, it checks the value is unchanged.
A copy that runs past the buffer changes it, so the program stops at once with "stack smashing detected". Write a greeting that copies into eight bytes and try it.
Write greet.c and greet ann
mkdir -p ~/mem/guard && cd ~/mem/guard && cat > greet.c <<'END'
#include <stdio.h>
#include <string.h>
void greet(const char *name) {
char buf[8];
strcpy(buf, name);
printf("hello %s\n", buf);
}
int main(int argc, char **argv) {
greet(argc > 1 ? argv[1] : "ann");
puts("goodbye");
return 0;
}
END
gcc -g -o greet greet.c && ./greet annShown here, not run. Press Start lab to type it into your own lab’s terminal.
Greet a long name
cd ~/mem/guard && ./greet Christopher-Robinson-Smith; echo "exit $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the canary checks in two builds
cd ~/mem/guard && gcc -g -fno-stack-protector -o greet-bare greet.c && for b in greet greet-bare; do echo "$b: $(readelf --dyn-syms -W $b | grep -c __stack_chk_fail)"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsASLR moves memory on every run
The kernel places the stack, the heap and the libraries at random addresses each time a program starts. A setting of 2 means everything is moved.
The program's own code moves too only if it was built as PIE. Ubuntu builds PIE by default, so build one without it to compare.
Write where.c and read the setting
mkdir -p ~/mem/guard && cd ~/mem/guard && cat > where.c <<'END'
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int count = 3;
char *note = malloc(16);
printf("code %p heap %p stack %p\n", (void *)main, (void *)note, (void *)&count);
free(note);
return 0;
}
END
gcc -g -o where where.c && cat /proc/sys/kernel/randomize_va_spaceShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run it three times
cd ~/mem/guard && for run in 1 2 3; do ./where; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build without PIE and run three times
cd ~/mem/guard && gcc -g -no-pie -o where-nopie where.c && for run in 1 2 3; do ./where-nopie; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Compare the two file types
cd ~/mem/guard && readelf -h where where-nopie | grep -E 'File|Type'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 mitigations make unsafe C safe. They only limit what a bug leads to, and each covers one kind of harm. A program behind all four still has its bug until you fix the code.
NX and RELRO protect what memory may do
NX marks the stack and heap as data that may never run as code; GNU_STACK with flags RW says so. RELRO makes the tables used to find library functions read-only after start-up. With BIND_NOW, all of them are filled first, so the whole table can be locked.
Check the stack is not executable
cd ~/mem/guard && readelf -lW where | grep GNU_STACK && grep -E 'r-xp|rw-p' /proc/self/maps | grep -E 'grep|stack' | awk '{print $2, $6}'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check RELRO and BIND_NOW
cd ~/mem/guard && readelf -lW where | grep GNU_RELRO | awk '{print $1, $(NF-1)}' && readelf -d where | grep -E 'FLAGS'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: four mitigations side by side
| Mitigation | What it does | What you see | Who provides it |
|---|---|---|---|
| Stack canary | Stops a function whose stack was overwritten | stack smashing detected | The compiler |
| NX | Data memory never runs as code | GNU_STACK RW, maps rw-p | The processor and kernel |
| ASLR with PIE | Addresses differ on every run | New addresses each run | The kernel and the build |
| RELRO with BIND_NOW | Start-up tables become read-only | GNU_RELRO, FLAGS BIND_NOW | The linker |
The cells students fill in are left blank in this preview.
Assignment: compare two runs
Each run of where gives a dictionary from a region ("code", "heap", "stack") to its address as an int. Write two functions.
moved(first, second) returns the sorted list of regions whose address differs between the two runs.
verdict(first, second) returns "aslr and pie" when all three moved, "aslr without pie" when the heap and stack moved but the code did not, and "no aslr" when nothing moved. Anything else is "unclear".
def moved(first, second):
return []
def verdict(first, second):
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go