內存已分配,但永遠不會釋放。
Memory leak 的產生有兩個常見(有時候這兩個原因會同時發生作用)原因:
- 錯誤狀況及其他異常情況。
— 不清楚由程序的哪一部分負責釋放內存。
大多數 memory leak 會導致一個常規軟件可靠性問題,但如果攻擊者能夠蓄意觸發 memory leak,他就可能會通過引發程序崩潰來發起一個 denial of service 攻擊,或者是利用因內存低的情況 [1] 所引發的其他意外的程序行為。
例 1:如果調用 read() 后沒有返回預期的字節數,以下 C 函數將會泄漏已分配的內存塊的信息:
char* getBlock(int fd) {
char* buf = (char*) malloc(BLOCK_SIZE);
if (!buf) {
return NULL;
}
if (read(fd, buf, BLOCK_SIZE) != BLOCK_SIZE) {
return NULL;
}
return buf;
}
[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