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

Compare with Current View Page History

« Previous Version 55 Next »

Integer values used in any of the the following ways must be guaranteed correct:

  • as an array index
  • in any pointer arithmetic
  • as a length or size of an object
  • as the bound of an array (for example, a loop counter)
  • in security critical code

Most integer operations can result in overflow if the resulting value cannot be represented by the underlying representation of the integer. The following table indicates which operators can result in overflow:

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

 

Operator

Overflow

+

yes

 

-=

yes

 

<<

yes

 

<

no

-

yes

 

*=

yes

 

>>

yes

 

>

no

*

yes

 

/=

yes

 

&

no

 

>=

no

/

yes

 

%=

no

 

|

no

 

<=

no

%

no

 

<<=

yes

 

^

no

 

==

no

++

yes

 

>>=

yes

 

~

no

 

!=

no

--

yes

 

&=

no

 

!

no

 

&&

no

=

no

 

|=

no

 

un +

no

 

||

no

+=

yes

 

^=

no

 

un -

yes

 

?:

no

The following sections examine specific operations that are susceptible to integer overflow. The specific tests that are required to guarantee that the operation does not result in an integer overflow depend on the signedness of the integer types. When operating on small types (smaller than int), integer conversion rules apply. The usual arithmetic conversions may also be applied to (implicitly) convert operands to equivalent types before arithmetic operations are performed. Make sure you understand implicit conversion rules before trying to implement secure arithmetic operations.

----

Unable to render {include} The included page could not be found.

----

Unable to render {include} The included page could not be found.

----

Unable to render {include} The included page could not be found.

----

Division

Division is between two operands of arithmetic type. Overflow can occur during twos-complement signed integer division when the dividend is equal to the minimum (negative) value for the signed integer type and the divisor is equal to -1. Both signed and unsigned division operations are also susceptible to divide-by-zero errors.

Non-Compliant Code Example

This code can result in a signed integer overflow during the division of the signed operands sl1 and sl2 or in a divide-by-zero error. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner that could lead to an exploitable vulnerability.

signed long sl1, sl2, result;

result = sl1 / sl2;

Compliant Solution

This compliant solution tests the suspect division operation to guarantee there is no possibility of signed overflow or divide-by-zero errors.

signed long sl1, sl2, result;

if ( (sl2 == 0) || ( (sl1 == LONG_MIN) && (sl2 == -1) ) ) {
  /* handle error condition */
}
result = sl1 / sl2;

----

Unary Negation

The unary negation operator takes an operand of arithmetic type. Overflow can occur during twos-complement unary negation when the operand is equal to the minimum (negative) value for the signed integer type.

Non-Compliant Code Example

This code can result in a signed integer overflow during the unary negation of the signed operand si1. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner that could lead to an exploitable vulnerability.

signed int si1, result;

result = -si1;

Compliant Solution

This compliant solution tests the suspect negation operation to guarantee there is no possibility of signed overflow.

signed int si1, result;

if (si1 == INT_MIN) {
  /* handle error condition */
}
result = -si1;

----

Left Shift Operator

The left shift operator is between two operands of integer type.

Non-Compliant Code Example

This code can result in an unsigned overflow during the shift operation of the unsigned operands ui1 and ui2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner that could lead to an exploitable vulnerability.

unsigned int ui1, ui2, result;

result = ui1 << ui2;

Compliant Solution

This compliant solution tests the suspect shift operation to guarantee there is no possibility of unsigned overflow.

unsigned int ui1, ui2, result;

if ( (ui2 < 0) || (ui2 >= sizeof(int)*8) ) {
  /* handle error condition */
}
result = ui1 << ui2;

----

Right Shift Operator

The shift operator is between two operands of integer type.

Non-Compliant Code Example

This code can result in an unsigned overflow during the shift operation of the unsigned operands ui1 and ui2. If this behavior is unanticipated, the resulting value may be used to allocate insufficient memory for a subsequent operation or in some other manner that could lead to an exploitable vulnerability.

unsigned int ui1, ui2, result;

result = ui1 >> ui2;

Compliant Solution

This compliant solution tests the suspect shift operation to guarantee there is no possibility of unsigned overflow.

unsigned int ui1, ui2, result;

if ( (ui2 < 0) || (ui2 >= sizeof(int)*8) ) {
  /* handle error condition */
}
result = ui1 >> ui2;

Exceptions

Unsigned integers can be allowed to exhibit modulo behavior if and only if

  1. the variable declaration is clearly commented as supporting modulo behavior
  2. each operation on that integer is also clearly commented as supporting modulo behavior

If the integer exhibiting modulo behavior contributes to the value of an integer not marked as exhibiting modulo behavior, the resulting integer must obey this rule.


Priority: P6 Level: L2

Integer overflow can lead to buffer overflows and the execution of arbitrary code by an attacker.

Component

Value

Severity

3 (high)

Likelihood

2 (probable)

Remediation cost

1 (high)

References

  • No labels