ASLR, PIE and RELRO
NX forced attackers to reuse existing code and existing data structures. Reusing them requires knowing where they are, and being able to overwrite the pointers that lead to them. Address space layout randomisation (ASLR) attacks the first requirement: the stack, the heap, the libraries and the program itself start at a random address at every run. RELRO attacks the second for one particularly attractive target, the table of library function addresses that every dynamically linked program carries. Both are cheap and on by default on modern Linux, Windows, macOS, Android and iOS, and both can be silently lost by one wrong build flag. Memory Protection watched the stack move between runs; this lesson shows what PIE adds and how RELRO works.
Definitions
- ASLR: the kernel picks random bases for the stack,
mmapregions (libraries, large allocations) and the heap (brk). On Linux,/proc/sys/kernel/randomize_va_spaceis2for full randomisation. - PIE (position-independent executable): a program compiled (
-fPIE) and linked (-pie) so that its own code and data can be loaded at any address. Without PIE, the program's code sits at a fixed address (0x400000on x86-64), so ASLR can move only everything else. - GOT (global offset table): a table of addresses of external functions and variables, filled in by the dynamic loader; calls to
printfgo through it (via the PLT, the procedure linkage table). - Lazy binding: the loader fills a GOT entry the first time the function is called, so the GOT must stay writable while the program runs.
- RELRO (relocation read-only): after start-up, the loader makes the relocated data read-only (
mprotect). Partial RELRO (-z relro) protects most of it but leaves the lazily bound part of the GOT (.got.plt) writable; full RELRO (-z relro -z now) binds every symbol at start-up (BIND_NOW) and makes the whole GOT read-only.
PIE: does the program itself move?
The toy prints four addresses: main (the program's code), printf (libc), a heap block and a local. The session runs it five times as a PIE (Ubuntu's default) and five times built with -no-pie, and counts the distinct values of each, so the output is stable even though the addresses aren't. It runs in your sandbox:
cat > addr.c <<'SRC'
#include <stdio.h>
#include <stdlib.h>
int main(void) {
void *h = malloc(16); int local;
printf("%p %p %p %p\n", (void *)main, (void *)printf, h, (void *)&local);
free(h); return 0;
}
SRC
gcc -O0 addr.c -o pie
gcc -O0 -no-pie addr.c -o nopie
for b in pie nopie; do
for i in 1 2 3 4 5; do ./$b; done > runs.txt
printf "%-6s distinct values in 5 runs: main %s, printf %s, heap %s, stack %s\n" $b \
$(for c in 1 2 3 4; do cut -d' ' -f$c runs.txt | sort -u | wc -l; done)
done
echo "main in the non-PIE build, every run: $(./nopie | cut -d' ' -f1)"
checksec --file=pie | tail -1 | awk '{print "pie: ", $7, $8}'
checksec --file=nopie | tail -1 | awk '{print "nopie:", $7, $8}'pie distinct values in 5 runs: main 5, printf 5, heap 5, stack 5
nopie distinct values in 5 runs: main 1, printf 5, heap 5, stack 5
main in the non-PIE build, every run: 0x401186
pie: PIE enabled
nopie: No PIE
In the PIE build all four addresses change on every run. In the non-PIE build the library, heap and stack still move (ASLR is a kernel feature), but main is at the same small address every time. Everything inside the program (its functions, its globals, its GOT and PLT) is then at a known place, which is exactly the "existing code" that ROP needs. PIE costs little on x86-64 (code addresses are relative to the instruction pointer anyway), which is why distributions made it the default between 2015 and 2017 (Fedora 23, Ubuntu 16.10, Debian 9).
RELRO: is the GOT writable?
An arbitrary-write bug (a heap overflow into a pointer, a %n format string) is most useful when it can overwrite a pointer that the program will soon call through. The GOT is ideal: it holds the address of printf, free, strcpy, and the next call jumps wherever the entry points. RELRO removes that target. The session builds the same program three ways and inspects them:
cat > hello.c <<'SRC'
#include <stdio.h>
int main(void) { puts("hello"); return 0; }
SRC
gcc -O0 -Wl,-z,relro,-z,now hello.c -o full
gcc -O0 -Wl,-z,relro,-z,lazy hello.c -o partial
gcc -O0 -Wl,-z,norelro hello.c -o none
for b in full partial none; do checksec --file=$b | tail -1 | awk '{print $1, $2, "\t" $NF}'; done
echo "== full: dynamic flags and the read-only segment"
readelf -d full | grep -E 'FLAGS'
readelf -lW full | grep -E 'GNU_RELRO' | awk '{print $1, "size", $6}'
echo "== partial: no BIND_NOW, so .got.plt stays writable for lazy binding"
readelf -d partial | grep -cE 'BIND_NOW|FLAGS.*NOW'
readelf -SW partial | awk '$2==".got.plt" {print $2, "flags", $(NF-3)}'Full RELRO full
Partial RELRO partial
No RELRO none
== full: dynamic flags and the read-only segment
0x000000000000001e (FLAGS) BIND_NOW
0x000000006ffffffb (FLAGS_1) Flags: NOW PIE
GNU_RELRO size 0x000248
== partial: no BIND_NOW, so .got.plt stays writable for lazy binding
0
.got.plt flags WA
Full RELRO comes from two linker options: -z relro creates the GNU_RELRO program header (the range the loader makes read-only after relocation), and -z now sets BIND_NOW so that nothing must be patched later. Ubuntu's GCC passes both by default. The partial build lacks BIND_NOW: its .got.plt (flags WA, writable) stays writable for the whole run because the loader fills it lazily. With -z norelro nothing is protected.
What ASLR and RELRO don't stop
- Leaks. One leaked pointer from a region (a format string
%p, an over-read, an uninitialised field) reveals that region's base, because everything inside a region keeps its relative position. Defeating ASLR with a leak is the normal first step of a modern exploit. - Low entropy. On 32-bit systems there are only a few hundred to a few thousand possible positions, small enough to brute-force against a service that restarts after each crash. 64-bit Linux uses about 28 bits for
mmaprandomisation by default. - Forking servers. Children of
fork()inherit the parent's layout, so every crash-and-retry tests the same addresses. - Other writable function pointers. RELRO protects the GOT and
.init_array; vtables in heap objects, callbacks in structures, andatexithandlers remain targets, which is what CFI addresses.
How weak builds are detected
checksec reports PIE enabled / No PIE and Full RELRO / Partial RELRO / No RELRO; readelf -h prog shows Type: DYN for a PIE (EXEC for a fixed-address program); readelf -d prog | grep BIND_NOW confirms full RELRO. Distributions check packages with tools such as Debian's hardening-check and Fedora's annocheck (which reads the build notes recorded by the annobin compiler plugin), and container scanners report binaries without PIE.
How they are deployed
Keep the defaults: -fPIE -pie and -Wl,-z,relro,-z,now (both default on Ubuntu, Fedora and Android; Debian packages get -z now only with hardening=+bindnow or +all), never set randomize_va_space to 0 in production, re-exec (not just fork) worker processes where the design allows, and treat information leaks as serious bugs, because they are what turn ASLR from a wall into a speed bump.
Pitfalls
- Static linking used to mean no ASLR for the program;
-static-pieexists but must be asked for. - Debugger defaults: gdb disables ASLR for the programs it starts (
set disable-randomization on), so addresses in your gdb session are not what production sees. The sandbox's.gdbinitturns that off. -Wl,-z,lazysneaks in from old build scripts and silently downgrades to partial RELRO.
Practice
1. Why did printf's address change in the non-PIE build?
printf lives in libc.so.6, a shared library mapped with mmap at a randomised base. Only the program's own image is fixed without PIE.
2. An attacker leaks one pointer into libc. What else do they now know?
The address of everything else in libc: functions and data keep fixed offsets from the library's base, so base = leaked pointer − its known offset.
3. Why does full RELRO need BIND_NOW?
With lazy binding the loader must write GOT entries during the run, so they can't be read-only. Binding everything at start-up lets the loader make the whole GOT read-only before main runs.
Test yourself
In the non-PIE build, which address was the same in all five runs?
- printf
- The heap block
- main
- The local variable
What does full RELRO protect?
- The stack
- The GOT and other relocated data, made read-only after start-up
- The heap
- The vtables of heap objects
Which linker flag pair gives full RELRO?
- -z execstack
- -z relro -z now
- -z lazy
- -no-pie
Why is a single leaked pointer enough to defeat ASLR for a whole region?
- Pointers are encrypted
- Everything in a region keeps its offset from the region's base, so one known address reveals the base
- ASLR only moves the stack
- The kernel prints the base