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 spaceNow 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?
Keep reading
Related articles from the SudoSecurity blog.
A Password-Free Path to Root: Inside the macOS Screen Sharing Vulnerability Now Under Attack
A logic bug in screensharingd let attackers skip authentication entirely and land as root — no password required. Here's how it was found, patched, reverse-engineered, and finally weaponized.
BlueHammer: The Unpatched Windows Zero-Day That Weaponizes Microsoft Defender Against Your Own System
BlueHammer is an unpatched, publicly released Windows local privilege escalation exploit that chains Microsoft Defender's update workflow, Volume Shadow Copy, Cloud Files callbacks, and opportunistic locks to reach NT AUTHORITY\SYSTEM from a standard user account.
What is Privilege Escalation? How Attackers Go From Foothold to Full Control
An attacker starts with a low-privileged shell or user account and escalates to root or Administrator using misconfigurations, SUID binaries, kernel exploits, and Active Directory attacks. Here's every technique, the tools professionals use, and how to harden against it.
Jellyfin's Threat Model Is Your Brother-in-Law
Four Jellyfin CVEs landed in 2026 and not one of them needs an admin account. One needs only the ability to name a file. Meanwhile Jellyfin 12 shipped in September, the version numbers stopped starting with 10, and every "am I patched" check you wrote is now wrong.