CWE-415: Double Free
Description
The product calls free() twice on the same memory address, potentially leading to modification of unexpected memory locations.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Extended Description
When a program calls free() twice with the same argument, the program's memory management data structures become corrupted. This corruption can cause the program to crash or, in some circumstances, cause two later calls to malloc() to return the same pointer. If malloc() returns the same value twice and the program later gives the attacker control over the data that is written into this doubly-allocated memory, the program becomes vulnerable to a buffer overflow attack.
Example - 1
The following code shows a simple example of a double free vulnerability. Double free vulnerabilities have two common (and sometimes overlapping) causes: Although some double free vulnerabilities are not much more complicated than this example, most are spread out across hundreds of lines of code or even different files. Programmers seem particularly susceptible to freeing global variables more than once.
free(ptr);
char* ptr = (char*)malloc (SIZE);...if (abrt) {}...free(ptr);
Example - 2
While contrived, this code should be exploitable on Linux distributions that do not ship with heap-chunk check summing turned on.
char *buf1R1;char *buf2R1;char *buf1R2;buf1R1 = (char *) malloc(BUFSIZE2);buf2R1 = (char *) malloc(BUFSIZE2);free(buf1R1);free(buf2R1);buf1R2 = (char *) malloc(BUFSIZE1);strncpy(buf1R2, argv[1], BUFSIZE1-1);free(buf2R1);free(buf1R2);#include <stdio.h>#include <unistd.h>#define BUFSIZE1 512#define BUFSIZE2 ((BUFSIZE1/2) - 8)int main(int argc, char **argv) {}
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-123: Write-what-where Condition
CWE-364: Signal Handler Race Condition
CWE-416: Use After Free
CWE-666: Operation on Resource in Wrong Phase of Lifetime
CWE-672: Operation on a Resource after Expiration or Release
CWE-825: Expired Pointer Dereference
CWE-1341: Multiple Releases of Same Resource or Handle
Visit http://cwe.mitre.org/ for more details.