#Why this matters
In introductory labs, a stack overflow overwrites a saved return address with a fixed pointer and jumps straight into stack memory. On modern Linux distributions, that direct path almost always crashes immediately because compilers, linkers, and the kernel enforce multiple layers of exploit mitigations.
Knowing what each mitigation does, and just as importantly what it does not stop, is essential for both defenders and security testers. Defenders use these protections to raise the cost of memory-corruption bugs, while analysts inspect them first in every binary challenge to understand which primitives (such as an address leak or an arbitrary write) matter.
#Inspecting protections with checksec and pwndbg
Before reading assembly or running a binary, analysts run checksec to list which ELF hardening features were compiled and linked into the executable, then confirm runtime permissions inside pwndbg.
$ checksec --file=./lab_service
RELRO STACK CANARY NX PIE RPATH RUNPATH
Partial RELRO Canary found NX enabled No PIE No RPATH No RUNPATH
$ gdb -q ./lab_service
pwndbg> start
pwndbg> vmmap
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
0x400000 0x401000 r--p 1000 0 /home/lab/lab_service
0x401000 0x402000 r-xp 1000 1000 /home/lab/lab_service
0x403000 0x404000 r--p 1000 2000 /home/lab/lab_service
0x404000 0x405000 rw-p 1000 3000 /home/lab/lab_service
0x7ffff7d80000 0x7ffff7da8000 r--p 28000 0 /lib/x86_64-linux-gnu/libc.so.6
0x7ffffffde000 0x7ffffffff000 rw-p 21000 0 [stack]Notice what vmmap reveals immediately: the main binary sits at the fixed base 0x400000 (No PIE), while libc and [stack] sit at high dynamic addresses. The [stack] region has rw-p permissions (read and write, no execute) because NX is enabled.
| Mitigation | Enforced by | Primary goal | Hardening flag |
|---|---|---|---|
NX (W^X) |
Kernel + CPU page tables | Block executing data pages | -z noexecstack (default) |
| Stack canary | Compiler (gcc/clang) |
Detect sequential stack overwrites | -fstack-protector-strong |
| ASLR | Kernel (randomize_va_space) |
Randomize stack, heap, libraries | Kernel setting 2 |
| PIE | Compiler + Linker | Randomize main binary base address | -fPIE -pie |
| Full RELRO | Linker + ld.so |
Make GOT read-only after startup | -Wl,-z,relro,-z,now |
#Stack canaries and NX
Stack canaries guard against linear stack buffer overflows. At function entry, the compiler inserts instructions that copy a secret per-process random value from thread-local storage (fs:0x28 on x86-64 Linux) onto the stack just before the saved base pointer (RBP) and return address (RIP). Before returning, the function compares the stack copy against fs:0x28 and calls __stack_chk_fail (aborting the process) if they differ.
- 4Saved Return Address (RIP)High address (target of control-flow hijack)
- 3Saved Base Pointer (RBP)Frame link pointer
- 2Stack Canary (fs:0x28)Random guard ending with 0x00 low byte
- 1Local char buffer[64]Low address (overflow starts here)
On Linux, the lowest byte of the stack canary is intentionally 0x00. Because C string functions stop printing at null bytes, a single accidental over-read via printf("%s") or puts() is less likely to leak the entire canary unless the trailing null byte itself is overwritten.
NX (No-Execute) enforces a Write XOR Execute (W^X) rule using the hardware NX/XD bit in page-table entries. Writable regions such as the stack and heap are marked rw-, so jumping the instruction pointer into user-supplied buffer data triggers SIGSEGV. Conceptually, NX prevents injected machine code from running directly on the stack, pushing control-flow attacks toward reusing existing executable instructions already mapped in memory (such as functions in libc or short instruction sequences ending in ret).
#ASLR, PIE, and RELRO
ASLR (Address Space Layout Randomization) randomizes the virtual base addresses of the stack, heap, mmap regions, and shared libraries (libc.so.6) each time a program starts. However, ASLR alone does not randomize the main executable unless the binary was compiled as a Position-Independent Executable (PIE) with -fPIE -pie. Without PIE, an analyst can still rely on fixed addresses inside the main binary's .text, .plt, and .got sections across every run.
RELRO (Relocation Read-Only) protects dynamic linking structures, specifically the Global Offset Table (GOT), which holds resolved addresses of external library functions:
- No RELRO: Both
.gotand.got.pltremain writable throughout execution. - Partial RELRO (
-Wl,-z,relro): Reorders sections so.gotsits before.bssand becomes read-only, while.got.pltstays writable to support lazy symbol resolution on first call. - Full RELRO (
-Wl,-z,relro,-z,now): Instructs the dynamic linker (ld.so) to resolve every external symbol at startup (BIND_NOW) and remap the entire GOT as read-only (r--), preventing runtime GOT pointer overwrites.
#Conceptual bypasses and lab diagnostics
In binary auditing and CTF challenges, mitigations are rarely defeated by brute force on 64-bit systems. Instead, they fail when combined with other memory bugs:
- Canary disclosure: A format-string read or uninitialized-buffer leak can expose the canary on the stack, allowing a later write to preserve the exact canary value. Note also that
fork()children inherit the parent's canary and ASLR layout unlessexecve()starts a fresh image. - ASLR and PIE defeated by pointer leaks: Randomization shifts a module's base address, not the relative offsets inside it. Leaking a single runtime pointer to a known function allows computing the randomized base address (
runtime_base = leaked_ptr - static_offset). - Non-linear corruption: An out-of-bounds array index or arbitrary write primitive can modify a heap function pointer or
.got.pltentry (under Partial RELRO) without touching the stack canary at all.
#Defending and common mistakes
Hardening modern Linux binaries requires enabling all complementary protections together, ideally enforced in CI/CD build checks with checksec or readelf.
#Common mistakes
- Relying on Partial RELRO (the default on many toolchains) when an application handles untrusted data:
.got.pltremains writable. - Enabling ASLR at the OS level while building key binaries with
-no-pie, leaving deterministic code and GOT addresses exposed. - Using
fork()for network workers without re-execing: all child processes share identical stack canaries and ASLR layouts, letting a crash-tolerant leak in one worker expose secrets for the next. - Treating NX as a complete fix for stack overflows rather than fixing unsafe bounds checks or enabling
-D_FORTIFY_SOURCE=2.
#Summary
- NX prevents executing writable memory pages, while stack canaries detect sequential stack overflows before a function returns.
- ASLR randomizes stack, heap, and shared libraries, and PIE extends that randomization to the main executable itself.
- Full RELRO (
-Wl,-z,relro,-z,now) resolves dynamic symbols at startup and marks the GOT read-only, closing GOT overwrite targets left open by Partial RELRO. - Mitigations work as a layered defense: a single information leak or arbitrary write can undermine individual protections, so strong builds combine full compiler hardening with memory-safe coding.