Free of Pointer not at Start of Buffer

Incomplete Variant
Structure: Simple
Description

This vulnerability occurs when a program incorrectly frees a memory pointer that no longer points to the beginning of the allocated heap buffer, often due to pointer arithmetic.

Extended Description

This issue typically happens when you allocate memory using functions like `malloc()`, `calloc()`, or `realloc()`, and then later modify the pointer—for example, by incrementing it to traverse a data structure. When you later pass this offset pointer to `free()`, the memory manager cannot correctly identify the original memory block's metadata, leading to heap corruption. This corruption can cause immediate crashes, unpredictable behavior, or even create opportunities for attackers to manipulate program data or execution flow. To prevent this, always ensure you free the exact pointer returned by the allocation function, or use a separate tracking variable to preserve the original starting address.

Common Consequences 1
Scope: IntegrityAvailabilityConfidentiality

Impact: Modify MemoryDoS: Crash, Exit, or RestartExecute Unauthorized Code or Commands

Potential Mitigations 5
Phase: Implementation
When utilizing pointer arithmetic to traverse a buffer, use a separate variable to track progress through memory and preserve the originally allocated address for later freeing.
Phase: Implementation
When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.
Phase: Architecture and Design

Strategy: Libraries or Frameworks

Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, glibc in Linux provides protection against free of invalid pointers.
Phase: Architecture and Design
Use a language that provides abstractions for memory allocation and deallocation.
Phase: Testing
Use a tool that dynamically detects memory management problems, such as valgrind.
Demonstrative Examples 3

ID : DX-77

In this example, the programmer dynamically allocates a buffer to hold a string and then searches for a specific character. After completing the search, the programmer attempts to release the allocated memory and return SUCCESS or FAILURE to the caller. Note: for simplification, this example uses a hard-coded "Search Me!" string and a constant string length of 20.

Code Example:

Bad
C
c

/* matched char, free string and return success / free(str); return SUCCESS;}

c
However, if the character is not at the beginning of the string, or if it is not in the string at all, then the pointer will not be at the start of the buffer when the programmer frees it.
Instead of freeing the pointer in the middle of the buffer, the programmer can use an indexing pointer to step through the memory or abstract the memory calculations by using array indexing.

Code Example:

Good
C
c

/* matched char, free string and return success / free(str); return SUCCESS;}

c

ID : DX-78

This code attempts to tokenize a string and place it into an array using the strsep function, which inserts a \0 byte in place of whitespace or a tab character. After finishing the loop, each string in the AP array points to a location within the input string.

Code Example:

Bad
C
c
Since strsep is not allocating any new memory, freeing an element in the middle of the array is equivalent to free a pointer in the middle of inputstring.

ID : DX-79

Consider the following code in the context of a parsing application to extract commands out of user data. The intent is to parse each command and add it to a queue of commands to be executed, discarding each malformed entry.

Code Example:

Bad
C

//hardcode input length for simplicity* char* input = (char*) malloc(40*sizeof(char)); char tok; char sep = " \t";

c
c
While the above code attempts to free memory associated with bad commands, since the memory was all allocated in one chunk, it must all be freed together.
One way to fix this problem would be to copy the commands into a new memory location before placing them in the queue. Then, after all commands have been processed, the memory can safely be freed.

Code Example:

Good
C

//hardcode input length for simplicity* char* input = (char*) malloc(40*sizeof(char)); char *tok, command; char sep = " \t";

c
c
Observed Examples 1
CVE-2019-11930function "internally calls 'calloc' and returns a pointer at an index... inside the allocated buffer. This led to freeing invalid memory."
References 2
boost C++ Library Smart Pointers
ID: REF-657
Valgrind
ID: REF-480
Modes of Introduction
Implementation
Functional Areas
  1. Memory Management
Affected Resources
  1. Memory
Taxonomy Mapping
  • Software Fault Patterns
Notes
MaintenanceCurrently, Release of Invalid Pointer or Reference is the parent, however it may be desirable to have an intermediate parent which is not function-specific, similar to how Mismatched Memory Management Routines is an intermediate parent between Release of Invalid Pointer or Reference and Free of Memory not on the Heap.