CWE-480 使用操作符不正确

Use of Incorrect Operator

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: Low

基本描述

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

扩展描述

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

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 670 cwe_View_ID: 1000 cwe_Ordinal: Primary

适用平台

Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Sometimes'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Sometimes'}, {'cwe_Name': 'Perl', 'cwe_Prevalence': 'Sometimes'}, {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}]

常见的影响

范围 影响 注释
Other Alter Execution Logic This weakness can cause unintended logic to be executed and other unexpected application behavior.

检测方法

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.

示例代码

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

bad C

int isValid(int value) {
if (value=100) {
printf("Value is valid\n");
return(1);
}
printf("Value is not valid\n");
return(0);
}

bad C#

bool isValid(int value) {
if (value=100) {
Console.WriteLine("Value is valid.");
return true;
}
Console.WriteLine("Value is not valid.");
return false;
}

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.

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.

bad C

#define SIZE 50
int tos, p1, stack[SIZE];

void push(int i) {
p1++;
if(p1==(tos+SIZE)) {

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

int pop(void) {
if(p1==tos) {

// Print stack underflow error message and exit
}
p1--;
return
(p1+1);
}

int main(int argc, char *argv[]) {

// initialize tos and p1 to point to the top of stack
tos = stack;
p1 = stack;
// code to add and remove items from stack
...
return 0;
}

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.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Using the wrong operator
CERT C Secure Coding EXP45-C CWE More Abstract Do not perform assignments in selection statements
CERT C Secure Coding EXP46-C CWE More Abstract Do not use a bitwise operator with a Boolean-like operand

引用