Skip to main content
Process Memory Layout, Stack Frames & Heap Overview
Lesson 13 of 20

Process Memory Layout, Stack Frames & Heap Overview

Understand how Linux arranges ELF sections, the stack, and the heap in virtual memory, and learn to inspect stack frames with readelf, checksec, and pwndbg.

Intermediate 26 min IoT & embedded
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain the virtual memory regions of a running Linux ELF process and their access permissions
  • Inspect binary mitigations and section layouts using checksec and readelf
  • Trace x86-64 stack frame layout and predict how a buffer overflow overwrites adjacent local variables
  • Distinguish stack allocation from glibc heap chunk management

#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.

  1. HighKernel SpaceReserved for the OS kernel (inaccessible to user mode)
  2. HighStackLocal variables, saved RBP, return addresses (rw-, grows down)
  3. MidMemory MappingsShared libraries like libc.so.6 and mmap regions
  4. LowHeapDynamic allocations via malloc/free (rw-, grows up)
  5. LowData and BSSInitialized (.data) and zeroed (.bss) globals (rw-)
  6. 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.

Inspecting a lab binary statically and inside pwndbg
$ 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 PIE

Because 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:

Checking live memory regions with pwndbg
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:

c
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 saved RBP; 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 gets and unbounded strcpy/memcpy with fgets, snprintf, or explicit length checks against sizeof(buf).

Common mistakes when studying memory layout include:

  • Assuming C variable declaration order matches stack order without checking disassembly (disass in pwndbg).
  • 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.
  • readelf and checksec inspect binary sections and mitigations on disk, while pwndbg's vmmap reveals 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 ptmalloc chunk metadata directly beside user data, making bounds checks and lifetime discipline essential in both stack and heap code.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge