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.

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.

Affected languages: Not Language-Specific
Affected technologies: Not Technology-Specific

Consequences:
  - Availability: DoS: Crash, Exit, or Restart
  - Integrity: DoS: Crash, Exit, or Restart
  - Access Control: Gain Privileges or Assume Identity
  - Access Control: Bypass Protection Mechanism

Detection methods:
  - Manual Analysis: This weakness can be detected using tools and techniques that require manual (human) analysis, such as penetration testing, threat modeling, and interactive tools that allow the tester to record and m
  - Automated Static Analysis: 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 havin
  - Fuzzing: Fuzz testing (fuzzing) is a powerful technique for generating large numbers of diverse inputs - either randomly or algorithmically - and dynamically invoking the code with those inputs. Even with rand

Mitigations:
  - [Implementation] Understand your programming language's underlying representation and how it interacts with numeric calculation. Pay close attention to byte size discrepancies, precision, signed/unsigned distinctions, truncation, conversion and casting between types, "not-a-number" calculations, and how your languag
  - [Implementation] Perform input validation on any numeric input by ensuring that it is within the expected range. Enforce that the input meets both the minimum and maximum requirements for the expected range.
  - [Implementation] Use the appropriate type for the desired action. For example, in C/C++, only use unsigned types for values that could never be negative, such as height, width, or other numbers related to quantity.
  - [Architecture and Design] Use languages, libraries, or frameworks that make it easier to handle numbers without unexpected consequences. 
                   Examples include safe integer handling packages such as SafeInt (C++) or IntegerLib (C or C++).

Demonstrative examples:
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); ...

This code attempts to calculate a football team's average number of yards gained per touchdown.
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.
... int touchdowns = team.getTouchdowns(); int yardsGained = team.getTotalYardage(); System.out.println(team.getName() + " averages " + yardsGained / touchdowns + "yards gained for every touchdown scored"); ...

Real-world examples:
  - CVE-2020-0022: chain: mobile phone Bluetooth implementation does not include offset when calculating packet length (CWE-682), leading to out-of-bounds write (CWE-787)
  - CVE-2010-1378: Chain: incorrect calculation (CWE-682) allows attackers to bypass certificate checks (CWE-295)
  - CVE-2004-1363: substitution overflow: buffer overflow using environment variables that are expanded after the length check is performed