Mitigations Compared: What Each Stops
Each mitigation of this section answers one step of an attack, and each leaves gaps that the next one fills. Seen one at a time, they can look like a pile of flags; side by side they form a layered design in which no single bypass is enough. This lesson puts them in one table, measures which ones a real build has with checksec, shows how one innocent flag silently removes another, and turns the result into a release gate.
One program, eight builds
The session builds one tiny program eight ways: Ubuntu's defaults, then with each protection switched off in turn, then with everything off. For each binary it prints checksec's verdicts and the CET property note. It runs in your sandbox:
cat > svc.c <<'SRC'
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
char name[16];
strcpy(name, argc > 1 ? argv[1] : "lab");
printf("hello %s\n", name);
return 0;
}
SRC
build() { out=$1; shift; gcc -O2 "$@" svc.c -o "$out" 2>/dev/null; }
build default
build no-canary -fno-stack-protector
build execstack -z execstack
build no-pie -no-pie
build no-relro -Wl,-z,norelro
build no-fortify -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
build no-cet -fcf-protection=none
build all-off -fno-stack-protector -z execstack -no-pie -Wl,-z,norelro -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -fcf-protection=none
printf "%-11s %-14s %-16s %-13s %-12s %-8s %s\n" BUILD RELRO CANARY NX PIE FORTIFY CET
for b in default no-canary execstack no-pie no-relro no-fortify no-cet all-off; do
line=$(checksec --file=$b | tail -1)
relro=$(echo "$line" | grep -oE '^(Full|Partial|No) RELRO')
can=$(echo "$line" | grep -oE '(Canary found|No canary found)')
nx=$(echo "$line" | grep -oE 'NX (enabled|disabled)')
pie=$(echo "$line" | grep -oE '(PIE enabled|No PIE)')
fort=$(echo "$line" | awk '{print $(NF-3)}')
cet=$(readelf -n $b | grep -o 'x86 feature: .*' | sed 's/x86 feature: //'); [ -z "$cet" ] && cet=none
printf "%-11s %-14s %-16s %-13s %-12s %-8s %s\n" $b "$relro" "$can" "$nx" "$pie" "$fort" "$cet"
doneBUILD RELRO CANARY NX PIE FORTIFY CET
default Full RELRO Canary found NX enabled PIE enabled Yes IBT, SHSTK
no-canary Full RELRO No canary found NX enabled PIE enabled Yes IBT, SHSTK
execstack Full RELRO Canary found NX disabled PIE enabled Yes IBT, SHSTK
no-pie Partial RELRO Canary found NX enabled No PIE Yes IBT, SHSTK
no-relro No RELRO Canary found NX enabled PIE enabled Yes IBT, SHSTK
no-fortify Full RELRO Canary found NX enabled PIE enabled No IBT, SHSTK
no-cet Full RELRO Canary found NX enabled PIE enabled Yes none
all-off No RELRO No canary found NX disabled No PIE No none
Each row changes one column, as expected, with one exception worth remembering: -no-pie also downgraded RELRO from Full to Partial. Ubuntu's GCC adds -z now only together with PIE, so a developer who disables PIE (for an old profiler, a legacy linker script) loses full RELRO without asking for it. Only measuring the binary reveals that. The defaults also show that nothing has to be added on a modern distribution: the job is to not remove protections and to check that they survived.
What each stops, and what it doesn't
| Mitigation | Where | Stops | Doesn't stop | Typical bypass needs |
|---|---|---|---|---|
| Stack canary | compiler | linear stack overflows reaching the return address | heap bugs, indexed writes, overwrites of locals, leaked canary | an info leak or a non-linear write |
| NX / DEP | CPU + loader | running injected code from the stack or heap | reusing existing code (ROP, return-to-libc) | code reuse |
| ASLR + PIE | kernel + compiler | knowing where code, libraries, heap and stack are | anything after one pointer leaks | an info leak |
| RELRO (full) | linker + loader | overwriting the GOT and other relocated data | other function pointers, vtables, data | another writable pointer |
| FORTIFY_SOURCE | compiler + libc | overflows in checked libc calls with known sizes | loops, unknown sizes, reads | a bug outside the checked calls |
| CFI (Clang CFI, CFG, IBT, BTI) | compiler / CPU | indirect calls to the wrong function (fine-grained) or into function middles (coarse) | calls to a valid target of the right type, data-only attacks | a type-compatible target |
| Shadow stack / PAC on returns | CPU + OS | returns to overwritten addresses | forward edges, data-only attacks | a forward-edge or data-only path |
| Memory tagging (MTE) | CPU + allocator | most heap overflows and UAFs at the first access (probabilistic) | in-granule overflows, 1-in-16 collisions | a tag guess or an in-granule bug |
| Hardened allocator | libc | metadata corruption, predictable placement, some double frees | in-object overflows, data in the same size class | heap grooming within a class |
| Memory-safe language | language | the bug classes themselves | unsafe/FFI code, logic bugs |
a bug in unsafe code |
Read the "bypass needs" column as the attacker's shopping list: a modern exploit typically needs a leak (for ASLR and canaries), a write (the original bug or a second one) and a way around CFI (a data-only path or a type-compatible target). Each additional requirement raises the cost, which is the whole point. Only the last row removes bugs instead of raising the cost.
Checking binaries automatically
A release pipeline should fail when a shipped binary lacks a required protection. The checker below reads checksec-style lines (here copied from the session above, so it runs in your browser without the tools) and applies a policy. In CI the same function would parse the output of checksec --file for every file in the release:
POLICY = {"RELRO": "Full RELRO", "CANARY": "Canary found", "NX": "NX enabled",
"PIE": "PIE enabled", "FORTIFY": "Yes", "CET": "IBT, SHSTK"}
REPORT = """\
default Full RELRO Canary found NX enabled PIE enabled Yes IBT, SHSTK
no-pie Partial RELRO Canary found NX enabled No PIE Yes IBT, SHSTK
no-fortify Full RELRO Canary found NX enabled PIE enabled No IBT, SHSTK
all-off No RELRO No canary found NX disabled No PIE No none"""
def parse(line):
fields = [f.strip() for f in line[12:].split(" ") if f.strip()]
return line[:11].strip(), dict(zip(POLICY, fields))
failed = 0
for line in REPORT.splitlines():
name, got = parse(line)
missing = [k for k, want in POLICY.items() if got.get(k) != want]
failed += bool(missing)
print(f"{name:11} {'PASS' if not missing else 'FAIL: ' + ', '.join(f'{k}={got[k]}' for k in missing)}")
print(f"{failed} binaries fail the hardening policy")default PASS
no-pie FAIL: RELRO=Partial RELRO, PIE=No PIE
no-fortify FAIL: FORTIFY=No
all-off FAIL: RELRO=No RELRO, CANARY=No canary found, NX=NX disabled, PIE=No PIE, FORTIFY=No, CET=none
3 binaries fail the hardening policy
The no-pie build fails on two counts, PIE and RELRO, which is how the gate catches the silent downgrade. Real pipelines add exceptions with a written reason (a static helper without libc calls may legitimately be "No" for FORTIFY) instead of weakening the policy.
Choosing what to require
For C and C++ services on Linux, a reasonable baseline is: PIE, full RELRO, -fstack-protector-strong, NX (no execstack), -D_FORTIFY_SOURCE=3 with -O2, -fcf-protection=full (x86-64) or -mbranch-protection=standard (AArch64), and -fstack-clash-protection. GCC 14 bundles most of these as -fhardened. Add Clang CFI and a hardened allocator for exposed, high-value services; enable MTE where the hardware has it. The OpenSSF's Compiler Options Hardening Guide for C and C++ keeps a maintained list with explanations.
Pitfalls
- Checking the source flags instead of the binary. Build systems override flags, distributions inject defaults, linkers drop them. Measure the artefact.
- One weak library. CET is enabled only if every loaded object is marked; an executable stack in one
.socan make the whole process's stack executable. - Mitigations as an excuse. "It's only a crash with all mitigations on" is a statement about today's attackers. Fix the bug.
Practice
1. Which two columns changed for the no-pie build, and why the second?
PIE (No PIE) and RELRO (Partial RELRO). Ubuntu's GCC passes -z now only when it builds PIE, so without PIE there is no BIND_NOW and the GOT's lazy part stays writable.
2. An attacker has a heap overflow and an info leak, and the target uses canaries, NX, ASLR, full RELRO and FORTIFY. Which row of the table is the most relevant obstacle left?
CFI (and shadow stacks): the leak defeats ASLR, canaries don't cover the heap, RELRO removes the GOT target but not other function pointers, so the next barrier is whether the corrupted pointer may be called. Without CFI, nothing stops it; that's why browsers deploy CFI and hardened allocators.
3. Extend the policy checker so that a binary can be exempted from one rule with a reason.
EXEMPT = {("no-fortify", "FORTIFY"): "static helper, no libc string calls"}
missing = [k for k, want in POLICY.items() if got.get(k) != want and (name, k) not in EXEMPT]Print the reasons in the report so reviewers see every exemption.
Test yourself
Which build lost two protections although only one flag was changed?
- no-canary
- no-pie: PIE and full RELRO
- no-fortify
- no-cet
Which mitigation removes bug classes instead of raising the cost of exploiting them?
- ASLR
- CFI
- A memory-safe language
- RELRO
What does a typical modern exploit need, according to the bypass column?
- Only a stack overflow
- An info leak, a write primitive and a way around CFI
- Root access
- A disabled NX
Why should a release gate check binaries rather than compiler flags?
- Flags are secret
- Build systems, distribution defaults and linkers can add or drop protections, so only the artefact shows the truth
- checksec is faster
- Binaries are smaller