Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Non-Compliant Code Example: free()

Wiki MarkupDynamic memory managers are not required to clear freed memory and generally do not because of the additional runtime overhead. Furthermore, dynamic memory managers are free to reallocate this same memory. As a result, it is possible to accidentally leak sensitive information if it is not cleared before calling a function that frees dynamic memory. Programmers also cannot rely on memory being cleared during allocation \[[allocation (see MEM09-A. Do not assume memory allocation routines initialize memory]\]).

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.

...

Code Block
bgColor#ccccff
char *new_secret;
size_t size = strlen(secret);
if (size == SIZE_MAX) {
  /* Handle Error */
}
/* 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(new_secret, '\0', size);
free(new_secret);
new_secret = NULL;

Wiki MarkupThe {{calloc()}} function ensures that the newly allocated memory has also been cleared. Because {{sizeof(char)}} is guaranteed to be 1, this solution does not need to check for a numeric overflow as a result of using {{calloc()}} \[[ (see MEM07-A. Ensure that the arguments to calloc() when multiplied can be represented as a size_t]\].unmigrated-wiki-markup).

NOTE: It is possible that the call to {{memset()}} in this example will be optimized out \[[out (see MSC06-A. Be aware of compiler optimization when dealing with sensitive data]\]). Be very careful to ensure that any sensitive data is actually cleared from memory.

Non-Compliant Code Example: realloc()

...

Code Block
bgColor#FFcccc
size_t secret_size;
/* ... */
if (secret_size > SIZE_MAX/2) {
   /* handle error condition */
}

secret = (char *)realloc(secret, secret_size * 2);

...

The {{secret_size}} is tested to ensure that the integer multiplication ({{secret_size * 2}}) does not result in an integer overflow \[[overflow (see INT32-C. Ensure that operations on signed integers do not result in overflow]\]).

Compliant Solution

A compliant program cannot rely on realloc() because it is not possible to clear the memory prior to the call. Instead, a custom function must be used that operates similar to realloc() but sanitizes sensitive information as heap-based buffers are resized. Again, this is done by overwriting the space to be deallocated with '\0' characters.

Code Block
bgColor#ccccff
size_t secret_size;
/* ... */
if (secret_size > SIZE_MAX/2) {
   /* handle error condition */
}
/* calloc() initializes memory to zero */
temp_buff = (char *)calloc(secret_size * 2, sizeof(char));
if (temp_buff == NULL) {
 /* Handle Error */
}

memcpy(temp_buff, secret, secret_size);

/* sanitize the buffer */
memset(secret, '\0', secret_size);

free(secret);
secret = temp_buff; /* install the resized buffer */
temp_buff = NULL;

Wiki MarkupThe {{calloc()}} function ensures that the newly allocated memory has also been cleared. Because {{sizeof(char)}} is guaranteed to be 1, this solution does not need to check for a numeric overflow as a result of using {{calloc()}} \[[ (see MEM07-A. Ensure that the arguments to calloc() when multiplied can be represented as a size_t]\]).

Risk Assessment

Wiki Markup
In practice, this type of [security flaw|BB. Definitions#security flaw] can expose sensitive information to unintended parties. The Sun tarball vulnerability discussed in _Secure Coding Principles & Practices: Designing and Implementing Secure Applications_ \[[Graf 03|AA. C References#Graf 03]\] and [Sun Security Bulletin #00122 | http://sunsolve.sun.com/search/document.do?assetkey=1-22-00122-1] illustrates a violation of this recommendation, leading to sensitive data being leaked. Attackers may also be able to leverage this defect to retrieve sensitive information using techniques such as _heap inspection_.

...