CWE-570 表达式永假

admin 2021年12月4日16:21:44评论63 views字数 4433阅读14分46秒阅读模式

CWE-570 表达式永假

Expression is Always False

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: unkown

基本描述

The software contains an expression that will always evaluate to false.

相关缺陷

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

  • cwe_Nature: CanPrecede cwe_CWE_ID: 561 cwe_View_ID: 1000

  • cwe_Nature: CanPrecede cwe_CWE_ID: 561 cwe_View_ID: 699

适用平台

Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
Other ['Quality Degradation', 'Varies by Context']

可能的缓解方案

Testing

策略:

Use Static Analysis tools to spot such conditions.

示例代码

In the following Java example the updateUserAccountOrder() method used within an e-business product ordering/inventory application will validate the product number that was ordered and the user account number. If they are valid, the method will update the product inventory, the user account, and the user order appropriately.

bad Java


public void updateUserAccountOrder(String productNumber, String accountNumber) {

boolean isValidProduct = false;
boolean isValidAccount = false;

if (validProductNumber(productNumber)) {

isValidProduct = true;
updateInventory(productNumber);

}
else {

return;

}

if (validAccountNumber(accountNumber)) {

isValidProduct = true;
updateAccount(accountNumber, productNumber);

}

if (isValidProduct && isValidAccount) {

updateAccountOrder(accountNumber, productNumber);

}

}

However, the method never sets the isValidAccount variable after initializing it to false so the isValidProduct is mistakenly used twice. The result is that the expression "isValidProduct && isValidAccount" will always evaluate to false, so the updateAccountOrder() method will never be invoked. This will create serious problems with the product ordering application since the user account and inventory databases will be updated but the order will not be updated.

This can be easily corrected by updating the appropriate variable.

good

...
if (validAccountNumber(accountNumber)) {

isValidAccount = true;
updateAccount(accountNumber, productNumber);

}
...

In the following example, the hasReadWriteAccess method uses bit masks and bit operators to determine if a user has read and write privileges for a particular process. The variable mask is defined as a bit mask from the BIT_READ and BIT_WRITE constants that have been defined. The variable mask is used within the predicate of the hasReadWriteAccess method to determine if the userMask input parameter has the read and write bits set.

bad C

#define BIT_READ 0x0001 // 00000001
#define BIT_WRITE 0x0010 // 00010000

unsigned int mask = BIT_READ & BIT_WRITE; / intended to use "|" /

// using "&", mask = 00000000
// using "|", mask = 00010001

// determine if user has read and write access
int hasReadWriteAccess(unsigned int userMask) {

// if the userMask has read and write bits set
// then return 1 (true)
if (userMask & mask) {

return 1;

}

// otherwise return 0 (false)
return 0;

}

However the bit operator used to initialize the mask variable is the AND operator rather than the intended OR operator (CWE-480), this resulted in the variable mask being set to 0. As a result, the if statement will always evaluate to false and never get executed.

The use of bit masks, bit operators and bitwise operations on variables can be difficult. If possible, try to use frameworks or libraries that provide appropriate functionality and abstract the implementation.

In the following example, the updateInventory method used within an e-business inventory application will update the inventory for a particular product. This method includes an if statement with an expression that will always evaluate to false. This is a common practice in C/C++ to introduce debugging statements quickly by simply changing the expression to evaluate to true and then removing those debugging statements by changing expression to evaluate to false. This is also a common practice for disabling features no longer needed.

bad C

int updateInventory(char* productNumber, int numberOfItems) {

int initCount = getProductCount(productNumber);

int updatedCount = initCount + numberOfItems;

int updated = updateProductCount(updatedCount);

// if statement for debugging purposes only
if (1 == 0) {


char productName[128];
productName = getProductName(productNumber);

printf("product %s initially has %d items in inventory n", productName, initCount);
printf("adding %d items to inventory for %s n", numberOfItems, productName);

if (updated == 0) {

printf("Inventory updated for product %s to %d items n", productName, updatedCount);

}

else {

printf("Inventory not updated for product: %s n", productName);

}

}

return updated;

}

Using this practice for introducing debugging statements or disabling features creates dead code that can cause problems during code maintenance and potentially introduce vulnerabilities. To avoid using expressions that evaluate to false for debugging purposes a logging API or debugging API should be used for the output of debugging messages.

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
CERT C Secure Coding MSC00-C Compile cleanly at high warning levels
Software Fault Patterns SFP1 Glitch in computation

文章来源于互联网:scap中文网

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月4日16:21:44
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   CWE-570 表达式永假https://cn-sec.com/archives/613386.html

发表评论

匿名网友 填写信息