Skip to main content
Be Bitwise
x86 AssemblyBuffer Overflows

Overwriting the Return Address

You already know local buffers and the return address share the same stack. Now we get specific. This lesson walks through what happens byte by byte when a write blows past the buffer boundary, mangles the saved frame pointer, and replaces the return address. The payoff is the most powerful primitive in exploitation: control of rip.

For the theoretical foundation, see Controlling Execution.

The Stack Before the Overflow

Cast back to our vulnerable function from the previous lesson. Call it with something tame like "AAAA" and the stack looks clean:

Buffer Overflow Simulator

Input: 0 bytes | Buffer: 64 bytes | Overflow: 0 bytes

Stack (high addresses at top):

0x7fffffffe04000buffer[0..63]
0x7fffffffe04400
0x7fffffffe04800
0x7fffffffe04c00
0x7fffffffe05000
0x7fffffffe05400
0x7fffffffe05800
0x7fffffffe05c00
0x7fffffffe06000
0x7fffffffe06400
0x7fffffffe06800
0x7fffffffe06c00
0x7fffffffe07000
0x7fffffffe07400
0x7fffffffe07800
0x7fffffffe07c00
0x7fffffffe0800x00007fffffffe0d0Saved RBP
0x7fffffffe0880x00005555555551a9Return Address
Buffer
Saved RBP
Return Addr

Type into the simulator to watch the bytes land. vuln was built with -fno-stack-protector, so there's no canary: the saved rbp starts right at byte 64 and the return address at byte 72.

Four bytes plus the null terminator fit nowhere near the edge of a 64-byte buffer. The saved rbp and return address stay intact. When ret runs, it pops 0x00000000004011a0 into rip and execution heads back to main like nothing happened.

The Stack After the Overflow

Now picture the input as 72 bytes of 'A' (0x41) followed by the target address. The 72 'A's fill the buffer and the saved rbp, and the next bytes land in the return-address slot.

There's a catch with the address. 0x00000000deadbeef in little-endian is ef be ad de 00 00 00 00, and strcpy stops at the first zero byte, so the four upper bytes can't be part of the string. They don't need to be. strcpy copies the 72 'A's and ef be ad de, then writes its terminating NUL into the fifth byte of the slot, 77 bytes in all starting at [rbp-64]. The original return address (0x00000000004011a0) already had zeros in its top bytes, so the slot now reads 0x00000000deadbeef. User-space addresses on x86-64 always have their top two bytes zero, which is why this trick works so often.

What Happens at ret

ret does one thing — pop the top of the stack into rip. The exact sequence around it:

nasm
leave ; equivalent to: mov rsp, rbp; pop rbp
ret ; equivalent to: pop rip

Step by step:

  1. mov rsp, rbp — drags the stack pointer back to the frame pointer. rsp now points at the saved rbp slot, which we've overwritten with 0x4141414141414141.

  2. pop rbp — pops 8 bytes off the stack into rbp, loading our garbage value 0x4141414141414141. rsp ticks up by 8 and lands on the return-address slot.

  3. ret (effectively pop rip) — pops 8 bytes into the instruction pointer, loading 0x00000000deadbeef.

  4. The CPU starts fetching from 0x00000000deadbeef. If something executable lives there, it runs. If not, the process eats a segmentation fault.

Controlling RIP = Arbitrary Code Execution

The instant an attacker controls rip, they decide what the CPU runs next. This is the control-flow hijack primitive, and it's functionally equivalent to arbitrary code execution. Every modern mitigation — DEP, ASLR, stack canaries, CFI — exists to stop or muddle exactly this one moment. Set rip to an address holding code you wrote, and the process is yours.

Calculating the Exact Offset

Pinning down the exact byte count between the buffer's start and the return address is your opening move. A few ways to get it:

Manual calculation from disassembly:

nasm
lea rax, [rbp-64] ; buffer starts at rbp - 64

The return address is at rbp + 8. So the offset is:

c
offset = (rbp + 8) - (rbp - 64) = 72 bytes

You need 72 bytes of padding, then your 8-byte target address.

Pattern-based discovery with cyclic patterns:

When the disassembly isn't handy, you can generate a De Bruijn sequence — a pattern where every 4- or 8-byte substring appears at most once. Feed the pattern in as input, watch the crash, and read the value sitting in rip:

c
Pattern: Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2...

Don't expect to find the pattern in rip, though. Eight ASCII bytes make a non-canonical address: x86-64 only accepts addresses whose top 16 bits are copies of bit 47, and anything else makes ret fault before it jumps. So the crash is reported at the ret instruction itself, and the pattern bytes are still sitting at the top of the stack. Read them with x/gx $rsp. For this binary you'd see 0x6341356341346341, the ASCII bytes "Ac4Ac5Ac", and that substring starts at index 72 of the pattern: the offset.

GDB confirmation:

c
(gdb) run $(python3 -c "print('A'*72 + 'B'*6)")
Program received signal SIGSEGV
RIP: 0x424242424242

Six Bs rather than eight keeps the address canonical: strcpy's NUL and the zero top byte of the old return address fill the slot out to 0x0000424242424242. Nothing is mapped there, so ret succeeds and the fault happens on the jump target, with the B's right there in rip. That confirms the offset is exactly 72 bytes.

Byte-by-Byte Corruption Walkthrough

Trace the corruption precisely. Our input is crafted like so:

python
payload = b'A' * 64 # fill the buffer
payload += b'B' * 8 # overwrite saved rbp
payload += b'\xef\xbe\xad\xde\x00\x00\x00\x00' # overwrite return address (little-endian)

Here's the frame just before leave, with that payload in place. Offsets count from the start of buffer. Bytes 0-63 are the As, bytes 64-71 the Bs over the saved rbp, and byte 72 is the start of the return address. The visualiser shows only that slot's first byte, 0xEF.

Stack Frame View

0041Buffer← SP
0141Buffer
0241Buffer
0341Buffer
0441Buffer
0541Buffer
0641Buffer
0741Buffer
0841Buffer
0941Buffer
0A41Buffer
0B41Buffer
0C41Buffer
0D41Buffer
0E41Buffer
0F41Buffer
1041Buffer
1141Buffer
1241Buffer
1341Buffer
1441Buffer
1541Buffer
1641Buffer
1741Buffer
1841Buffer
1941Buffer
1A41Buffer
1B41Buffer
1C41Buffer
1D41Buffer
1E41Buffer
1F41Buffer
2041Buffer
2141Buffer
2241Buffer
2341Buffer
2441Buffer
2541Buffer
2641Buffer
2741Buffer
2841Buffer
2941Buffer
2A41Buffer
2B41Buffer
2C41Buffer
2D41Buffer
2E41Buffer
2F41Buffer
3041Buffer
3141Buffer
3241Buffer
3341Buffer
3441Buffer
3541Buffer
3641Buffer
3741Buffer
3841Buffer
3941Buffer
3A41Buffer
3B41Buffer
3C41Buffer
3D41Buffer
3E41Buffer
3F41Buffer
4042Saved rbp
4142Saved rbp
4242Saved rbp
4342Saved rbp
4442Saved rbp
4542Saved rbp
4642Saved rbp
4742Saved rbp
48EFReturn addr
4900Return addr
4A00Return addr
4B00Return addr
4C00Return addr
4D00Return addr
4E00Return addr
4F00Return addr
Regions
Buffer64 B
Saved rbp8 B
0x4242424242424242
Return addr8 B
0x00000000000000ef
Overflow direction
Buffer→Saved rbp→Return addr

Mind the little-endian byte order. We write \xef\xbe\xad\xde\x00\x00\x00\x00 and the 64-bit value parked in memory reads as 0x00000000deadbeef.

The Exploit Flow

Attacker Input
Buffer
Overflow
Return Addr
RET Executes
Attacker Code

Hover over each step to see details

A stack-based buffer overflow exploit follows a tidy six-step recipe:

  1. Identify the vulnerable function - find an unchecked copy into a stack buffer
  2. Calculate the offset - determine the distance from buffer start to return address
  3. Craft the payload - padding bytes + new return address (+ optional shellcode)
  4. Deliver the payload - feed it as input to the vulnerable function
  5. Trigger the return - when the function returns, rip loads the attacker's value
  6. Achieve execution - the CPU jumps to attacker-chosen code
The Saved RBP Matters Too

Trampling the saved rbp is more than collateral damage. After leave runs, the caller's rbp is sitting at an attacker-chosen value. Any code in the caller that does rbp-relative addressing for its own locals will now read or write at a location the attacker picked. That's how a single overflow can snowball into a chain of corruption across the caller's frame.

Why Does strcpy Not Stop?

A fair question: why doesn't strcpy notice it's left the buffer? Because strcpy doesn't even know what a buffer is. Here's the gist of how it's implemented:

nasm
; Simplified strcpy logic
.copy_loop:
mov al, byte [rsi] ; load one byte from source
mov byte [rdi], al ; store it at destination
inc rsi ; advance source pointer
inc rdi ; advance destination pointer
test al, al ; was the byte zero?
jnz .copy_loop ; if not, keep copying

The loop tests one condition — is this byte zero? — and that's it. The destination's size is a fact strcpy was never told. That single oversight is the lineage behind every strcpy-based buffer overflow ever filed.

The Address Conundrum

Fair question: now that we own rip, where do we aim it? This is the heart of exploitation:

  • To shellcode in the buffer - classic technique, but defeated by DEP/NX (the stack is not executable on modern systems)
  • To a library function - ret2libc, which we will cover in Module 11
  • To a chain of gadgets - ROP, also covered in Module 11
  • To a known address - only works without ASLR

Different targets need different tradecraft, and modern defences make each one harder to land. They all share the same starting point though — owning the return address via a buffer overflow.

Confirming the Crash in GDB

A debugger makes the overwrite easy to confirm:

nasm
; Set a breakpoint on leave, before the frame is torn down
; (gdb) break *vulnerable+62
; (gdb) run $(python3 -c "print('A'*72 + 'B'*6)")
;
; rbp still points at the frame here (leave hasn't run yet),
; so read the saved-rbp slot and the return address off it:
; (gdb) x/2xg $rbp
; 0x7fffffffe1e0: 0x4141414141414141 <- saved rbp (overwritten)
; 0x7fffffffe1e8: 0x0000424242424242 <- return address (overwritten)
;
; Continue execution:
; (gdb) continue
; Program received signal SIGSEGV, Segmentation fault.
; 0x0000424242424242 in ?? ()

The segfault at 0x0000424242424242 is the receipt: full control of the instruction pointer. Try it with eight Bs instead and the crash moves to the ret instruction, for the non-canonical reason above.

What is Next

Stack smashing now makes sense at the byte level — how data crosses the buffer boundary, mauls the frame pointer, and replaces the return address. The next lesson covers the classic exploitation move on top of it — NOP sleds and shellcode delivery — and explains why modern defences have all but retired the technique while the underlying ideas still matter.