BUFFER OVERFLOW

Writing past the end rewrites control flow

L215 MINSECURITY
PREDICT

A function copies its input into an 8-byte stack buffer with strcpy and no length check. It is compiled with -fstack-protector, which places a random canary value between the buffer and the saved frame pointer. A 32-character input overwrites the canary, the saved frame pointer and the return address. What happens?

A function’s local buffer lives on the stack, right below the saved frame pointer and the return address. strcpy copies until it finds a NUL. It has no idea how big the destination is. Give it a long enough input and it walks out of the buffer and over the return address. When the function returns, it jumps wherever those bytes now point.

There are two kinds of defence. A stack canary detects the overwrite in the epilogue and aborts before ret. A bounded copy never makes the overwrite at all.

▶ SIMULATION
PROGRAM
build: —
bytes written: 0 / 8
pc: main()
CONTROL FLOW
past the buffer: 0 bytes
ret goes to: —
ATTACKER
input length: 32
—
✓ control returns to the legitimate caller (main) or the program aborts
SCHEDULE
01[BUILD]built without -fstack-protector; copy is strcpy (no bounds)
02[PROGRAM]main() calls parse(): push return address, saved fp, then locals
03[ATTACKER]attacker sends INPUT LENGTH bytes of input
04[PROGRAM]parse() copies the input into char buf[8]
05[PROGRAM]epilogue: check canary (if any), restore saved fp
06[PROGRAM]ret: pop the return address and jump to it
Start of no protection scenario. 6 steps.
void parse(const char *input) {
    char buf[8];
    strcpy(buf, input);   // copies until NUL: no idea how big buf is
}
// Frame, low → high:  buf[0..7] | saved fp | return address | caller...
// 16 chars + NUL already reach the return address.

Watch the return-address row and the invariant strip. NO PROTECTION jumps to 0xATTACKER. STACK CANARY takes the same 32 bytes, the return address is just as overwritten, and the program aborts instead of returning. BOUNDED COPY writes 8 bytes and returns to main. Drag INPUT LENGTH and watch each region fill.

BREAK IT NOT YET

No canary, plain strcpy. Find the shortest INPUT LENGTH whose copy first touches the return address: exactly one byte of it overwritten.

GOAL: exactly 1 of the 8 return-address bytes is overwritten

FIX IT NOT YET

Write safeCopy(dst, dstSize, src). dst is an array of single characters; src is a string. Copy as much of src as fits, never write at or past index dstSize, and always NUL-terminate ('\0') when dstSize > 0. Return the number of characters copied. The grader puts guard cells right after dst.

Mental model:

The stack mixes your data and the machine’s control data in one contiguous region. A copy that does not know the destination size treats both as the same bytes. The real fix is a bound on every write. Canaries, ASLR and non-executable stacks turn a successful overwrite into a crash or a harder exploit. They are mitigations, not fixes.

IN PRODUCTION

C and C++ parsers for network input, file formats and firmware: strcpy, gets, sprintf, and memcpy with a length field the attacker controls. The symptom is a crash with *** stack smashing detected *** or a segfault at a nonsense address.

IN INTERVIEWS

“What is a stack buffer overflow and how do you defend against it?” Draw the frame (buffer, canary, saved frame pointer, return address), say why the overwrite redirects control on ret, and name the layers: bounds-checked copies and memory-safe languages fix it; canaries, ASLR, NX and CFI mitigate it.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: SQL INJECTION →