對內存中的敏感數據刪除不當可能會危及數據的安全。
Compiler optimization 錯誤會在以下情況中出現:
1. 機密數據儲存在內存中。
2. 內存中的機密數據通過覆蓋內容的方式進行清除。
3. 源代碼使用一個優化編譯器進行編譯,從而標識和刪除那些將相關內容作為死存儲進行覆蓋的函數,因為在隨后的操作中不會再使用這一內存。
示例:以下代碼從用戶那里讀取一個密碼,使用該密碼連接到一個終端大型機,然后嘗試使用 memset() 來清除內存中的密碼。
void GetData(char *MFAddr) {
char pwd[64];
if (GetPasswordFromUser(pwd, sizeof(pwd))) {
if (ConnectToMainframe(MFAddr, pwd)) {
// Interaction with mainframe
}
}
memset(pwd, 0, sizeof(pwd));
}
memset() 的調用會被當作一個死存儲清除,因為 pwd 緩沖區在其數值被覆蓋之后便不會再次使用了 [2]。因為緩沖區 pwd 包含一個敏感值,所以如果數據長期駐留在內存中,應用程序會很容易受到攻擊。如果攻擊者能夠訪問正確的內存區域,那么他們就能使用復原后的密碼來獲取系統的控制權。memset() 調用解析為一段 dead code,這是因為隨后的操作不會再使用被寫入的內存,然而顯而易見的是這樣做會引發安全問題。這里的問題是,很多編譯器,實際上是很多編程語言在努力提高效率的同時沒有考慮這個及其他安全問題。 [1] Standards Mapping - OWASP Top 10 2010 - (OWASP 2010) A7 Insecure Cryptographic Storage
[2] Standards Mapping - OWASP Top 10 2007 - (OWASP 2007) A8 Insecure Cryptographic Storage
[3] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A8 Insecure Storage
[4] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3230.2 CAT II
[5] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3230.2 CAT II
[6] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 14
[7] Standards Mapping - FIPS200 - (FISMA) MP
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 3.4, Requirement 6.3.1.3, Requirement 6.5.8, Requirement 8.4
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 3.4, Requirement 6.5.3, Requirement 8.4
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 3.4, Requirement 6.5.8, Requirement 8.4
[11] M. Howard Some Bad News and Some Good News Microsoft
[12] M. Howard, D. LeBlanc Writing Secure Code, Second Edition Microsoft Press