CWE-404: Improper Resource Shutdown or Release
Description
The product does not release or incorrectly releases a resource before it is made available for re-use.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-10-26 00:00:00+00:00
Organization :
MITRE
Extended Description
When a resource is created or allocated, the developer is responsible for properly releasing the resource as well as accounting for all potential paths of expiration or invalidation, such as a set period of time or revocation.
Example - 1
This code attempts to open a connection to a database and catches any exceptions that may occur. If an exception occurs after establishing the database connection and before the same connection closes, the pool of database connections may become exhausted. If the number of available connections is exceeded, other users cannot access this resource, effectively denying access to the application.Connection con = DriverManager.getConnection(some_connection_string);
log( e );try {}catch ( Exception e ) {}
Example - 2
Under normal conditions the following C# code executes a database query, processes the results returned by the database, and closes the allocated SqlConnection object. But if an exception occurs while executing the SQL or processing the results, the SqlConnection object is not closed. If this happens often enough, the database will run out of available cursors and not be able to execute any more SQL queries.
...SqlConnection conn = new SqlConnection(connString);SqlCommand cmd = new SqlCommand(queryString);cmd.Connection = conn;conn.Open();SqlDataReader rdr = cmd.ExecuteReader();HarvestResults(rdr);conn.Connection.Close();...
Example - 3
The following C function does not close the file handle it opens if an error occurs. If the process is long-lived, the process can run out of file handles.
printf("cannot open %s\n", fName);return DECODE_FAIL;
return DECODE_FAIL;
decodeBlock(buf);if (!checkChecksum(buf)) {}else {}while (fgets(buf, BUF_SZ, f)) {}
char buf[BUF_SZ];FILE* f = fopen(fName, "r");if (!f) {}else {}fclose(f);return DECODE_SUCCESS;int decodeFile(char* fName) {}
Example - 4
In this example, the program does not use matching functions such as malloc/free, new/delete, and new[]/delete[] to allocate/deallocate the resource.void foo();
int *ptr;ptr = (int*)malloc(sizeof(int));delete ptr;class A {};void A::foo(){}
Example - 5
In this example, the program calls the delete[] function on non-heap memory.void foo(bool);
11,22
p = new int[2];
int localArray[2] = {};int *p = localArray;if (heap){}delete[] p;class A{};void A::foo(bool heap) {}
Example - 6
The following method never closes the new file handle. Given enough time, the Finalize() method for BufferReader should eventually call Close(), but there is no guarantee as to how long this action will take. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, the Operating System could use up all of the available file handles before the Close() function is called. The good code example simply adds an explicit call to the Close() function when the system is done using the file. Within a simple example such as this the problem is easy to see and fix. In a real system, the problem may be considerably more obscure.
processLine(line);BufferReader fil = new BufferReader(new FileReader(fName));String line;while ((line = fil.ReadLine()) != null){}private void processFile(string fName){}
processLine(line);
BufferReader fil = new BufferReader(new FileReader(fName));String line;while ((line = fil.ReadLine()) != null){}fil.Close();private void processFile(string fName){}
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-239: Failure to Handle Incomplete Element
CWE-299: Improper Check for Certificate Revocation
CWE-401: Missing Release of Memory after Effective Lifetime
CWE-405: Asymmetric Resource Consumption (Amplification)
CWE-459: Incomplete Cleanup
CWE-619: Dangling Database Cursor ('Cursor Injection')
CWE-664: Improper Control of a Resource Through its Lifetime
CWE-761: Free of Pointer not at Start of Buffer
CWE-762: Mismatched Memory Management Routines
CWE-763: Release of Invalid Pointer or Reference
CWE-772: Missing Release of Resource after Effective Lifetime
CWE-775: Missing Release of File Descriptor or Handle after Effective Lifetime
CWE-1266: Improper Scrubbing of Sensitive Data from Decommissioned Device
Visit http://cwe.mitre.org/ for more details.