CWE-761 Variant Incomplete

Free of Pointer not at Start of Buffer

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.

Definition

What is CWE-761?

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.
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.
Auswirkungen in der Praxis

Real-world CVEs caused by CWE-761

  • function "internally calls 'calloc' and returns a pointer at an index... inside the allocated buffer. This led to freeing invalid memory."

Wie Angreifer es ausnutzen

Angreiferpfad Schritt für Schritt

  1. 1

    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.

  2. 2

    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.

  3. 3

    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.

  4. 4

    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.

  5. 5

    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.

Verwundbares Codebeispiel

Vulnerable C

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.

Verwundbar C
#define SUCCESS (1)
  #define FAILURE (0)
  int contains_char(char c){
  		char *str;
  		str = (char*)malloc(20*sizeof(char));
  		strcpy(str, "Search Me!");
  		while( *str != NULL){
  				if( *str == c ){
```
/* matched char, free string and return success */* 
  						free(str);
  						return SUCCESS;}
  				
  				 */* didn't match yet, increment pointer and try next char */* 
  				
  				str = str + 1;}
  		
  		 */* we did not match the char in the string, free mem and return failure */* 
  		
  		free(str);
  		return FAILURE;}
Sicheres Codebeispiel

Secure C

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.

Sicher C
#define SUCCESS (1)
  #define FAILURE (0)
  int cointains_char(char c){
  		char *str;
  		int i = 0;
  		str = (char*)malloc(20*sizeof(char));
  		strcpy(str, "Search Me!");
  		while( i < strlen(str) ){
  				if( str[i] == c ){
```
/* matched char, free string and return success */* 
  						free(str);
  						return SUCCESS;}
  				
  				 */* didn't match yet, increment pointer and try next char */* 
  				
  				i = i + 1;}
  		
  		 */* we did not match the char in the string, free mem and return failure */* 
  		
  		free(str);
  		return FAILURE;}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Präventions-Checkliste

How to prevent CWE-761

  • 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.
  • Testing Use a tool that dynamically detects memory management problems, such as valgrind.
Erkennungssignale

How to detect CWE-761

SAST High

Führe statische Analyse (SAST) auf der Codebasis aus und suche im Datenfluss nach dem unsicheren Muster.

DAST Moderate

Führe dynamische Application-Security-Tests gegen den Live-Endpoint aus.

Runtime Moderate

Beobachte Runtime-Logs auf ungewöhnliche Exception-Traces, fehlerhafte Eingaben oder Versuche, Autorisierung zu umgehen.

Code review Moderate

Code Review: Markiere jeden neuen Code, der Eingaben von dieser Oberfläche ohne validierte Framework-Helper verarbeitet.

Plexicus Auto-Fix

Plexicus erkennt CWE-761 automatisch und öffnet in unter 60 Sekunden einen Fix-PR.

Codex Remedium scannt jeden Commit, identifiziert genau diese Schwachstelle und liefert einen reviewer-ready Pull Request mit dem Patch. Keine Tickets. Keine Hand-offs.

Häufig gestellte Fragen

Frequently asked questions

Was ist CWE-761?

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.

Wie gravierend ist CWE-761?

MITRE hat für diese Schwachstelle keine Exploit-Wahrscheinlichkeit veröffentlicht. Behandle sie als mittlere Auswirkung, bis dein Threat Model anderes belegt.

Welche Sprachen oder Plattformen sind von CWE-761 betroffen?

MITRE hat für diese CWE keine betroffenen Plattformen spezifiziert — sie kann in den meisten Anwendungs-Stacks auftreten.

Wie kann ich CWE-761 verhindern?

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. When programming in C++, consider using smart pointers provided by the boost library to help correctly and consistently manage memory.

Wie erkennt und behebt Plexicus CWE-761?

Die SAST-Engine von Plexicus erkennt die Datenfluss-Signatur von CWE-761 bei jedem Commit. Bei einem Treffer öffnet unser Codex-Remedium-Agent einen Fix-PR mit korrigiertem Code, Tests und einer einzeiligen Zusammenfassung für den Reviewer.

Wo erfahre ich mehr über CWE-761?

MITRE veröffentlicht die kanonische Definition unter https://cwe.mitre.org/data/definitions/761.html. Für ergänzende Hinweise kannst du auch die OWASP- und NIST-Dokumentation heranziehen.

Bereit, wenn du es bist

Schluss mit dem Bezahlen pro Entwickler.
Schließ den Kreislauf.

Plexicus ist die KI-native ASPM, die scannt, filtert, fixt, pentestet und erklärt — autonom. Unbegrenzte Entwickler, unbegrenzte Repos, Fair-Use-KI-Aktionen. Echter kostenloser Tarif, €269/mo jährlich, wenn du bereit bist.