The Process Address Space
Every bug in this course is a statement about where bytes are: a buffer on the stack next to a return address, a heap block next to another heap block, a global next to a function-pointer table. Before studying the bugs you need the map. On a 64-bit Linux system each process sees its own virtual address space of 2⁴⁷ bytes for user code (128 TiB), almost all of it unmapped. The kernel maps a handful of regions into it, each with its own permissions. The operating-system side (pages, page tables, the MMU) is taught in Operating Systems' Virtual Memory & Paging and Memory Management; this lesson is about which region each C object lives in, and why that matters for security.
The regions
| Region | Holds | Permissions | Security note |
|---|---|---|---|
.text |
machine code of the program | r-x |
never writable (W^X); with PIE its address is randomised |
.rodata |
string literals, const tables |
r-- |
writing a string literal crashes |
.data, .bss |
initialised and zero-initialised globals and static variables |
rw- |
global overflows (ASan global-buffer-overflow) corrupt neighbouring globals |
.got, .init_array |
addresses of library functions and constructors | rw-, then r-- with full RELRO |
a classic overwrite target, which is why RELRO exists |
| heap | malloc, new |
rw- |
allocator metadata sits between user blocks |
| shared libraries | libc.so.6 and friends, each with its own code and data |
as above | ASLR picks a random base |
| stack | one per thread: locals, saved registers, return addresses | rw- |
the return address is next to local arrays |
Watching a program find its objects
The toy program below takes the address of one object of each kind, looks the address up in /proc/self/maps (the kernel's list of the process's mappings) and prints the region's permissions and name. Printing the regions instead of raw addresses makes the output the same on every run, despite ASLR. It runs in your sandbox:
cat > where.c <<'SRC'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int counter = 42; /* .data: initialised global */
int zeroed; /* .bss: zero-initialised global */
const char banner[] = "lab"; /* .rodata: read-only constant */
static void where(const char *what, const void *p) {
FILE *f = fopen("/proc/self/maps", "r");
char line[512];
unsigned long lo, hi, a = (unsigned long)p;
while (fgets(line, sizeof line, f)) {
char perms[5], path[256] = "";
if (sscanf(line, "%lx-%lx %4s %*s %*s %*s %255s", &lo, &hi, perms, path) >= 3 && a >= lo && a < hi) {
const char *base = strrchr(path, '/');
printf("%-22s %s %s\n", what, perms, base ? base + 1 : (path[0] ? path : "(anonymous)"));
break;
}
}
fclose(f);
}
int main(void) {
int local = 1;
char *heap = malloc(32);
where("main (code)", (const void *)main);
where("banner (.rodata)", banner);
where("counter (.data)", &counter);
where("zeroed (.bss)", &zeroed);
where("malloc(32)", heap);
where("local variable", &local);
where("printf (libc code)", (const void *)printf);
free(heap);
return 0;
}
SRC
gcc -g -O0 where.c -o where && ./where
echo "== section flags in the file (A alloc, W write, X execute)"
readelf -SW where | awk '$2 ~ /^\.(text|rodata|data|bss|got|init_array)$/ {print $2, $(NF-3)}'main (code) r-xp where
banner (.rodata) r--p where
counter (.data) rw-p where
zeroed (.bss) rw-p where
malloc(32) rw-p [heap]
local variable rw-p [stack]
printf (libc code) r-xp libc.so.6
== section flags in the file (A alloc, W write, X execute)
.text AX
.rodata A
.init_array WA
.got WA
.data WA
.bss WA
Every object is where the table says: code r-xp, the constant r--p, both globals rw-p in the program's own mapping, the malloc block in [heap], the local in [stack] and printf in libc's code. The file's section flags say the same thing before the program runs: .text is AX (allocated, executable), .rodata only A, and .data, .bss and .got WA (writable). The p in r-xp means private: a write would make a copy for this process only.
Why the layout matters for security
- Neighbours. An overflow corrupts whatever is next in the same region. On the stack that is other locals, the saved frame pointer and the return address; on the heap, the next block's header; in
.data, the next global. Different regions are separated by unmapped gaps, so a linear overflow from the heap never reaches the stack. - Permissions. Because no page is both writable and executable, corrupted data can't simply be run as code. Attacks therefore aim at control data: return addresses, function pointers, vtable pointers, the GOT.
- Randomisation. ASLR places the stack, heap, libraries and (with PIE) the program at random bases at each start, so an attacker doesn't know where anything is without first leaking an address.
How layout bugs are detected
ASan names the region in every report: located in stack of thread T0, located 0 bytes after 16-byte region (heap), global variable 'name'. gdb's info proc mappings and /proc/<pid>/maps give the same map of a running process; readelf -lW shows the program headers the loader turns into mappings, including GNU_STACK (is the stack executable?) and GNU_RELRO.
How it is used defensively
The defaults of a modern toolchain already arrange the regions well: PIE so the program moves, W^X so nothing is writable and executable, RELRO so the GOT turns read-only, guard pages below each thread's stack, and stack-clash protection (-fstack-clash-protection) so a large local array can't jump over the guard page. Verifying them is the job of checksec.
Pitfalls
- "The heap" is not one thing. Large allocations (128 KiB and up by default in glibc) get their own anonymous
mmapregion instead of[heap]; each thread may use its own arena. - 32-bit processes have only 4 GiB of address space and far less room for randomisation; old embedded targets may have no MMU at all.
staticlocals live in.data/.bss, not on the stack: an overflow of astatic char buf[64]is a global overflow.
Practice
1. In which region is char *s = "abc"; stored, and what happens on s[0] = 'X';?
The pointer s is a local (stack), the bytes "abc" are in .rodata. The write hits a read-only page and the program gets SIGSEGV. Writing to a string literal is undefined behaviour.
2. Why can't a heap overflow overwrite a return address directly?
Return addresses are on the stack, a different mapping separated from the heap by a large unmapped gap. A linear heap overflow corrupts heap neighbours; reaching control data takes a heap pointer or function pointer among those neighbours.
3. Change the program to locate static char cache[64]; declared inside main. Which line do you expect?
rw-p where: static locals live in .bss (or .data if initialised), in the program's own writable mapping, not on the stack.
Test yourself
Which permissions does the stack mapping have on a modern Linux system?
- r-x
- rw-
- rwx
- r--
Where does a string literal such as "lab" live?
- On the stack
- On the heap
- In .rodata, read-only
- In .bss
Why do attacks target return addresses, function pointers and the GOT?
- They are large
- They are control data: overwriting them changes where the program executes, while W^X prevents running injected data
- They are on the heap
- They are never randomised
How did the toy program get output that is the same on every run?
- It disabled ASLR
- It printed the region and permissions from /proc/self/maps, not the raw addresses
- It ran as root
- It used static linking