...
Using realloc()
to resize dynamic memory may inadvertently expose sensitive information, or it may allow heap inspection as described in the Fortify Taxonomy: Software Security Errors [Fortify 2006] and NIST's Source Code Analysis Tool Functional Specification [Black 2007]. When realloc()
is called it may allocate a new, larger object, copy the contents of secret
to this new object, free()
the original object, and assign the newly allocated object to secret
. However, the contents of the original object may remain in memory.
...
The secret_size
is tested to ensure that the integer multiplication (secret_size * 2
) does not result in an integer overflow. (See rule INT32-C. Ensure that operations on non-atomic 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.
...
In practice, this type of 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 2003] and Sun Security Bulletin #00122 [Sun] shows 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.
...
MITRE CWE: CWE-244: Failure to Clear Heap Memory Before Release ('Heap Inspection')
ISO/IEC 9899:1999 Section 7.20.3, "Memory management functions"
ISO/IEC TR 24772 "XZK Sensitive Information Uncleared Before Use"
Bibliography
[Black 2007]
[Fortify 2006]
[Graff 2003]
...