{"@ID": "1339", "@Name": "Insufficient Precision or Accuracy of a Real Number", "@Abstraction": "Base", "@Structure": "Simple", "@Status": "Draft", "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": {"xhtml:p": ["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 \u03a0 or \u221a2. Rounding of numbers can lead to situations where the computer results do not adequately match the result of sufficiently accurate math."]}, "Related_Weaknesses": {"Related_Weakness": [{"@Nature": "ChildOf", "@CWE_ID": "682", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "PeerOf", "@CWE_ID": "190", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "CanPrecede", "@CWE_ID": "834", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "CanPrecede", "@CWE_ID": "119", "@View_ID": "1000"}]}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": {"@Class": "Not Language-Specific", "@Prevalence": "Undetermined"}, "Operating_System": {"@Class": "Not OS-Specific", "@Prevalence": "Undetermined"}, "Architecture": {"@Class": "Not Architecture-Specific", "@Prevalence": "Undetermined"}, "Technology": {"@Class": "Not Technology-Specific", "@Prevalence": "Undetermined"}}, "Background_Details": {"Background_Detail": {"xhtml:ul": {"xhtml:li": ["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."]}, "#text": "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."}}, "Modes_Of_Introduction": {"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": {"Consequence": [{"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", "Availability", "Access Control"], "Impact": ["Read Application Data", "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."}]}, "Detection_Methods": {"Detection_Method": {"@Detection_Method_ID": "DM-14", "Method": "Automated Static Analysis", "Description": "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 having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect \"sources\" (origins of input) with \"sinks\" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)"}}, "Potential_Mitigations": {"Mitigation": {"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."}}, "Demonstrative_Examples": {"Demonstrative_Example": [{"Intro_Text": "Muller's Recurrence is a series that is supposed to converge to the number 5. When running this series with the following code, different implementations of real numbers fail at specific iterations:", "Example_Code": [{"@Nature": "Bad", "@Language": "Rust", "xhtml:br": [null, null, null, null, null, null, null], "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "108.0 - ((815.0 - 1500.0 / z) / y);"}, {"@style": "margin-left:1em;", "xhtml:br": [null, null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "x.push(rec_float(x[number - 1], x[number - 2]));"}, "#text": "let mut x: Vec<f64> = vec![4.0, 4.25];\n\t\t\t(2..turns + 1).for_each(|number| \n\t\t\t{\n\t\t\t\n\t\t\t});\n\t\t\t\n\t\t\tx[turns]"}], "#text": "fn rec_float(y: f64, z: f64) -> f64 \n\t\t      {\n\t\t      \n\t\t      }\n\t\t      \n\t\t      fn float_calc(turns: usize) -> f64 \n\t\t      {\n\t\t      \n\t\t      }"}, {"@Nature": "Good", "@Language": "Rust", "xhtml:br": [null, null, null, null, null, null, null, null, null], "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "#text": "- ((BigRational::from_integer(BigInt::from(815))\n\t\t\t  - BigRational::from_integer(BigInt::from(1500)) / z)\n\t\t\t  / y)"}, "#text": "BigRational::from_integer(BigInt::from(108))"}, {"@style": "margin-left:1em;", "xhtml:br": [null, null, null, null, null, null], "xhtml:div": {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "x.push(rec_big(x[number - 1].clone(), x[number - 2].clone()));"}, "#text": "let mut x: Vec<BigRational> = vec![BigRational::from_float(4.0).unwrap(), BigRational::from_float(4.25).unwrap(),];\n\t\t\t\n\t\t\t(2..turns + 1).for_each(|number| \n\t\t\t{\n\t\t\t\n\t\t\t});\n\t\t\tx[turns].clone()"}], "#text": "Use num_rational::BigRational;\n                      \n\t\t      fn rec_big(y: BigRational, z: BigRational) -> BigRational\n\t\t      {\n\t\t      \n\t\t      }\n\t\t      \n\t\t      fn big_calc(turns: usize) -> BigRational \n\t\t      {\n\t\t      \n\t\t      }"}], "Body_Text": {"xhtml:img": {"@src": "/data/images/Mullers-Recurrence-CWE-1339.png", "@alt": "Muller's Recurrence"}, "#text": "The chart below shows values for different data structures in the rust language when Muller's recurrence is executed to 80 iterations. The data structure f64 is a 64 bit float. The data structures I<number>F<number> are fixed representations 128 bits in length that use the first number as the size of the integer and the second size as the size of the fraction (e.g. I16F112 uses 16 bits for the integer and 112 bits for the fraction). The data structure of Ratio comes in three different implementations: i32 uses a ratio of 32 bit signed  integers,  i64 uses a ratio of 64 bit signed integers and BigInt uses a ratio of signed integer with up to 2^32 digits of base 256.  Notice how even with 112 bits of fractions or ratios of 64bit unsigned integers, this math still does not converge to an expected value of 5."}}, {"Intro_Text": "On February 25, 1991, during the eve of the Iraqi invasion of Saudi Arabia, a Scud missile fired from Iraqi positions hit a US Army barracks in Dhahran, Saudi Arabia. It miscalculated time and killed 28 people [REF-1190].", "References": {"Reference": {"@External_Reference_ID": "REF-1190"}}}, {"Intro_Text": "Sleipner A, an offshore drilling platform in the North Sea, was incorrectly constructed with an underestimate of 50% of strength in a critical cluster of buoyancy cells needed for construction. This led to a leak in buoyancy cells during lowering, causing a seismic event of 3.0 on the Richter Scale and about $700M loss [REF-1281].", "References": {"Reference": {"@External_Reference_ID": "REF-1281"}}}]}, "Observed_Examples": {"Observed_Example": [{"Reference": "CVE-2018-16069", "Description": "Chain: series of floating-point precision errors\n\t\t\t(CWE-1339) in a web browser rendering engine causes out-of-bounds read\n\t\t\t(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\n\t\t\t(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\n\t\t\tbuffer overflow (CWE-122) when some integer-oriented bounds are\n\t\t\tcalculated by using ceiling() and floor() on floating point values\n\t\t\t(CWE-1339)", "Link": "https://www.cve.org/CVERecord?id=CVE-2021-29529"}, {"Reference": "CVE-2008-2108", "Description": "Chain: insufficient precision (CWE-1339) in\n\t\t\trandom-number generator causes some zero bits to be reliably\n\t\t\tgenerated, 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\n\t\t\tlooping logic [that relies on] floating point math [CWE-1339] to exit\n\t\t\tthe loop [CWE-835]\"", "Link": "https://www.cve.org/CVERecord?id=CVE-2006-6499"}]}, "References": {"Reference": [{"@External_Reference_ID": "REF-1186"}, {"@External_Reference_ID": "REF-1187"}, {"@External_Reference_ID": "REF-1188"}, {"@External_Reference_ID": "REF-1189"}, {"@External_Reference_ID": "REF-1190"}, {"@External_Reference_ID": "REF-1191"}]}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.", "Comments": "Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.", "Reasons": {"Reason": {"@Type": "Acceptable-Use"}}}, "Content_History": {"Submission": {"Submission_Name": "CWE Content Team", "Submission_Organization": "MITRE", "Submission_Date": "2021-07-08", "Submission_Version": "4.5", "Submission_ReleaseDate": "2021-07-20"}, "Modification": [{"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2022-10-13", "Modification_Version": "4.9", "Modification_ReleaseDate": "2022-10-13", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-01-31", "Modification_Version": "4.10", "Modification_ReleaseDate": "2023-01-31", "Modification_Comment": "updated Common_Consequences, Description, Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2024-02-29", "Modification_Version": "4.14", "Modification_ReleaseDate": "2024-02-29", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-04-03", "Modification_Version": "4.17", "Modification_ReleaseDate": "2025-04-03", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Detection_Factors, Weakness_Ordinalities"}]}}
