...
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 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 before the call. Instead, a custom function must be used that operates similarly 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.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
| SV.USAGERULES.UNINTENDED_COPY |
| |||||||
Compass/ROSE |
|
| Could detect possible violations of this rule by first flagging any usage of | ||||||
PRQA QA-C |
| warncall for realloc | Partially implemented |
Related Vulnerabilities
...
ISO/IEC 9899:2011 Section 7.22.3, "Memory management functions"
ISO/IEC TR 24772 "XZK Sensitive information uncleared before use"
Bibliography
[Black 2007]
[Fortify 2006]
[Graff 2003]
...