Skip to content

CWE-682: Incorrect Calculation

PillarLikelihood of exploit: High

The product performs a calculation that generates incorrect or unintended results that are later used in security-critical decisions or resource management.

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.

Consequences

  • Availability: DoS: Crash, Exit, or Restart
  • Integrity, Confidentiality, Availability: DoS: Crash, Exit, or Restart, DoS: Resource Consumption (Other), Execute Unauthorized Code or Commands
  • Access Control: Gain Privileges or Assume Identity
  • Access Control: Bypass Protection Mechanism

How to prevent it

  • 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 language handles numbers that are too large or too small for its underlying representation.
  • 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++).
  • 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++).
  • Implementation: Examine compiler warnings closely and eliminate problems with potential security implications, such as signed / unsigned mismatch in memory operations, or use of uninitialized variables. Even if the weakness is rarely exploitable, a single failure may lead to the compromise of the entire system.

Recent advisories

  • GHSA-4v7v-gqf9-ww2g — Vyper: Call stack corruption when passing complex type containing non-base type members as argument
  • GHSA-p7gw-2pcp-5pf8 — Klever: Marketplace settlement mints KLV when referral % + royalty % exceed the bid (negative seller share silently skipped)
  • GHSA-c4v7-w88g-m6c4 — ImageMagick: Heap Buffer Over-Write in JP2 encoder when due to incorrect handling of arguments
  • GHSA-jv4h-j224-23cc — Zebra's Block Validator Undercounts Coinbase and P2SH Sigops
  • GHSA-479m-364c-43vc — validateSignature Loop Variable Capture Signature Bypass in goxmldsig
  • GHSA-2p94-8669-xg86 — Vyper's sqrt doesn't define rounding behavior
  • GHSA-6m97-7527-mh74 — incorrect storage layout for contracts containing large arrays
  • GHSA-cx2q-hfxr-rj97 — Vyper's `_abi_decode` input not validated in complex expressions
  • GHSA-hhvx-8755-4cvw — Hashicorp Nomad ACLs Cannot Deny Access to Workload’s Own Variables
  • GO-2023-1621 — Incorrect calculation on P256 curves in crypto/internal/nistec
  • GHSA-878m-3g6q-594q — OpenZeppelin Contracts contains Incorrect Calculation
  • GHSA-xrc4-737v-9q75 — OpenZeppelin Contracts's GovernorVotesQuorumFraction updates to quorum may affect past defeated proposals
  • GHSA-jqwc-c49r-4w2x — Miscompilation of `i8x16.swizzle` and `select` with v128 inputs
  • GHSA-w37f-pvvx-wcwm — Incorrect Calculation in moodle
  • GHSA-7f6x-jwh5-m9r4 — Cranelift vulnerable to miscompilation of constant values in division on AArch64

All advisories for CWE-682

Is this in your code?

MergeHarbor checks every repository's dependencies against this database, ranks what matters by exploitability and reachability, routes it to the owning team and opens the fix pull request.

Check your repos with MergeHarbor