Compiler Removal of Code to Clear Buffers

Draft Variant
Structure: Simple
Description

Sensitive memory is cleared according to the source code, but compiler optimizations leave the memory untouched when it is not read from again, aka "dead store removal."

Extended Description

This compiler optimization error occurs when: 1. Secret data are stored in memory. 1. The secret data are scrubbed from memory by overwriting its contents. 1. The source code is compiled using an optimizing compiler, which identifies and removes the function that overwrites the contents as a dead store because the memory is not used subsequently.

Common Consequences 1
Scope: ConfidentialityAccess Control

Impact: Read MemoryBypass Protection Mechanism

This weakness will allow data that has not been cleared from memory to be read. If this data contains sensitive password information, then an attacker can read the password and use the information to bypass protection mechanisms.

Detection Methods 2
Black Box
This specific weakness is impossible to detect using black box methods. While an analyst could examine memory to see that it has not been scrubbed, an analysis of the executable would not be successful. This is because the compiler has already removed the relevant code. Only the source code shows whether the programmer intended to clear the memory or not, so this weakness is indistinguishable from others.
White Box
This weakness is only detectable using white box methods (see black box detection factor). Careful analysis is required to determine if the code is likely to be removed by the compiler.
Potential Mitigations 3
Phase: Implementation
Store the sensitive data in a "volatile" memory location if available.
Phase: Build and Compilation
If possible, configure your compiler so that it does not remove dead stores.
Phase: Architecture and Design
Where possible, encrypt sensitive data that are used by a software system.
Demonstrative Examples 1

ID : DX-200

The following code reads a password from the user, uses the password to connect to a back-end mainframe and then attempts to scrub the password from memory using memset().

Code Example:

Bad
C
c

// Interaction with mainframe* }} memset(pwd, 0, sizeof(pwd));}

The code in the example will behave correctly if it is executed verbatim, but if the code is compiled using an optimizing compiler, such as Microsoft Visual C++ .NET or GCC 3.x, then the call to memset() will be removed as a dead store because the buffer pwd is not used after its value is overwritten [18]. Because the buffer pwd contains a sensitive value, the application may be vulnerable to attack if the data are left memory resident. If attackers are able to access the correct region of memory, they may use the recovered password to gain control of the system.
It is common practice to overwrite sensitive data manipulated in memory, such as passwords or cryptographic keys, in order to prevent attackers from learning system secrets. However, with the advent of optimizing compilers, programs do not always behave as their source code alone would suggest. In the example, the compiler interprets the call to memset() as dead code because the memory being written to is not subsequently used, despite the fact that there is clearly a security motivation for the operation to occur. The problem here is that many compilers, and in fact many programming languages, do not take this and other security concerns into consideration in their efforts to improve efficiency.
Attackers typically exploit this type of vulnerability by using a core dump or runtime mechanism to access the memory used by a particular application and recover the secret information. Once an attacker has access to the secret information, it is relatively straightforward to further exploit the system and possibly compromise other resources with which the application interacts.
References 5
Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors
Katrina Tsipenyuk, Brian Chess, and Gary McGraw
NIST Workshop on Software Security Assurance Tools Techniques and MetricsNIST
07-11-2005
ID: REF-6
Writing Secure Code
Michael Howard and David LeBlanc
Microsoft Press
04-12-2002
ID: REF-7
When scrubbing secrets in memory doesn't work
Michael Howard
BugTraq
05-11-2002
ID: REF-124
Some Bad News and Some Good News
Michael Howard
Microsoft
21-10-2002
ID: REF-125
GNU GCC: Optimizer Removes Code Necessary for Security
Joseph Wagner
Bugtraq
16-11-2002
ID: REF-126
Applicable Platforms
Languages:
C : UndeterminedC++ : Undetermined
Modes of Introduction
Implementation
Build and Compilation
Affected Resources
  1. Memory
Taxonomy Mapping
  • 7 Pernicious Kingdoms
  • PLOVER
  • OWASP Top Ten 2004
  • CERT C Secure Coding
  • Software Fault Patterns