Notes
The idea
The classic example is a buffer overflow. A buffer is a fixed-size piece of memory for some input. If the program copies input into it without checking the length, extra bytes spill past the end and overwrite whatever is next to it in memory. In the worst case that neighbouring memory includes the return address, the place the program jumps to when the current function finishes. Overwrite that, and you can redirect the program to code of your choosing. That is the heart of memory corruption.
- a buffer is fixed-size memory for input
- the code copies input without checking the length
- extra bytes spill past the end, overwriting nearby memory
- that memory can include a return address: where the program jumps next
- control it, and you redirect the program
Why it happens
Why does this happen at all? In languages like C and C plus plus, the programmer manages memory by hand and there are no automatic bounds checks, so an off-by-one or a missing length check becomes an overflow, a use-after-free or a double-free. Memory-safe languages, Rust, Go, Java, Python, C sharp, check bounds and manage memory for you, so this entire class of bug largely disappears. That is why, for new code handling untrusted input, choosing a memory-safe language is itself a security decision.
- C and C++: manual memory, no automatic bounds checks → overflows, use-after-free, double-free are possible
- Rust, Go, Java, Python, C#: memory-safe, bounds checked → this whole class mostly goes away
- most big memory CVEs are in C/C++ code
What attackers want
The attacker's goal is to turn corrupted memory into running their own logic. Historically they injected code and jumped to it. Modern defenses stopped that, so attackers now reuse pieces of the program's own code, chained in a new order, called return-oriented programming. Either way the prize is remote code execution, often the most severe outcome there is. Sometimes an overflow only crashes the program, still a denial-of-service bug. The details are deep; the point is the stakes.
- overwrite the return address or a function pointer
- redirect execution to:
- injected code (old style), or
- existing code reused in a new order (ROP, modern)
- result: remote code execution, often the worst outcome
- sometimes just a crash (DoS)
The defenses
Modern systems do not rely on programmers never erring; they stack defenses. DEP, also called NX, marks memory as writable or executable but never both, so injected code will not run. ASLR randomises where things sit in memory each run, so the attacker cannot predict the addresses they need. A stack canary puts a secret value just before the return address; if an overflow changes it, the program aborts instead of returning into attacker control. RELRO and control-flow integrity add more layers.
- DEP/NX: memory is writable OR executable, not both → injected code will not run
- ASLR: randomise addresses each run → attacker cannot predict where things are stack canary: a secret value before the return address; changed = abort
- RELRO, CFI, FORTIFY, safe libs
Defense in depth
No single one of these is perfect. ASLR can be defeated if another bug leaks an address. A canary can sometimes be guessed or bypassed. Return-oriented programming was invented specifically to get around DEP. But that is the point of defense in depth: defeating all of them at once is much harder and usually needs more than one vulnerability, for example an information leak plus a memory-corruption bug. Each layer raises the cost, and raising the cost is how you win against all but the most determined attackers.
- any single defense can be defeated with effort
- ASLR can leak; canaries can be guessed or skipped; ROP beats DEP
- but defeating ALL of them at once is much harder and needs more than one bug
- so modern exploits often chain an info-leak + a corruption
What you actually do
So what does a security engineer do? Not write exploits. You make these bugs rare and hard to use. For new code handling untrusted input, prefer a memory-safe language. For C and C plus plus, compile with the protections on: stack protector, RELRO, fortified functions and position-independent executables so ASLR applies. Keep ASLR and DEP enabled system-wide. Patch promptly, since most memory bugs you meet are known CVEs with fixes. And fuzz your inputs, which Part eight covers.
- prefer memory-safe languages for new, exposed code
- compile with the protections on (stack-protector, RELRO, FORTIFY, PIE for ASLR)
- keep ASLR/DEP enabled system- wide (CIS checks it)
- patch promptly: most are known CVEs with fixes
- fuzz inputs to find them early (Part 8)
Beyond the stack
Buffer overflows on the stack are the textbook case, but the family is larger. Heap overflows corrupt a different region of memory with the same consequences. Use-after-free bugs use memory that was already freed, which an attacker can reclaim and control. Integer overflows can trick a program into allocating too small a buffer, leading back to an overflow. You do not need to master each one. The unifying fact is that they are all memory-safety failures, and memory-safe languages prevent the great majority of them.
- heap overflows: same idea, different memory region
- use-after-free: use memory that was already freed
- integer overflow → undersized buffer → overflow
- format-string bugs
- all are memory-safety issues; memory-safe languages prevent most of them
Common mistakes
Mistakes. Writing new, internet-exposed code in unsafe C when a memory-safe language would do. Compiling without the protective flags, so canaries and RELRO are missing. Disabling ASLR because it made debugging easier and then forgetting. Treating memory-corruption CVEs as low priority when they are often remote code execution. Not fuzzing the parts of your code that parse untrusted input. And the root of it all, trusting that input will be the length you expected.
- new exposed code in unsafe C for no reason
- compiling without protections
- disabling ASLR for convenience
- ignoring memory-corruption CVEs
- no fuzzing of input parsers
- trusting input lengths
Say it like this
In the interview: I defend against memory-corruption attacks by preferring memory-safe languages for new, exposed code, and for existing C or C plus plus, compiling with stack protector, RELRO, fortified functions and position-independent executables, with ASLR and DEP enabled system-wide. These are defense in depth, so a working exploit usually needs an information leak as well as a corruption bug. On top of that I patch memory CVEs quickly and fuzz the code that parses untrusted input.
- "Prefer memory-safe languages
- for exposed code; for C/C++, compile with stack protector, RELRO, FORTIFY and PIE, and keep ASLR and DEP on. They are defense in depth, so an exploit
- usually needs an info-leak too.
- Then patch CVEs fast and fuzz
- input parsers."