...
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 7572.) 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 it 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 rsizesize_t or sizersize_t for all integer values representing the size of an object.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stddef.h>
extern void do_work(int *array, size_t size);
void func(size_t size) {
int vla[size];
do_work(vla, size);
}
|
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdlib.h>
#include <string.h>
enum { N1 = 4096 };
void *func(size_t n2) {
typedef int A[n2][N1];
A *array = malloc(sizeof(A));
if (!array) {
/* Handle error */
return NULL;
}
for (size_t i = 0; i != n2; ++i) {
memset(array[i], 0, N1 * sizeof(int));
}
return array;
}
|
Furthermore, this code also violates ARR39-C. Do not add or subtract a scaled integer to a pointer, where array is a pointer to the two-dimensional array, where it should really be a pointer to the latter dimension instead. This means that the memset() call does out-of-bounds writes on all of its invocations except the first.
Compliant Solution (sizeof)
This compliant solution prevents sizeof wrapping by detecting the condition before it occurs and avoiding the subsequent computation when the condition is detected. The code also uses an additional typedef to fix the type of array so that memset() never writes past the two-dimensional array.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4096 };
void *func(size_t n2) {
if (n2 > SIZE_MAX / (N1 * sizeof(int))) {
/* Prevent sizeof wrapping */
return NULL;
}
typedef int A1[N1];
typedef A1 A[n2][N1];
AA1 *array = (A1*) malloc(sizeof(A));
if (!array) {
/* Handle error */
return NULL;
}
for (size_t i = 0; i != n2; ++i) {
memset(array[i], 0, N1 * sizeof(int));
}
return array;
}
|
...
Failure to properly specify the size of a variable length array may allow arbitrary code execution or result in stack exhaustion.
Rule | Severity | Likelihood | Detectable | RepairableRemediation Cost | Priority | Level |
|---|---|---|---|---|---|---|
ARR32-C | High | Probable | No | HighNo | P6 | L2 |
Automated Detection
Tool | Version | Checker | Description | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| ALLOC.SIZE.IOFLOW | Integer Overflow of Allocation Size | |||||||||||||||||
| Coverity |
| REVERSE_NEGATIVE | Fully implemented | |||||||||||||||||
| Cppcheck |
| negativeArraySize | ||||||||||||||||||
| Cppcheck Premium |
| negativeArraySize premium-cert-arr32-c | ||||||||||||||||||
| Helix QAC |
| C1051 | ||||||||||||||||||
| Klocwork |
| MISRA.ARRAY.VAR_LENGTH.2012 | ||||||||||||||||||
| LDRA tool suite |
| 621 S | Enhanced enforcement | |||||||||||||||||
| Parasoft C/C++test |
| CERT_C-ARR32-a | Ensure the size of the variable length array is in valid range | |||||||||||||||||
| PC-lint Plus |
| 9035 | Assistance provided | |||||||||||||||||
| Polyspace Bug Finder |
| Checks for:
Rule fully covered. | PRQA QA-C | |||||||||||||||||
| Include Page | PRQA QA-C_v | PRQA QA-C_v | 1051 | Partially implemented | Cppcheck | |||||||||||||||
| Include Page | Cppcheck_V | Cppcheck_V | negativeArraySize | |||||||||||||||||
| Security Reviewer - Static Reviewer | 6.02 | C101 | Fully Implemented | Context sensitive analysis|||||||||||||||||
| TrustInSoft Analyzer |
| alloca_bounds | Exhaustively verified. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...