界: Code Quality

程式碼品質不佳,會導致無法預料的行為。從使用者的角度來看,這通常表現為可用性不佳。對於攻擊者而言,這提供了以意想不到的方式向系統施加壓力的機會。

1 找到的項目
弱點
Abstract
在同一個記憶體位址呼叫 free() 兩次會導致 Buffer overflow。
Explanation
當不止一次地在同一個記憶體位址將 free() 做為一個引數來呼叫,就會出現 double free (釋放相同的記憶體空間兩次) 錯誤。



以相同值呼叫 free() 兩次,會導致 Buffer overflow。當程式使用相同引數呼叫 free() 兩次,就會毀損程式中的記憶體管理資料結構。這樣的毀損會使程式當機,或者在某些情況下,還會導致之後兩次的 malloc() 呼叫回傳相同的指標。如果 malloc() 兩次回傳值都相同,且程式之後讓攻擊者控制整個已經寫入雙倍分配記憶體的資料,此程式就變得容易受到 Buffer overflow 攻擊。

範例 1:以下程式碼顯示關於 double free (釋放相同的記憶體空間兩次) 弱點的簡單範例。


char* ptr = (char*)malloc (SIZE);
...
if (abrt) {
free(ptr);
}
...
free(ptr);


Double free 弱點的產生有兩個常見的原因 (有時候這兩個原因會同時出現):

- 錯誤條件以及其他異常情況。

- 對於程式中負責釋放記憶體的部分混淆了。

雖然某些 double free (釋放相同的記憶體空間兩次) 弱點的複雜程度不會比之前這個範例高出太多,但是其中大部分弱點分散在好幾百行的程式碼中,甚至是不同的檔案中。程式設計師似乎特別容易受到多次釋放全域變數的影響。
References
[1] J. Koziol et al. The Shellcoder's Handbook: Discovering and Exploiting Security Holes John Wiley & Sons
[2] Standards Mapping - Common Weakness Enumeration CWE ID 415
[3] Standards Mapping - Common Weakness Enumeration Top 25 2019 [1] CWE ID 119
[4] Standards Mapping - Common Weakness Enumeration Top 25 2020 [5] CWE ID 119
[5] Standards Mapping - Common Weakness Enumeration Top 25 2021 [17] CWE ID 119
[6] Standards Mapping - Common Weakness Enumeration Top 25 2022 [19] CWE ID 119
[7] Standards Mapping - Common Weakness Enumeration Top 25 2023 [17] CWE ID 119
[8] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-002824
[9] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[10] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 21.3
[11] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 18-4-1
[12] Standards Mapping - NIST Special Publication 800-53 Revision 4 SI-16 Memory Protection (P1)
[13] Standards Mapping - NIST Special Publication 800-53 Revision 5 SI-16 Memory Protection
[14] Standards Mapping - OWASP Top 10 2004 A5 Buffer Overflow
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.5
[16] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 Requirement 6.5.2
[17] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.2
[18] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.2
[19] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.2
[20] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.2
[21] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[22] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[23] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection
[24] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection
[25] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3590.1 CAT I
[26] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3590.1 CAT I
[27] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3590.1 CAT I
[28] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3590.1 CAT I
[29] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3590.1 CAT I
[30] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3590.1 CAT I
[31] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3590.1 CAT I
[32] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002590 CAT I
[33] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002590 CAT I
[34] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002590 CAT I
[35] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002590 CAT I
[36] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002590 CAT I
[37] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002590 CAT I
[38] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002590 CAT I
[39] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002590 CAT I
[40] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002590 CAT I
[41] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002590 CAT I
[42] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002590 CAT I
[43] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002590 CAT I
[44] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002590 CAT I
[45] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002590 CAT I
desc.controlflow.cpp.double_free