Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Non-Compliant Code Example

...

(integer overflow)

In this non-compliant code example, cBlocks is multiplied by 16 and the result is stored in the unsigned long long int alloc.

Code Block
bgColor#FFcccc

enum { BLOCKSIZE = 16 };
...
void* AllocBlocks(size_t cBlocks) {
  if (cBlocks == 0) return NULL;
  unsigned long long alloc = cBlocks * 16BLOCKSIZE ;
  return (alloc < UINT_MAX)
     ? malloc(cBlocks * BLOCKSIZE 16)
     : NULL;
}

If size_t is represented as a 32-bit unsigned value and unsigned long long 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.

Compliant Solution 1a

Wiki Markup
Make sure that integer values passed asSee \[[INT35-C. Evaluate integer expressions in a larger size argumentsbefore tocomparing memory allocation functions are valid and have not been corrupted due to integer overflow, truncation, or sign error \[[Integers (INT)|04. Integers (INT)]\]. In the following example, the {{multsize_t()}} function multiples two values of type {{size_t}} and sets {{errno}} to a non-zero value if the resulting value cannot be represented as a {{size_t}} or to zero if it was representable.

Code Block
bgColor#ccccff

void *AllocBlocks(size_t cBlocks) {
  size_t alloc;

  if (cBlocks == 0) return NULL;
  alloc = multsize_t(cBlocks, 16);
  if (errno) {
    return NULL;
  }
  else {
    return malloc(alloc);
  }
} /* end AllocBlocks */

Compliant Solution 1b

Alternately, since multsize_t() is not available on all platforms, you may wish to use a more portable solution. You can check for invalid cBlocks values (zero or too much) as follows:

or assigning to that size]] for more information on upcasting.

Compliant Solution (integer overflow)

Wiki Markup
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 integer operations do not result in an overflow]\] or truncation \[[INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data]\]. 

Code Block
bgColor#ccccff
enum { BLOCKSIZE = 16 };
...
void *AllocBlocks(size_t cBlocks) {
  size_t alloc;

  if (cBlocks == 0 || cBlocks > SIZE_MAX / BLOCKSIZE) return NULL;
  return malloc (cBlocks * BLOCKSIZE);
} /* end AllocBlocks */

Wiki Markup
This example checks the value of {{cBlocks}} to make sure the subsequent multiplication operation cannot result in an integer overflow.  The code also ensures that {{cBlocks}} is not equal to zero (see \[[MEM04-A. Do not make assumptions about the result of allocating 0 bytes]])
In either case, note that we have left the handling of allocation errors as an exercise for the reader
.

Non-Compliant Code Example

...

(Range Checking)

In this non-compliant code example, the string referenced by str and the string length represented by len orginate 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 could 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.

Code Block
bgColor#FFcccc
int len;
char *str;
char buf[BUFF_SIZE];

/* ... */
if (len < BUFF_SIZE){
  memcpy(buf, str, len);
}
/* ... */

Compliant Solution

...

(Range Checking)

In this compliant solution, len is declared as a size_t to there is no possibility of this variable having a negative value and bypassing the range check.

Code Block
bgColor#ccccff
size_t len;
char *str;
char buf[BUFF_SIZE];

/* ... */
if (len < BUFF_SIZE){
  memcpy(buf, str, len);
}
/* ... */

Wiki Markup
See \[[INT01-A. 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.

Non-Compliant Code Example

...

(Incorrectly Calculating a Size)

In this non-compliant code example, an array of long integers 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. This example also checks for unsigned numeric overflow in compliance with INT32-C. Ensure that integer operations do not result in an overflow.

Code Block
bgColor#FFcccc
void function(size_t len) {
   long *p;
   if (len > SIZE_MAX / sizeof(long)) {
      /* handle overflow */
   }
   p = 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 integer operations do not result in an overflow.

Compliant Solution

...

(Incorrectly Calculating a Size)

To correct this example, sizeof(long) is used to size the memory allocation.

...