ASLR Implementation
Virtual Memory Advanced · Memory & Systems track
6 min read
In this module
Randomizing the Attack Surface
Address Space Layout Randomisation (ASLR) is one of the most effective exploit mitigations ever shipped. By scrambling where code and data get loaded, ASLR forces attackers to guess addresses instead of hardcoding them. A bad guess crashes the program rather than handing over control.
ASLR adds entropy to the locations of code and data, not their contents. The attacker still has the same exploit primitive; they just don't know where to point it. Defeating ASLR usually means leaking an address from somewhere else first.
How ASLR Randomizes
Linux ASLR randomizes several independent regions, each with its own entropy source.
Stack: the kernel sprinkles a random offset onto the initial stack pointer. On x86-64 Linux, that gives roughly 22 bits of entropy (a random multiple of 16 bytes within a range). The stack's exact position shifts by millions of addresses between runs.
mmap base: shared libraries and large mmap allocations sit on top of a randomized base address. On x86-64, around 28 bits of entropy. Because every library is loaded relative to that base — with fixed offsets between them determined by the dynamic linker — leaking any single library address gives up the base for all of them.
Heap: the heap (brk region) gets a random offset of up to about 13 bits on x86-64. Less entropy than other regions, because the heap has to stay anchored near the data segment.
Executable (PIE): if the executable is compiled as a Position-Independent Executable (PIE), its base address is randomized with around 28 bits of entropy. Non-PIE executables get loaded at a fixed address (typically 0x400000 on x86-64), giving attackers a stable starting point — which is exactly what they want.
$ cat /proc/self/maps# Run 1:557d8a400000-557d8a402000 r--p 00000000 ... /bin/cat7f3b2c000000-7f3b2c022000 r--p 00000000 ... /lib/libc.so.67ffc6e800000-7ffc6e821000 rw-p 00000000 ... [stack]
# Run 2 (different random offsets):563a1b200000-563a1b202000 r--p 00000000 ... /bin/cat7f8d3a400000-7f8d3a422000 r--p 00000000 ... /lib/libc.so.67ffd2b600000-7ffd2b621000 rw-p 00000000 ... [stack]28 bits of entropy means 2^28 (~268 million) possible base addresses. A brute-force attack against a remote service that crashes and restarts on wrong guesses would need ~134 million attempts on average. For a local exploit or one that can be retried quickly, this may be feasible. For a remote service with rate limiting, it is impractical. Less entropy (like the heap's ~13 bits) is more vulnerable to brute force, and 32-bit processes are the classic soft target: Linux gives a 32-bit x86 process only 8 bits of mmap randomisation by default, and Shacham et al. (CCS 2004) brute-forced 16-bit PaX ASLR on 32-bit Linux in a few minutes. The amount of entropy directly determines ASLR's practical security strength.
PIE: Position-Independent Executables
ASLR only randomizes the executable's base address if it's compiled as a Position-Independent Executable (PIE). PIE binaries use position-independent code (PIC) for their own code, not only for shared libraries. The compiler emits RIP-relative addressing in place of absolute addresses.
# Compile as PIE (default on most modern distros)gcc -pie -fPIE program.c -o program
# Compile as non-PIE (fixed base address)gcc -no-pie program.c -o programWithout PIE, the executable lands at a fixed address (0x400000) and all of its code and data sit at known addresses. That hands attackers a stable source of ROP gadgets and known data locations, gutting most of what ASLR offers.
Most modern Linux distributions ship every package as PIE by default. Verify with file or checksec. A PIE binary has the ELF type ET_DYN, the same as a shared library, so older versions of file call it a "shared object"; current ones print "pie executable". A non-PIE binary shows up as a plain "executable".
Information Leaks That Defeat ASLR
ASLR is only as strong as the attacker's inability to learn the randomized addresses. Information leaks — bugs that spill memory contents to the attacker — can reveal addresses and completely undo ASLR.
Common information leak sources:
- Format string vulnerabilities:
printf(user_input)leaks stack contents, including return addresses and library pointers - Uninitialised memory disclosure: reading uninitialised heap or stack memory may contain leftover pointers
- Over-read bugs (like Heartbleed): reading past the end of a buffer may disclose adjacent pointers
- Error messages: stack traces, debug output, and verbose error messages may contain addresses
- Timing side channels: cache timing attacks can infer which addresses are mapped
A single leaked pointer often gives up the entire library layout (since libraries sit at fixed offsets relative to each other) and the executable layout too (if it was mapped relative to the leaked pointer).
Per-Boot vs Per-Exec Randomisation
Per-exec randomisation, the standard for user-space processes, generates fresh random offsets every time a program starts. Even if an attacker leaks an address in one run, the addresses are completely different in the next. The damage from any leak is contained to a single process instance.
Per-boot randomisation is what KASLR (Kernel ASLR) uses. The kernel's base address is randomized once at boot and stays put until reboot. Weaker model: one kernel address leak reveals the kernel layout for the entire system uptime. Entropy is typically only about 9 bits on x86-64 (512 possible offsets), making brute force feasible whenever the kernel doesn't crash on wrong guesses.
ASLR on Other Platforms
Windows has shipped ASLR since Vista, with improvements in every version since. It randomizes the executable (if compiled with /DYNAMICBASE), DLLs, the stack, and the heap. Shared DLLs are randomized per-boot rather than per-exec, so code pages can still be shared across processes.
macOS has had ASLR since OS X Leopard (10.5) for libraries and Mountain Lion (10.8) for the entire address space. Apple's implementation is generally considered strong, with healthy entropy and per-exec randomisation.
ARM systems also support ASLR. The amount of entropy depends on the OS and virtual address space configuration, typically 28-33 bits for 48-bit VA systems.