Description
The product does not release or incorrectly releases a resource before it is made available for re-use.
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.
Potential Impact
Availability, Other
DoS: Resource Consumption (Other), Varies by Context
Confidentiality
Read Application Data
Demonstrative Examples
private void processFile(string fName){BufferReader fil = new BufferReader(new FileReader(fName));String line;while ((line = fil.ReadLine()) != null){processLine(line);}}private void processFile(string fName){BufferReader fil = new BufferReader(new FileReader(fName));String line;while ((line = fil.ReadLine()) != null){processLine(line);}fil.Close();}try {Connection con = DriverManager.getConnection(some_connection_string);}catch ( Exception e ) {log( e );}...SqlConnection conn = new SqlConnection(connString);SqlCommand cmd = new SqlCommand(queryString);cmd.Connection = conn;conn.Open();SqlDataReader rdr = cmd.ExecuteReader();HarvestResults(rdr);conn.Connection.Close();...int decodeFile(char* fName) {
char buf[BUF_SZ];FILE* f = fopen(fName, "r");if (!f) {printf("cannot open %s\n", fName);return DECODE_FAIL;}else {
while (fgets(buf, BUF_SZ, f)) {if (!checkChecksum(buf)) {return DECODE_FAIL;}else {decodeBlock(buf);}}
}fclose(f);return DECODE_SUCCESS;
}Mitigations & Prevention
Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, languages such as Java, Ruby, and Lisp perform automatic garbage collection that releases memory for objects that have been deallocated.
It is good practice to be responsible for freeing all resources you allocate and to be consistent with how and where you free memory in a function. If you allocate memory that you intend to free upon completion of the function, you must be sure to free the memory at all exit points for that function including error conditions.
Memory should be allocated/freed using matching functions such as malloc/free, new/delete, and new[]/delete[].
When releasing a complex object or structure, ensure that you properly dispose of all of its member components, not just the object itself.
Detection Methods
- Automated Dynamic Analysis Moderate — This weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The software's operation may slow down, but it should not become unstable, crash
- Manual Dynamic Analysis — Identify error conditions that are not likely to occur during normal usage and trigger them. For example, run the product under low memory conditions, run with insufficient privileges or permissions, interrupt a transaction before it is completed, or disable connectivity to basic network services su
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-1999-1127 | Does not shut down named pipe connections if malformed data is sent. |
| CVE-2001-0830 | Sockets not properly closed when attacker repeatedly connects and disconnects from server. |
| CVE-2002-1372 | Chain: Return values of file/socket operations are not checked (CWE-252), allowing resultant consumption of file descriptors (CWE-772). |
Related Weaknesses
Taxonomy Mappings
- PLOVER: — Improper resource shutdown or release
- 7 Pernicious Kingdoms: — Unreleased Resource
- OWASP Top Ten 2004: A9 — Denial of Service
- CERT C Secure Coding: FIO42-C — Close files when they are no longer needed
- CERT C Secure Coding: MEM31-C — Free dynamically allocated memory when no longer needed
- The CERT Oracle Secure Coding Standard for Java (2011): FIO04-J — Release resources when they are no longer needed
- Software Fault Patterns: SFP14 — Failure to release resource
Frequently Asked Questions
What is CWE-404?
CWE-404 (Improper Resource Shutdown or Release) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Class-level weakness. The product does not release or incorrectly releases a resource before it is made available for re-use.
How can CWE-404 be exploited?
Attackers can exploit CWE-404 (Improper Resource Shutdown or Release) to dos: resource consumption (other), varies by context. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-404?
Key mitigations include: Use a language that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid. For example, languages such as Java, Ruby, and Lisp perfor
What is the severity of CWE-404?
CWE-404 is classified as a Class-level weakness (High abstraction). It has been observed in 3 real-world CVEs.