對象為成員變量分配了內存,但在其 dealloc() 方法中未能將其釋放。
Memory leak 的產生有兩個常見(有時候這兩個原因會同時發生作用)原因:
- 錯誤狀況及其他異常情況。
— 不清楚由程序的哪一部分負責釋放內存。
大多數 memory leak 會導致一個常規軟件可靠性問題,但如果攻擊者能夠蓄意觸發 memory leak,他就可能會通過引發程序崩潰來發起一個 denial of service 攻擊,或者是利用因內存低的情況 [1] 所引發的其他意外的程序行為。
例 1:該 Objective-C 對象通過 init() 方法分配內存,但無法通過 deallocate() 方法將其釋放,導致 memory leak:
- (void)init
{
myVar = [NSString alloc] init];
...
}
- (void)dealloc
{
[otherVar release];
}
[1] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A9 Application Denial of Service
[2] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP6080 CAT II
[3] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP6080 CAT II
[4] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 401
[5] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.9