According to C99, Section 6.2.5, "Types", para. 9, and the C standard [ISO/IEC 9899:2011], states:
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
This behavior is more informally referred to as informally called unsigned integer wrapping. Unsigned integer operations can wrap if the resulting value cannot be represented by the underlying representation of the integer. The following table indicates which operators can result in wrapping:
Operator | Wrap |
| Operator | Wrap |
| Operator | Wrap |
| Operator | Wrap |
---|---|---|---|---|---|---|---|---|---|---|
yes |
| yes |
|
| yes |
|
| no | ||
yes |
| yes |
|
| no |
|
| no | ||
yes |
|
| no |
|
| no |
|
| no | |
| no |
|
| no |
|
| no |
|
| no |
| no |
| yes |
|
| no |
|
| no | |
| yes |
|
| no |
|
| no |
|
| no |
| yes |
|
| no |
|
| no |
|
| no |
| no |
|
| no |
|
| no |
|
| no |
yes |
|
| no |
|
| yes |
|
| no |
Although unsigned left shift <<
can result in wrapping, modulo behavior is permitted by this standard because of common usage, because this behavior is usually expected by the programmer, and because the behavior is well-defined.
The following sections examine specific operations that are susceptible to unsigned integer wrap. When operating on small integer types (smaller than int
), integer promotions are applied. The usual arithmetic conversions may also be applied to (implicitly) convert operands to equivalent types before arithmetic operations are performed. Make sure you understand integer conversion rules before trying to implement secure arithmetic operations. (See recommendation INT02-C. Understand integer conversion rules.)
...
Addition is between two operands of arithmetic type or between a pointer to an object type and an integer type. (See rules ARR37-C. Do not add or subtract an integer to a pointer to a non-array object and ARR38-C. Do not add or subtract an integer to a pointer if the resulting value does not refer to a valid array element for information about adding a pointer to an integer.) Incrementing is equivalent to adding one1.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
unsigned int ui1, ui2, usum; /* Initialize ui1 and ui2 */ usum = ui1 + ui2; |
Compliant Solution (
...
Precondition Test)
This compliant solution performs a pre-condition precondition test of the operands of the addition to guarantee there is no possibility of unsigned wrap.
Code Block | ||||
---|---|---|---|---|
| ||||
unsigned int ui1, ui2, usum; /* Initialize ui1 and ui2 */ if (UINT_MAX - ui1 < ui2) { /* handle error condition */ } else { usum = ui1 + ui2; } |
Compliant Solution (
...
Postcondition Test)
This compliant solution performs a post-condition postcondition test to ensure that the result of the unsigned addition operation usum
is not less than the first operand.
...
Subtraction is between two operands of arithmetic type, between two pointers to qualified or unqualified versions of compatible object types, or between a pointer to an object type and an integer type. See rules 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 ARR38-C. Do not add or subtract an integer to a pointer if the resulting value does not refer to a valid array element for information about pointer subtraction. Decrementing is equivalent to subtracting one1.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
unsigned int ui1, ui2, udiff; /* Initialize ui1 and ui2 */ udiff = ui1 - ui2; |
Compliant Solution (
...
Precondition Test)
This compliant solution performs a pre-condition precondition test of the unsigned operands of the subtraction operation to guarantee there is no possibility of unsigned wrap.
Code Block | ||||
---|---|---|---|---|
| ||||
unsigned int ui1, ui2, udiff; /* Initialize ui1 and ui2 */ if (ui1 < ui2){ /* handle error condition */ } else { udiff = ui1 - ui2; } |
Compliant Solution (
...
Postcondition Test)
This compliant solution performs a post-condition postcondition test that the result of the unsigned subtraction operation udiff
is not greater than the minuend.
...
The Mozilla Scalable Vector Graphics (SVG) viewer contains a heap buffer overflow vulnerability 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 recommendation INT02-C. Understand integer conversion rules.)
...
Atomic Integers
The C Standard [ISO/IEC 9899:2011] defines arithmetic on atomic integer types as read-modify-write operations , with the same representation as non-atomic nonatomic integer types. As a result, wrapping of atomic unsigned integers is identical to non-atomic nonatomic unsigned integers and should also be prevented or detected.
This section only includes an example only for the addition of atomic integer types. For other operations, you can use tests similar to the precondition tests for non-atomic nonatomic integer types.
Noncompliant Code Example
...
Code Block |
---|
atomic_int i; int ui1; /* Initialize i, ui1 */ atomic_fetch_add(&i, ui1); |
Compliant Solution
This compliant solution performs a post-condition postcondition test to ensure that the result of the unsigned addition operation to i
is not less than the operand ui1
.
Code Block |
---|
atomic_int i; int ui1; /* Initialize ui1, i */ atomic_fetch_add(&i, ui1); if (atomic_load(&i) < ui1) { /* handle error condition */ } |
Exceptions
INT30-EX1. Unsigned integers can exhibit modulo behavior (wrapping) only when this behavior is 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.
INT32-EX2. Checks for wraparound can be omitted when it can be determined at compile - time that wraparound will not occur. As such, the following operations on unsigned integers require no validation:
- Operations on two compile-time constants.
- Operations on a variable and 0 (except division by 0, of course).
- Subtracting any variable from its type's maximum. For instance, any
unsigned int
may safely be subtracted fromUINT_MAX
. - Multiplying any variable by 1.
- Division, as long as the divisor is nonzero.
- Right-shifting any type maximum by any number smaller than the type size. For instance,
UINT_MAX >> x
is valid as long asx < sizeof(unsigned int)
. - Left-shifting 1 by any number smaller than the type size.
...
Tool | Version | Checker | Description | section|
---|---|---|---|---|
Fortify SCA section | V. 5.0 |
| Section | Can detect violations of this rule with the CERT C Rule Pack section. |
Compass/ROSE |
|
| Section | Can detect violations of this rule by ensuring that operations are checked for overflow before being performed. Be mindful of exception INT30-EX2 because it excuses many operations from requiring validation, including all the operations that would validate a potentially dangerous operation. For instance, adding two s together requires validation involving subtracting one of the numbers from because it cannot wrap. |
Related Vulnerabilities
CVE-2009-1385 results from a violation of this rule. The value performs an unchecked subtraction on the length
of a buffer , and then adds that many bytes of data to another buffer [xorl 2009]. This can cause a buffer overflow, which allows an attacker to execute arbitrary code.
A Linux kernel vmsplice exploit, described at described by Rafal Wojtczuk [Wojtczuk 2008], documents a vulnerability and exploit arising from a buffer overflow (caused by unsigned integer wrapping).
...
CERT C++ Secure Coding Standard: INT30-CPP. Ensure that unsigned integer operations do not wrap
ISO/IEC 9899:19992011 Section 6.2.5, "Types," Section 6.5, "Expressions," and Section 7.10, "Sizes of integer types <limits.h>
"
ISO/IEC TR 24772 "XYY Wrap-around Errorerror"
MITRE CWE: CWE-190, "Integer Overflow overflow (Wrap wrap or Wraparoundwraparound)"
Bibliography
[Dowd 2006] Chapter 6, "C Language Issues" (Arithmetic Boundary Conditionsboundary conditions, pp. 211-223211–223)
[Seacord 2005] Chapter 5, "Integers"
[Viega 2005] Section 5.2.7, "Integer overflow"
[VU#551436]
[Warren 2002] Chapter 2, "Basics"
[Wojtczuk 2008]
[xorl 2009] "CVE-2009-1385: Linux kernel E1000 Integer Underflowinteger underflow"
...