CWE-416: Use After Free
Official CWE-416 CWE context with Glexia analysis, remediation guidance, related CVEs, and ATT&CK context.
Glexia's Take
CWE-416: Dangling pointer
Use After Free represents a recurring weakness pattern that can create exploitable paths when design, validation, or implementation controls are missing.
Executive Impact
- Integrity: 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.
- Availability: 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.
- Confidentiality: Read Memory: Read operations on freed memory can sometimes leak sensitive information instead of causing a crash
- Integrity,Confidentiality,Availability: 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.
Developer Pattern
CWE-416 is the kind of defect developers can usually prevent with explicit validation, safer framework defaults, and tests that exercise hostile input or unsafe state transitions.
Confidence
high confidence from CWE-416, 4.20.
Official CWE Definition
CWE-416: Use After Free
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.
Developer And Remediation Guidance
How teams prevent and detect this weakness
Causes
- The following example demonstrates the weakness.
- The following code illustrates a use after free error: When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.
Remediation
- Architecture and Design: Choose a language that provides automatic memory management.
- Implementation: 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.
Detection
- Fuzzing: 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 Analysis: 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.)
- Automated Dynamic Analysis: Use tools that are integrated during compilation to insert runtime error-checking mechanisms related to memory safety errors, such as AddressSanitizer (ASan) for C/C++ [REF-1518].
Mappings
Related CVEs, CWEs, and ATT&CK context
Related CWEs
- CWE-1265: Unintended Reentrant Invocation of Non-reentrant Code Via Nested Calls
- CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
- CWE-364: Signal Handler Race Condition
- CWE-415: Double Free
- CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
- CWE-123: Write-what-where Condition
- CWE-672: Operation on a Resource after Expiration or Release
- CWE-672: Operation on a Resource after Expiration or Release
- CWE-672: Operation on a Resource after Expiration or Release
- CWE-825: Expired Pointer Dereference
- CWE-754: Improper Check for Unusual or Exceptional Conditions
ATT&CK Relevance
ATT&CK relevance is shown only when reviewed or responsibly inferred.