CWE-772 对已超过有效生命周期的资源丧失索引

Missing Release of Resource after Effective Lifetime

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: High

基本描述

The software does not release a resource after its effective lifetime has ended, i.e., after the resource is no longer needed.

扩展描述

When a resource is not released after use, it can allow attackers to cause a denial of service by causing the allocation of resources without triggering their release. Frequently-affected resources include memory, CPU, disk space, power or battery, etc.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 404 cwe_View_ID: 699 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 404 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 404 cwe_View_ID: 1003 cwe_Ordinal: Primary

适用平台

Paradigm: {'cwe_Name': 'Mobile', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
Availability DoS: Resource Consumption (Other) An attacker that can influence the allocation of resources that are not properly released could deplete the available resource pool and prevent all other processes from accessing the same type of resource.

可能的缓解方案

MIT-3 Requirements

策略: Language Selection

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.

Implementation

策略:

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.

MIT-47 ['Operation', 'Architecture and Design']

策略: Resource Limitation

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 resources to privileged users; alternately, begin releasing resources for less-privileged users. While this mitigation may protect the system from attack, it will not necessarily stop attackers from adversely impacting other users. Ensure that the application performs the appropriate error checks and error handling in case resources become unavailable (CWE-703).

示例代码

The following code attempts to process a file by reading it in line by line until the end has been reached.

bad Java

private void processFile(string fName)
{
BufferReader in = new BufferReader(new FileReader(fName));
String line;
while ((line = in.ReadLine()) != null)
{
processLine(line);
}
}

The problem with the above code is that it never closes the file handle it opens. The Finalize() method for BufferReader eventually calls Close(), but there is no guarantee as to how long it will take before the Finalize() method is invoked. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, this can result in the VM using up all of its available file handles.

The following code attempts to open a new connection to a database, process the results returned by the database, and close the allocated SqlConnection object.

bad C#

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(queryString);
cmd.Connection = conn;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
HarvestResults(rdr);
conn.Connection.Close();

The problem with the above code is that 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.

The following method never closes the file handle it opens. The Finalize() method for StreamReader eventually calls Close(), but there is no guarantee as to how long it will take before the Finalize() method is invoked. In fact, there is no guarantee that Finalize() will ever be invoked. In a busy environment, this can result in the VM using up all of its available file handles.

bad Java

private void processFile(string fName) {
StreamWriter sw = new StreamWriter(fName);
string line;
while ((line = sr.ReadLine()) != null){
processLine(line);
}
}

This code attempts to open a connection to a database and catches any exceptions that may occur.

bad Java

try {
Connection con = DriverManager.getConnection(some_connection_string);
}
catch ( Exception e ) {
log( e );
}

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.

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.

bad C#

...
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(queryString);
cmd.Connection = conn;
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
HarvestResults(rdr);
conn.Connection.Close();
...

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.

bad C

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;
}

分析过的案例

标识 说明 链接
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. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-0897
CVE-2001-0830 Sockets not properly closed when attacker repeatedly connects and disconnects from server. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2001-0830
CVE-1999-1127 Does not shut down named pipe connections if malformed data is sent. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-1999-1127
CVE-2009-2858 Chain: memory leak (CWE-404) leads to resource exhaustion. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2858
CVE-2009-2054 Product allows exhaustion of file descriptors when processing a large number of TCP packets. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2054
CVE-2008-2122 Port scan triggers CPU consumption with processes that attempt to read data from closed sockets. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2122
CVE-2007-4103 Product allows resource exhaustion via a large number of calls that do not complete a 3-way handshake. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-4103
CVE-2002-1372 Return values of file/socket operations not checked, allowing resultant consumption of file descriptors. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2002-1372

Notes

Maintenance "Resource exhaustion" (CWE-400) is currently treated as a weakness, although it is more like a category of weaknesses that all have the same type of consequence. While this entry treats CWE-400 as a parent in view 1000, the relationship is probably more appropriately described as a chain. Theoretical Vulnerability theory is largely about how behaviors and resources interact. "Resource exhaustion" can be regarded as either a consequence or an attack, depending on the perspective. This entry is an attempt to reflect one of the underlying weaknesses that enable these attacks (or consequences) to take place.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
Software Fault Patterns SFP14 Failure to release resource
CERT C Secure Coding FIO42-C CWE More Abstract Close files when they are no longer needed
CERT C Secure Coding MEM31-C CWE More Abstract Free dynamically allocated memory when no longer needed
OMG ASCSM ASCSM-CWE-772
OMG ASCRM ASCRM-CWE-772

相关攻击模式

  • CAPEC-469

引用