Difficulty: Advanced
What is a buffer overflow, why is it dangerous, and what mitigations exist?
A buffer overflow happens when a program writes more data into a fixed-size memory buffer than it can hold, so the extra bytes overwrite adjacent memory. In languages without bounds checking, such as C and C++, an attacker who controls that input can corrupt variables, crash the program or overwrite a return address and redirect execution.
When a C function runs, its stack frame holds local variables, the saved frame pointer and the return address that tells the CPU where to resume after the function returns. On x86-64 the stack grows towards lower addresses, but a buffer is filled towards higher addresses. So if char buf[64] receives 200 bytes through an unbounded copy, the write runs past buf, over the saved frame pointer and onto the return address. When the function returns, the CPU jumps wherever that value now points. Historically that meant code planted in the buffer; today it usually means chaining together snippets of existing code (return-oriented programming, ROP).
malloced block, corrupting allocator metadata or neighbouring objects such as vtable pointers.\0 terminator.| Mitigation | How it works | Limitation |
|---|---|---|
Stack canaries (-fstack-protector-strong) | Random value before the return address, checked on return | Bypassed by info leaks or non-linear writes |
| NX / DEP | Stack and heap marked non-executable | Defeated by ROP reusing existing code |
| ASLR and PIE | Randomise addresses of stack, heap, libraries and binary | Weakened by a memory-disclosure bug |
_FORTIFY_SOURCE | Compiler swaps unsafe calls for checked versions when sizes are known | Only covers calls it can analyse |
| CFI and shadow stacks (Intel CET) | Hardware or compiler checks that returns and indirect calls hit valid targets | Needs OS, compiler and CPU support |
| Memory-safe languages | Rust, Go, Java and C# bounds-check every access | Unsafe blocks and FFI still need care |
None of these replaces correct code; they raise the cost of exploitation. This is why agencies such as CISA and major vendors now push memory-safe languages for new systems code.
Your Node.js or Python service is memory-safe, but it sits on native code: OpenSSL, image libraries like libwebp, video codecs, database drivers and the language runtime itself. Heartbleed (2014) was a buffer over-read in OpenSSL, and CVE-2023-4863 was a heap overflow in libwebp that affected browsers and almost any app decoding WebP images. Keeping native dependencies patched and processing untrusted files in sandboxes are your practical defences.
fgets, snprintf and explicit length checks.strncpy always null-terminates. It does not when the source is too long.#include <stdio.h>
#include <string.h>
void unsafe_copy(const char *input) {
char buf[64];
strcpy(buf, input); /* no length check: overflow if input > 63 */
printf("Hello %s\n", buf);
}
void safe_copy(const char *input) {
char buf[64];
/* snprintf never writes more than sizeof buf and always terminates */
int n = snprintf(buf, sizeof buf, "%s", input);
if (n < 0 || (size_t)n >= sizeof buf) {
fprintf(stderr, "input too long, rejected\n");
return;
}
printf("Hello %s\n", buf);
}
The unbounded copy writes until it finds a null byte regardless of the destination size. snprintf bounds the write and reports truncation so you can reject oversized input.
# Stack canaries, fortified libc calls, PIE for ASLR, full RELRO, CET
gcc -O2 -fstack-protector-strong -D_FORTIFY_SOURCE=3 \
-fPIE -pie -Wl,-z,relro,-z,now -fcf-protection=full \
app.c -o app
# Inspect the protections on a binary (checksec from pwntools)
checksec --file=./app
_FORTIFY_SOURCE=3 needs GCC 12 or newer with optimisation enabled; older toolchains use level 2. These flags make exploitation harder but do not fix the underlying bug.
Memory Corruption, Stack Canaries, ASLR, NX/DEP, Memory Safety