Integer Underflow (Wrap or Wraparound)

Draft Base
Structure: Simple
Description

Integer underflow occurs when a subtraction operation results in a value smaller than the data type's minimum limit, causing the value to wrap around to a large, incorrect number.

Extended Description

Integer underflow, also known as wraparound, happens when a program subtracts a larger number from a smaller one, pushing the result below the minimum value the integer type can hold. Instead of throwing an error, the value wraps around to the maximum end of the range—for example, an unsigned 8-bit integer going from 0 to 255 after subtracting 1. This creates a logic flaw where the program operates on a wildly incorrect value, often leading to unexpected behavior. This vulnerability affects both signed and unsigned integers and is a common source of critical security flaws. In signed integers, underflow can flip a positive value to a massive negative one, while in unsigned integers, it creates an unexpectedly large positive value. Developers must validate all subtraction inputs, use safe math libraries, or choose data types with sufficient range to prevent these dangerous wrap-around scenarios.

Common Consequences 3
Scope: Availability

Impact: DoS: Crash, Exit, or RestartDoS: Resource Consumption (CPU)DoS: Resource Consumption (Memory)DoS: Instability

This weakness will generally lead to undefined behavior and therefore crashes. In the case of overflows involving loop index variables, the likelihood of infinite loops is also high.

Scope: Integrity

Impact: Modify Memory

If the value in question is important to data (as opposed to flow), simple data corruption has occurred. Also, if the wrap around results in other conditions such as buffer overflows, further memory corruption may occur.

Scope: ConfidentialityAvailabilityAccess Control

Impact: Execute Unauthorized Code or CommandsBypass Protection Mechanism

This weakness can sometimes trigger buffer overflows which can be used to execute arbitrary code. This is usually outside the scope of a program's implicit security policy.

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Demonstrative Examples 2
The following example subtracts from a 32 bit signed integer.

Code Example:

Bad
C
c
The example has an integer underflow. The value of i is already at the lowest negative value possible, so after subtracting 1, the new value of i is 2147483647.

ID : DX-137

This code performs a stack allocation based on a length calculation.

Code Example:

Bad
C
c
Since a and b are declared as signed ints, the "a - b" subtraction gives a negative result (-1). However, since len is declared to be unsigned, len is cast to an extremely large positive number (on 32-bit systems - 4294967295). As a result, the buffer buf[len] declaration uses an extremely large size to allocate on the stack, very likely more than the entire computer's memory space.
Miscalculations usually will not be so obvious. The calculation will either be complicated or the result of an attacker's input to attain the negative value.
Observed Examples 4
CVE-2004-0816Integer underflow in firewall via malformed packet.
CVE-2004-1002Integer underflow by packet with invalid length.
CVE-2005-0199Long input causes incorrect length calculation.
CVE-2005-1891Malformed icon causes integer underflow in loop counter variable.
References 1
24 Deadly Sins of Software Security
Michael Howard, David LeBlanc, and John Viega
McGraw-Hill
2010
ID: REF-44
Applicable Platforms
Languages:
C : UndeterminedC++ : UndeterminedJava : UndeterminedC# : Undetermined
Modes of Introduction
Implementation
Alternate Terms

Integer underflow

"Integer underflow" is sometimes used to identify signedness errors in which an originally positive number becomes negative as a result of subtraction. However, there are cases of bad subtraction in which unsigned integers are involved, so it's not always a signedness issue. "Integer underflow" is occasionally used to describe array index errors in which the index is negative.
Taxonomy Mapping
  • PLOVER
  • Software Fault Patterns
  • CERT C Secure Coding
  • CERT C Secure Coding