CWE-825: Expired Pointer Dereference

Description

The product dereferences a pointer that contains a location for memory that was previously valid, but is no longer valid.

Submission Date :

Sept. 22, 2010, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

When a product releases memory, but it maintains a pointer to that memory, then the memory might be re-allocated at a later time. If the original pointer is accessed to read or write data, then this could cause the product to read or modify data that is in use by a different function or process. Depending on how the newly-allocated memory is used, this could lead to a denial of service, information exposure, or code execution.

Example Vulnerable Codes

Example - 1

The following code shows a simple example of a use after free error:



abrt = 1;free(ptr);
logError("operation aborted before commit", ptr);char* ptr = (char*)malloc (SIZE);if (err) {}...if (abrt) {}

When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.

Example - 2

The following code shows a simple example of a double free error:


free(ptr);
char* ptr = (char*)malloc (SIZE);...if (abrt) {}...free(ptr);

Double free vulnerabilities have two common (and sometimes overlapping) causes:

Error conditions and other exceptional circumstancesConfusion over which part of the program is responsible for freeing the memory

Although some double free vulnerabilities are not much more complicated than the previous 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.

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.

Visit http://cwe.mitre.org/ for more details.