Part 6 of 7 · 3 min read
Layers and fixes
Layers, and removing the bug
Think of a castle with a moat, a wall, a gate and a guard. Any one of them can fail. The attacker must pass all four. The analogy stops in one place: in a castle each layer is built by a separate crew, while here the same compiler and kernel set all layers at once, and one wrong flag can remove several.
In plain words: The four defences above stack up. The canary detects an overflow that reaches the return address. NX stops injected code. ASLR and PIE hide where the code to reuse lies. Each can be bypassed alone, and the three together raise the cost of an attack a great deal. They treat the symptom. The cure is to remove the bug. For C and C++ programs this means bounded copies (snprintf, strlcpy where present, memcpy with a checked length), the _FORTIFY_SOURCE option that makes the library check sizes (it stopped the overflow in the test with *** buffer overflow detected ***), and tests that run with AddressSanitizer (-fsanitize=address). On the test machine AddressSanitizer reported stack-buffer-overflow ... WRITE of size 21 and the line of the faulty strcpy. A memory-safe language (Rust, Go, Java, Python) checks every index and removes this whole class of bug, at some cost in speed or in the freedom to manage memory.
A worked example. Count the layers in a real build. A binary built with -fstack-protector-all -pie and run on a system with randomize_va_space = 2 has a canary, NX (default GNU_STACK RW), PIE and ASLR: four layers. The same source built with -fno-stack-protector -no-pie -z execstack and run with setarch -R has none. Same source, and the first costs the attacker many guesses while the second costs none.
The key lines. The order of work is: write code without the bug, find the remaining bugs with sanitizers and tests, and keep the four layers on in the release build as a safety net.
Defence in depth
Defence in depth is the design of several independent protections, so that a failure or bypass of one does not give an attacker control. It does not replace removing the underlying fault.
Example: a canary, a non-executable stack, ASLR and a bounded copy together protect a function that reads user input.
Interview angle
A common question is "What is a stack buffer overflow and how do modern systems mitigate it?" A good answer names the overflow and the saved return address, then the canary, NX, ASLR with PIE, and ends with safe functions, sanitizers and memory-safe languages as the real fix.
Check yourself:
1. Name four layers that make an overflow harder to use.
2. Which tool reported the line of the bad strcpy in the test?
3. Why is a memory-safe language a stronger answer than a canary?
Words you will meet
| Word | Meaning |
|---|---|
| Buffer overflow | Writing past the end of an array. |
| Return address | The place a function returns to. |
| Canary | A guard value checked before a function returns. |
| NX / W^X | Memory that may be written is not executed. |
| ASLR / PIE | Random addresses per run / code that can load anywhere. |
| AddressSanitizer | A tool that reports memory errors while a program runs. |
Quick notes
- A buffer overflow writes past an array and can change a return address.
- Canary: detects a changed guard value and stops the program (exit 134 in the test).
- NX: a
GNU_STACK RWstack cannot run code.RWEcan. - ASLR: stack, heap and libraries move on each run. PIE moves the program's own code too.
- On the test machine library placement had 2^28 = 268,435,456 positions.
- Layers help, but the fix is to remove the bug: bounded copies, sanitizers, memory-safe languages.