...
To prevent information leakage, sensitive information must be cleared from dynamically allocated buffers before they are freed. Calling free()
on a block of dynamic memory causes the space to be deallocated; that is, the memory block is made available for future allocation. However, the data stored in the block of memory to be recycled may be preserved. If this memory block contains sensitive information, that information may be unintentionally exposed.
In this noncompliant example, sensitive information stored in the dynamically allocated memory referenced by secret
is copied to the dynamically allocated buffer, new_secret
, which is processed and eventually deallocated by a call to free()
. Because the memory is not cleared, it may be reallocated to another section of the program where the information stored in new_secret
may be unintentionally leaked.
Code Block | ||||
---|---|---|---|---|
| ||||
char *secret; /* Initialize secret to a null-terminated byte string, of less than SIZE_MAX chars */ char *new_secret; size_t size = strlen(secret); if (size == SIZE_MAX) { /* Handle error */ } char *new_secret; new_secret = (char *)malloc(size+1); if (!new_secret) { /* Handle error */ } strcpy(new_secret, secret); /* Process new_secret... */ free(new_secret); new_secret = NULL; |
...
Code Block | ||||
---|---|---|---|---|
| ||||
char *secret; /* Initialize secret to a null-terminated byte string, of less than SIZE_MAX chars */ char *new_secret; size_t size = strlen(secret); if (size == SIZE_MAX) { /* Handle error */ } char *new_secret; /* Use calloc() to zero-out allocated space */ new_secret = (char *)calloc(size+1, sizeof(char)); if (!new_secret) { /* Handle error */ } strcpy(new_secret, secret); /* Process new_secret... */ /* Sanitize memory */ memset_s(new_secret, '\0', size); free(new_secret); new_secret = NULL; |
...
Tool | Version | Checker | Description | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| (customization) | Users can add a custom check for use of realloc() . | |||||||||||||||||
Compass/ROSE | Could detect possible violations of this rule by first flagging any usage of | |||||||||||||||||||
Helix QAC |
| C5010 | ||||||||||||||||||
LDRA tool suite |
| 44 S | Enhanced Enforcement | |||||||||||||||||
Parasoft C/C++test |
| CERT_C-MEM03-a | Sensitive data should be cleared before being deallocated | |||||||||||||||||
Polyspace Bug Finder |
|
| Checks for: R2016a
| Sensitive data not cleared or released by memory routine Variable in stack is not cleared and contains sensitive data | PRQA QA-C | |||||||||||||||
Include Page | PRQA QA-C_v | PRQA QA-C_v | 5010 | Partially implemented | Rec. partially covered. | |||||||||||||||
PVS-Studio |
| V1072 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
ISO/IEC TR 24772:2013 | Sensitive Information Uncleared Before Use [XZK] |
MITRE CWE | CWE-226, Sensitive information uncleared before release CWE-244, Failure to clear heap memory before release ("heap inspection") |
...