CWE-190: Integer Overflow or Wraparound
Description
The product performs a calculation that can produce an integer overflow or wraparound, when the logic assumes that the resulting value will always be larger than the original value. This can introduce other weaknesses when the calculation is used for resource management or execution control.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-10-26 00:00:00+00:00
Organization :
MITRE
Extended Description
An integer overflow or wraparound occurs when an integer value is incremented to a value that is too large to store in the associated representation. When this occurs, the value may wrap to become a very small or negative number. While this may be intended behavior in circumstances that rely on wrapping, it can have security consequences if the wrap is unexpected. This is especially the case if the integer overflow can be triggered using user-supplied inputs. This becomes security-critical when the result is used to control looping, make a security decision, or determine the offset or size in behaviors such as memory allocation, copying, concatenation, etc.
Example - 1
The following image processing code allocates a table for images. This code intends to allocate a table of size num_imgs, however as num_imgs grows large, the calculation determining the size of the list will eventually overflow (CWE-190). This will result in a very small list to be allocated instead. If the subsequent code operates on the list as if it were num_imgs long, it may result in many types of out-of-bounds problems (CWE-119).
img_t table_ptr; /*struct containing img data, 10kB each*/int num_imgs;...num_imgs = get_num_imgs();table_ptr = (img_t*)malloc(sizeof(img_t)*num_imgs);...
Example - 2
The following code excerpt from OpenSSH 3.3 demonstrates a classic case of integer overflow:
response = xmalloc(nresp*sizeof(char*));for (i = 0; i < nresp; i++) response[i] = packet_get_string(NULL);nresp = packet_get_int();if (nresp > 0) {}
If nresp has the value 1073741824 and sizeof(char*) has its typical value of 4, then the result of the operation nresp*sizeof(char*) overflows, and the argument to xmalloc() will be 0. Most malloc() implementations will happily allocate a 0-byte buffer, causing the subsequent loop iterations to overflow the heap buffer response.
Example - 3
Integer overflows can be complicated and difficult to detect. The following example is an attempt to show how an integer overflow may lead to undefined looping behavior:
bytesRec += getFromInput(buf+bytesRec);short int bytesRec = 0;char buf[SOMEBIGNUM];while(bytesRec < MAXGET) {}
In the above case, it is entirely possible that bytesRec may overflow, continuously creating a lower number than MAXGET and also overwriting the first MAXGET-1 bytes of buf.
Example - 4
In this example the method determineFirstQuarterRevenue is used to determine the first quarter revenue for an accounting/business application. The method retrieves the monthly sales totals for the first three months of the year, calculates the first quarter sales totals from the monthly sales totals, calculates the first quarter revenue based on the first quarter sales, and finally saves the first quarter revenue results to the database.
// // Variable for sales revenue for the quarter//
// // Calculate quarterly total//
// // Calculate the total revenue for the quarter//
float quarterRevenue = 0.0f;short JanSold = getMonthlySales(JAN); /* Get sales in January */short FebSold = getMonthlySales(FEB); /* Get sales in February */short MarSold = getMonthlySales(MAR); /* Get sales in March */short quarterSold = JanSold + FebSold + MarSold;quarterRevenue = calculateRevenueForQuarter(quarterSold);saveFirstQuarterRevenue(quarterRevenue);return 0;#define JAN 1#define FEB 2#define MAR 3short getMonthlySales(int month) {...}float calculateRevenueForQuarter(short quarterSold) {...}int determineFirstQuarterRevenue() {}
However, in this example the primitive type short int is used for both the monthly and the quarterly sales variables. In C the short int primitive type has a maximum value of 32768. This creates a potential integer overflow if the value for the three monthly sales adds up to more than the maximum value for the short int primitive type. An integer overflow can lead to data corruption, unexpected behavior, infinite loops and system crashes. To correct the situation the appropriate primitive type should be used, as in the example below, and/or provide some validation mechanism to ensure that the maximum value for the primitive type is not exceeded.
// // Calculate quarterly total//
// // Calculate the total revenue for the quarter//
...long quarterSold = JanSold + FebSold + MarSold;quarterRevenue = calculateRevenueForQuarter(quarterSold);......float calculateRevenueForQuarter(long quarterSold) {...}int determineFirstQuarterRevenue() {}
Note that an integer overflow could also occur if the quarterSold variable has a primitive type long but the method calculateRevenueForQuarter has a parameter of type short.
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-20: Improper Input Validation
CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer
CWE-128: Wrap-around Error
CWE-680: Integer Overflow to Buffer Overflow
CWE-682: Incorrect Calculation
CWE-1339: Insufficient Precision or Accuracy of a Real Number
Visit http://cwe.mitre.org/ for more details.