Skip to content

Lesson 2 of 9 · 26m

Stack Frames, Corruption, and the Mental Model That Matters

Understand what a stack frame contains, how overwrite direction and allocator behavior shape corruption, and why diagrams beat folklore.

Every exploit developer carries a memory map in their head, but the useful one is not a poster full of magic addresses. It is a causal model: what data is adjacent, what metadata trusts it, and what later operation will consume corrupted bytes. If you cannot answer those three questions, an instruction-pointer overwrite is luck rather than understanding.

A frame is a contract, not a diagram on a slide

For a simple non-PIE x86-64 function, the compiler arranges local variables, saved frame state, return addresses, and arguments according to its calling convention and optimization choices:

higher addresses
  return address
  saved rbp or frame metadata
  compiler padding / red zone / alignment
  local char name[64]
lower addresses

Do not assume a neat textbook layout. Optimizers reorder, inline, remove frames, promote variables to registers, insert stack protector cookies, and add alignment padding. Inspect the real binary:

objdump -d -M intel /tmp/vuln-greet | sed -n '/<greet>:/,/^$/p'
gdb -q /tmp/vuln-greet -ex 'disassemble /s greet' -ex quit

The disassembly is the ground truth. The C source is intent; the machine code is behavior.

Corruption is directional and contextual

A stack overwrite often writes toward higher or lower addresses depending on copy direction and buffer layout, while heap corruption is dominated by allocator metadata and object adjacency. This is why "overflow by N bytes" is not enough. You need:

source operation       strcpy, memcpy, loop write, format handling
destination object     stack buffer, heap object, global, struct member
bounds decision        absent, wrong length, signedness, integer overflow
write granularity      bytes, chunks, repeated writes, terminating NUL
consumer               return path, function pointer, vtable, free logic

The consumer matters as much as the write. Corrupted bytes become exploitable when trusted control data is later interpreted.

Terminators are part of the primitive

strcpy(first, user_input) writes strlen(user_input) + 1 bytes. That +1 decides whether a 32-byte input fits exactly or becomes a one-byte overflow. snprintf may write partial output while returning the intended length, and memcpy may trust a confused count; count bytes, including terminators and alignment.

Modern compilers complicate simple models

Stack reordering may place arrays away from saved control data. Stack protectors insert a canary between locals and control flow. AddressSanitizer changes layout intentionally to catch violations. None of this means the model is useless; it means the model must be derived per build.

Useful commands include readelf -h /tmp/vuln-greet for ELF type and gdb -ex 'info proc mappings' for live layout.

Field notes

  • Integer overflow in a length calculation can turn a "safe" copy into a heap overwrite before the copy even starts.
  • Use-after-free is a lifetime bug, not an overflow; controlling reused memory depends on allocator timing, not just byte counts.
  • Do not extrapolate from a 32-bit tutorial to x86-64, ARM64, or a different compiler without re-inspecting the frame.
  • A single NUL byte can terminate strings, truncate payload delivery, or overwrite a pointer's low byte with surprising precision.
  • Draw adjacency from observed memory in gdb, not from a generic diagram copied from the internet.

Was this lesson helpful?

Sign in to rate