...
This compliant solution accesses the buffer again after the call to memset and should cause most compilers not to optimize out the call to memset. Check compilter compiler documentation to guarantee this behavior for a specific platform.
Compliant Code Example 2 (Windows)
| Code Block |
|---|
void getPassword() {
char pwd[64];
if(GetPassword(pwd, sizeof(pwd)) {
/* checking of password, secure operations, etc */
}
SecureZeroMemory(pwd, sizeof(pwd));
}
|
This compliant solution uses a SecureZeroMemory() function provided by many version of the Microsoft Visual Studio compiler. The documentation for the SecureZeroMemory() function garauntees guarantees that the compiler will not optimize out this call when zeroing memory.
Compliant Code Example 3 (Windows)
| Code Block |
|---|
void getPassword() {
char pwd[64];
if(GetPassword(pwd, sizeof(pwd)) {
/* checking of password, secure operations, etc */
}
#pragma optimize("", off)
memset(pwd, 0, sizeof(pwd));
#pragma optimize("", on)
}
|
The #pragma directives here instructs the compiler to avoid optimizing the enclosed code. This #pragma directive is support on some versions of Microsoft Visual Studio, and may be supported on other compilers. Check compiler documentation to ensure its availability and its optimization garaunteesguarantees.
Risk Assessment
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
DRAFT | 2 (Medium) | 2 (Probable) | 2 (Medium) | P8 | L2 |
...