Part 5 of 7 · 3 min read
ASLR and PIE
Think of a bank that moves its vault to a different room each night, and the thief has only a map from last week. The analogy stops in one place: a thief can try rooms, and a program that crashes on a wrong guess may be restarted by the system, which allows many guesses.
In plain words: ASLR (address space layout randomisation) makes the kernel place the stack, the heap and the libraries at random addresses each time a program starts, so an attacker cannot know where code or data lies. The setting /proc/sys/kernel/randomize_va_space was 2 on the test machine, which means full randomisation. The program's own code is moved too only if it is built as a PIE (position-independent executable), whose code uses addresses relative to where it is loaded. Without PIE the program's code and global data stay at fixed addresses, though the stack, heap and libraries still move.
A worked example. This program prints the address of its code, a global, a heap block, a stack variable and a library function.
#include <stdio.h>
#include <stdlib.h>
int global;
int main(void) {
int local;
void *heap = malloc(16);
printf("code %p global %p heap %p stack %p libc(printf) %p\n",
(void *)main, (void *)&global, heap, (void *)&local, (void *)printf);
free(heap);
return 0;
}
Three runs of the PIE build on the test machine:
code 0x5571ce883199 global 0x5571ce886014 heap 0x5571ee2ef2a0 stack 0x7ffcc54e7f0c libc(printf) 0x7ff165a60100
code 0x55a029511199 global 0x55a029514014 heap 0x55a0661f02a0 stack 0x7ffdfdcfd15c libc(printf) 0x7fd94de60100
code 0x55bfd859b199 global 0x55bfd859e014 heap 0x55bffce102a0 stack 0x7ffcf22fda7c libc(printf) 0x7fc8aa060100
Every address changed on every run. Ten runs gave ten different code addresses. The non-PIE build printed the same code 0x401186 and global 0x40402c in all three runs, while its heap, stack and library addresses still changed. Running with setarch -R (which turns randomisation off for that run) printed the same addresses twice: code at 0x555555555199 and stack at 0x7fffffff9b4c. The test machine had /proc/sys/vm/mmap_rnd_bits at 28, so the library base can take 2^28 = 268,435,456 positions. An attacker who guesses blindly succeeds with probability 1 in 268 million per try.
The key lines. Randomisation turns a certain attack into a guess, and the guess is harder if a wrong try kills the program and the program is not restarted at once. A memory leak that prints an address gives the attacker the missing information, so an information leak is as serious as an overflow.
ASLR
ASLR (address space layout randomisation) is a kernel feature that places a process's stack, heap and libraries (and, for a PIE, its code) at random addresses on each run, so that an attacker cannot predict where code or data will be.
Example: three runs of the same program printed three different stack addresses.
Common mistake
ASLR is not a fix. It hides addresses, and a bug that leaks one address can defeat it. A program with a buffer overflow is still a program with a bug.
Check yourself: 1. What changes on each run with ASLR, and what stays fixed in a non-PIE program? 2. What does PIE add? 3. Why is a memory leak a threat to ASLR?