1563
Example 1:
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().
Example Language: C (Bad)
void GetData(char *MFAddr) {
char pwd[64];
if (GetPasswordFromUser(pwd, sizeof(pwd))) {
if (ConnectToMainframe(MFAddr, pwd)) {
// 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.
Observed Examples
Reference Description
CVE-2008-1685 C compiler optimization, as allowed by specifications, removes code that is
used to perform checks to detect integer overflows.
https://www.cve.org/CVERecord?id=CVE-2008-1685
CVE-2019-1010006Chain: compiler optimization (CWE-733) removes or modifies code used to
detect integer overflow (CWE-190), allowing out-of-bounds write (CWE-787).
https://www.cve.org/CVERecord?id=CVE-2019-1010006
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
Nature Type ID Name Page
MemberOf 976 SFP Secondary Cluster: Compiler 888 2407
MemberOf 1398 Comprehensive Categorization: Component Interaction 1400 2524
Related Attack Patterns
CAPEC-ID Attack Pattern Name
8 Buffer Overflow in an API Call
CWE Version 4.14
CWE-749: Exposed Dangerous Method or Function
CWE-749: Exposed Dangerous Method or Function