CWE-672: Operation on a Resource after Expiration or Release
Description
The product uses, accesses, or otherwise operates on a resource after that resource has been expired, released, or revoked.
Submission Date :
April 11, 2008, midnight
Modification Date :
2023-10-26 00:00:00+00:00
Organization :
MITRE
Example - 1
The following code shows a simple example of a use after free error: When an error occurs, the pointer is immediately freed. However, this pointer is later incorrectly used in the logError function.
abrt = 1;free(ptr);
logError("operation aborted before commit", ptr);char* ptr = (char*)malloc (SIZE);if (err) {}...if (abrt) {}
Example - 2
The following code shows a simple example of a double free error: Double free vulnerabilities have two common (and sometimes overlapping) causes: 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.
free(ptr);
char* ptr = (char*)malloc (SIZE);...if (abrt) {}...free(ptr);
Example - 3
In the following C/C++ example the method processMessage is used to process a message received in the input array of char arrays. The input message array contains two char arrays: the first is the length of the message and the second is the body of the message. The length of the message is retrieved and used to allocate enough memory for a local char array, messageBody, to be created for the message body. The messageBody is processed in the method processMessageBody that will return an error if an error occurs while processing. If an error occurs then the return result variable is set to indicate an error and the messageBody char array memory is released using the method free and an error message is sent to the logError method.
result = ERROR;free(messageBody);messageBody = (char*)malloc(length*sizeof(char));messageBody = &message[1][0];int success = processMessageBody(messageBody);if (success == ERROR) {}
printf("Unable to process message; invalid message length");result = FAIL;
logError("Error processing message", messageBody);
int result = SUCCESS;int length = getMessageLength(message[0]);char *messageBody;if ((length > 0) && (length < MAX_MESSAGE_SIZE)) {}else {}if (result == ERROR) {}return result;#define FAIL 0#define SUCCESS 1#define ERROR -1#define MAX_MESSAGE_SIZE 32int processMessage(char **message){}
However, the call to the method logError includes the messageBody after the memory for messageBody has been released using the free method. This can cause unexpected results and may lead to system crashes. A variable should never be used after its memory resources have been released.
result = ERROR;logError("Error processing message", messageBody);free(messageBody);
...messageBody = (char*)malloc(length*sizeof(char));messageBody = &message[1][0];int success = processMessageBody(messageBody);if (success == ERROR) {}...
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-298: Improper Validation of Certificate Expiration
CWE-324: Use of a Key Past its Expiration Date
CWE-415: Double Free
CWE-416: Use After Free
CWE-562: Return of Stack Variable Address
CWE-613: Insufficient Session Expiration
CWE-666: Operation on Resource in Wrong Phase of Lifetime
CWE-825: Expired Pointer Dereference
CWE-826: Premature Release of Resource During Expected Lifetime
CWE-910: Use of Expired File Descriptor
CWE-911: Improper Update of Reference Count
CWE-1341: Multiple Releases of Same Resource or Handle
Visit http://cwe.mitre.org/ for more details.