界: Code Quality

コードの質が低いと、予測できない動作につながります。ユーザーの視点には、それがしばしば使い勝手の悪さとなって現れます。攻撃者にとっては、予期せぬ方法でシステムにストレスを与える機会となります。

Double Free

Abstract
free() を同じメモリアドレスで 2 回コールすると、Buffer Overflow が発生する可能性があります。
Explanation
free() が同じメモリ アドレスを引数にして複数回コールされた場合、Double Free エラーが発生します。

free() を同じ値で 2 回コールすると、Buffer Overflow が発生する可能性があります。プログラムが free() を同じ引数で 2 回コールすると、プログラムのメモリ管理データ構造が破損します。これにより、プログラムがクラッシュするか、一部の環境では、その後に malloc() を 2 回コールして同じポインタを戻すことになります。malloc() が同じ値を 2 回戻し、その後に攻撃者が、この二重に割り当てられたメモリに書き込まれたデータの制御をプログラムから得た場合、プログラムは Buffer Overflow 攻撃に対して脆弱になります。

例 1: 次のコードは、Double Free の脆弱性が含まれる単純な例です。


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


Double Free の脆弱性のよくある原因には、次の 2 つがあります (この2 つが重なる場合もあります)。

- エラー条件などの例外的状況。

- プログラムのどの部分でメモリを解放するかに関して混乱が発生している。

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