Compiler & Linker Hardening
Binary Hardening & Compiler Defenses · Security Fundamentals track
7 min read
In this module
Compiler and Linker Flags That Matter
Stack canaries and CFI are the headline acts, but compilers and linkers ship a quieter arsenal of hardening flags. Knowing them matters whether you're shipping software or picking apart someone else's binary to figure out which exploitation paths are still open.
RELRO: Protecting the GOT
On Linux, dynamically linked binaries use the Global Offset Table (GOT) to resolve addresses of functions in shared libraries. By default the GOT is lazily populated. First call to a library function goes through the PLT (Procedure Linkage Table), which invokes the dynamic linker to resolve the address and write it into the GOT. Every subsequent call reads the resolved address directly from the GOT.
That's a writable table of function pointers sitting at a known offset from the binary's base. Hand an attacker an arbitrary write primitive and they overwrite a GOT entry, redirecting any library call to code of their choosing.
Partial RELRO (-Wl,-z,relro) marks some relocation sections as read-only after the dynamic linker processes them, but leaves the GOT writable for lazy binding.
Full RELRO (-Wl,-z,relro,-z,now) resolves every symbol at load time and then marks the entire GOT read-only. The GOT vanishes as an overwrite target. The price: slower startup, since all symbols must be resolved before main runs.
The sequence below traces two calls to puts. Under lazy binding, the first call detours through the dynamic linker, which writes the real address into the GOT; the second call goes straight there. With BIND_NOW every entry is filled before main, and the GOT is then made read-only, so there's no later write for an attacker to imitate.
# Check RELRO status of a binaryreadelf -l /usr/bin/ssh | grep GNU_RELRO# Full RELRO shows both GNU_RELRO and BIND_NOWreadelf -d /usr/bin/ssh | grep BIND_NOWPIE: Position Independent Executables
A Position Independent Executable (PIE) can be loaded at any address in memory, which lets ASLR randomise the main executable rather than only shared libraries. Without PIE, the executable lands at a fixed address and an attacker knows exactly where every byte of code and data lives.
# Compile as PIE (default on most modern distros)gcc -pie -fPIE -o program program.c
# Check if a binary is PIEfile /usr/bin/ssh# Recent versions of file print "pie executable" for PIE and plain "executable"# for non-PIE; older versions call a PIE binary a "shared object"On 32-bit, PIE costs you a register — one has to hold the GOT base address. On 64-bit with RIP-relative addressing, the cost is rounding-error tiny. Most modern Linux distros now build every package PIE by default.
PIE lets ASLR randomise the executable, but ASLR also has to be on at the kernel level (/proc/sys/kernel/randomize_va_space). PIE without ASLR is pointless indirection. ASLR without PIE still scrambles the stack and shared libraries, but the executable itself stays anchored — its gadgets and GOT remain fixed targets.
FORTIFY_SOURCE
_FORTIFY_SOURCE is a macro that replaces unsafe C library functions with checked versions. It works at two levels:
Level 1 (-D_FORTIFY_SOURCE=1): when the compiler can determine the destination buffer size at compile time, calls to memcpy, strcpy, sprintf, read and friends get swapped for versions that check the copy length against the buffer size. Requires at least -O1.
Level 2 (-D_FORTIFY_SOURCE=2): tighter still. %n in format strings is rejected when the format string lives in writable memory (killing format string attacks), and checks use the size of the enclosing struct member or array rather than the whole object, catching overflows from one field into the next.
Level 3 (-D_FORTIFY_SOURCE=3, GCC 12 or later with glibc 2.34 or later): also covers buffers whose size is only known at run time, such as a malloc(n) result, using __builtin_dynamic_object_size.
char buf[10];// With FORTIFY_SOURCE=2, this call is replaced with __memcpy_chk// which aborts if n > sizeof(buf)memcpy(buf, src, n);The performance hit is barely measurable. Most checks resolve at compile time — if the compiler can prove the copy is safe it emits nothing, and if it can prove the copy overflows you get a warning before the binary ships, not a crash afterwards.
Stack Clash Protection
The stack clash attack abuses the thin guard region between the stack and whatever sits next to it (heap, mmap'd pages, etc.). With a large enough allocation — deep recursion, a fat alloca, a giant on-stack array — an attacker can hop the stack right over its guard page and land inside someone else's memory, quietly corrupting heap data or other allocations.
-fstack-clash-protection tells the compiler to sprinkle probes through any large allocation: cheap memory accesses at page-sized intervals. The probes guarantee the guard page gets touched before the stack pointer crosses it, so the process segfaults rather than silently overlapping with adjacent memory.
// Without stack clash protection:char big_buffer[1024 * 1024]; // Might skip guard page
// With stack clash protection, the compiler inserts:// touch each page between current SP and new SP// to ensure the guard page is hit if stack grows too farThe -fno-delete-null-pointer-checks Flag
By the C standard, dereferencing a null pointer is undefined behaviour, so the compiler is free to optimise on that assumption. Translation: it'll happily delete a null check it deems redundant, reasoning "the pointer was already dereferenced, so it can't be null."
In kernel land, that optimisation has shipped real CVEs. A user-space process could once map page zero itself (the vm.mmap_min_addr sysctl now forbids this by default), so a kernel null dereference would read attacker-controlled data from that page. The compiler was erasing defences kernel developers had written deliberately, and for a while nobody noticed.
-fno-delete-null-pointer-checks tells the compiler to stop making that assumption, so explicit null checks survive into the final binary. The Linux kernel builds with this flag.
A Full Set of Hardening Flags
A fully hardened build on Linux typically uses all of the following:
| Flag | What it does |
|---|---|
-fstack-protector-strong | Stack canaries |
-fstack-clash-protection | Stack clash defence |
-fcf-protection=full | Intel CET (shadow stack + IBT) on x86 |
-mbranch-protection=standard | PAC and BTI, the AArch64 equivalent |
-D_FORTIFY_SOURCE=3 | Checked C library functions (use 2 on older toolchains) |
-D_GLIBCXX_ASSERTIONS | Bounds checks in libstdc++ containers (C++) |
-ftrivial-auto-var-init=zero | Zero uninitialised locals |
-Wformat -Werror=format-security | Reject non-literal format strings |
-fPIE -pie | Position independent executable |
-Wl,-z,relro,-z,now | Full RELRO |
-Wl,-z,noexecstack | Non-executable stack |
-Wl,-z,separate-code | Keep code and read-only data on separate pages |
-fno-delete-null-pointer-checks | Preserve null checks |
On x86-64 that becomes:
gcc -O2 \ -fstack-protector-strong -fstack-clash-protection \ -fcf-protection=full \ -D_FORTIFY_SOURCE=3 \ -ftrivial-auto-var-init=zero \ -Wformat -Werror=format-security \ -fPIE -pie \ -Wl,-z,relro,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code \ -fno-delete-null-pointer-checks \ -o program program.cVerifying Hardening
Tools like checksec (from pwntools) and hardening-check (from Debian's hardening-includes) can crack open a binary and tell you which protections are actually enabled:
checksec --file=/usr/bin/ssh# Reports: RELRO, Stack Canary, NX, PIE, FORTIFY statusWhether you're shipping the binary or pulling one apart, the same flag knowledge cuts both ways — what defences you want enabled, and what defences you can rule out before picking your exploit technique.
Every major Linux distribution now turns most of these flags on by default. Ubuntu, Fedora, Debian, Arch — they all build packages with PIE, RELRO, stack canaries, and FORTIFY_SOURCE. Ship software without them and you're handing users weaker protections than their system's default packages already give them.