CWE-682: Incorrect Calculation
Description
The product performs a calculation that generates incorrect or unintended results that are later used in security-critical decisions or resource management.
Submission Date :
April 11, 2008, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Extended Description
When product performs a security-critical calculation incorrectly, it might lead to incorrect resource allocations, incorrect privilege assignments, or failed comparisons among other things. Many of the direct results of an incorrect calculation can lead to even larger problems such as failed protection mechanisms or even arbitrary code execution.
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
This code attempts to calculate a football team's average number of yards gained per touchdown.
...int touchdowns = team.getTouchdowns();int yardsGained = team.getTotalYardage();System.out.println(team.getName() + " averages " + yardsGained / touchdowns + "yards gained for every touchdown scored");...
The code does not consider the event that the team they are querying has not scored a touchdown, but has gained yardage. In that case, we should expect an ArithmeticException to be thrown by the JVM. This could lead to a loss of availability if our error handling code is not set up correctly.
Example - 3
This example attempts to calculate the position of the second byte of a pointer. In this example, second_char is intended to point to the second byte of p. But, adding 1 to p actually adds sizeof(int) to p, giving a result that is incorrect (3 bytes off on 32-bit platforms). If the resulting memory address is read, this could potentially be an information leak. If it is a write, it could be a security-critical write to unauthorized memory-- whether or not it is a buffer overflow. Note that the above code may also be wrong in other ways, particularly in a little endian environment.
int *p = x;char * second_char = (char *)(p + 1);
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-128: Wrap-around Error
CWE-131: Incorrect Calculation of Buffer Size
CWE-135: Incorrect Calculation of Multi-Byte String Length
CWE-170: Improper Null Termination
CWE-190: Integer Overflow or Wraparound
CWE-191: Integer Underflow (Wrap or Wraparound)
CWE-193: Off-by-one Error
CWE-369: Divide By Zero
CWE-468: Incorrect Pointer Scaling
CWE-469: Use of Pointer Subtraction to Determine Size
CWE-681: Incorrect Conversion between Numeric Types
CWE-839: Numeric Range Comparison Without Minimum Check
CWE-1335: Incorrect Bitwise Shift of Integer
CWE-1339: Insufficient Precision or Accuracy of a Real Number
Visit http://cwe.mitre.org/ for more details.