#Why this matters
Memory corruption bugs do not modify data at random. When an unchecked copy writes past the end of an array in C, the extra bytes land in whatever sits immediately next to that array in virtual memory. Knowing what lives at lower and higher addresses turns a mysterious crash (SIGSEGV) into something you can diagnose, reproduce, and prevent.
#Virtual memory map of a Linux ELF process
When the Linux kernel loads an Executable and Linkable Format (ELF) binary, it gives the process an isolated virtual address space. Each region has explicit read (r), write (w), and execute (x) permissions enforced by the CPU memory management unit.
- HighKernel SpaceReserved for the OS kernel (inaccessible to user mode)
- HighStackLocal variables, saved RBP, return addresses (rw-, grows down)
- MidMemory MappingsShared libraries like libc.so.6 and mmap regions
- LowHeapDynamic allocations via malloc/free (rw-, grows up)
- LowData and BSSInitialized (.data) and zeroed (.bss) globals (rw-)
- LowText and RodataMachine instructions (.text, r-x) and constants (.rodata, r--)
| Region | Typical Permissions | Grows | Managed By |
|---|---|---|---|
.text |
r-x (read, execute) |
Fixed at load | Kernel ELF loader |
.rodata |
r-- (read only) |
Fixed at load | Kernel ELF loader |
.data / .bss |
rw- (read, write) |
Fixed at load | Kernel ELF loader |
| Heap | rw- (read, write) |
Upward (toward higher addresses) | Allocator (brk, mmap) |
| Stack | rw- (read, write) |
Downward (toward lower addresses) | CPU (RSP) and function prologues |
Notice the security principle behind those permissions: W^X (Write XOR Execute). Code sections are readable and executable (r-x) so the CPU can fetch instructions, while data regions like the stack and heap are writable (rw-) but non-executable when NX is enabled.
#Inspecting sections and live mappings
Before running a binary, inspect its static ELF structure and compile-time mitigations. readelf reveals the sections inside the file on disk, while checksec summarizes active protections.
$ readelf -S ./chall | grep -E "Name|\.text|\.data|\.bss"
[Nr] Name Type Address Offset
[14] .text PROGBITS 0000000000401080 00001080
[23] .data PROGBITS 0000000000404018 00003018
[24] .bss NOBITS 0000000000404028 00003028
$ checksec --file=./chall
RELRO STACK CANARY NX PIE
Partial RELRO No canary found NX enabled No PIEBecause No PIE (Position-Independent Executable disabled) appears here, the .text and .data sections load at predictable virtual addresses (0x401080, 0x404018) every run. Inside GDB with pwndbg, the vmmap command displays the live runtime mappings (equivalent to /proc/<pid>/maps), including libc and the stack:
pwndbg> vmmap
0x401000 0x402000 r-xp 1000 1000 ./chall
0x404000 0x405000 rw-p 1000 3000 ./chall
0x405000 0x426000 rw-p 21000 0 [heap]
0x7ffff7d80000 0x7ffff7f35000 r-xp 1b5000 28000 /lib/x86_64-linux-gnu/libc.so.6
0x7ffffffde000 0x7ffffffff000 rw-p 21000 0 [stack]Reading vmmap confirms that the heap starts just above .bss at 0x405000, shared libraries sit in high 0x7ffff7... addresses, and the stack sits near the top of user space at 0x7ffffffde000.
#Stack frames and local variable overwrites
On x86-64 Linux, the stack pointer RSP tracks the current top of the stack (the lowest active address), and RBP typically marks the base of the current function's frame. Calling a function pushes the 8-byte return address onto the stack; the callee then saves the caller's RBP and subtracts from RSP to reserve space for local variables:
void check_access(void) {
char buf[16];
volatile int is_admin = 0;
read(0, buf, 48); // Bug: reads up to 48 bytes into a 16-byte buffer
if (is_admin == 0x41424344) {
puts("Lab check passed");
}
}Here is the crucial spatial relationship: the stack grows downward, while buffers write upward. When read() writes buf[0], buf[1], and so on, the bytes advance toward higher memory addresses, moving from buf toward is_admin, then saved RBP, and finally the saved return address.
Do not guess variable offsets from C source order alone, because compilers can reorder or pad local variables. Always verify the disassembled frame in pwndbg: if buf is at [rbp-0x20] and is_admin is at [rbp-0x10], then 0x20 - 0x10 = 16 bytes fill buf and bytes 17 through 20 overwrite is_admin.
#Heap overview: dynamic chunks and metadata
Unlike the stack, which automatically creates and destroys frames on every call and return, the heap serves allocations whose lifetimes cross function boundaries (malloc, calloc, realloc, free). The heap grows upward from the end of .bss toward higher addresses.
Under Linux's standard glibc allocator (ptmalloc), each allocation is stored inside a chunk: a small metadata header (storing the chunk size and low-order status flags such as PREV_INUSE) immediately followed by the payload pointer returned to your program. When you call free(), the allocator does not zero your memory; instead, it places the chunk onto a size-indexed free list (such as tcache or fastbins) for quick reuse. Overflowing a heap buffer corrupts the adjacent chunk's size field or a neighboring object's data, while using a pointer after free() lets new allocations overwrite stale data.
#Defending and common mistakes
Modern toolchains mitigate memory corruption through layered defaults:
- Stack canaries (
-fstack-protector-strong): place a random secret value between local buffers and savedRBP; the function checks it before returning. - ASLR and PIE: randomize the base addresses of the stack, heap, libraries, and main executable on each launch.
- Bounds checking and safe APIs: replace
getsand unboundedstrcpy/memcpywithfgets,snprintf, or explicit length checks againstsizeof(buf).
Common mistakes when studying memory layout include:
- Assuming C variable declaration order matches stack order without checking disassembly (
disassinpwndbg). - Confusing stack growth direction (downward) with buffer write direction (upward).
- Forgetting little-endian byte order when writing multi-byte integers into memory on x86-64.
- Treating ASLR or NX alone as a fix for buffer overflows instead of removing the out-of-bounds write.
#Summary
- A Linux ELF process splits virtual memory into fixed code/data sections (
.text,.rodata,.data,.bss), an upward-growing heap, shared libraries, and a downward-growing stack. readelfandchecksecinspect binary sections and mitigations on disk, whilepwndbg'svmmapreveals live runtime addresses and permissions.- Because buffers write toward higher addresses while stack frames stack downward, overflowing a local buffer overwrites adjacent locals, saved
RBP, and the return address in sequence. - Heap allocations store
ptmallocchunk metadata directly beside user data, making bounds checks and lifetime discipline essential in both stack and heap code.