CWE-416

Use After Free. [Improper-Control-Of-A-Resource-Through-Its-Lifetime, Top25-2024-8]

Required inputs: IR, StaticSemanticAnalysis

Referencing memory after it has been freed can cause a program to crash, use unexpected values, or execute code.

The use of previously-freed memory can have any number of adverse consequences, ranging from the corruption of valid data to the execution of arbitrary code, depending on the instantiation and timing of the flaw. The simplest way data corruption may occur involves the system's reuse of the freed memory. Use-after-free errors have two common and sometimes overlapping causes:

  • Error conditions and other exceptional circumstances.
  • Confusion over which part of the program is responsible for freeing the memory.

In this scenario, the memory in question is allocated to another pointer validly at some point after it has been freed. The original pointer to the freed memory is used again and points to somewhere within the new allocation. As the data is changed, it corrupts the validly used memory; this induces undefined behavior in the process.

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.

Demonstrative Examples
Example 1

The following example demonstrates the weakness.

Example Language:C
    #include <stdio.h>
    #include <unistd.h>
    #define BUFSIZER1 512
    #define BUFSIZER2 ((BUFSIZER1/2) - 8)
    int main(int argc, char **argv) {
        char *buf1R1;
        char *buf2R1;
        char *buf2R2;
        char *buf3R2;
        buf1R1 = (char *) malloc(BUFSIZER1);
        buf2R1 = (char *) malloc(BUFSIZER1);
        free(buf2R1);
        buf2R2 = (char *) malloc(BUFSIZER2);
        buf3R2 = (char *) malloc(BUFSIZER2);
        strncpy(buf2R1, argv[1], BUFSIZER1-1);
        free(buf1R1);
        free(buf2R2);
        free(buf3R2);
    }
Example 2

The following code illustrates a use after free error:

Example Language:C
    char* ptr = (char*)malloc (SIZE);
    if (err) {
        abrt = 1;
        free(ptr);
    }
    ...
    if (abrt) {
        logError("operation aborted before commit", ptr);
    }

When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.

Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

double_free

Dynamic memory released here was already released earlier

None

False

possible_double_free

Dynamic memory released here possibly already released earlier

None

False

possible_use_after_free

Dynamic memory possibly used after it was previously released

None

False

use_after_free

Dynamic memory used after it was previously released

None

False

Options

functions_with_ignored_deallocators

functions_with_ignored_deallocators : set[str] = set()

Set of functions (given by their qualified name) where all deallocators are ignored. For these functions, the check will never report a use-after-free. It will also assume that these functions never create freed pointers, neither by return value, out param, nor by modifying global state.
 

report_freed_this_at_call

report_freed_this_at_call : bool = False

This option controls findings when a freed pointer is used in C++ to call a non-static member function. When set to true, the use at the call is directly reported. When false, the analysis waits for an actual dereference (of the this-pointer then) inside the callee, and only reports those.
 

report_read_pointer_args_in_calls_to_undefined

report_read_pointer_args_in_calls_to_undefined : bool = True

Report when freed pointers are passed to undefined (external) functions.
 

resources

resources

Type: set[str]

Default: {'C++ArrayHeapMemory', 'C++HeapMemory', 'CudaAsyncMemory', 'CudaDeviceMemory', 'CudaDriverAsyncMemory', 'CudaHostMemory', 'CudaManagedMemory', 'FileHandle', 'HeapMemory', 'UniquePtrHeapMemory'}

Set of resources to be checked (selection of rules in the Resources group).
 

witness_paths

witness_paths : bool = True

Whether witness paths should be determined and included in the issue.