...
In cases where the memory allocation functions return a non-null pointer, using this pointer results in undefined behavior. Typically the pointer refers to a zero-length block of memory consisting entirely of control structures. Overwriting these control structures will damage the data structures used by the memory manager.
malloc()
...
Noncompliant Code Example
The result of calling malloc(0)
to allocate 0 bytes is implementation-defined. In this example, a dynamic array of integers is allocated to store size
elements. However, if size
is zero, the call to malloc(size)
may return a reference to a block of memory of size 0 instead of a null
pointer. When (nonempty) data is copied to this location, a heap-buffer overflow occurs.
Code Block | ||
---|---|---|
| ||
size_t size; /* initialize size, possibly by user-controlled input */ int *list = (int *)malloc(size); if (list == NULL) { /* Handle Allocation Error */ } /* Continue Processing list */ |
Compliant Code Example
To ensure that zero is never passed as a size argument to malloc()
, size
is checked to ensure it has a positive value.
Code Block | ||
---|---|---|
| ||
size_t size; /* initialize size, possibly by user-controlled input */ if (size == 0) { /* Handle Error */ } int *list = (int *)malloc(size); if (list == NULL) { /* Handle Allocation Error */ } /* Continue Processing list */ |
realloc()
...
Noncompliant Code Example
The realloc()
function deallocates the old object and returns a pointer to a new object of a specified size. If memory for the new object cannot be allocated, the realloc()
function does not deallocate the old object and its value is unchanged. If the realloc()
function returns NULL, failing to free the original memory will result in a memory leak. As a result, the following idiom is often recommended for reallocating memory:
...
However, this commonly recommended idiom has problems with zero-length allocations. If the value of nsize
in this example is 0, the standard allows the option of either returning a null pointer or returning a pointer to an invalid (e.g., zero-length) object. In cases where the realloc()
function frees the memory but returns a null pointer, execution of the code in this example results in a double free. If the realloc()
function returns non-NULL, but the size was 0, the returned memory will be of size 0, and a heap overflow will occur if nonempty data is copied there.
Implementation Details
If this non-compliant noncompliant code is compiled with GCC 3.4.6 and linked with libc 2.3.4, invoking realloc(p, 0)
returns a non-null pointer to a zero-sized object (the same as malloc(0)
). However, if the same code is compiled with either Microsoft Visual Studio Version 7.1 or GCC version 4.1.0 , realloc(p, 0)
returns a null pointer, resulting in a double-free vulnerability.
Compliant Solution
This compliant solution does not pass a size argument of zero to the realloc()
function.
Code Block | ||
---|---|---|
| ||
size_t nsize; /* initialize nsize */ char *p2; char *p = (char *)malloc(100); if (p == NULL) { /* Handle Error */ } /* ... */ p2 = NULL; if (nsize != 0) { p2 = (char *)realloc(p, nsize); } if (p2 == NULL) { free(p); p = NULL; return NULL; } p = p2; |
Automated Detection
Compass/ROSE can some violations of this rule. Is particular, it warns when when the argument to malloc()
is a variable that has not been compared against NULL, or is known at compile time to be 0.
Risk Assessment
Allocating zero bytes can lead to abnormal program termination.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MEM04-A C | low | likely | medium | P6 | L2 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.20.3, "Memory Management Functions" \[[Seacord 05|AA. C References#Seacord 05]\] Chapter 4, "Dynamic Memory Management" |
...
MEM03-C. Clear sensitive information stored in reusable resources returned for reuse 08. Memory Management (MEM) MEM05-A. Avoid large stack allocations