Integer overflow is a vulnerability that lets a malicious hacker trick the program into performing an integer operation whose result exceeds the allocated memory space. Apart from causing unexpected program behavior, this can also lead to the much more dangerous buffer overflow.
The integer overflow vulnerability was listed by CWE (the Common Weakness Enumeration system) in 2022 at position 13, outranking several major web vulnerabilities.
Severity: |
![]() ![]() ![]() |
severe |
Prevalence: |
![]() |
discovered rarely |
Scope: |
![]() ![]() |
applications with direct memory allocation |
Technical impact: | unexpected program behavior | |
Worst-case consequences: | buffer overflow | |
Quick fix: | examine the operands and results |
In most programming languages, integer values are allocated a fixed number of bits in memory. For example, space reserved for a 32-bit integer data type may hold an unsigned integer between 0 and 4,294,967,295 or a signed integer between −2,147,483,648 and 2,147,483,647. For signed integers, the most significant (first) bit usually indicates whether the integer is a positive value or a negative value.
But what happens when you perform a calculation like 4,294,967,295 + 1 and attempt to store the result as normal even though it exceeds the maximum value for the integer type? The behavior depends completely on the language and the compiler. Most languages and compilers raise no error at all – they might perform a modulo operation, wraparound, or truncation, or have other undefined behavior. In most cases, the result of the above increment operation will be 0.
With signed integer overflows, the results can be even more unexpected than for unsigned int. When you go above the maximum value for a signed integer, the result usually becomes a negative number. For example, 2,147,483,647 +1 is usually −2,147,483,648. When you go below the minimum value of a negative number (integer underflow), the result usually becomes a positive number. For example, −2,147,483,648 − 1 would give you 2,147,483,647.
In addition to typical operations such as addition, subtraction, or multiplication, integer overflows may also happen due to typecasting. For example, one operation may treat the same integer as unsigned and another operation as signed, therefore interpreting the value incorrectly.
If you want to know more about integer overflows, we recommend the extensive Phrack article by blexim.
An excellent example of an integer overflow is a real-life vulnerability in an older version of OpenSSH (3.3). The following code snippet comes directly from this software:
nresp = packet_get_int();
if (nresp > 0) {
 response = xmalloc(nresp*sizeof(char*));
 for (i = 0; i < nresp; i++)
  response[i] = packet_get_string(NULL);
}
If nresp
is 1073741824 and sizeof(char*)
is 4 (which is the typical size of one character), then calculating the buffer size using nresp*sizeof(char*)
gives a zero size, resulting in an overflow. Therefore, xmalloc()
receives and allocates a 0-byte buffer. The subsequent loop causes a heap buffer overflow, which may, in turn, be used by an attacker to execute arbitrary code.
Another example of a known, recent, and serious integer overflow vulnerability is CVE-2022-36934 in WhatsApp, which allows the attacker to follow up with remote code execution in an established video call.
Most integer overflow conditions lead to erroneous program behavior but do not cause vulnerabilities. However, for some applications and algorithms, integer overflows may have severe consequences:
The best way to detect integer overflow vulnerabilities depends on whether they are already known or unknown.
Depending on the language, you may be able to come across frameworks, libraries, or mechanisms that help you prevent integer overflows.
In languages that provide different integer types to allocate different amounts of memory based on the size of the integer, programmers can prevent integer overflows by taking care to always choose a type big enough to accommodate all potential results. Some programming languages, such as Python, make such allocations automatically, preventing most integer overflows by design.
Classification | ID |
---|---|
CAPEC | 92 |
CWE | 190, 680 |
WASC | 3 |
OWASP 2021 | – |
Integer overflow is a software weakness that causes a program to behave unexpectedly when it exceeds the maximum supported integer value. In most cases, the affected program does not even report an error, only generates an incorrect result. Integer overflow weaknesses are difficult to discover and exploit but can appear in most programming languages and most types of software.
Learn more about integer overflows from an extensive article in Phrack.
The impact of integer overflow errors depends on how the resulting integer values are used. If they are used, for example, for financial operations, attackers may be able to tamper with balances or perform other types of fraud. If they are used to allocate memory, the attacker may follow up with a buffer overflow attack.
Find out more about buffer overflow, which may be the result of integer overflow.
Integer overflow errors are very difficult to detect and prevent. The most serious problem is that there is no error or warning, and you simply get the incorrect result of the operation. The only way to detect and prevent integer overflow is the validation of the operands or the result. You may also be able to find frameworks, libraries, or other mechanisms for your programming language that help with such issues.