Use After Free

Stable Variant
Structure: Simple
Description

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. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.

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. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.
Common Consequences 3
Scope: Integrity

Impact: Modify Memory

The use of previously freed memory may corrupt valid data, if the memory area in question has been allocated and used properly elsewhere.

Scope: Availability

Impact: DoS: Crash, Exit, or Restart

If chunk consolidation occurs after the use of previously freed data, the process may crash when invalid data is used as chunk information.

Scope: IntegrityConfidentialityAvailability

Impact: Execute Unauthorized Code or Commands

If malicious data is entered before chunk consolidation can take place, it may be possible to take advantage of a write-what-where primitive to execute arbitrary code. If the newly allocated data happens to hold a class, in C++ for example, various function pointers may be scattered within the heap data. If one of these function pointers is overwritten with an address to valid shellcode, execution of arbitrary code can be achieved.

Detection Methods 2
FuzzingHigh
Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with random inputs, it is often capable of generating unexpected results such as crashes, memory corruption, or resource consumption. Fuzzing effectively produces repeatable test cases that clearly indicate bugs, which helps developers to diagnose the issues.
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 2
Phase: Architecture and Design

Strategy: Language Selection

Choose a language that provides automatic memory management.
Phase: Implementation

Strategy: Attack Surface Reduction

When freeing pointers, be sure to set them to NULL once they are freed. However, the utilization of multiple or complex data structures may lower the usefulness of this strategy.

Effectiveness: Defense in Depth

Demonstrative Examples 2
The following example demonstrates the weakness.

Code Example:

Bad
C
c
The following code illustrates a use after free error:

Code Example:

Bad
C
c
When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.
Observed Examples 28
CVE-2022-20141Chain: an operating system kernel has insufficent resource locking (Improper Resource Locking) leading to a use after free (Use After Free).
CVE-2022-2621Chain: two threads in a web browser use the same resource (Race Condition within a Thread), but one of those threads can destroy the resource before the other has completed (Use After Free).
CVE-2021-0920Chain: mobile platform race condition (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')) leading to use-after-free (Use After Free), as exploited in the wild per CISA KEV.
CVE-2020-6819Chain: race condition (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')) leads to use-after-free (Use After Free), as exploited in the wild per CISA KEV.
CVE-2010-4168Use-after-free triggered by closing a connection while data is still being transmitted.
CVE-2010-2941Improper allocation for invalid data leads to use-after-free.
CVE-2010-2547certificate with a large number of Subject Alternate Names not properly handled in realloc, leading to use-after-free
CVE-2010-1772Timers are not disabled when a related object is deleted
CVE-2010-1437Access to a "dead" object that is being cleaned up
CVE-2010-1208object is deleted even with a non-zero reference count, and later accessed
CVE-2010-0629use-after-free involving request containing an invalid version number
CVE-2010-0378unload of an object that is currently being accessed by other functionality
CVE-2010-0302incorrectly tracking a reference count leads to use-after-free
CVE-2010-0249use-after-free related to use of uninitialized memory
CVE-2010-0050HTML document with incorrectly-nested tags
CVE-2009-3658Use after free in ActiveX object by providing a malformed argument to a method
CVE-2009-3616use-after-free by disconnecting during data transfer, or a message containing incorrect data types
CVE-2009-3553disconnect during a large data transfer causes incorrect reference count, leading to use-after-free
CVE-2009-2416use-after-free found by fuzzing
CVE-2009-1837Chain: race condition (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')) from improper handling of a page transition in web client while an applet is loading (Context Switching Race Condition) leads to use after free (Use After Free)
CVE-2009-0749realloc generates new buffer and pointer, but previous pointer is still retained, leading to use after free
CVE-2010-3328Use-after-free in web browser, probably resultant from not initializing memory.
CVE-2008-5038use-after-free when one thread accessed memory that was freed by another thread
CVE-2008-0077assignment of malformed values to certain properties triggers use after free
CVE-2006-4434mail server does not properly handle a long header.
CVE-2010-2753chain: integer overflow leads to use-after-free
CVE-2006-4997freed pointer dereference
CVE-2003-0813Chain: A multi-threaded race condition (Time-of-check Time-of-use (TOCTOU) Race Condition) allows attackers to cause two threads to process the same RPC request, which causes a use-after-free (Use After Free) in one thread
References 3
Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors
Katrina Tsipenyuk, Brian Chess, and Gary McGraw
NIST Workshop on Software Security Assurance Tools Techniques and MetricsNIST
07-11-2005
ID: REF-6
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
24 Deadly Sins of Software Security
Michael Howard, David LeBlanc, and John Viega
McGraw-Hill
2010
ID: REF-44
Likelihood of Exploit

High

Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Alternate Terms

Dangling pointer

a pointer that no longer points to valid memory, often after it has been freed

UAF

commonly used acronym for Use After Free

Use-After-Free

Functional Areas
  1. Memory Management
Affected Resources
  1. Memory
Taxonomy Mapping
  • ISA/IEC 62443
  • 7 Pernicious Kingdoms
  • CLASP
  • CERT C Secure Coding
  • CERT C Secure Coding
  • CERT C Secure Coding
  • Software Fault Patterns