CWE-1339: Insufficient Precision or Accuracy of a Real Number (Base)

Description: The product processes a real number with an implementation in which the number's representation does not preserve required accuracy and precision in its fractional part, causing an incorrect result.

Extended Description: When a security decision or calculation requires highly precise, accurate numbers such as financial calculations or prices, then small variations in the number could be exploited by an attacker. There are multiple ways to store the fractional part of a real number in a computer. In all of these cases, there is a limit to the accuracy of recording a fraction. If the fraction can be represented in a fixed number of digits (binary or decimal), there might not be enough digits assigned to represent the number. In other cases the number cannot be represented in a fixed number of digits due to repeating in decimal or binary notation (e.g. 0.333333...) or due to a transcendental number such as Π or √2. Rounding of numbers can lead to situations where the computer results do not adequately match the result of sufficiently accurate math.

Background Details: ::There are three major ways to store real numbers in computers. Each method is described along with the limitations of how they store their numbers. Fixed: Some implementations use a fixed number of binary bits to represent both the integer and the fraction. In the demonstrative example about Muller's Recurrence, the fraction 108.0 - ((815.0 - 1500.0 / z) / y) cannot be represented in 8 binary digits. The numeric accuracy within languages such as PL/1, COBOL and Ada is expressed in decimal digits rather than binary digits. In SQL and most databases, the length of the integer and the fraction are specified by the programmer in decimal. In the language C, fixed reals are implemented according to ISO/IEC TR18037 Floating: The number is stored in a version of scientific notation with a fixed length for the base and the significand. This allows flexibility for more accuracy when the integer portion is smaller. When dealing with large integers, the fractional accuracy is less. Languages such as PL/1, COBOL and Ada set the accuracy by decimal digit representation rather than using binary digits. Python also implements decimal floating-point numbers using the IEEE 754-2008 encoding method. Ratio: The number is stored as the ratio of two integers. These integers also have their limits. These integers can be stored in a fixed number of bits or in a vector of digits. While the vector of digits method provides for very large integers, they cannot truly represent a repeating or transcendental number as those numbers do not ever have a fixed length.::


Applicable Platforms: ::LANGUAGE CLASS:Not Language-Specific:LANGUAGE PREVALENCE:Undetermined::OPERATING SYSTEM CLASS:Not OS-Specific:OPERATING SYSTEM PREVALENCE:Undetermined::ARCHITECTURE CLASS:Not Architecture-Specific:ARCHITECTURE PREVALENCE:Undetermined::TECHNOLOGY CLASS:Not Technology-Specific:TECHNOLOGY PREVALENCE:Undetermined::

Modes of Introduction: ::PHASE:Implementation:NOTE:This weakness is introduced when the developer picks a method to represent a real number. The weakness may only be visible with very specific numeric inputs.::



Common Consequences: ::SCOPE:Availability:IMPACT:DoS: Crash, Exit, or Restart:NOTE:This weakness will generally lead to undefined results and therefore crashes. In some implementations the program will halt if the weakness causes an overflow during a calculation.::SCOPE:Integrity:IMPACT:Execute Unauthorized Code or Commands:NOTE:The results of the math are not as expected. This could cause issues where a value would not be properly calculated and provide an incorrect answer.::SCOPE:Confidentiality:SCOPE:Availability:SCOPE:Access Control:IMPACT:Read Application Data:IMPACT:Modify Application Data:NOTE:This weakness can sometimes trigger buffer overflows which can be used to execute arbitrary code. This is usually outside the scope of a product's implicit security policy.::


Potential Mitigations: ::PHASE:Implementation Patching and Maintenance:DESCRIPTION:The developer or maintainer can move to a more accurate representation of real numbers. In extreme cases, the programmer can move to representations such as ratios of BigInts which can represent real numbers to extremely fine precision. The programmer can also use the concept of an Unum real. The memory and CPU tradeoffs of this change must be examined. Since floating point reals are used in many products and many locations, they are implemented in hardware and most format changes will cause the calculations to be moved into software resulting in slower products.::

Observed Examples: ::REFERENCE:CVE-2018-16069:DESCRIPTION:Chain: series of floating-point precision errors (CWE-1339) in a web browser rendering engine causes out-of-bounds read (CWE-125), giving access to cross-origin data:LINK:https://www.cve.org/CVERecord?id=CVE-2018-16069::REFERENCE:CVE-2017-7619:DESCRIPTION:Chain: rounding error in floating-point calculations (CWE-1339) in image processor leads to infinite loop (CWE-835):LINK:https://www.cve.org/CVERecord?id=CVE-2017-7619::REFERENCE:CVE-2021-29529:DESCRIPTION:Chain: machine-learning product can have a heap-based buffer overflow (CWE-122) when some integer-oriented bounds are calculated by using ceiling() and floor() on floating point values (CWE-1339):LINK:https://www.cve.org/CVERecord?id=CVE-2021-29529::REFERENCE:CVE-2008-2108:DESCRIPTION:Chain: insufficient precision (CWE-1339) in random-number generator causes some zero bits to be reliably generated, reducing the amount of entropy (CWE-331):LINK:https://www.cve.org/CVERecord?id=CVE-2008-2108::REFERENCE:CVE-2006-6499:DESCRIPTION:Chain: web browser crashes due to infinite loop - bad looping logic [that relies on] floating point math [CWE-1339] to exit the loop [CWE-835]:LINK:https://www.cve.org/CVERecord?id=CVE-2006-6499::