You check out of a hotel but forget to hand back your key card. That evening the room goes to a new guest, and your old card still opens the door. You can walk in, read their documents, and change things. The hotel did nothing "wrong" when it gave the room away; the problem is that your card was never cancelled.
Programs make exactly this mistake, and it has a name: a use-after-free. It's behind some of the most dangerous vulnerabilities in browsers, operating systems and servers. This guide explains what it is, why attackers love it, real cases, and how to prevent it.
What a use-after-free is
MITRE (CWE-416) describes it like this: "The product reuses or references memory after it has been freed. At some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation."
The stale pointer has a name too: a dangling pointer. It still holds a valid-looking address, but the memory at that address now belongs to someone else.
How it works
In languages like C and C++, programs manage memory by hand:
struct session *s = malloc(sizeof *s); // 1. allocate
s->is_admin = 0;
free(s); // 2. free it (user logged out)
struct session *t = malloc(sizeof *t); // 3. the allocator often hands back the same memory
t->is_admin = 1; // ...to someone else
if (s->is_admin) grant_admin(); // 4. use after free: reads t's data through s
Step 3 is the key. Memory allocators reuse freed memory quickly, often for the next allocation of a similar size. So the dangling pointer doesn't point at garbage; it points at the next object, and an attacker who can trigger allocations can often choose what that object is.
Our test: a toy allocator
We built a small model in Python: a "heap" of slots that hands out the first free slot, like many real allocators reuse recently freed memory. No real memory is involved.
$ python3 hotel.py
1) raw pointer (C-style)
alice's session in slot 0: {'user': 'alice', 'is_admin': False}
freed, then mallory's admin session reuses slot 0
alice's old pointer now reads: {'user': 'mallory', 'is_admin': True} <- use after free
2) checked handle (the fix)
use after free refused: stale handle
The fix in part 2 is a generation counter: each handle remembers which "version" of the slot it was given, and the slot's version changes every time it's reused, so a stale handle is refused instead of reading the new owner's data.
Why attackers love it
A use-after-free gives an attacker something powerful: the program trusts a pointer, and the attacker controls what's behind it. In browsers, for example, attackers run JavaScript that frees an object, quickly fills the freed memory with data they crafted, and then makes the browser use the old pointer. If that memory held a function pointer, the browser now calls code the attacker chose.
That's why use-after-free bugs show up so often in lists of exploited vulnerabilities. When we checked CISA's Known Exploited Vulnerabilities catalog on 6 November 2026, 95 of its 1,768 entries were tagged CWE-416.
Real cases
Chrome CVE-2019-5786 (2019)
In March 2019 Google shipped Chrome 72.0.3626.121 with one security fix: "Use-after-free in FileReader", reported by Clement Lecigne of Google's Threat Analysis Group on 27 February 2019. Google's release note added: "Google is aware of reports that an exploit for CVE-2019-5786 exists in the wild." CISA later added it to its Known Exploited Vulnerabilities catalog as a "Google Chrome Blink Use-After-Free Vulnerability". Source: Chrome Releases, 1 March 2019.
Linux CVE-2026-98432 (2026)
In the Linux kernel's IPv4 TCP code, the options saved from a client's connection request were handed to a new socket without being copied, and could be freed while another CPU was still using them to send a reply. The kernel's CVE numbering authority rated it CVSS 9.8. Our full breakdown: the video and the write-up.
Where use-after-frees show up
- Browsers and JavaScript engines: objects freed by one script action and used by another.
- Kernels and drivers: memory shared between threads or CPUs, freed by one while another still uses it (often combined with a race condition).
- Callbacks and event handlers: an object is destroyed while a callback that refers to it is still queued.
- Error paths: cleanup code frees something, and the normal path uses it again.
- Caches and lists: an item is removed and freed, but another structure still points to it.
How to prevent it
1. Use memory-safe languages for new code. Rust's ownership rules stop use-after-free at compile time; Go, Java, C# and Swift manage memory for you so freed objects can't be reached. This is the single most effective fix, and it's why many projects are moving parsers and network code to memory-safe languages.
2. In C and C++, make ownership explicit. Prefer std::unique_ptr and std::shared_ptr over raw new/delete.
Decide which code owns each object and which only borrows it.
3. Clear pointers when you free them.
free(s);
s = NULL; // a later use crashes cleanly instead of reading someone else's data
It doesn't help other copies of the pointer, but it removes the most common case.
4. Use generation-checked handles for objects that come and go (game entities, connections, sessions), as in our lab, so stale references are detected.
5. Find them before attackers do. Compile tests with AddressSanitizer (-fsanitize=address), which reports a
use-after-free at the exact line, and fuzz anything that parses untrusted input.
Myths vs reality
- "It just crashes." Sometimes, but attackers turn it into code execution by controlling what lands in the freed memory.
- "Garbage-collected languages are immune." Mostly, but their runtimes and the C libraries they call aren't.
- "Setting the pointer to NULL fixes it." Only for that one pointer; copies elsewhere still dangle.
Getting started: a checklist
- Write new parsers and network-facing code in a memory-safe language where you can.
- In existing C/C++, replace raw owning pointers with smart pointers, one module at a time.
- Run your test suite with AddressSanitizer in CI.
- Fuzz every input parser.
- In code review, ask: "who frees this, and can anyone still be holding it?"
- Patch browsers and kernels quickly: many exploited bugs in both are use-after-frees.
Try it yourself
The lab is in SpiderCob/cve-labs: concepts/use-after-free:
hotel.py: a toy allocator where a freed slot is reused and a stale pointer reads the new owner's data, then a generation-checked handle that refuses.tests/: proves both behaviours.
It's a model in plain Python; it doesn't touch real memory and sends nothing over the network.
Key takeaways
- A use-after-free is a stale pointer to memory that's been handed to something else.
- Allocators reuse memory quickly, so attackers can choose what the stale pointer sees.
- It's one of the most exploited bug types, in browsers and kernels alike.
- Memory-safe languages remove it; in C/C++, use smart pointers, clear ownership, AddressSanitizer and fuzzing.
Glossary
- Use-after-free (CWE-416): using memory after it has been freed.
- Dangling pointer: a pointer to memory that has been freed.
- Allocator: the code that hands out and reclaims memory (
malloc/free). - Heap: the memory area allocators manage.
- AddressSanitizer: a compiler tool that detects memory errors at runtime.
- Memory-safe language: a language that prevents this class of bug by design.
Sources
- MITRE CWE-416, Use After Free: https://cwe.mitre.org/data/definitions/416.html
- Chrome Releases, Stable Channel Update for Desktop (1 March 2019, updated 5 March): https://chromereleases.googleblog.com/2019/03/stable-channel-update-for-desktop.html
- CISA Known Exploited Vulnerabilities catalog (checked 6 Nov 2026): https://www.cisa.gov/known-exploited-vulnerabilities-catalog
- SpiderCob, Linux CVE-2026-98432: https://spidercob.com/cve/2026-98432/