Cybersecurity glossary
What is a Buffer Overflow?
Learn what a buffer overflow is, how writing past a buffer corrupts adjacent memory, how attackers turn overflows into code execution, and which coding practices and mitigations reduce risk.
Definition
A buffer overflow is a memory safety flaw in which a program writes more data into a fixed-size buffer than the buffer can hold, overwriting adjacent memory and potentially corrupting data, control flow, or security-critical state.
Why buffer overflows matter
Programs constantly move bytes into temporary storage—network packets into receive buffers, file chunks into decode arrays, form fields into C strings. When the amount written exceeds what was allocated, neighboring memory silently changes.
Buffer Overflow remains one of the oldest and most consequential vulnerability classes because that neighboring memory may hold return addresses, function pointers, object lengths, or authentication flags. A single unchecked copy can turn a parsing bug into remote code execution.
How a buffer overflow unfolds
Allocate a fixed buffer
The program reserves a stack array, heap block, or static region with a chosen capacity.
Accept attacker-influenced data
A packet, file, string, or protocol field supplies more content than expected.
Copy without a hard stop
An unbounded strcpy, memcpy with a wrong length, or off-by-one loop writes past the end.
Adjacent state is overwritten
Nearby variables, metadata, or control data take attacker-controlled values.
Crash or take control
The process may abort—or follow corrupted pointers into attacker-chosen behavior.
Where overflows commonly hide
String handling in C
Classic strcpy, sprintf, and gets patterns that ignore destination capacity.
Binary format parsers
Length fields that are trusted without checking against remaining buffer size.
Integer wrap in size math
A wrapped length allocates too little memory, then a large copy overflows it.
Native codecs and drivers
Image, audio, PDF, and protocol stacks that parse untrusted bytes at high speed.
Controls that reduce overflow risk
| Control | Notes |
|---|---|
| Bounds-carrying APIs | Prefer memcpy_s, strlcpy, span/slice types, or language-level checked copies |
| Validate before copy | Compare claimed lengths to actual capacity and remaining input; reject mismatches |
| Compiler hardening | Enable stack canaries, fortify source, and warnings-as-errors for unsafe APIs |
| Memory-safe languages | Eliminate most overflow classes for new code; isolate remaining unsafe FFI |
| Fuzzing | Continuously fuzz parsers with coverage guidance; treat crashes as security bugs |
| Sandbox parsers | Contain codecs and converters so a single overflow cannot own the whole host |
- Inventory all native copy and string APIs that touch untrusted input.
- Replace unbounded C string functions with size-aware alternatives.
- Audit length arithmetic for integer overflow before any allocation or copy.
- Turn on stack protection, ASLR, DEP/NX, and fortify options in release builds.
- Fuzz every public file and protocol parser that runs in C/C++.
- Sandbox high-risk native modules behind privilege-reduced workers.
- Prefer memory-safe languages for new untrusted-input parsers.
- Patch browsers, TLS libraries, VPN clients, and media codecs promptly.
The practical takeaway
A buffer overflow happens when a write exceeds a buffer’s capacity and silently rewrites nearby memory. Fix the bounds checks, prefer safer languages and APIs, and assume public-facing native parsers will be probed.
If you still maintain C/C++ parsers, treat every crash on malformed input as a potential security incident—not just a reliability issue.
Related security terms
Stack Buffer Overflow
Overflow that specifically targets stack-allocated buffers and often return addresses.
Heap Overflow
Overflow into dynamically allocated heap metadata or neighboring objects.
Out-of-Bounds Write
Broader class of writes outside valid object bounds, including overflows.
Memory Corruption
Umbrella category covering overflows, use-after-free, and related flaws.
Frequently asked questions
What is a buffer overflow in simple terms?
A program reserves a limited space for data, then copies more bytes than fit. The extra bytes spill into neighboring memory and can change values the program relies on.
Are stack and heap overflows the same bug?
Both are buffer overflows. Stack overflows corrupt stack frames; heap overflows corrupt heap objects or allocator metadata. Exploitation techniques differ, but the root cause is the same: unbounded or miscalculated writes.
Which languages are most affected?
C and C++ are classic sources because they allow raw pointer arithmetic and unchecked copies. Memory-safe languages reduce this class unless they call unsafe native code.
Do stack canaries and ASLR stop buffer overflows?
They make exploitation harder and catch some cases, but they do not remove the bug. Information leaks and advanced techniques can still bypass mitigations.
Is every overflow exploitable for code execution?
No. Some only crash the process. Others overwrite security flags, lengths, or pointers in ways that become reliable exploits. Treat overflows as serious until analyzed.
What is the best prevention strategy?
Prefer memory-safe languages and APIs that carry bounds, avoid unsafe copy functions, validate lengths before copying, enable compiler hardening, and fuzz parsers that handle untrusted input.
Where do buffer overflows appear in modern stacks?
Browsers, image and media codecs, VPN clients, TLS libraries, IoT firmware, and native modules behind web services remain frequent targets.
References
Explore authoritative guidance and frameworks related to buffer overflow.
Explore every security definition
Return to the glossary to search by term, alias, starting letter, or security category.