CWE-404 不恰当的资源关闭或释放
Improper Resource Shutdown or Release
结构: Simple
Abstraction: Class
状态: Draft
被利用可能性: Medium
基本描述
The program 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.
相关缺陷
-
cwe_Nature: ChildOf cwe_CWE_ID: 664 cwe_View_ID: 1000 cwe_Ordinal: Primary
-
cwe_Nature: PeerOf cwe_CWE_ID: 405 cwe_View_ID: 1000
-
cwe_Nature: CanPrecede cwe_CWE_ID: 619 cwe_View_ID: 1000
-
cwe_Nature: CanPrecede cwe_CWE_ID: 619 cwe_View_ID: 699
适用平台
Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}
常见的影响
范围 | 影响 | 注释 |
---|---|---|
['Availability', 'Other'] | ['DoS: Resource Consumption (Other)', 'Varies by Context'] | Most unreleased resource issues result in general software reliability problems, but if an attacker can intentionally trigger a resource leak, the attacker might be able to launch a denial of service attack by depleting the resource pool. |
Confidentiality | Read Application Data | When a resource containing sensitive information is not correctly shutdown, it may expose the sensitive data in a subsequent allocation. |
检测方法
DM-2 Automated Dynamic Analysis
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, or generate incorrect results.
Resource clean up errors might be detected with a stress-test by calling the software simultaneously from a large number of threads or processes, and look for evidence of any unexpected behavior. The software's operation may slow down, but it should not become unstable, crash, or generate incorrect results.
DM-12 Manual Dynamic Analysis
可能的缓解方案
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 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.
Implementation
策略:
Memory should be allocated/freed using matching functions such as malloc/free, new/delete, and new[]/delete[].
Implementation
策略:
When releasing a complex object or structure, ensure that you properly dispose of all of its member components, not just the object itself.
示例代码
例
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
string line;
while ((line = sr.ReadLine()) != null){
}
}
例
This code attempts to open a connection to a database and catches any exceptions that may occur.
bad Java
}
catch ( Exception 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
FILE f = fopen(fName, "r");
if (!f) {
return DECODE_FAIL;
}
else {
}
else {
}
}
}
fclose(f);
return DECODE_SUCCESS;
}
例
In this example, the program does not use matching functions such as malloc/free, new/delete, and new[]/delete[] to allocate/deallocate the resource.
bad C++
};
void A::foo(){
ptr = (int)malloc(sizeof(int));
delete ptr;
}
例
In this example, the program calls the delete[] function on non-heap memory.
bad C++
};
void A::foo(bool heap) {
};
int *p = localArray;
if (heap){
}
delete[] p;
}
分析过的案例
标识 | 说明 | 链接 |
---|---|---|
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-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-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
分类映射
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
PLOVER | Improper resource shutdown or release | ||
7 Pernicious Kingdoms | Unreleased Resource | ||
OWASP Top Ten 2004 | A9 | CWE More Specific | Denial of Service |
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 |
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 |
相关攻击模式
- CAPEC-125
- CAPEC-130
- CAPEC-131
- CAPEC-494
- CAPEC-495
- CAPEC-496
引用
文章来源于互联网:scap中文网
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论