Integer values used as a size argument to malloc()
, calloc()
, or realloc()
must be valid and large enough to contain the objects to be stored. If size arguments are incorrect or can be manipulated by an attacker, then a buffer overflow may occur. Incorrect size arguments, inadequate range checking, integer overflow, or truncation can result in the allocation of an inadequately sized buffer. The programmer must ensure that size arguments to memory allocation functions allocate sufficient memory.
In this noncompliant code example, num_blocks
is multiplied by 16 and the result is stored in the alloc
.
enum { BLOCKSIZE = 16 }; /* ... */ void *alloc_blocks(size_t num_blocks) { if (num_blocks == 0) { return NULL; } unsigned long long alloc = num_blocks * BLOCKSIZE ; return (alloc < UINT_MAX) ? malloc(num_blocks * BLOCKSIZE ) : NULL; } |
If size_t
is represented as a 32-bit unsigned value and unsigned long long
is represented as a 64-bit unsigned value, for example, the result of this multiplication can still overflow because the actual multiplication is a 32-bit operation. As a result, the value stored in alloc
will always be less than UINT_MAX
.
If both size_t
and unsigned long long
types are represented as a 64-bit unsigned value, the result of the multiplication operation may not be representable as an unsigned long long
value. See INT35-C. Evaluate integer expressions in a larger size before comparing or assigning to that size for more information on upcasting.
In this compliant solution, the integer values passed as size arguments to memory allocation functions are of the correct size and have not been altered due to integer overflow (INT32-C. Ensure that operations on signed integers do not result in overflow) or truncation (INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data).
enum { BLOCKSIZE = 16 }; /* ... */ void *alloc_blocks(size_t num_blocks) { if (num_blocks == 0 || num_blocks > SIZE_MAX / BLOCKSIZE) return NULL; return malloc(num_blocks * BLOCKSIZE); } |
This example checks the value of num_blocks
to make sure the subsequent multiplication operation cannot result in an integer overflow. The code also ensures that num_blocks
is not equal to zero (see MEM04-C. Do not perform zero length allocations).
In this noncompliant code example, the string referenced by str
and the string length represented by len
originate from untrusted sources. The length is used to perform a memcpy()
into the fixed-size static array buf
. The len
variable is guaranteed to be less than BUFF_SIZE
. However, because len
is declared as an int
, it can have a negative value that would bypass the check. The memcpy()
function implicitly converts len
to an unsigned size_t
type, and the resulting operation results in a buffer overflow.
int len; char *str; char buf[BUFF_SIZE]; /* ... */ if (len < BUFF_SIZE){ memcpy(buf, str, len); } /* ... */ |
In this compliant solution, len
is declared as a size_t
so there is no possibility of this variable having a negative value and bypassing the range check.
size_t len; char *str; char buf[BUFF_SIZE]; /* ... */ if (len < BUFF_SIZE){ memcpy(buf, str, len); } /* ... */ |
See INT01-C. Use rsize_t or size_t for all integer values representing the size of an object for more information on representing the size of objects.
In this noncompliant code example, an array of long
is allocated and assigned to p
. However, sizeof(int)
is used to size the allocated memory. If sizeof(long)
is larger than sizeof(int)
then an insufficient amount of memory is allocated.
void function(size_t len) { long *p; if (len == 0 || len > SIZE_MAX / sizeof(long)) { /* Handle overflow */ } p = (long *)malloc(len * sizeof(int)); if (p == NULL) { /* Handle error */ } /* ... */ free(p); } |
This example also checks for unsigned integer overflow in compliance with INT32-C. Ensure that operations on signed integers do not result in overflow.
To correct the noncompliant code example, sizeof(long)
is used to size the memory allocation.
void function(size_t len) { long *p; if (len == 0 || len > SIZE_MAX / sizeof(long)) { /* Handle overflow */ } p = (long *)malloc(len * sizeof(long)); if (p == NULL) { /* Handle error */ } /* ... */ free(p); } |
Alternatively, sizeof(*p)
can be used to properly size the allocation.
void function(size_t len) { long *p; if (len == 0 || len > SIZE_MAX / sizeof(*p)) { /* handle overflow */ } p = (long *)malloc(len * sizeof(*p)); if (p == NULL) { /* handle error */ } /* ... */ free(p); } |
The code also ensures that len
is not equal to zero (see MEM04-C. Do not perform zero length allocations).
Providing invalid size arguments to memory allocation functions can lead to buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
MEM35-C |
high |
probable |
high |
P6 |
L2 |
Fortify SCA Version 5.0 with CERT C Rule Pack can detect violations of this rule, except those involving the sizeof
operator.
Coverity Prevent. The SIZECHECK checker finds memory allocations that are assigned to a pointer that reference objects larger than the allocated block. Coverity Prevent cannot discover all violations of this rule so further verification is necessary.
Compass/ROSE could check violations of this rule by examining the size expression to malloc()
or memcpy()
functions. Specifically the size argument should be bounded by 0, SIZE_MAX, and, unless it is a variable of type size_t
or rsize_t
, it should be bounds-checked before the malloc()
call. If the argument is of the expression a*b
, then an appropriate check is:
if (a < SIZE_MAX / b && a > 0) ... |
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
This rule appears in the C++ Secure Coding Standard as MEM35-CPP. Allocate sufficient memory for an object.
\[[Coverity 07|AA. C References#Coverity 07]\] \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.20.3, "Memory Management Functions" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "XYB Buffer Overflow in Heap" \[[MITRE 07|AA. C References#MITRE 07]\] [CWE ID 190|http://cwe.mitre.org/data/definitions/190.html], "Integer Overflow (Wrap or Wraparound)," and [CWE ID 131|http://cwe.mitre.org/data/definitions/131.html], "Incorrect Calculation of Buffer Size" \[[Seacord 05|AA. C References#Seacord 05]\] Chapter 4, "Dynamic Memory Management," and Chapter 5, "Integer Security" |
08. Memory Management (MEM) 09. Input Output (FIO)