CWE-761: Free of Pointer not at Start of Buffer
The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.
How it's found
Free of Pointer not at Start of Buffer is a specific, narrowly defined instance of a broader pattern. Testers confirm it with targeted code review and a proof-of-concept input that exercises the exact code path this weakness describes.
This can cause the product to crash, or in some cases, modify critical program variables or execute code. This weakness often occurs when the memory is allocated explicitly on the heap with one of the malloc() family functions and free() is called, but pointer arithmetic has caused the pointer to be in the interior or end of the buffer.
Vulnerable vs. safe
/* matched char, free string and return success */free(str);return SUCCESS;
/* didn't match yet, increment pointer and try next char */if( *str == c ){}str = str + 1;/* we did not match the char in the string, free mem and return failure */char *str;str = (char*)malloc(20*sizeof(char));strcpy(str, "Search Me!");while( *str != NULL){}free(str);return FAILURE;#define SUCCESS (1)#define FAILURE (0)int contains_char(char c){}/* matched char, free string and return success */free(str);return SUCCESS;
/* didn't match yet, increment pointer and try next char */if( str[i] == c ){}i = i + 1;/* we did not match the char in the string, free mem and return failure */char *str;int i = 0;str = (char*)malloc(20*sizeof(char));strcpy(str, "Search Me!");while( i < strlen(str) ){}free(str);return FAILURE;#define SUCCESS (1)#define FAILURE (0)int cointains_char(char c){}Consequences
- Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands
Mitigations
- 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.
- Implementation: When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.
- Architecture and Design: 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.
- Architecture and Design: Use a language that provides abstractions for memory allocation and deallocation.
Where this fits in a TurboPentest engagement
TurboPentest's agentic pentest is powerful and covers a broad range of issues automatically. This particular class is best confirmed in a manual IntegSec engagement, where human pentesters apply deeper methodology and a larger context window than any automated pass.
Frequently asked questions
What is CWE-761?
The product calls free() on a pointer to a memory resource that was allocated on the heap, but the pointer is not at the start of the buffer.
How do you find Free of Pointer not at Start of Buffer?
Free of Pointer not at Start of Buffer is a specific, narrowly defined instance of a broader pattern. Testers confirm it with targeted code review and a proof-of-concept input that exercises the exact code path this weakness describes.
What is the impact of CWE-761?
Modify Memory, DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands
Does TurboPentest test for Free of Pointer not at Start of Buffer?
TurboPentest's agentic pentest is powerful and covers a broad range of issues automatically. This particular class is best confirmed in a manual IntegSec engagement, where human pentesters apply deeper methodology and a larger context window than any automated pass.
Related CWEs
Find these issues before an attacker does
TurboPentest runs an agentic AI pentest against your target and reports findings with proof, from $99 per target.
Start a $99 pentest