Description
The product does not release a resource after its effective lifetime has ended, i.e., after the resource is no longer needed.
Potential Impact
Availability
DoS: Resource Consumption (Other), DoS: Resource Consumption (Memory), DoS: Resource Consumption (CPU)
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();}SqlConnection conn = new SqlConnection(connString);SqlCommand cmd = new SqlCommand(queryString);cmd.Connection = conn;conn.Open();SqlDataReader rdr = cmd.ExecuteReader();HarvestResults(rdr);conn.Connection.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();...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 resources in a function. If you allocate resources that you intend to free upon completion of the function, you must be sure to free the resources at all exit points for that function including error conditions.
Use resource-limiting settings provided by the operating system or environment. For example, when managing system resources in POSIX, setrlimit() can be used to set limits for certain types of resources, and getrlimit() can determine how many resources are available. However, these functions are not available on all operating systems. When the current levels get close to the maximum that is defined for the application (see CWE-770), then limit the allocation of further resour
Detection Methods
- 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-2007-0897 | Chain: anti-virus product encounters a malformed file but returns from a function without closing a file descriptor (CWE-775) leading to file descriptor consumption (CWE-400) and failed scans. |
| CVE-2001-0830 | Sockets not properly closed when attacker repeatedly connects and disconnects from server. |
| CVE-1999-1127 | Does not shut down named pipe connections if malformed data is sent. |
| CVE-2009-2858 | Chain: memory leak (CWE-404) leads to resource exhaustion. |
| CVE-2009-2054 | Product allows exhaustion of file descriptors when processing a large number of TCP packets. |
| CVE-2008-2122 | Port scan triggers CPU consumption with processes that attempt to read data from closed sockets. |
| CVE-2007-4103 | Product allows resource exhaustion via a large number of calls that do not complete a 3-way handshake. |
| 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
- 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
- OMG ASCSM: ASCSM-CWE-772 —
- OMG ASCRM: ASCRM-CWE-772 —
- Software Fault Patterns: SFP14 — Failure to Release Resource
Frequently Asked Questions
What is CWE-772?
CWE-772 (Missing Release of Resource after Effective Lifetime) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. The product does not release a resource after its effective lifetime has ended, i.e., after the resource is no longer needed.
How can CWE-772 be exploited?
Attackers can exploit CWE-772 (Missing Release of Resource after Effective Lifetime) to dos: resource consumption (other), dos: resource consumption (memory), dos: resource consumption (cpu). This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-772?
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-772?
CWE-772 is classified as a Base-level weakness (Medium abstraction). It has been observed in 8 real-world CVEs.