...
The C Standard defines arithmetic on atomic integer types as read-modify-write operations with the same representation as regular integer types. As a result, wrapping of atomic unsigned integers is identical to regular unsigned integers and should also be prevented or detected.
Anchor | ||||
---|---|---|---|---|
|
Addition
Addition is between two operands of arithmetic type or between a pointer to an object type and an integer type. This rule only applies to addition between two operands of arithmetic type (see ARR37-C. Do not add or subtract an integer to a pointer to a non-array object and ARR30-C. Do not form or use out-of-bounds pointers or array subscripts).
Incrementing is equivalent to adding 1.
Noncompliant Code Example
This noncompliant code example can result in an unsigned integer wrap during the addition of the unsigned operands ui_a
and ui_b
. If this behavior is unexpected, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner that can lead to an exploitable vulnerability.
Code Block | ||||
---|---|---|---|---|
| ||||
void func(unsigned int ui_a, unsigned int ui_b) { unsigned int usum = ui_a + ui_b; /* ... */ } |
Compliant Solution (Precondition Test)
This compliant solution performs a precondition test of the operands of the addition to guarantee there is no possibility of unsigned wrap:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <limits.h> void func(unsigned int ui_a, unsigned int ui_b) { unsigned int usum; if (UINT_MAX - ui_a < ui_b) { /* Handle error */ } else { usum = ui_a + ui_b; } /* ... */ } |
Compliant Solution (Postcondition Test)
This compliant solution performs a postcondition test to ensure that the result of the unsigned addition operation usum
is not less than the first operand:
...
Anchor | ||||
---|---|---|---|---|
|
Subtraction
Subtraction is between two operands of arithmetic type, two pointers to qualified or unqualified versions of compatible object types, or a pointer to an object type and an integer type. This rule only applies to subtraction between two operands of arithmetic type (see ARR36-C. Do not subtract or compare two pointers that do not refer to the same array, ARR37-C. Do not add or subtract an integer to a pointer to a non-array object, and ARR30-C. Do not form or use out-of-bounds pointers or array subscripts for information about pointer subtraction).
Decrementing is equivalent to subtracting 1.
Noncompliant Code Example
This noncompliant code example can result in an unsigned integer wrap during the subtraction of the unsigned operands ui_a
and ui_b
. If this behavior is unanticipated, it may lead to an exploitable vulnerability.
Code Block | ||||
---|---|---|---|---|
| ||||
void func(unsigned int ui_a, unsigned int ui_b) { unsigned int udiff = ui_a - ui_b; /* ... */ } |
Compliant Solution (Precondition Test)
This compliant solution performs a precondition test of the unsigned operands of the subtraction operation to guarantee there is no possibility of unsigned wrap:
Code Block | ||||
---|---|---|---|---|
| ||||
void func(unsigned int ui_a, unsigned int ui_b) { unsigned int udiff; if (ui_a < ui_b){ /* Handle error */ } else { udiff = ui_a - ui_b; } /* ... */ } |
Compliant Solution (Postcondition Test)
This compliant solution performs a postcondition test that the result of the unsigned subtraction operation udiff
is not greater than the minuend:
...
Anchor | ||||
---|---|---|---|---|
|
Multiplication
Multiplication is between two operands of arithmetic type.
Noncompliant Code Example
The Mozilla Foundation Security Advisory 2007-01 describes a heap buffer overflow vulnerability in the Mozilla Scalable Vector Graphics (SVG) viewer resulting from an unsigned integer wrap during the multiplication of the signed int
value pen->num_vertices
and the size_t
value sizeof(cairo_pen_vertex_t)
[VU#551436]. The signed int
operand is converted to size_t
prior to the multiplication operation so that the multiplication takes place between two size_t
integers, which are unsigned (see INT02-C. Understand integer conversion rules).
...
The unsigned integer wrap can result in allocating memory of insufficient size.
Compliant Solution
This compliant solution tests the operands of the multiplication to guarantee that there is no unsigned integer wrap:
Code Block | ||||
---|---|---|---|---|
| ||||
pen->num_vertices = _cairo_pen_vertices_needed( gstate->tolerance, radius, &gstate->ctm ); if (pen->num_vertices > SIZE_MAX / sizeof(cairo_pen_vertex_t)) { /* Handle error */ } pen->vertices = malloc( pen->num_vertices * sizeof(cairo_pen_vertex_t) ); |
Exceptions
INT30-C-EX1: Unsigned integers can exhibit modulo behavior (wrapping) when necessary for the proper execution of the program. It is recommended that the variable declaration be clearly commented as supporting modulo behavior and that each operation on that integer also be clearly commented as supporting modulo behavior.
...