There are two ways to deal with a class of bug that has been exploited since the late 1980s. You can build fences around it, so that when the bug is triggered the attacker gains little. Or you can make the bug impossible to write. I will explain both, and then say plainly which I think deserves more of your budget.
What the bug is
Languages like C and C++ let the programmer manage memory directly. If a program reserves room for 16 characters and then copies in 40 because nobody checked the length, the extra 24 spill into whatever sits next to the buffer. That is a buffer overflow. It might crash the program. If the neighbouring memory holds something the program trusts, such as a saved return address (where the function goes back to when it finishes), a function pointer, or a flag deciding whether the user is an admin, the spill can change program behaviour in ways the attacker chooses.
The relatives: reading past the end of a buffer (leaking secrets from nearby memory), use-after-free (using memory after it was released and reassigned), double free, and integer overflows that cause a too-small allocation. All fall under memory corruption or memory safety errors. I will not describe how to turn one into an exploit; the defensive discussion does not need it.
Approach one: fences
Operating systems and compilers have piled on mitigations, each addressing one step an attacker needs:
- Stack canaries. The compiler places a secret value before the saved return address and checks it before returning. An overflow that tramples the return address also changes the canary, and the program aborts.
- DEP or NX (non-executable memory). Data areas such as the stack and heap are marked as not executable, so injected bytes cannot simply be run as code.
- ASLR (address space layout randomisation). The locations of the program, libraries, stack and heap change on every run, so the attacker cannot hard-code the addresses they need.
- Control-flow integrity and shadow stacks. Hardware and compilers check that jumps and returns go to plausible places.
These genuinely raised the cost of exploitation. A bug that was once reliably exploitable in an afternoon may now need a second bug that leaks an address, and a technique like return-oriented programming, which reuses small pieces of existing code instead of injecting new code. But look at the language I just used: raised the cost, needed another bug. Mitigations are probabilistic and bypassable. Determined, well-funded attackers chain bugs to get round them, and an information leak can undo ASLR in one stroke.
Approach two: take the bug away
Languages such as Java, C#, Go, Python and Rust do not let ordinary code write past the end of an array; they check bounds, or, in Rust’s case, enforce ownership and borrowing rules at compile time so that use-after-free and data races are rejected before the program ever runs. Garbage-collected languages remove most use-after-free by design. Rust and similar languages do this with little runtime cost, which matters for operating systems and browsers.
The evidence for why this matters is public. Microsoft’s security response team reported in 2019 that around 70 per cent of the vulnerabilities it assigned CVEs to each year were memory safety issues, and the Chromium project has published a similar figure for its serious bugs. Agencies including the US CISA and NSA have urged software makers to move to memory-safe languages and to publish roadmaps for doing it. The numbers vary by codebase, so treat them as order-of-magnitude, but the direction is clear.
The side I take
Opinion: for new code, choose a memory-safe language unless you have a specific reason not to, and spend mitigation effort on the C and C++ you cannot yet replace. Fences are necessary, because the world runs on enormous legacy codebases and you cannot rewrite them next quarter. But fences are a tax you pay forever, and every new overflow in unsafe code is another chance for a bypass. Removing the category beats chasing instances.
Counter-arguments, fairly stated. Rewrites are costly and introduce fresh bugs. Memory-safe languages still have logic flaws, injection bugs, bad authentication and insecure dependencies; they fix one big class, not security as a whole. Rust has an unsafe escape hatch, and any language ends up calling native libraries somewhere. Embedded and performance-critical systems sometimes lack a practical alternative. Those are real constraints, which is why the practical answer is a mix.
What a team can do on Monday
- Check that your build enables the platform mitigations: canaries, position-independent executables for ASLR, non-executable stacks, and fortified library functions. Defaults vary by compiler and operating system, so verify instead of assuming.
- Run fuzzers and sanitisers (tools that detect out-of-bounds access and use-after-free at test time) against parsers and any code that handles untrusted input: file formats, network protocols, image decoders.
- Put new components that parse untrusted data in a memory-safe language, and isolate old ones in low-privilege processes or sandboxes.
- Patch quickly. A fence only helps while the surrounding platform is current, and memory bugs in libraries you did not write arrive through updates.
If you are a defender rather than a developer, the takeaway is what to ask vendors: which languages is the product written in, are mitigations enabled, and how fast do they ship fixes for memory corruption advisories? Those three answers predict more than a glossy feature list.
Leave a Reply