Use of Incorrect Operator

Draft Base
Structure: Simple
Description

The product accidentally uses the wrong operator, which changes the logic in security-relevant ways.

Extended Description

These types of errors are generally the result of a typo by the programmer.

Common Consequences 1
Scope: Other

Impact: Alter Execution Logic

This weakness can cause unintended logic to be executed and other unexpected application behavior.

Detection Methods 2
Automated Static Analysis
This weakness can be found easily using static analysis. However in some cases an operator might appear to be incorrect, but is actually correct and reflects unusual logic within the program.
Manual Static Analysis
This weakness can be found easily using static analysis. However in some cases an operator might appear to be incorrect, but is actually correct and reflects unusual logic within the program.
Demonstrative Examples 3

ID : DX-140

The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.

Code Example:

Bad
C
c

Code Example:

Bad
C#
c#
However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". The result of using the assignment operator instead of the comparison operator causes the int variable to be reassigned locally and the expression in the if statement will always evaluate to the value on the right hand side of the expression. This will result in the input value not being properly validated, which can cause unexpected results.

ID : DX-103

The following C/C++ example shows a simple implementation of a stack that includes methods for adding and removing integer values from the stack. The example uses pointers to add and remove integer values to the stack array variable.

Code Example:

Bad
C
c

// Print stack overflow error message and exit* } *p1 == i;}

c
c

// initialize tos and p1 to point to the top of stack* tos = stack; p1 = stack;

c
The push method includes an expression to assign the integer value to the location in the stack pointed to by the pointer variable.
However, this expression uses the comparison operator "==" rather than the assignment operator "=". The result of using the comparison operator instead of the assignment operator causes erroneous values to be entered into the stack and can cause unexpected results.
The example code below is taken from the CVA6 processor core of the HACK@DAC'21 buggy OpenPiton SoC. Debug access allows users to access internal hardware registers that are otherwise not exposed for user access or restricted access through access control protocols. Hence, requests to enter debug mode are checked and authorized only if the processor has sufficient privileges. In addition, debug accesses are also locked behind password checkers. Thus, the processor enters debug mode only when the privilege level requirement is met, and the correct debug password is provided.
The following code [REF-1377] illustrates an instance of a vulnerable implementation of debug mode. The core correctly checks if the debug requests have sufficient privileges and enables the debug_mode_d and debug_mode_q signals. It also correctly checks for debug password and enables umode_i signal.

Code Example:

Bad
Verilog

module csr_regfile #( ...

verilog

assign priv_lvl_o = (debug_mode_q || umode_i) ? riscv::PRIV_LVL_M : priv_lvl_q;** ...

verilog
However, it grants debug access and changes the privilege level, priv_lvl_o, even when one of the two checks is satisfied and the other is not. Because of this, debug access can be granted by simply requesting with sufficient privileges (i.e., debug_mode_q is enabled) and failing the password check (i.e., umode_i is disabled). This allows an attacker to bypass the debug password checking and gain debug access to the core, compromising the security of the processor.
A fix to this issue is to only change the privilege level of the processor when both checks are satisfied, i.e., the request has enough privileges (i.e., debug_mode_q is enabled) and the password checking is successful (i.e., umode_i is enabled) [REF-1378].

Code Example:

Good
Verilog

module csr_regfile #( ...

verilog

(debug_mode_q && umode_i) ? riscv::PRIV_LVL_M : priv_lvl_q;** ...

verilog
Observed Examples 2
CVE-2022-3979Chain: data visualization program written in PHP uses the "!=" operator instead of the type-strict "!==" operator (Use of Incorrect Operator) when validating hash values, potentially leading to an incorrect type conversion (Incorrect Type Conversion or Cast)
CVE-2021-3116Chain: Python-based HTTP Proxy server uses the wrong boolean operators (Use of Incorrect Operator) causing an incorrect comparison (Incorrect Comparison) that identifies an authN failure if all three conditions are met instead of only one, allowing bypass of the proxy authentication (Weak Authentication)
References 4
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
Likelihood of Exploit

Low

Applicable Platforms
Languages:
C : SometimesC++ : SometimesPerl : SometimesNot Language-Specific : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • CLASP
  • CERT C Secure Coding
  • CERT C Secure Coding
  • Software Fault Patterns