界: Code Quality

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

4 找到的項目
弱點
Abstract
未定義函數運作方式,除非將其控制參數設為某特定值。
Explanation
Linux Standard Base Specification 2.0.1 for libc 對一些內部函數的引數設定某些限制 [1]。如果函數參數不滿足這些約束條件的話,就無法成功定義此函數運作方式。


在下列 File System 函數中,必須將 1 值傳送給第一個參數 (版本編號):


__xmknod


在下列寬字元字串函數中,必須將 2 值傳送給第三個參數 (群組引數):


__wcstod_internal
__wcstof_internal
_wcstol_internal
__wcstold_internal
__wcstoul_internal


在下列 File System 函數中,必須將 3 值傳送給第一個參數 (版本編號):


__xstat
__lxstat
__fxstat
__xstat64
__lxstat64
__fxstat64

References
[1] The Linux Standard Base Specification 2.0.1, Interfaces Definitions for libc.
[2] Standards Mapping - Common Weakness Enumeration CWE ID 475
[3] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 1.3
desc.semantic.cpp.undefined_behavior
Abstract
應用程式使用解除參照系統 FILE 物件的指派。
Explanation
根據使用中的特定 C 編譯器,系統 FILE 物件的位址可能對於將 FILE 物件用作串流十分重要。使用沒有關聯位址的 FILE 物件副本可能會導致未定義的行為,進而導致潛在的系統資訊洩漏、系統當機或惡意動作執行者能夠自行讀取或編輯檔案。

範例 1:以下程式碼顯示一個使用值解除參照及複製的系統 FILE 物件。


FILE *sysfile = fopen(test.file, "w+");
FILE insecureFile = *sysfile;


由於指派 insecureFile 時將 sysfile 解除參照,使用 insecureFile 便可能會導致各種問題。
References
[1] Standards Mapping - Common Weakness Enumeration CWE ID 706
[2] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 22.5
desc.structural.cpp.undefined_behavior_file_pointer_dereference
Abstract
應用程式在關閉的檔案指標上使用檔案操作。
Explanation
在系統 FILE 物件的關聯串流關閉後對其執行檔案操作,會導致未定義的行為。根據使用中的特定 C 編譯器,檔案操作可能會導致系統當機,甚至可能導致相同或不同檔案的可能修改或讀取。

範例 1:以下程式碼顯示在關閉對應的串流後嘗試讀取系統 FILE 物件。


FILE *sysfile = fopen(test.file, "r+");
res = fclose(sysfile);
if(res == 0){
printf("%c", getc(sysfile));
}


由於 getc() 函數在 sysfile 的檔案串流關閉之後執行,因此 getc() 導致未定義的行為,並可能導致系統當機或導致相同或不同檔案的可能修改或讀取。
References
[1] Standards Mapping - Common Weakness Enumeration CWE ID 910
[2] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 22.6
desc.controlflow.cpp.undefined_behavior_file_pointer_use_after_close
Abstract
明確地刪除受管理的指標會造成程式當機或功能異常。
Explanation
刪除受管理的指標會造成程式當機,或在之後當指標管理程式碼假設該指標有效時造成錯誤。以下範例說明這個錯誤。


std::auto_ptr<foo> p(new foo);
foo* rawFoo = p.get();
delete rawFoo;


此規則唯一的異常是,當受管理的指標類別支援「分離」操作,會讓程式設計師得以控制特定指標的記憶體管理。如果程式在呼叫 delete 前從管理類別分離該指標,管理類別未來將不會再使用該指標。
References
[1] Standards Mapping - Common Weakness Enumeration CWE ID 730
[2] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[3] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[4] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[5] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[7] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[28] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[29] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.cpp.redundant_delete