结构: Simple
Abstraction: Base
状态: Draft
被利用可能性: Low
The program uses an expression in which operator precedence causes incorrect logic to be used.
While often just a bug, operator precedence logic errors can have serious consequences if they are used in security-critical code, such as making an authentication decision.
Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Rarely'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Rarely'}, {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Rarely'}]
范围 | 影响 | 注释 |
---|---|---|
['Confidentiality', 'Integrity', 'Availability'] | ['Varies by Context', 'Unexpected State'] | The consequences will vary based on the context surrounding the incorrect precedence. In a security decision, integrity or confidentiality are the most likely results. Otherwise, a crash may occur due to the software reaching an unexpected state. |
策略:
Regularly wrap sub-expressions in parentheses, especially in security-critical code.
In the following example, the method validateUser makes a call to another method to authenticate a username and password for a user and returns a success or failure code.
bad C
However, the method that authenticates the username and password is called within an if statement with incorrect operator precedence logic. Because the comparison operator "==" has a higher precedence than the assignment operator "=", the comparison operator will be evaluated first and if the method returns FAIL then the comparison will be true, the return variable will be set to true and SUCCESS will be returned. This operator precedence logic error can be easily resolved by properly using parentheses within the expression of the if statement, as shown below.
good C
In this example, the method calculates the return on investment for an accounting/financial application. The return on investment is calculated by subtracting the initial investment costs from the current value and then dividing by the initial investment costs.
bad Java
However, the return on investment calculation will not produce correct results because of the incorrect operator precedence logic in the equation. The divide operator has a higher precedence than the minus operator, therefore the equation will divide the initial investment costs by the initial investment costs which will only subtract one from the current value. Again this operator precedence logic error can be resolved by the correct use of parentheses within the equation, as shown below.
good Java
Note that the initialInvestment variable in this example should be validated to ensure that it is greater than zero to avoid a potential divide by zero error (CWE-369).
标识 | 说明 | 链接 |
---|---|---|
CVE-2008-2516 | Authentication module allows authentication bypass because it uses "(x = call(args) == SUCCESS)" instead of "((x = call(args)) == SUCCESS)". | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2516 |
CVE-2008-0599 | Chain: Language interpreter calculates wrong buffer size (CWE-131) by using "size = ptr ? X : Y" instead of "size = (ptr ? X : Y)" expression. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0599 |
CVE-2001-1155 | Chain: product does not properly check the result of a reverse DNS lookup because of operator precedence (CWE-783), allowing bypass of DNS-based access restrictions. | https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2001-1155 |
映射的分类名 | ImNode ID | Fit | Mapped Node Name |
---|---|---|---|
CERT C Secure Coding | EXP00-C | Exact | Use parentheses for precedence of operation |
SEI CERT Perl Coding Standard | EXP04-PL | CWE More Abstract | Do not mix the early-precedence logical operators with late-precedence logical operators |