Skip to main content
Be Bitwise

Real-World Stack Overflows

Stack-Based Buffer Overflows · Exploitation Fundamentals track

9 min read

Part ofSystems Programming Companion, week 5

In this module

The C Functions That Started It All

The buffer overflow technique you have been studying has been exploited in the real world for decades, and the root cause is almost always the same: C library functions that copy data without checking how much space is available.

Dangerous Functions

Several classic C functions are notorious for enabling buffer overflows:

  • gets() - reads a line from standard input into a buffer with absolutely no length limit. It was so dangerous that it was removed from the C language standard in C11.
  • strcpy() - copies a string into a destination buffer until it hits a null terminator, with no check on the destination size.
  • sprintf() - formats a string into a buffer with no bounds checking.

All three share the same flaw: the programmer must allocate a buffer, but the function will happily write past its end if the input is too long. The CPU does not enforce boundaries - as you saw in the simulator, memory is just a flat sequence of bytes.

Bounds Checking

Bounds checking means verifying that every memory access stays within the allocated region. Languages like Rust and Java enforce bounds checking automatically. In C and C++, it is the programmer's responsibility - and forgetting is the single most common source of exploitable vulnerabilities.

Historical Exploits

Buffer overflows have caused some of the most consequential security incidents in computing:

  • Morris Worm (1988) — one of the first internet worms, exploiting a buffer overflow in the Unix fingerd service to replicate across thousands of machines.
  • Code Red (2001) — a buffer overflow in Microsoft IIS infected over 350,000 servers in under 14 hours.
  • Blaster Worm (2003) — targeted a buffer overflow in Windows RPC services, triggering mass crashes and reboots.

The same technique, every time: send more data than a buffer can hold, overwrite the return address, redirect execution.

SQL Slammer, also from 2003, made the point at network speed. The overflow was in SQL Server's Resolution Service on UDP port 1434, and the whole worm fit in one 376-byte packet: no handshake, no second stage, just a datagram that overflowed a stack buffer and started sending copies of itself. It spread so fast that it congested parts of the internet within minutes.

Worked Example: The Morris Worm's Way In

The fingerd bug is worth looking at closely, because it's the textbook overflow in its purest form. fingerd answered "who is logged in?" queries from the network. It read the incoming request into a 512-byte buffer on the stack with a single line:

c
char line[512];
gets(line); /* reads until newline, however long that is */

The worm sent a request of 536 bytes. The first part was a small piece of VAX machine code; the rest was padding, ending in a new return address that pointed back into the buffer. When the function returned, the VAX jumped into the worm's own code, which started a shell with its input and output wired to the network connection. Every step you've learned in this module is in there: an unchecked copy, a frame laid out with the buffer below the return address, and a return address overwritten with an address the attacker chose.

The fix was one line: replace gets(line) with fgets(line, sizeof line, stdin), which stops at the buffer's size. That one-line difference is the whole story of this lesson.

The Safer Replacements, and Their Traps

Instead ofUseWatch out for
gets(buf)fgets(buf, sizeof buf, stdin)Keeps the trailing newline in the buffer
sprintf(buf, ...)snprintf(buf, sizeof buf, ...)Returns the length it wanted to write, so check for truncation
strcpy(dst, src)snprintf(dst, sizeof dst, "%s", src), or strlcpy where availablestrncpy looks like the answer but isn't (see below)

strncpy has caught out generations of C programmers. It stops at the size you give it, but if the source is that long or longer, it doesn't write a terminating null byte.

Predict: char buf[8]; strncpy(buf, "overflowing", sizeof buf); printf("%s\n", buf); What happens?

It prints overflow followed by whatever bytes happen to sit after buf, until it stumbles on a zero. strncpy copied exactly 8 bytes and stopped, with no room left for a terminator. printf then reads straight past the end of the buffer. No write overflow this time, but an out-of-bounds read that can leak neighbouring stack data, such as a canary or a saved pointer.

Where Stack Overflows Live Now

gets is gone from the standard and most compilers warn about strcpy, yet stack overflows keep turning up in advisories. The modern version usually looks less like a dangerous function and more like a trusted number. A parser reads a length field from a packet, a file header or a USB descriptor, then copies that many bytes into a fixed buffer:

c
struct msg {
uint16_t len; /* sender says how long the name is */
uint8_t data[];
};
void handle(const struct msg *m) {
char name[64];
memcpy(name, m->data, m->len); /* len comes straight off the wire */
name[63] = 0;
greet(name);
}

Nothing here is a banned function. memcpy is fine; the bug is that m->len can be anything up to 65,535 and nobody compares it with sizeof name. Unlike strcpy, memcpy copies zero bytes, newlines and everything else without complaint, so the attacker has no bad-byte restrictions at all. The name[63] = 0 line is a nice touch of false security: it terminates the string after the damage is done.

On an x86-64 desktop or server built with a current toolchain, that bug has to fight through a canary, ASLR, a non-executable stack and probably _FORTIFY_SOURCE. Embedded devices are a different story. Routers, cameras and industrial controllers often run on MIPS or ARM firmware built with old toolchains and few or none of those protections, and their network-facing parsers are written in C with exactly this pattern. Research on such firmware regularly finds overflows that behave much like Morris's fingerd did, return address and all. Running checksec on a binary tells you which protections it has; Exploit Mitigations covers reading its output.

Worked Example: What _FORTIFY_SOURCE Catches

Compile handle on x86-64 with gcc -O2 -D_FORTIFY_SOURCE=2. The compiler knows name is 64 bytes, so it rewrites the memcpy as a call to __memcpy_chk(name, m->data, m->len, 64). That function compares the length with 64 at run time. Send a message with len = 200 and the program stops before writing a single out-of-bounds byte:

text
*** buffer overflow detected ***: terminated
Aborted (core dumped)

Now change one thing. Move the copy into a helper that takes char *dst from its caller. Inside the helper the compiler can't see how big dst is, so __builtin_object_size returns "unknown", no check is inserted, and the overflow goes through. Level 3 (-D_FORTIFY_SOURCE=3, GCC 12 or newer with a recent glibc) uses __builtin_dynamic_object_size and catches more cases where the size is only known at run time, but neither level can follow a pointer through every call. Fortification also only guards calls into the C library. A hand-written copy loop gets no check at all.

Predict: the off-by-one loop for (i = 0; i <= 64; i++) name[i] = src[i]; is compiled with -O2 -D_FORTIFY_SOURCE=2 and a stack protector. Which of the two catches the extra byte?

Possibly neither. Fortify only wraps library calls like memcpy and strcpy, and this is a plain loop, so it's out of the picture. The canary only fires if the 65th byte lands on it, which depends on the frame. With the array placed right under the canary it lands on the canary's lowest byte, and the check catches it unless the byte copied happens to be 00, the value glibc always keeps there. If the compiler left padding between the array and the canary, the byte lands in padding and nothing notices. Mitigations narrow the target; they don't make an off-by-one safe.

Reading the Crash From the Field

When an overflow happens in production, the first evidence is usually a line in a log. Each of the three common endings tells you which layer noticed:

What you seeWho stopped itWhat it means
*** stack smashing detected ***: terminatedThe canary check, __stack_chk_failThe overflow already happened and reached the canary; the function was stopped at its epilogue
*** buffer overflow detected ***: terminatedA fortified function such as __memcpy_chkThe copy was refused before it wrote out of bounds
Segmentation fault at a ret instructionNobody; the CPU faultedNo canary in that frame, and the return address was overwritten with something unmapped or non-canonical

The exact wording varies a little between glibc versions (older ones print the program name too). The third row is the dangerous one: it means the attacker's bytes reached ret unchallenged, and the only thing missing for a working exploit was the right value. A crash report like that deserves the same urgency as a working proof of concept.

Modern Defences

Operating systems and compilers have developed multiple layers of defence against stack buffer overflows:

  • Stack canaries - a secret value placed between the buffer and the return address. Before returning, the function checks if the canary was modified.
  • ASLR (Address Space Layout Randomisation) - randomises where code and data are loaded in memory, making it harder for attackers to predict addresses.
  • DEP/NX (No-Execute) - marks the stack as non-executable so injected shellcode cannot run. This defence is so effective that it gave rise to an entirely new attack technique - return-oriented programming, which you will learn about in Return-Oriented Programming.
  • _FORTIFY_SOURCE - with optimisation on and -D_FORTIFY_SOURCE=2, glibc swaps calls like strcpy and memcpy for checked versions whenever the compiler knows the destination's size. An overflow then stops the program with *** buffer overflow detected *** instead of corrupting the stack.

You'll take each one apart in Exploit Mitigations, including how attackers leak a canary or an address to get past it. If you want to see the canary from the attacker's side first, the Canary Bypass lab has you leak one and put it back in your payload, and the Stack Smash lab shows the unprotected baseline.

These defences raise the bar significantly. None of them are perfect — if they were, this module would be a curiosity rather than mandatory reading. Understanding the underlying vulnerability is what makes you dangerous with a compiler and not just a patcher of other people's bugs.