Skip to content

Lesson 2 of 14 · 12m

Stack Frames and the System V ABI

What call, push rbp, and ret actually do: where the saved return address sits relative to your buffers, and why ret doesn't care who wrote it.

The stack is a call log the CPU keeps for you

Here is what happens when C code calls a function on x86-64. The caller places up to six integer arguments in rdi, rsi, rdx, rcx, r8, r9 — that's the System V AMD64 ABI, the convention every Linux compiler emits — and executes call. call pushes the address of the next instruction onto the stack and jumps. Inside the callee, the classic prologue runs:

push rbp
mov  rbp, rsp
sub  rsp, 0x40        ; 64 bytes of local space

Now rbp is a stable anchor: locals live at negative offsets, rbp-0x4 might be an int, rbp-0x40 a 56-byte char buf[56]. Above rbp sit two things the caller left behind: the saved rbp at [rbp] and the saved return address at [rbp+8]. Arguments past the sixth spill to [rbp+0x10] and up. On leave/ret the frame unwinds: rsp is restored, rbp popped, and ret pops the saved return address into rip. That last pop is the whole ballgame. ret does not know or care who pushed the value it jumps to.

The wrong mental model

Beginners picture "the return address" as a special CPU register the machine protects. There is no such register. The return address is eight bytes of ordinary writable stack memory, sitting a fixed distance above every local buffer, distinguished only by the convention that ret will consume it. A strcpy writing into buf doesn't "corrupt control flow" — it just writes bytes upward through buf's end, through the saved rbp, and on through the saved rip. The CPU happily executes whatever address those bytes decode to. The bug is never in the CPU's bookkeeping. The bookkeeping is the bug's target.

One more subtlety the ABI forces on you: at any call instruction, rsp must be 16-byte aligned so that SSE instructions like movaps inside the callee don't fault. Keep this in your pocket — it causes the single most common "my exploit crashes in libc" failure, and it is not a bug in your chain.

Two more details in the ABI are worth memorizing now. Floating-point and variadic calls place al = number of vector registers used — that's why printf reads al first. And binaries built with Intel CET emit endbr64 at every function entry, a no-op on older CPUs that becomes a landing-pad check on CET hardware: an indirect jump that lands anywhere but an endbr64 faults. Both details appear in real disassembly and confuse nobody who has met them before.

Reading a frame

Disassemble any function and the structure is right there:

gef> disas greet
   0x4011a9 <+0>:  push rbp
   0x4011aa <+1>:  mov  rbp,rsp
   0x4011ad <+4>:  sub  rsp,0x50
   0x4011b1 <+8>:  lea  rax,[rbp-0x50]
   0x4011b5 <+12>: mov  rdi,rax
   0x4011b8 <+15>: call 0x401060 <gets@plt>

buf is 0x50 bytes below rbp; the saved return address is at rbp+8, i.e. 0x50 + 8 = 88 bytes of input away. That arithmetic — buffer size plus saved rbp — is how you compute your offset before you write a single byte of payload. Modern compilers sometimes omit rbp as a frame pointer (-fomit-frame-pointer, default at -O1+); then you compute the distance from rsp at the point of the overflowing write, which changes as rsp moves — one reason frame-pointer-less binaries are mildly annoying to exploit by hand.

Land on practice

Do it once by hand before pwntools does it for you. Compile gcc -fno-stack-protector -no-pie target.c, open in GDB, b *greet+15, run, x/16gx $rsp. Find your input bytes in memory, count to the saved rip slot, confirm 88. Every later technique — canaries, format strings, ROP — is bookkeeping layered on this one layout. If you can read a stack dump and point at the saved return address, you already understand what a stack overflow buys an attacker: nothing more and nothing less than choosing the next rip.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.