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
This weakness is not covered by the automated black-box pentest. IntegSec pentesters cover it in a manual engagement.
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?
This weakness is not covered by the automated black-box pentest. IntegSec pentesters cover it in a manual engagement.
Related CWEs
Written and reviewed by
Michel Chamberland - Founder & CEO, IntegSec
CISSP, OSCP, OSCE, CEH, GIAC, CCSK · 20+ years in offensive security
Michel has spent 20+ years on offensive security teams including IBM X-Force Red and Trustwave SpiderLabs, leading penetration tests, red team engagements, and breach response for Fortune 500 customers. He is the founder of IntegSec and the architect of TurboPentest.
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 pentest