Integer values used as a size argument to malloc(), calloc(), realloc(), or aligned_alloc() 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.
Noncompliant Code Example
...
In this noncompliant code example, num_blocks is multiplied by 16 and the result is stored in the alloc:too little space is allocated for a struct tm object because the size of the pointer is being used to determine the size of the pointed-to object. This is a demonstration of EXP01-C. Do not take the size of a pointer to determine the size of the pointed-to type.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib<time.h> enum { BLOCKSIZE = 16 }; void *alloc_blocks(size_t num_blocks) { if (num_blocks == 0) { struct tm *make_tm(int year, int mon, int day, int hour, return NULL; } unsigned long long alloc = num_blocks * BLOCKSIZE ;int min, int sec) { returnstruct (alloc < UINT_MAX)tm *tmb; tmb = (struct ?tm *)malloc(num_blocks * BLOCKSIZE )sizeof(tmb)); if (tmb == NULL) { :return 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 INT18-C. Evaluate integer expressions in a larger size before comparing or assigning to that size for more information on upcasting.
Compliant Solution (Integer Overflow)
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 or truncation. (See INT32-C. Ensure that operations on signed integers do not result in overflow and INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data.)
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
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.)
Noncompliant Code Example (Range Checking)
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.
tmb->tm_sec = sec;
tmb->tm_min = min;
tmb->tm_hour = hour;
tmb->tm_mday = day;
tmb->tm_mon = mon;
tmb->tm_year = year;
return tmb;
} |
Compliant Solution
In this compliant solution, the correct amount of memory is allocated for the struct tm object.
| Code Block | ||||
|---|---|---|---|---|
| ||||
| Code Block | ||||
| ||||
#include <string<time.h> enumstruct {tm BUFF_SIZE = 32 }; void func(const char *str*make_tm(int year, int mon, int day, int hour, int min, int lensec) { struct char buf[BUFF_SIZE]tm *tmb; tmb if= (len < BUFF_SIZE){ memcpy(buf, str, len); } } |
Compliant Solution (Range Checking)
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:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <string.h>
enum { BUFF_SIZE = 32 };
void func(const char *str, size_t len) {
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.
struct tm *)malloc(sizeof(struct tm));
if (tmb == NULL) {
return NULL;
}
tmb->tm_sec = sec;
tmb->tm_min = min;
tmb->tm_hour = hour;
tmb->tm_mday = day;
tmb->tm_mon = mon;
tmb->tm_year = year;
return tmb;
} |
Noncompliant Code Example (Size Calculation)
...
To correct the noncompliant code example, sizeof(long) is used to size the memory allocation:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
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:
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
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);
}
|
...
...
| [Coverity 2007] | |
| [Seacord 2013] | Chapter 4, "Dynamic Memory Management" Chapter 5, "Integer Security" |
| [xorl 2009] | CVE-2009-0587: Evolution Data Server Base64 Integer Overflows |
...