Writing Past the Buffer
Stack-Based Buffer Overflows · Exploitation Fundamentals track
8 min read
In this module
A Simple Write Loop
Let us see what happens when a program writes data into a stack buffer using a loop - and what goes wrong when the loop writes one element too many. This is the fundamental mechanism behind every stack buffer overflow.
The Safe Case
Imagine a function that allocates a 3-slot buffer on the stack and fills it with values. When the loop runs exactly 3 times, everything works correctly - the buffer is filled, the return address is untouched, and RET sends execution back to the caller.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Step through the simulation above. Watch addresses 200, 201, and 202 receive values 1, 2, and 3. Then check that address 203 (our simulated return address) still holds 15. Everything is fine.
The Overflow Case
Now imagine the counter is set to 4 instead of 3. The buffer only has room for 3 values, but the loop runs four times. The fourth write lands at address 203 - right on top of the return address.
For the best simulation experience, use a desktop browser.
Assembly
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Step through this version. Watch the fourth write land at address 203, overwriting the return address from 15 to 4. If this function tried to RET, the CPU would jump to address 4 instead of address 15.
Now make the bug worse. In the editor, change MOV R2, #4 to MOV R2, #5, click Load and run it again. The fifth write reaches address 204 and replaces the caller's 99 as well. Try 6, then 8. A count of N always writes addresses 200 through 199 + N, so every extra iteration takes out exactly one more neighbour, in order, moving away from the buffer. That's why attackers care about the precise length: one byte too few and the return address survives, a few too many and they've trashed data the program needs before it ever reaches RET.
An off-by-one error is when a loop iterates one time too many or one time too few. In the context of buffer overflows, even a single extra byte can be enough to corrupt the return address and change where the program goes next.
The C version of that bug is almost always a <= where a < belonged: for (i = 0; i <= 16; i++) buf[i] = src[i]; on a 16-byte buf writes 17 bytes. The compiler won't warn about it in most cases, and the program usually keeps running, which is exactly what makes it survive code review.
Worked Example: Which Bytes Does Each Input Reach?
The simulator uses a toy layout. Here's the real one from Anatomy of a Stack Frame: char buf[16] at rbp-0x20, 8 bytes of padding, the canary at rbp-0x08, saved rbp at rbp+0x00, and the return address at rbp+0x08. The function copies its argument with strcpy(buf, name), which writes every character and then a terminating zero byte.
Characters in name | Bytes written | Furthest byte reached | Effect |
|---|---|---|---|
| 15 | 16 | last byte of buf | Fits exactly |
| 16 | 17 | first byte of the padding | One byte too many, lands in padding; no visible harm in this layout |
| 25 | 26 | second byte of the canary | Canary changed; the program aborts with stack smashing detected |
| 40 | 41 | first byte of the return address | Canary and saved rbp destroyed; low byte of the return address set to zero |
| 48 | 49 | byte after the return address | Whole return address replaced with input bytes |
The 16-character row is the sneakiest. The overflow is a single zero byte, and in this particular frame it lands in padding nobody reads. Recompile with different flags, or add one local variable, and the same byte might land on a saved pointer instead. "It doesn't crash" is never evidence that an off-by-one is harmless.
Predict: the input is exactly 24 A characters. Does the canary check catch it?
No, and it's not luck. 24 characters fill buf and the padding, and the terminating zero byte lands on the first byte of the canary, which is its lowest byte. On glibc that byte is always 00. So the write replaces 00 with 00, the canary is unchanged, and the check passes. That's exactly why the canary's low byte is zero: a string copy that stops just short leaves it intact, and one that goes further can't write the zero byte back in the middle of the string.
Watching It Happen in gdb
Tables are tidy. Memory dumps are more convincing. Break just before the strcpy in the frame above and dump six 8-byte words starting at buf. The x/6gx command means "examine 6 giant (8-byte) words in hex", and on x86-64 each one is exactly one stack slot:
(gdb) x/6gx $rbp-0x200x7fffffffe0a0: 0x0000000000000000 0x00007ffff7fe5af00x7fffffffe0b0: 0x0000000000000000 0x8a3f1c6e2d9b47000x7fffffffe0c0: 0x00007fffffffe0e0 0x0000555555555214Read it left to right, top to bottom, low addresses first. The first two words are buf (still holding leftover junk from an earlier call, which is normal for an uninitialised local). The third is padding. The fourth is the canary: random, except for the 00 in its lowest byte, which you can see at the right-hand end. Then saved rbp, which points a little further up the stack, and finally the return address, somewhere inside a PIE binary mapped at 0x555555554000. All the addresses here are illustrative; yours will differ.
Step over the strcpy with 40 A characters as input and dump again:
(gdb) next(gdb) x/6gx $rbp-0x200x7fffffffe0a0: 0x4141414141414141 0x41414141414141410x7fffffffe0b0: 0x4141414141414141 0x41414141414141410x7fffffffe0c0: 0x4141414141414141 0x0000555555555200Five slots are pure 0x41. The sixth shows the terminator's handiwork: only the low byte of the return address changed, from 14 to 00, because x86-64 is little-endian and the lowest byte sits at the lowest address. The function won't get to use that damaged address, mind you. The epilogue reloads the canary, sees 0x4141414141414141 where 0x8a3f1c6e2d9b4700 should be, and calls __stack_chk_fail before ret ever runs.
What Sits Next to the Buffer
The return address gets the attention, but it's the last thing an overflow reaches. Everything between the buffer and it is fair game first, and on real frames that space holds more than padding:
- Other locals. An
int is_adminor asize_t lendeclared beside the buffer can be overwritten long before anything else. Change a flag from 0 to 1 and you've bypassed a check without touching control flow at all. - Function pointers. A local callback pointer that the function calls before returning is a quicker target than the return address, because it fires earlier. That's the setup in the Stack Smash: Function Pointer Overwrite lab.
- Saved
rbp. In builds that keep the frame pointer, this sits directly below the return address. - The canary, if the function has one, guarding the saved registers.
Compilers know this and fight back with layout. When GCC's stack protector is on, it places character arrays at the top of the locals, right under the canary, and pushes scalars and pointers below them. An overflow that runs upward from the array can then only hit the canary and what lies past it. Turn the protector off with -fno-stack-protector and the declaration order (or whatever the optimiser chose) decides who gets trampled. That's why lab binaries are built without it: the layout is one you can reason about.
When the Copy Stops: Bad Bytes
Every copying function has its own idea of where input ends, and that idea decides which bytes you can't put in a payload. Exploit writers call these bad bytes.
| Function | Stops at | What that means for a payload |
|---|---|---|
strcpy, strcat, sprintf("%s") | 00 | No zero bytes until the very end |
gets | 0a (newline), which it drops before adding 00 | No newlines; zeros are fine |
scanf("%s") | whitespace: 09 0a 0b 0c 0d 20 | No tabs, newlines or spaces |
read, recv, memcpy | nothing, it copies the count it's given | Any byte at all |
The 00 row hurts most on x86-64. User-space addresses are at most 47 bits wide, so every one of them packs to 8 bytes with 00 00 at the top. A strcpy overflow can therefore write one address, and only as the last thing in the payload: six non-zero bytes, then the terminator supplies the seventh, and the eighth was already zero in the old return address. That's enough for a single jump. It is not enough for the multi-address chains you'll build in Return-Oriented Programming, which is why read-based bugs are the attacker's favourite.
Predict: a program reads your name with scanf("%s", buf) and you want to return to a function at 0x401020. Will the payload arrive intact?
No. Packed little-endian, 0x401020 is 20 10 40 00 00 00 00 00, and 20 is a space. scanf stops reading at the space, so not a single byte of the address gets copied. Real exploits work around this by picking a different target with the same effect (another entry point into the function, or a gadget that jumps there) whose address contains no bad bytes.
Overwriting Only Saved rbp
Suppose a function without a canary lets you write exactly 8 bytes past the buffer, enough for saved rbp and nothing more. The function's own ret is untouched and returns normally. The damage shows up one frame later. The epilogue's leave instruction is mov rsp, rbp; pop rbp, so the caller now runs with rbp set to your value. When the caller reaches its own leave; ret, rsp is loaded from your fake rbp, and ret pops a return address from memory you chose. A two-step hijack from an 8-byte overflow. It's the seed of a technique called a stack pivot, and it only works where the frame pointer is in use: GCC drops it by default from -O1 upwards on x86-64, freeing rbp as an ordinary register.
Try It on a Real Binary
The Stack Smash: Basic Buffer Overflow lab gives you a 64-byte buffer with saved rbp above it and no canary, so the return address starts 72 bytes in. Dump the frame with x/12gx before and after your input, and match each slot to the table above before you try to redirect anything. Then move on to Stack Smash: Function Pointer Overwrite, where the first thing past the buffer is a pointer the program is about to call.
The Key Insight
The CPU has no concept of "buffer memory" versus "return address memory." It's all just bytes at addresses. When a write advances past the end of the buffer, it silently stomps whatever comes next — and on the stack, what comes next is the return address.
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.