Memory, safety and Rust ownership · Part 26 of 36

Memory safety and why it matters for security

Expert 17 min

Buffer overflows and use-after-free, why about 70 % of severe bugs in large C/C++ code bases were memory-safety bugs, and the shift to memory-safe languages.

What you will learn

  • You can explain spatial and temporal memory-safety bugs.
  • You can summarise the evidence and the 2023–2025 government guidance on memory-safe languages.
  • You can name mitigations for code that stays in C or C++.

Before this: Stack, heap and garbage collection

What "memory safe" means

A language is memory safe when a program cannot read or write memory it isn't supposed to — whatever bugs it has. Bugs then cause exceptions or wrong results, not silent corruption that an attacker can steer.

Two families of memory-safety bugs:

  • Spatial: access outside an object's bounds. The classic is the buffer overflow: copying 100 bytes into a 64-byte array overwrites whatever lies behind it — other variables, return addresses, function pointers.
  • Temporal: access at the wrong time — use-after-free, double free, reading uninitialised memory.

C and C++ check neither by default: buf[100] on a 64-element array just computes an address and uses it. JavaScript, Python, Java, C#, Go, Swift and safe Rust check bounds and manage lifetimes, so these bugs can't corrupt memory.

A row of lockers with one locker door guarded by a small shield

The evidence

Large vendors have counted for years. Microsoft reported in 2019 that about 70 % of the vulnerabilities it fixed with a CVE were memory-safety bugs; Google's Chromium project reported a similar ~70 % for serious security bugs (2020). Google's Android team reported in September 2024 that memory-safety bugs fell from 76 % of Android vulnerabilities in 2019 to 24 % in 2024 — mainly because new code was written in memory-safe languages (Rust, Kotlin, Java) while old C/C++ code, which becomes safer with age, stayed.

Government guidance

Security agencies now say this clearly:

  • December 2023: "The Case for Memory Safe Roadmaps" (CISA, NSA, FBI and partners from the UK, Canada, Australia, New Zealand) asked software makers to publish roadmaps for moving to memory-safe languages.
  • June 2025: CISA and NSA published "Memory Safe Languages: Reducing Vulnerabilities in Modern Software Development", again urging adoption for new code and gradual migration of critical components.

Rust has been accepted for drivers in the Linux kernel since version 6.1 (December 2022) and is used in Windows, Android and Chromium components.

Living with C and C++

Rewriting everything is impossible, so existing code is hardened:

  • Compiler and OS mitigations: stack canaries, non-executable memory, address-space layout randomisation (ASLR), control-flow integrity. They make exploitation harder, not impossible.
  • Sanitizers (AddressSanitizer, MemorySanitizer, UBSan) and fuzzing (OSS-Fuzz has found tens of thousands of bugs in open-source projects) find bugs before attackers do.
  • Safer subsets and libraries: bounds-checked containers, std::span, smart pointers; the C++ community is working on "profiles" and hardened standard libraries.
  • Hardware: memory tagging (Arm MTE) and capability hardware (CHERI) catch many violations at run time.
  • Isolation: run risky parsers in sandboxes with few privileges.

Memory safety is not all of security

Memory-safe code still has logic bugs, injection, broken access control and supply-chain problems (lesson "OWASP Top 10:2025"). And "unsafe" escape hatches — Rust's unsafe blocks, native extensions in Python — need careful review. But removing an entire class that made up the majority of severe bugs is the largest single improvement available.

Key points

  • Memory-safe languages prevent out-of-bounds and wrong-lifetime memory access.
  • Spatial bugs: buffer overflows; temporal bugs: use-after-free, double free.
  • About 70 % of severe vulnerabilities in large C/C++ code bases were memory-safety bugs; Android cut its share from 76 % (2019) to 24 % (2024) by writing new code in safe languages.
  • CISA/NSA guidance (2023, 2025) urges memory-safe roadmaps.
  • For existing C/C++: mitigations, sanitizers, fuzzing, safer libraries, hardware tagging, sandboxing.

Flashcards

Question: What is a buffer overflow?
Answer: Writing past the end of a buffer, overwriting neighbouring memory.
Question: Is use-after-free a spatial or temporal bug?
Answer: Temporal — memory is used after its lifetime ended.
Question: How did Android reduce memory-safety vulnerabilities?
Answer: By writing new code in memory-safe languages (Rust, Kotlin, Java).
Question: What does fuzzing do?
Answer: Feeds huge numbers of generated inputs to a program to find crashes and bugs.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. Copying 100 bytes into a 64-byte array in C is a…

    • spatial memory-safety bug (buffer overflow)
    • temporal bug
    • type error caught by the compiler
    • harmless warning
    Show the answer

    spatial memory-safety bug (buffer overflow)

    It writes outside the array’s bounds.

    Source: CISA & NSA, Memory Safe Languages 2025

  2. According to Google, what share (in %) of Android vulnerabilities were memory-safety bugs in 2024?

    Show the answer

    24 % (± 1 %)

    It fell from 76 % in 2019 to 24 % in 2024.

    Source: Google Security Blog, Sept 2024

  3. Which are memory-safe languages for normal code?

    • Python
    • Rust (outside unsafe blocks)
    • C
    • Java
    Show the answer

    Python; Rust (outside unsafe blocks); Java

    C does not check bounds or lifetimes.

    Source: CISA & NSA, 2025

  4. Which technique finds bugs by feeding generated inputs to a program?

    • Fuzzing
    • ASLR
    • Stack canaries
    • Reference counting
    Show the answer

    Fuzzing

    Fuzzers generate many inputs and watch for crashes and sanitizer reports.

    Source: OSS-Fuzz documentation

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.