Use-after-free (UAF) is a memory safety vulnerability in which a program continues to access a region of heap memory after that memory has been released back to the allocator. Cybersecurity classifies it as CWE-416, and among vulnerabilities in software written in C and C++ it is one of the most frequently exploited classes, especially in web browsers, operating system kernels and media parsers.
The flaw is dangerous because freed memory is quickly reused for new objects. If an attacker can control what is allocated in the freed slot, a stale reference that the program still trusts suddenly points to attacker-shaped data, which can lead to information disclosure, privilege escalation or remote code execution (RCE).
How a use-after-free occurs
A use-after-free occurs when a dangling pointer, a reference that still holds the address of a freed object, is dereferenced. The typical sequence has three steps: the program allocates an object, frees it while some other code path still keeps a pointer to it, and later reads, writes or calls through that stale pointer.
The root cause is almost always a lifetime management error. Common patterns include an object freed in an error path but still referenced by a caller, a callback that destroys its own owner, a cache that is not cleared when an entry is released, and race conditions in which one thread frees an object while another is still using it.
The table below shows the lifecycle stages of a typical UAF and what happens to the memory at each point.
| Stage | Program action | State of the memory |
|---|---|---|
| Allocation | Object created with malloc or new | Valid and owned by the object |
| Aliasing | Second pointer to the object is stored | Two references to one block |
| Release | Object freed through one reference | Returned to the allocator free list |
| Reallocation | Another allocation of similar size | Slot now holds different data |
| Stale use | Old pointer is dereferenced | Reads or writes the new occupant |
Why use-after-free bugs are exploitable
Use-after-free bugs are exploitable because modern heap allocators reuse freed chunks deterministically for allocations of the same size class. An attacker who triggers the free and then performs carefully sized allocations, a technique called heap grooming or heap spraying, can place controlled data exactly where the stale pointer looks.
In object-oriented code the most powerful outcome is virtual table hijacking. Many C++ objects begin with a pointer to a table of function addresses; when the stale object is replaced by attacker data, the next virtual method call jumps to an address the attacker chose. In scripting engines, the same primitive is often turned into arbitrary read and write capability first and then chained into code execution.
The severity of a UAF depends on what the stale pointer is used for, as summarized here.
| Type of stale access | Typical primitive | Possible impact |
|---|---|---|
| Read of a field | Leak of heap or code addresses | Bypass of address space layout randomization |
| Write to a field | Corruption of a neighboring object | Arbitrary memory write |
| Virtual method call | Control of the instruction pointer | Remote code execution |
| Second free | Double free of the same chunk | Allocator corruption |
| Kernel object use | Overwrite of credentials structure | Local privilege escalation |
Use-after-free compared with related memory bugs
Use-after-free is a temporal memory safety error, meaning the access happens at the wrong time, whereas a buffer overflow is a spatial memory safety error, meaning the access goes to the wrong place. Both corrupt memory, but they require different defenses and appear in different code patterns.
| Weakness | CWE identifier | Core mistake |
|---|---|---|
| Use-after-free | CWE-416 | Accessing memory after its lifetime ends |
| Double free | CWE-415 | Releasing the same block twice |
| Buffer overflow | CWE-120 and CWE-787 | Writing beyond the bounds of a buffer |
| Integer overflow | CWE-190 | Arithmetic wraparound in size calculations |
| Null pointer dereference | CWE-476 | Using a pointer that holds no address |
In practice these classes often interact. An integer overflow may cause an undersized allocation that leads to an overflow, and a double free is frequently a special case of a UAF in which the stale use is a second call to free. In the CVE and NVD records, many critical browser and kernel advisories list CWE-416 as the root weakness.
Detecting and preventing use-after-free
Use-after-free is best prevented by memory-safe languages and ownership models, and best detected by dynamic instrumentation combined with fuzzing. Languages such as Rust enforce object lifetimes at compile time through a borrow checker, while garbage-collected languages such as Java, Go and C# make this class largely impossible in safe code.
For existing C and C++ code, several layers of defense reduce the risk, as shown in the following table.
| Defense | Stage | What it achieves |
|---|---|---|
| Smart pointers and RAII | Development | Ties object lifetime to clear ownership |
| AddressSanitizer | Testing | Quarantines freed memory and flags stale access |
| Coverage-guided fuzzing | Testing | Finds rare paths that trigger the free |
| Hardened allocators | Runtime | Delays reuse and isolates object types |
| Memory tagging (ARM MTE) | Runtime | Detects mismatched pointer and memory tags |
| Control-flow integrity | Runtime | Restricts hijacked indirect calls |
Setting a pointer to null after free is a useful but incomplete habit: it protects only that single variable and does nothing for other copies of the same address. Effective remediation fixes the ownership logic itself, for example by using reference counting correctly or by redesigning callbacks so they cannot destroy their caller.
Use-after-free in vulnerability management
In vulnerability management, a confirmed UAF in an internet-facing component usually receives a high or critical CVSS score, because the potential impact reaches code execution. Exploitation still requires skill, so EPSS probabilities vary widely, yet browser and kernel UAF flaws appear regularly in the CISA KEV catalog as exploited zero-day vulnerabilities.
- Prioritize patches for browsers, kernels and document viewers, where UAF exploitation is most common.
- Do not dismiss crashes in freed memory as mere stability bugs; many are exploitable.
- Treat sandbox escapes as the usual second stage after a renderer UAF.
In everyday language
A use-after-free happens when a program throws away a piece of memory but keeps a note telling it where that memory was. Later it follows the old note and uses whatever is there now.
Picture returning a hotel room key card, but the card still opens the door. By the time you come back, a new guest has moved in, and you are now using their room. An attacker can arrange to be that new guest and leave instructions that the program blindly follows. That is why this small bookkeeping mistake can let someone take control of a computer.
Seen in the real world
A security researcher fuzzes the rendering engine of a web browser with millions of generated HTML pages. AddressSanitizer reports a heap use-after-free in which a layout object is freed when a script removes an element, while a pending event handler still holds a pointer to it.
The researcher shows that, by allocating strings of exactly the freed object’s size, the stale pointer can be made to call a chosen address, giving code execution inside the renderer process. The flaw is reported through coordinated vulnerability disclosure, assigned a CVE with a CVSS base score of 8.8, and fixed by switching the handler to a reference-counted pointer. Users receive the patch in a routine browser update within a few weeks, before any public exploit appears.