Skip to content

Lesson 4 of 14 · 12m

Anatomy of a Buffer Overflow

What gets() does to a 64-byte buf: a linear overwrite through saved rbp into saved rip, and why the compiler never stood between your input and control flow.

The overflow is a layout problem, not a syntax problem

Here is what happens when gets(buf) meets a 64-byte buf. gets reads until newline or EOF. It has no length parameter because it has no concept of one — the man page has carried the word BUGS in bold since the 1980s and C11 finally removed the function entirely. Input goes in at buf's address and writes upward: through buf, through the saved rbp, through the saved rip, through whatever stack follows. Nothing checks the boundary because on x86 there is no boundary — there is only the convention that buf ends where the compiler stopped allocating.

The canonical code shape:

void vuln() {
    char buf[64];
    gets(buf);          /* or strcpy(dst, src), scanf("%s", buf),
                           memcpy(dst, src, attacker_len)        */
}

Feed it 100 bytes of A. rbp-64 holds your first 'A'; rbp holds 'A's; rbp+8 — the saved return address — holds 0x4141414141414141. When vuln hits ret, the CPU pops that value and jumps to 0x4141414141414141. Segfault, SIGSEGV, and a crash report that is secretly the exploit working: you just proved you control rip. The difference between a crash and a shell is replacing 'A' with an address that exists.

The wrong mental model

The wrong model is "the compiler or the OS prevents this." The compiler emits sub rsp, 0x40 and calls it a day; the gets call site has no bounds metadata because C passes raw pointers. No sandbox sits between your input and the saved rip. This is also why strncpy(dst, src, sizeof(dst)) does not save you — bound the copy, not the destination size, and the bug survives (or, in strncpy's case, the buffer may come back unterminated, which is a second bug). The correct fix is a function that takes the destination's capacity: fgets(buf, sizeof buf, stdin) — and sizeof only works where buf is still an array, not a decayed char*. The number of production bugs that are "someone passed a pointer where the bound computation needed the array" is not small.

A second wrong model hides inside the first: "overflows only happen with gets." The modern smash comes through memcpy(dst, src, attacker_controlled_len), read(fd, buf, len_from_packet), snprintf off-by-one, integer truncation before a length check — the sink changes, the layout doesn't. If attacker bytes or an attacker-derived length reach a copy with no capacity argument, you have the bug.

Prove it in the real world

This mechanism has been the same since before Linux existed. November 2, 1988: the Morris worm — the first internet worm — spread through a stack overflow in BSD fingerd, which called gets() on a 512-byte buffer the worm overran with a return address pointing at execve("/bin/sh") shellcode. The writeup that made the technique famous, Aleph One's "Smashing the Stack for Fun and Profit," ran in Phrack 49 (November 1996) and walked the exact arithmetic above. Fast-forward: CVE-2017-0144, the SMBv1 overflow weaponized as EternalBlue and then WannaCry in May 2017 — same primitive, kernel-side, a memcpy sizing error in Microsoft's SMB trans2 handling. The bug class has not changed in forty years; only the mitigations around it have.

Land on practice

Reproduce it before you defend against it. Compile with -fno-stack-protector -no-pie -z execstack (you are deliberately building a 1996 binary), feed input, watch rip=0x4141414141414141 in GDB's info registers. Then grep the codebase you actually work on for the dangerous verbs: gets, strcpy, strcat, sprintf, scanf("%s") — and every memcpy/read/recv whose length is attacker-derived. The audit rule is one line: the bound must come from the destination's capacity, computed where the destination is still an array. Everything else is negotiation with the attacker.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.