Skip to main content
Be Bitwise

Anatomy of a Stack Frame

Stack-Based Buffer Overflows · Exploitation Fundamentals track

6 min read

Part ofSystems Programming Companion, week 5

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:

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

  1. call greet in the caller pushes the return address, the address of the instruction right after the call. rsp drops by 8.
  2. push rbp saves the caller's frame pointer. rsp drops by another 8.
  3. mov rbp, rsp makes rbp the fixed reference point for this frame. Everything is now addressed as rbp plus or minus an offset.
  4. sub rsp, 0x30 reserves 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 the name argument. At -O0, gcc spills register arguments like rdi onto 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.

Buffer

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 rbpSizeContents
rbp+0x088 bytesReturn address (pushed by call)
rbp+0x008 bytesSaved rbp (the caller's frame pointer)
rbp-0x088 bytesStack canary
rbp-0x108 bytesPadding
rbp-0x2016 byteschar buf[16]
rbp-0x288 bytesCopy 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:

text
(gdb) x/6gx $rbp-0x20
0x7fffffffe0a0: 0x0000000000626f42 0x0000000000000000
0x7fffffffe0b0: 0x00007ffff7fe5af0 0x9c3d5e1a7b2f4400
0x7fffffffe0c0: 0x00007fffffffe0d0 0x00005555555551a9

Walk it in order:

  • 0x...626f42 is "Bob\0" read backwards. x86 is little-endian, so the byte 42 (B) sits at the lowest address but prints at the right-hand end of the word.
  • 0x00007ffff7fe5af0 is padding. It's leftover junk from whoever used this memory last, which is normal.
  • 0x9c3d5e1a7b2f4400 is the canary. Its lowest byte is always 00 on glibc, so a runaway string copy hits a terminator before it can leak or rewrite the rest.
  • 0x00007fffffffe0d0 is the saved rbp, a pointer to the caller's frame a little higher up the stack.
  • 0x00005555555551a9 is 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.

0x7fffffffe0a0006F620000000000Buffer
0x7fffffffe0a80000000000000000Buffer
0x7fffffffe0b0F05AFEF7FF7F0000Padding
0x7fffffffe0b800442F7B1A5E3D9CCanary
0x7fffffffe0c0D0E0FFFFFF7F0000Saved rbp
0x7fffffffe0c8A951555555550000Return addr

Each row is 8 bytes, lowest address on the left. Read it right to left for the little-endian value.

Regions
Buffer16 B
Padding8 B
Canary8 B
0x9c3d5e1a7b2f4400
Saved rbp8 B
0x00007fffffffe0d0
Return addr8 B
0x00005555555551a9
Overflow direction
Buffer→Canary→Saved rbp→Return addr
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.