Cleartext storage of sensitive information in bufferΒΆ
ID: cpp/cleartext-storage-buffer
Kind: path-problem
Severity: warning
Precision: medium
Tags:
- security
- external/cwe/cwe-312
Query suites:
- cpp-security-extended.qls
- cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
Sensitive information that is stored unencrypted is accessible to an attacker who gains access to the storage.
RecommendationΒΆ
Ensure that sensitive information is always encrypted before being stored to a file or transmitted over the network. It may be wise to encrypt information before it is put into a buffer that may be readable in memory.
In general, decrypt sensitive information only at the point where it is necessary for it to be used in cleartext.
ExampleΒΆ
The following example shows two ways of storing user credentials in a file. In the βBADβ case, the credentials are simply stored in cleartext. In the βGOODβ case, the credentials are encrypted before storing them.
void writeCredentials() {
char *password = "cleartext password";
FILE* file = fopen("credentials.txt", "w");
// BAD: write password to disk in cleartext
fputs(password, file);
// GOOD: encrypt password first
char *encrypted = encrypt(password);
fputs(encrypted, file);
}
ReferencesΒΆ
M. Dowd, J. McDonald and J. Schuhm, The Art of Software Security Assessment, 1st Edition, Chapter 2 - βCommon Vulnerabilities of Encryptionβ, p. 43. Addison Wesley, 2006.
M. Howard and D. LeBlanc, Writing Secure Code, 2nd Edition, Chapter 9 - βProtecting Secret Dataβ, p. 299. Microsoft, 2002.
Common Weakness Enumeration: CWE-312.