You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 131 Next »

Variable length arrays (VLAs), a conditionally supported language feature, are essentially the same as traditional C arrays except that they are declared with a size that is not a constant integer expression and can be declared only at block scope or function prototype scope and no linkage. When supported, a variable length array can be declared

{ /* Block scope */
  char vla[size];
}

where the integer expression size and the declaration of vla are both evaluated at runtime. If the size argument supplied to a variable length array is not a positive integer value, the behavior is undefined (see undefined behavior 75).  Additionally, if the magnitude of the argument is excessive, the program may behave in an unexpected way. An attacker may be able to leverage this behavior to overwrite critical program data [Griffiths 2006]. The programmer must ensure that size arguments to variable length arrays, especially those derived from untrusted data, are in a valid range.

Because variable length arrays are a conditionally supported feature of C11, their use in portable code should be guarded by testing the value of the macro __STDC_NO_VLA__. Implementations that do not support variable length arrays indicate so by setting __STDC_NO_VLA__ to the integer constant 1.

Noncompliant Code Example

In this noncompliant code example, a variable length array of size size is declared. The size is declared as size_t in compliance with INT01-C. Use rsize_t or size_t for all integer values representing the size of an object.

#include <stddef.h>
 
void func(size_t size) {
  int vla[size];
  /* ... */
}

However, the value of size may be zero or excessive, potentially giving rise to a security vulnerability.

Compliant Code Solution

This compliant solution ensures the size argument used to allocate vla is in a valid range (between 1 and a programmer-defined maximum); otherwise, it uses an algorithm that relies on dynamic memory allocation:

#include <stdlib.h>
 
enum { MAX_ARRAY = 1024 };
extern void do_work(int *array, size_t size);
 
void func(size_t size) {
  if (0 < size && size < MAX_ARRAY) {
    int vla[size];
    do_work(vla, size);
  } else {
    int *array = (int *)malloc(size * sizeof(int));
    if (array == NULL) {
      /* Handle error */
    }
    do_work(array, size);
    free(array);
  }
}

Implementation Details

Microsoft

Variable length arrays are not supported by Microsoft compilers.

GCC

Variable length arrays should be used on GCC with great care. Newer versions of GCC have incorporated variable length arrays but do not yet claim full C conformance. GCC has limited incomplete support for parts of this standard, enabled with -std=c11 or -std=iso9899:2011.

On an example Debian GNU/Linux Intel 32-bit test machine with GCC 4.2.2, the value of a variable length array's size is interpreted as a 32-bit signed integer. Passing in a negative number for the size will likely cause the program stack to become corrupted, and passing in a large positive number may cause a terminal stack overflow. It is important to note that this information may become outdated as GCC evolves.

Risk Assessment

Failure to properly specify the size of a variable length array may allow arbitrary code execution or result in stack exhaustion.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ARR32-C

High

Probable

High

P6

L2

Automated Detection

Tool

Version

Checker

Description

Coverity6.5REVERSE_NEGATIVEFully implemented
LDRA tool suite 9.7.1621 SEnhanced Enforcement
PRQA QA-C
Unable to render {include} The included page could not be found.
1051Partially implemented
Cppcheck

This page was automatically generated and should not be edited.

The information on this page was provided by outside contributors and has not been verified by SEI CERT.

The table below can be re-ordered, by clicking column headers.

Tool Version:  2.15

Checker

Guideline

arrayIndexOutOfBounds ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
arrayIndexOutOfBoundsCond ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
arrayIndexThenCheck ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
cert.py EXP42-C. Do not compare padding data
cert.py EXP46-C. Do not use a bitwise operator with a Boolean-like operand
ignoredReturnValue EXP12-C. Do not ignore values returned by functions
leakReturnValNotUsed MEM31-C. Free dynamically allocated memory when no longer needed
leakReturnValNotUsed EXP12-C. Do not ignore values returned by functions
memsetValueOutOfRange INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data
negativeArraySize ARR32-C. Ensure size arguments for variable length arrays are in a valid range
negativeIndex ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
nullPointer EXP34-C. Do not dereference null pointers
nullPointerDefaultArg EXP34-C. Do not dereference null pointers
nullPointerRedundantCheck EXP34-C. Do not dereference null pointers
outOfBounds ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
possibleBufferAccessOutOfBounds ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
preprocessorErrorDirective PRE30-C. Do not create a universal character name through concatenation
preprocessorErrorDirective PRE32-C. Do not use preprocessor directives in invocations of function-like macros
shiftNegative INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand
shiftTooManyBits INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand
uninitdata EXP33-C. Do not read uninitialized memory
uninitMemberVar EXP33-C. Do not read uninitialized memory
uninitstring EXP33-C. Do not read uninitialized memory
uninitStructMember EXP33-C. Do not read uninitialized memory
uninitvar EXP33-C. Do not read uninitialized memory
zerodiv INT33-C. Ensure that division and remainder operations do not result in divide-by-zero errors
zerodivcond INT33-C. Ensure that division and remainder operations do not result in divide-by-zero errors
negativeArraySize

Context sensitive analysis.

Will only warn if given size is negative.

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

CERT C Secure Coding StandardINT01-C. Use rsize_t or size_t for all integer values representing the size of an object
ISO/IEC TR 24772:2013Unchecked Array Indexing [XYZ]
ISO/IEC TS 17961:2013Tainted, potentially mutilated, or out-of-domain integer values are used in a restricted sink [taintsink]

Bibliography

 


  • No labels