界: Code Quality

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

93 找到的項目
弱點
Abstract
偵測到 PendingIntent 的旗標值設為 FLAG_MUTABLE。建立的 Pending Intent 具有 FLAG_MUTABLE 旗標值時,很容易受到下游設定的未指定 Intent 欄位的影響,這可能會修改 Intent 的能力並使系統容易受到攻擊。
Explanation
若允許在建立 PendingIntent 後修改其底層 Intent,可能會使系統容易受到攻擊。這主要取決於底層 Intent 的整體能力。在大多數情況下,最佳方式是將 PendingIntent 旗標設為 FLAG_IMMUTABLE 來防止潛在問題。

範例 1:以下包括建立一個使用 FLAG_MUTABLE 旗標值建立 PendingIntent


...
val intent_flag_mut = Intent(Intent.ACTION_GTALK_SERVICE_DISCONNECTED, Uri.EMPTY, this, DownloadService::class.java)
val flag_mut = PendingIntent.FLAG_MUTABLE

val pi_flagmutable = PendingIntent.getService(
this,
0,
intent_flag_mut,
flag_mut
)
...
References
[1] Remediation for Implicit PendingIntent Vulnerability
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Microsoft Azure Foundations Benchmark complete
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Cloud Computing Platform Benchmark partial
[7] Standards Mapping - CIS Google Kubernetes Engine Benchmark integrity
[8] Standards Mapping - CIS Kubernetes Benchmark partial
[9] Standards Mapping - Common Weakness Enumeration CWE ID 99
[10] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[11] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[12] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[13] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[14] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.1
[16] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 Requirement 6.3.1.1, Requirement 6.5.4
[17] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 Requirement 6.5.8
[18] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.8
[19] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.8
[20] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.8
[21] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.8
[22] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[23] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[39] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[40] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[44] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[45] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.java.intent_manipulation_mutable_pending_intent
Abstract
記憶體被分配後將永遠不會被釋放。
Explanation
記憶體洩露有兩個常見的原因 (有時這兩個原因會一起出現):

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

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

大部分記憶體洩漏會導致一般軟體可靠性問題,但是如果攻擊者能夠蓄意觸發資源的洩漏,攻擊者就可能會發動 Denial of Service 攻擊 (藉由損壞程式),或利用因記憶體不足的狀況 [1] 導致的其他非預期程式行為。

範例 1:以下 C 函數將會洩漏已分配的記憶體區塊的資訊,當程式呼叫 read() 時未能回傳預期的位元組數:


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;
}
References
[1] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
desc.controlflow.cpp.memory_leak
Abstract
記憶體已配置但從未釋放。
Explanation
記憶體洩漏有兩個常見且有時重疊的原因:

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

- 不確定程式的哪一部分負責釋放記憶體。

大多數的記憶體洩漏會導致一般的軟體可靠性問題,但是如果攻擊者蓄意觸發記憶體洩漏,則攻擊者可能可以發動 Denial of Service 攻擊 (透過使程式當機的方式),或利用由記憶不足狀況導致的其他意外程式行為[1]。

範例 1:如果發生錯誤,以下 Micro Focus COBOL 程式將會洩漏配置的記憶體區塊:


CALL "CBL_ALLOC_MEM"
USING mem-pointer
BY VALUE mem-size
BY VALUE flags
RETURNING status-code
END-CALL

IF status-code NOT = 0
DISPLAY "Error!"
GOBACK
ELSE
SET ADDRESS OF mem TO mem-pointer
END-IF

PERFORM write-data
IF ws-status-code NOT = 0
DISPLAY "Error!"
GOBACK
ELSE
DISPLAY "Success!"
END-IF

CALL "CBL_FREE_MEM"
USING BY VALUE mem-pointer
RETURNING status-code
END-CALL

GOBACK
.
References
[1] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
desc.controlflow.cobol.memory_leak
Abstract
物件會為成員變數分配記憶體但無法在其 dealloc() 方法中釋放記憶體。
Explanation
記憶體洩露有兩個常見的原因 (有時這兩個原因會一起出現):

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

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

大部分記憶體洩漏會導致一般軟體可靠性問題,但是如果攻擊者能夠蓄意觸發資源的洩漏,攻擊者就可能會發動阻斷服務攻擊 (藉由損壞程式),或利用因記憶體不足的狀況 [1] 導致的其他非預期程式行為。

範例 1:Objective-C 物件在 init() 方法中分配了記憶體,但無法在 deallocate() 方法中將其釋放,導致記憶體洩露:


- (void)init
{
myVar = [NSString alloc] init];
...
}

- (void)dealloc
{
[otherVar release];
}
References
[1] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
desc.structural.objc.memory_leak
Abstract
程式重新調整分配記憶體區塊的大小。若重新調整大小失敗,就會洩露原始區塊。
Explanation
記憶體洩露有兩個常見的原因 (有時這兩個原因會一起出現):

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

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

大部分記憶體洩漏會導致一般軟體可靠性問題,但是如果攻擊者能夠蓄意觸發資源的洩漏,攻擊者就可能會發動 Denial of Service 攻擊 (藉由損壞程式),或利用因記憶體不足的狀況 [1] 導致的其他非預期程式行為。

範例 1:以下的 C 函數在呼叫 realloc() 調整原始分配大小失敗時,洩露了分配記憶體區塊。


char* getBlocks(int fd) {
int amt;
int request = BLOCK_SIZE;
char* buf = (char*) malloc(BLOCK_SIZE + 1);
if (!buf) {
goto ERR;
}
amt = read(fd, buf, request);
while ((amt % BLOCK_SIZE) != 0) {
if (amt < request) {
goto ERR;
}
request = request + BLOCK_SIZE;
buf = realloc(buf, request);
if (!buf) {
goto ERR;
}
amt = read(fd, buf, request);
}

return buf;

ERR:
if (buf) {
free(buf);
}
return NULL;
}
References
[1] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 401
[7] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[8] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[9] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 21.3
[10] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 18-4-1
[11] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[12] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[13] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[14] Standards Mapping - OWASP Mobile Application Security Verification Standard 2.0 MASVS-STORAGE-2
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[16] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[37] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[38] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.cpp.memory_leak_reallocation
Abstract
程式會調整配置的記憶體區塊大小。如果調整大小失敗,就會洩漏原始區塊。
Explanation
記憶體洩漏有兩個常見且有時重疊的原因:

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

- 不確定程式的哪一部分負責釋放記憶體。

大多數的記憶體洩漏會導致一般的軟體可靠性問題,但是如果攻擊者蓄意觸發記憶體洩漏,則攻擊者可能可以發動 Denial of Service 攻擊 (透過使程式當機的方式),或利用由記憶不足狀況導致的其他意外程式行為[1]。

範例 1:如果對 realloc() 的呼叫未能調整原始配置的大小,則以下 Micro Focus COBOL 程式就會洩漏配置的記憶體區塊。


CALL "malloc" USING
BY VALUE mem-size
RETURNING mem-pointer
END-CALL

ADD 1000 TO mem-size

CALL "realloc" USING
BY VALUE mem-pointer
BY VALUE mem-size
RETURNING mem-pointer
END-CALL

IF mem-pointer <> null
CALL "free" USING
BY VALUE mem-pointer
END-CALL
END-IF
References
[1] J. Whittaker and H. Thompson How to Break Software Security Addison Wesley
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 401
[7] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[8] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[9] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 21.3
[10] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C++ Guidelines 2008 Rule 18-4-1
[11] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[12] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[13] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[14] Standards Mapping - OWASP Mobile Application Security Verification Standard 2.0 MASVS-STORAGE-2
[15] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[16] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[37] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[38] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.controlflow.cobol.memory_leak_reallocation
Abstract
程式可能會解除參照 Null 指標,因此造成 NullException
Explanation
Null 指標錯誤通常是由於一個或者多個程式設計人員的假設被破解而造成的。

大部分的 Null 指標問題都會導致一般軟體可靠性問題,但是如果攻擊者蓄意觸發 Null 指標解除參照的話,攻擊者就可能會使用引發的異常來略過安全性邏輯,或是使應用程式洩漏出除錯資訊,這些資訊對於計畫後續攻擊很有價值。

範例 1:在以下程式碼中,程式設計人員假設系統永遠會定義一個名為「cmd」的屬性。如果攻擊者可以控制程式環境,使「cmd」變成未定義狀態,那麼程式會在嘗試呼叫 Trim() 方法時拋出一個 Null 指標異常。


string cmd = null;
...
cmd = Environment.GetEnvironmentVariable("cmd");
cmd = cmd.Trim();
desc.controlflow.dotnet.null_dereference
Abstract
程式可能解除參照一個 Null 指標,造成分段錯誤。
Explanation
Null 指標異常通常是由於一個或者多個程式設計師的假設遭到破解而造成的。此問題至少有三項特點:解除參照之後檢查、檢查後解除參照及儲存後解除參照。當程式在檢查指標是否為 null 之前先解除參照可能為 null 的指標,將會造成「解除參照之後檢查」的錯誤。當程式執行明確的 null 檢查,但仍繼續解除參照已知為 null 的指標時,將會造成「檢查後解除參照」的錯誤。這種類型的錯誤通常是打字錯誤或程式設計師的疏忽所造成。當程式明確的將某指標設定為 null,卻在之後解除參考該指標,則會發生儲存後解除參照錯誤。這種錯誤通常是由於程式設計師在宣告變數時將變數初始化為 null 所致。

大部分的 Null 指標問題都會導致一般軟體可靠性問題,但是如果攻擊者蓄意觸發 Null 指標解除參照的話,攻擊者就可能會使用引發的異常來略過安全性邏輯以掛載 Denial of Service 攻擊,或是使應用程式洩漏出除錯資訊,這些資訊對於計畫後續攻擊很有價值。

範例 1:在以下程式碼中,程式設計師假設變數 ptr 不是 NULL。當程式設計師取消參照指標時,這個假設就變得清楚。當程式設計師檢查 ptrNULL 時,這個假設其實就出現其矛盾之處。若在 if 指令中檢查 ptr 時,其可為 NULL,則解除參照時也可為 NULL,並產生分段錯誤。


ptr->field = val;
...
if (ptr != NULL) {
...
}
範例 2:在以下程式碼中,程式設計師確定變數 ptrNULL,並在之後以錯誤的方式解除參照。若在 if 陳述式中檢查 ptr 時,其非 NULL,就會發生 null 解除參照,從而導致分段錯誤。


if (ptr == null) {
ptr->field = val;
...
}
範例 3:在以下程式碼中,程式設計師忘記了字串 '\0' 實際上是 0 還是 NULL,因此解除參照 Null 指標,並導致分段錯誤。


if (ptr == '\0') {
*ptr = val;
...
}
範例 4:在以下程式碼中,程式設計師明確地將變數 ptr 設定為 NULL。之後,程式設計師先解除參照 ptr,再檢查物件是否為 null 值。


*ptr = NULL;
...
ptr->field = val;
...
}
desc.controlflow.cpp.null_dereference
Abstract
程式可能會解除參照 Null 指標,因此造成 NullPointerException
Explanation
Null 指標錯誤通常是由於一個或者多個程式設計人員的假設被破解而造成的。

大部分的 Null 指標問題都會導致一般軟體可靠性問題,但是如果攻擊者蓄意觸發 Null 指標解除參照的話,攻擊者就可能會使用引發的異常來略過安全性邏輯,或是使應用程式洩漏出除錯資訊,這些資訊對於計畫後續攻擊很有價值。

範例:在以下程式碼中,程式設計人員假設系統永遠會定義一個名為「cmd」的屬性。如果攻擊者可以控制程式環境,使「cmd」變成未定義狀態,那麼程式會在嘗試呼叫 trim() 方法時拋出一個 Null 指標異常。


String val = null;
...
cmd = System.getProperty("cmd");
if (cmd)
val = util.translateCommand(cmd);
...
cmd = val.trim();
desc.controlflow.java.null_dereference
Abstract
使用不推薦或過時的函數可能表示有程式碼被忽略。
Explanation
一般而言,隨著程式語言的演變,有時有些方法可能會過時,原因如下:

- 語言進化
- 更加了解應如何有效且安全地執行作業

- 管理某些特定作業的慣例發生變化

在程式語言中,舊陳述式會被新陳述式所取代,而新陳述式會以不同但更好的方式執行同樣的任務。

特別是,SAP ABAP 已發展為包含 ABAP 物件 (ABAP 的物件導向擴充) 並可在與 Unicode 相容的環境中操作。因此,類別或 Unicode 程式中執行的語法更嚴格。過時的建構仍然可用,原因僅僅是與舊版本相容,但只能在類別外部或者非 Unicode 程式中使用。所有過時的語言元素具有替代建構,用以改善程式的效率及可讀性。過時語法中許多隱含、不明確的類型/長度/記憶體規格必須在新語法中以更加精確而明確的方式指定。建議採用新語法,以使程式更易理解、更加可靠、更易維護。


並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
desc.semantic.abap.obsolete
Abstract
使用不推薦或過時的函數可能表示有程式碼被忽略。
Explanation
隨著程式語言的發展,函數遭淘汰的原因有以下幾點:

- 語言進化
- 更加瞭解應如何有效且安全地執行作業

- 管理某些特定作業的慣例發生變化


在程式語言中,舊函數會被新函數所取代,而新函數會以不同但更好的方式執行同樣的任務。
範例:以下程式碼建構了一個新的 SqlClientPermission 物件,其規定使用者可連接到資料庫的方式。在此範例中,程式把 false 作為第二個參數傳遞給構造函數,控制使用者是否可使用空白密碼進行連接。若傳送的參數值為 false,表示不允許密碼為空白。


...
SCP = new SqlClientPermission(pstate, false);
...


不過,因為作為第一個參數傳遞的 PermissionState 物件代替了所有傳遞至第二個參數的值,因此構造函數允許使用空白密碼進行資料庫連線,這與第二個引數互相矛盾。若要禁止使用空白密碼,程式應該把 PermissionState.None 傳遞給建構函數的第一個參數。因為其功能的不明確,SqlClientPermission 構造函數的雙參數版本已遭反對,改用單參數版本,單參數版本可提供與雙參數版本相同的功能,但是避免了誤譯的風險。

並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
desc.semantic.dotnet.obsolete
Abstract
使用不推薦或過時的函數可能表示有程式碼被忽略。
Explanation
隨著程式語言的發展,函數遭淘汰的原因有以下幾點:

- 語言進化。
- 對操作的有效性、安全性有更深一步的瞭解。
- 對操作管理規則進行的修改。

在編程語言中,舊函數會被新函數所替代,這是因為比起舊函數,新函數更能以我們所期望的方式執行任務。
範例:以下程式碼使用了不推薦使用的函數 getpw() 來檢驗一個純文字密碼是否與使用者加密密碼匹配。如果密碼是有效的,那函數將把 result 設為 1;如果無效,將其設為 0。


...
getpw(uid, pwdline);
for (i=0; i<3; i++){
cryptpw=strtok(pwdline, ":");
pwdline=0;
}
result = strcmp(crypt(plainpw,cryptpw), cryptpw) == 0;
...


雖然此程式碼的行為通常正確無誤,但是從安全性的角度來看,使用 getpw() 函數會產生問題,因為這會導致傳遞至其第二個參數的緩衝區發生溢位。鑒於存在這一弱點,getpw() 已由 getpwuid() 取代,後者會執行與 getpw() 相同的查詢,但是會傳回指向靜態分配之結構的指標以降低風險。

並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
desc.semantic.cpp.obsolete
Abstract
使用不推薦或過時的函數,表示有被閒置的程式碼或使用過時的 ColdFusion 版本。
Explanation
隨著程式語言的演變,有時有些方法可能會過時,原因如下:

- 語言進化
- 更加了解應如何有效且安全地執行作業

- 管理某些特定作業的慣例發生變化

在某種語言中,我們通常都會捨棄某些方法,並以較新且類似的方法取代。新的方法可以使用稍微不同但較好的方式來執行同樣的工作。


並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
desc.semantic.cfml.obsolete
Abstract
使用不推薦或過時的函數可能表示有程式碼被忽略。
Explanation
隨著程式語言的演變,有時有些方法可能會過時,原因如下:

- 語言進化
- 更加了解應如何有效且安全地
執行作業
- 管理某些特定作業的慣例發生變化

在某種語言中,我們通常都會捨棄某些方法,並以較新且類似的方法取代。新的方法可以使用稍微不同但較好的方式來執行同樣的工作。
範例:以下程式碼會使用位元組陣列和指定每個 16 位元 Unicode 字元前 8 個位元的值來建構一個字串物件。


...
String name = new String(nameBytes, highByte);
...


在此範例中,建構函式可能無法正確地將位元組轉換成字元,這需視要使用哪個字元組來編碼由 nameBytes 所呈現的字串而定。由於用於編碼字串的字元組的演變,所以不推薦使用此建構函式,取而代之的是新的構造函式。新的構造函式可接受作為名為 charset 的其中一個參數,用來編碼字元組以進行字元轉換。

並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
References
[1] MET02-J. Do not use deprecated or obsolete classes or methods CERT
desc.semantic.java.obsolete
Abstract
使用不推薦或過時的函數可能表示有程式碼被忽略。
Explanation
隨著程式語言的演變,有時有些方法可能會過時,原因如下:

- 語言進化
- 更加了解應如何有效且安全地執行作業

- 對操作管理規則進行的修改。

在某種語言中,我們通常都會捨棄某些方法,並以較新且類似的方法取代。新的方法可以使用稍微不同但較好的方式來執行同樣的工作。
範例:以下程式碼使用 Digest::HMAC stdlib,但由於發行涉及意外,因此在文件中明確勸阻這樣的使用。


require 'digest/hmac'

hmac = Digest::HMAC.new("foo", Digest::RMD160)
...
hmac.update(buf)
...


在此範例中,由於發行涉及意外,在涉及 Digest::HMAC 類別時便會立即捨棄。由於實驗性和未正確測試的程式碼造成此要求無法如預期般運作,強烈勸阻使用此類別,尤其考慮到加密式功能涉及的關係 HMAC。

並不是所有函數都因為有安全性風險,才不推薦使用或被取代。但是,過時函數的存在通常表示周圍的程式碼已經沒有作用,甚至有可能處在廢棄狀態。長久以來,軟體的安全性始終不是優先的考量,甚或不納入考量。如果程式使用不推薦或過時的函數,則安全性問題更有可能伺機出沒。
desc.structural.ruby.obsolete
Abstract
使用了已被取代的函數。
Explanation
由於智慧合約的快節奏特性,函數和運算子可能會隨較新的編譯器版本而被取代,使用它們可能會導致低品質的程式碼、意外的副面影響和/或編譯錯誤。

範例 1:以下程式碼使用自 Solidity 編譯器 0.5.0 版本起就已被取代的 block.blockhash() 取得目前區塊的雜湊。


bytes32 blockhash = block.blockhash(0);
desc.structural.solidity.swc111
Abstract
此函數已過時,且無法保證指標有效或參照的記憶體可安全使用。
Explanation
不使用函數 IsBadXXXPtr() 類別的原因很多。這些函數的特性如下:
1) 非執行緒安全。
2) 通常會發生因探測無效記憶體位址而引起的當機。
3) 誤認為能夠在發生異常條件時執行適當的錯誤處理。

範例:下列程式碼使用 IsBadWritePtr() 以嘗試防止不良的記憶體寫入。

if (IsBadWritePtr(ptr, length))
{
[handle error]
}


程式設計師通常使用這些函數,目的是希望可藉其偵測異常狀況,但是函數造成的問題通常多過他們可解決的範圍。
References
[1] Raymond Chen IsBadXxxPtr should really be called CrashProgramRandomly
[2] IsBadWritePtr Function Microsoft
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[7] Standards Mapping - Common Weakness Enumeration CWE ID 730
[8] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[9] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[10] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[11] Standards Mapping - OWASP Top 10 2004 A9 Application Denial of Service
[12] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.9
[13] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP6080 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP6080 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP6080 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP6080 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP6080 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP6080 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP6080 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[24] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[25] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[26] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[27] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[28] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
[34] Standards Mapping - Web Application Security Consortium Version 2.00 Denial of Service (WASC-10)
[35] Standards Mapping - Web Application Security Consortium 24 + 2 Denial of Service
desc.semantic.cpp.obsolete_inadequate_pointer_validation
Abstract
類別包含名稱相同的欄位和方法。
Explanation
在同一個類別中,成員的欄位與方法擁有相同的名稱,很讓人困擾。這很容易讓程式設計師想存取某欄位時卻不小心呼叫與之同名的方法,而當他想要呼叫某方法時卻存取了與之同名的欄位。

範例 1:

public class Totaller {
private int total;
public int total() {
...
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398, CWE ID 710
[6] Standards Mapping - Smart Contract Weakness Classification SWC-119
desc.structural.java.poor_style_confusing_naming.member_and_method
Abstract
合約使用了影子變數,該變數含糊不清,容易被誤用。
Explanation
Solidity 允許開發人員以含糊不清的方式宣告狀態變數。這意味著即使兩個不同情境下的兩個不同變數,也可以使用相同的名稱來宣告,但使用它們可能會導致混淆和誤用。

在函數層級和繼承層級都可能發生這個情況。例如,如果 Contract1 宣告 var1 並繼承自 Contract2,而 Contract2 也宣告一個名為 var1 的變數,則該變數含糊不清,在之後的智慧合約執行中很容易彼此混淆。

範例 1:以下程式碼使用繼承,並在兩個智慧合約中宣告一個具有相同名稱的狀態變數。權杖銷售的實際 hardcap 是很難判斷的。


contract Tokensale {
uint hardcap = 10000 ether;

function Tokensale() { }

function fetchCap() public constant returns(uint) {
return hardcap;
}
}

contract Presale is Tokensale {
uint hardcap = 1000 ether;

function Presale() Tokensale() { }
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398, CWE ID 710
[6] Standards Mapping - Smart Contract Weakness Classification SWC-119
desc.structural.solidity.swc119