Abstract:
Improperly scrubbing sensitive data from memory can compromise security.
Explanation:
Compiler optimization errors occur when:
1. Secret data is stored in memory.
2. The secret data is scrubbed from memory by overwriting its contents.
3. 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.
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().
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++(R) .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 [2]. Because the buffer pwd contains a sensitive value, the application may be vulnerable to attack if the data is 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. After 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.
Recommendations:
Optimizing compilers are hugely beneficial to performance, so disabling optimization is rarely a reasonable option. The solution is to communicate to the compiler exactly how the program should behave. Because support for this communication is imperfect and varies from platform to platform, current solutions to the problem are imperfect as well.
It is often possible to force the compiler into retaining calls to scrubbing functions by reading from the variable after it is cleaned in memory. Another option involves volatile pointers, which are not currently optimized because they can be modified from outside the application. You can make use of this fact to trick the compiler by casting pointers to sensitive data to volatile pointers. This could be accomplished in Example 1 by adding the following line immediately after the call to memset():
*(volatile char*)pwd = *(volatile char*)pwd;
Although both of these solutions prevent existing compilers from optimizing out calls to scrubbing functions such as the one shown in Example 1, they rely on current optimization techniques, which will continue to evolve in the future. The insidious aspect of this is that, as compiler technology evolves, security flaws such as this one may be reintroduced even if an application's source code has remained unchanged.
On recent Windows(R) platforms, consider using SecureZeroMemory(), which is a secure replacement for ZeroMemory() that uses the preceding volatile pointer trick to protect itself from optimization [2]. Additionally, in most versions of Microsoft Visual C++(R) it is possible to use the #pragma optimize construct to prevent the compiler from optimizing specific blocks of code. For example:
#pragma optimize("",off);
memset(pwd, 0, sizeof(pwd));
#pragma optimize("",on);
0 comments:
Post a Comment