On this page
A stack buffer overflow happens when a program copies more data into a fixed-size buffer on the stack than the buffer can hold. The extra bytes overwrite what sits next to it in memory: other local variables, the saved frame pointer and the return address that tells the CPU where to continue when the function ends. Corrupting the return address usually crashes the program, and in the worst case lets an attacker redirect it. Modern systems defend against this with stack canaries, non-executable memory (NX/DEP), ASLR and safer functions, which is why understanding the stack is still the basis of both exploit research and secure coding.
This article explains the stack frame, shows a vulnerable function and its fix, and walks through each protection. The lab at the end lets you overflow a model stack and see what each protection does.
The stack frame
Every time a function is called, the CPU reserves a block of stack memory for it, called a stack frame. On x86-64 Linux a simple frame looks like this (higher addresses at the top, the stack grows downwards):
| Position | Content | Written by |
|---|---|---|
| Higher addresses | Caller’s frame | Caller |
| Return address | The call instruction | |
| Saved frame pointer (RBP) | Function prologue | |
| Stack canary (if enabled) | Function prologue | |
| Lower addresses | Local buffer, e.g. char name[16] | The function |
A buffer is filled from its lowest address upwards, towards the canary, the saved frame pointer and the return address. That direction is the whole problem: writing past the end of the buffer walks straight into the data that controls where the function returns.
The registers and calling convention behind this are explained in x86-64 Registers and Assembly.
A vulnerable function
#include <stdio.h>
#include <string.h>
void greet(const char *input) {
char name[16];
strcpy(name, input); /* no length check */
printf("Hello, %s\n", name);
}
int main(int argc, char **argv) {
if (argc > 1) greet(argv[1]);
return 0;
}
strcpy copies until it finds the end of the input string. If the input is longer than 15 characters plus the terminating zero, it keeps writing past name. With a short input the program works; with a long one it overwrites the frame. With protections switched on, the program stops with a message such as “stack smashing detected”. Without them, it usually crashes with a segmentation fault when the function returns to a garbage address.
The fix
void greet(const char *input) {
char name[16];
snprintf(name, sizeof name, "%s", input); /* never writes more than 16 bytes */
printf("Hello, %s\n", name);
}
Use functions that take the buffer size (snprintf, fgets, strlcpy where available), check lengths before copying, and prefer higher-level string handling in new code. Compiler warnings (-Wall -Wextra) and static analysers flag many unsafe calls.
The protections
Stack canaries
The compiler places a random value (the canary) between the local buffers and the saved frame pointer, and checks it just before the function returns. An overflow that reaches the return address must pass through the canary and change it, so the program aborts instead of returning. GCC and Clang enable this with -fstack-protector-strong, which most Linux distributions use by default.
NX / DEP
Memory pages are marked either writable or executable, not both. The stack is writable data, so code placed there by an attacker cannot run. This is called NX on Linux and DEP on Windows, and it is on by default on modern systems.
ASLR
Address Space Layout Randomization loads the stack, heap, libraries and (for position-independent executables) the program itself at random addresses every time. An attacker cannot rely on fixed addresses. Compile with -fPIE -pie to make the executable itself randomisable.
Fortify source and newer hardware protection
-D_FORTIFY_SOURCE=2 (or 3) makes the compiler replace calls like strcpy with checked versions where the buffer size is known. Newer CPUs and operating systems add shadow stacks (Intel CET, Windows hardware-enforced stack protection), which keep a second copy of return addresses that ordinary writes cannot change.
Check a binary’s protections
# Compile with the usual hardening flags
gcc -O2 -Wall -Wextra -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE -pie greet.c -o greet
# Show which protections a binary has (from pwntools or the checksec script)
checksec --file=./greet
checksec reports RELRO, the stack canary, NX, PIE and Fortify for a binary. During a security review it is the first thing to run on an unfamiliar program. Older or embedded binaries without these protections are where overflows remain most dangerous.
Interactive lab: overflow a model stack
| Slot | Bytes | Content after strcpy |
|---|
Frequently asked questions
What is the difference between a stack and a heap overflow?
A stack overflow writes past a buffer in a function's stack frame, where return addresses and saved registers live. A heap overflow writes past a buffer allocated with malloc or new, corrupting heap metadata or neighbouring objects. The protections and the consequences differ.
Why does a buffer overflow cause a segmentation fault?
The overflow corrupts the saved return address or frame pointer. When the function returns, the CPU jumps to an address that is not valid code, and the operating system stops the program with a segmentation fault (access violation on Windows).
What does "stack smashing detected" mean?
It means the stack canary check failed: something overwrote the canary value placed before the saved frame pointer, and the program aborted to prevent a corrupted return. It points to a buffer overflow bug in the function that was returning.
Do stack canaries stop all buffer overflows?
No. They detect overflows that overwrite the canary before the function returns. They do not protect against overwriting other local variables before the canary, information leaks that reveal the canary, or heap overflows. That is why canaries, NX, ASLR and safe code are used together.
Which languages prevent buffer overflows?
Memory-safe languages such as Rust, Go, Java, C# and Python check bounds and stop out-of-range writes. C and C++ do not check by default, so they rely on careful code, compiler protections and testing tools such as AddressSanitizer and fuzzers.
Where to go next
Reading crashes in a debugger, understanding shellcode and analysing how real vulnerabilities work are covered in Shellcode Hacking. For the assembly side, start with x86-64 Registers and Assembly and From Assembly to Machine Code.


