Anatomy of a Stack Frame
Stack-Based Buffer Overflows · Exploitation Fundamentals track
6 min read
In this module
What Lives on the Stack
In The Stack & The Heap, you learned that the stack is the region of memory where functions keep their temporary data. Every call builds a stack frame: a block of memory holding the function's locals, a little bookkeeping, and the address it has to return to.
Overflows only make sense once you can draw that frame from memory, byte offsets and all. So this lesson builds one for a real x86-64 Linux function, then reads it back out of a debugger.
Building a Frame, One Instruction at a Time
Here's the function we'll dissect:
void greet(const char *name) { char buf[16]; strcpy(buf, name); printf("hi %s\n", buf);}Compiled with gcc -O0 on a typical Linux distribution (which turns on -fstack-protector-strong by default), the frame gets built in four steps:
call greetin the caller pushes the return address, the address of the instruction right after the call.rspdrops by 8.push rbpsaves the caller's frame pointer.rspdrops by another 8.mov rbp, rspmakesrbpthe fixed reference point for this frame. Everything is now addressed asrbpplus or minus an offset.sub rsp, 0x30reserves room for the locals: the 16-byte buffer, 8 bytes of padding, the 8-byte stack canary the compiler slipped in, and a copy of thenameargument. At-O0, gcc spills register arguments likerdionto the stack so the debugger can always find them.
The canary is a secret value copied from fs:0x28 at the start of the function and checked again just before ret. If it's changed, the program calls __stack_chk_fail and dies instead of returning. You'll take it apart properly in Exploit Mitigations. For now, just note where it sits.
A buffer is a contiguous block of memory that holds a sequence of data, such as a string of characters or an array of bytes. On the stack, a buffer is just a local variable that spans several consecutive addresses.
The Layout, Top to Bottom
The stack grows towards lower addresses, so the oldest things (pushed first) sit highest. For greet, a typical gcc build gives:
Offset from rbp | Size | Contents |
|---|---|---|
rbp+0x08 | 8 bytes | Return address (pushed by call) |
rbp+0x00 | 8 bytes | Saved rbp (the caller's frame pointer) |
rbp-0x08 | 8 bytes | Stack canary |
rbp-0x10 | 8 bytes | Padding |
rbp-0x20 | 16 bytes | char buf[16] |
rbp-0x28 | 8 bytes | Copy of the name argument, spilled from rdi |
Exact offsets shift between compiler versions and flags, so treat the table as typical rather than guaranteed. The order is the part that matters: buffer lowest, then canary, then saved rbp, then the return address.
Reading It in a Debugger
Break on the printf line, call greet("Bob"), and dump six 8-byte words starting at the buffer:
(gdb) x/6gx $rbp-0x200x7fffffffe0a0: 0x0000000000626f42 0x00000000000000000x7fffffffe0b0: 0x00007ffff7fe5af0 0x9c3d5e1a7b2f44000x7fffffffe0c0: 0x00007fffffffe0d0 0x00005555555551a9Walk it in order:
0x...626f42is"Bob\0"read backwards. x86 is little-endian, so the byte42(B) sits at the lowest address but prints at the right-hand end of the word.0x00007ffff7fe5af0is padding. It's leftover junk from whoever used this memory last, which is normal.0x9c3d5e1a7b2f4400is the canary. Its lowest byte is always00on glibc, so a runaway string copy hits a terminator before it can leak or rewrite the rest.0x00007fffffffe0d0is the savedrbp, a pointer to the caller's frame a little higher up the stack.0x00005555555551a9is the return address, somewhere inside the caller's code.
The visualiser below draws the same frame from the same dump, one 8-byte word per row. Now drag the slider to hand greet longer and longer names. At 16 characters the string fits, but strcpy still writes its terminating 0x00 one byte past the end. Watch what happens at 24 characters, when that 0x00 lands on the canary, and then at 25.
greet's frame (gcc -O0, stack protector on)
Every byte stays inside the buffer.
Each row is 8 bytes, lowest address on the left. Read it right to left for the little-endian value.
Predict: how many bytes from the start of buf to the first byte of the return address?
40. The buffer starts at rbp-0x20 and the return address at rbp+0x08, so the gap is 0x20 + 0x08 = 0x28. Without a canary, gcc usually puts buf at rbp-0x10 and the answer drops to 24. Get used to measuring this rather than assuming it.
Why Everything Is a Multiple of 16
You may have wondered why the compiler reserved 48 bytes when the table only accounts for 40. The System V ABI requires rsp to be 16-byte aligned at every call instruction. The call itself pushes 8 bytes, push rbp pushes 8 more, and so rsp is aligned again inside the function. The compiler then rounds the space it subtracts up to a multiple of 16 to keep it that way, because SSE instructions such as movaps fault on misaligned addresses.
Predict: a function's locals need 52 bytes. After push rbp, what's the smallest sub rsp, N that keeps the stack aligned?
64. 52 isn't a multiple of 16, so it rounds up to the next one. The spare 12 bytes are padding, the same kind you saw at rbp-0x10 above.
At -O2 and above, gcc often skips the rbp frame pointer entirely and addresses locals from rsp instead. The return address is still there; you just have to count from the other end.
Why the Layout Matters
The buffer sits at the lowest address in the frame, and writes into it run upwards. Copy more than 16 bytes into buf and the excess pours into the padding, then the canary, then the saved rbp, and finally the return address: the value ret loads into rip when greet finishes.
That's the whole trick behind a stack buffer overflow. Control the bytes at rbp+0x08 and you decide where the CPU goes next.
Connecting to What You Know
In Functions & Control Flow, you saw CALL push a return address and RET pop it. Now you can see that the return address lives in the same block of memory as the function's own data, separated from a writable buffer by nothing more than a few bytes of padding and a canary. Any bug that writes past a buffer's end can reach it.
Try the Simulations
Safe Buffer Write
A function writes exactly into its 3-slot buffer on the stack. The return address is untouched and RET works normally.
Buffer Overflow
Write one element too many into a 3-slot buffer. The fourth write clobbers the return address - the CPU would jump to the wrong place.
Controlled Overflow
An attacker deliberately overwrites the return address with a chosen value, redirecting execution to their target address.