Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How mitigations stop a bug going further

After this lesson: I can explain what each standard mitigation does when a memory bug happens.

60 min lesson9 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
Mitigationa protection that limits the harm a bug can do
Stack canarya secret value checked before a function returns
NXmemory that holds data may not run as code
ASLRplacing memory regions at random addresses each run
PIEa program that can be loaded at any address-fPIE -pie
RELROmaking start-up tables read-only once loaded

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
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 ann

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Greet a long name

Command
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

Command
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)"; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

ASLR 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

Command
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_space

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run it three times

Command
cd ~/mem/guard && for run in 1 2 3; do ./where; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Build without PIE and run three times

Command
cd ~/mem/guard && gcc -g -no-pie -o where-nopie where.c && for run in 1 2 3; do ./where-nopie; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Compare the two file types

Command
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
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

Command
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

MitigationWhat it doesWhat you seeWho provides it
Stack canaryStops a function whose stack was overwrittenstack smashing detectedThe compiler
NXData memory never runs as codeGNU_STACK RW, maps rw-pThe processor and kernel
ASLR with PIEAddresses differ on every runNew addresses each runThe kernel and the build
RELRO with BIND_NOWStart-up tables become read-onlyGNU_RELRO, FLAGS BIND_NOWThe 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".

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.