Versions Compared

Key

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

Integer values The types of integer expressions used as a size argument arguments to malloc(), calloc(), realloc(), or aligned_alloc() must be of sufficient size to contain have sufficient range to represent the size of 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.

Typically, the amount of memory to allocate will be the size of the type of object to allocate. When allocating space for an array, the size of the object will be multiplied by the bounds of the array. When allocating space for a structure containing a flexible array member, the size of the array member must be added to the size of the structure (see MEM33-C. Allocate and copy structures containing a flexible array member dynamically). Use the correct type of the object when computing the size of chunk memory to allocate.

STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator is a specific instance of this rule.

...

In this noncompliant code example, an array of long is allocated and assigned to p. The code checks for unsigned integer overflow in compliance with INT32-C. Ensure that operations on signed integers do not result in overflow and also ensures that len is not equal to zero (see MEM04-C. Beware of zero-length allocations). ) UnfortunatelyHowever, because sizeof(int) is used to size the allocated memorycompute the size, and not sizeof(long), an insufficient amount of memory can be allocated on implementations where sizeof(long) is larger than sizeof(int) .

Code Block
bgColor#FFcccc
langc
#include <stdint.h>
#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(int));
   if (p == NULL) {
      /* Handle error */
   }
   free(p);
}

Compliant Solution (Integer)

...

Code Block
bgColor#ccccff
langc
#include <stdint.h>
#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);
}

Compliant Solution (Integer)

...

Code Block
bgColor#ccccff
langc
#include <stdint.h>
#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);
}

Noncompliant Code Example (Pointer)

In this noncompliant code example, inadequate 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.:

Code Block
bgColor#FFcccc
langc
#include <stdlib.h>
#include <time.h>
 
struct tm *make_tm(int year, int mon, int day, int hour,
                   int min, int sec) {
  struct tm *tmb;
  tmb = (struct tm *)malloc(sizeof(tmb));
  if (tmb == NULL) {
    return NULL;
  }
  tmb->tm*tmb = (struct tm) {
    .tm_sec = sec;
  tmb->tm, .tm_min = min;
  tmb->tm, .tm_hour = hour;,
  tmb->tm  .tm_mday = day;
  tmb->tm, .tm_mon = mon;
  tmb->tm, .tm_year = year
  };
  return return tmb;
}

Compliant Solution (Pointer)

 In this compliant solution, the correct amount of memory is allocated for the struct tm object. When allocating  space for a single object, passing the (dereferenced) pointer type to the sizeof operator is a simple way to allocate sufficient memory. Because the sizeof operator does not evaluate its operand, dereferencing an uninitialized or null pointer in this context is well-defined behavior.

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
#include <time.h>
 
struct tm *make_tm(int year, int mon, int day, int hour,
                   int min, int sec) {
  struct tm *tmb;
  tmb = (struct tm *)malloc(sizeof(*tmb));
  if (tmb == NULL) {
    return NULL;
  }
   tmb->tm*tmb = (struct tm) {
    .tm_sec = sec;
  tmb->tm, .tm_min = min;
  tmb->tm, .tm_hour = hour;,
  tmb->tm    .tm_mday = day;
  tmb->tm, .tm_mon = mon;
  tmb->tm, .tm_year = year
  };
  return tmb;
}

Risk Assessment

...

Tool

Version

Checker

Description

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:

Code Block
if (a < SIZE_MAX / b && a > 0) ...

Coverity

Include Page
Coverity_V
Coverity_V

BAD_ALLOC_STRLEN


SIZECHECK

Can find instances where string length is miscalculated (length calculated may be one less than intended) for memory allocation purposes. Coverity Prevent cannot discover all violations of this rule, so further verification is necessary

Finds memory allocations that are assigned to a pointer that reference objects larger than the allocated block

Fortify SCA

5.0

 

Can detect violations of this rule with CERT C Rule Pack, except those involving the sizeof operator

Klocwork

9.1

INCORRECT.ALLOC_SIZE

 

LDRA tool suite

Include Page
LDRA_V
LDRA_V

487 S
577 S 

Fully implemented
Fully implemented 

Splint

3.1.1  

...

Bibliography

[Coverity 2007] 
[Drepper 2006]Section 2.1.1, "Respecting Memory Bounds"
[Seacord 2013]Chapter 4, "Dynamic Memory Management"
Chapter 5, "Integer Security"
[Viega 2005]Section 5.6.8, "Use of sizeof() on a Pointer Type"
[xorl 2009]CVE-2009-0587: Evolution Data Server Base64 Integer Overflows

...