Use-after-free

Also known as: UAF

On this page 7

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.

StageProgram actionState of the memory
AllocationObject created with malloc or newValid and owned by the object
AliasingSecond pointer to the object is storedTwo references to one block
ReleaseObject freed through one referenceReturned to the allocator free list
ReallocationAnother allocation of similar sizeSlot now holds different data
Stale useOld pointer is dereferencedReads or writes the new occupant
Lifecycle of a use-after-free

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 accessTypical primitivePossible impact
Read of a fieldLeak of heap or code addressesBypass of address space layout randomization
Write to a fieldCorruption of a neighboring objectArbitrary memory write
Virtual method callControl of the instruction pointerRemote code execution
Second freeDouble free of the same chunkAllocator corruption
Kernel object useOverwrite of credentials structureLocal privilege escalation
Impact by type of stale access

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.

WeaknessCWE identifierCore mistake
Use-after-freeCWE-416Accessing memory after its lifetime ends
Double freeCWE-415Releasing the same block twice
Buffer overflowCWE-120 and CWE-787Writing beyond the bounds of a buffer
Integer overflowCWE-190Arithmetic wraparound in size calculations
Null pointer dereferenceCWE-476Using a pointer that holds no address
Use-after-free and neighboring weakness classes

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.

DefenseStageWhat it achieves
Smart pointers and RAIIDevelopmentTies object lifetime to clear ownership
AddressSanitizerTestingQuarantines freed memory and flags stale access
Coverage-guided fuzzingTestingFinds rare paths that trigger the free
Hardened allocatorsRuntimeDelays reuse and isolates object types
Memory tagging (ARM MTE)RuntimeDetects mismatched pointer and memory tags
Control-flow integrityRuntimeRestricts hijacked indirect calls
Defenses against use-after-free

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.