CWE-413 资源加锁不恰当

admin 2022年1月5日20:59:33CWE(弱点枚举)评论14 views4311字阅读14分22秒阅读模式

CWE-413 资源加锁不恰当

Improper Resource Locking

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: unkown

基本描述

The software does not lock or does not correctly lock a resource when the software must have exclusive access to the resource.

扩展描述

When a resource is not properly locked, an attacker could modify the resource while it is being operated on by the software. This might violate the software's assumption that the resource will not change, potentially leading to unexpected behaviors.

相关缺陷

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

适用平台

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

常见的影响

范围 影响 注释
['Integrity', 'Availability'] ['Modify Application Data', 'DoS: Instability', 'DoS: Crash, Exit, or Restart']

可能的缓解方案

Architecture and Design

策略:

Use a non-conflicting privilege scheme.

['Architecture and Design', 'Implementation']

策略:

Use synchronization when locking a resource.

示例代码

The following function attempts to acquire a lock in order to perform operations on a shared resource.

bad C

void f(pthread_mutex_t mutex) {

pthread_mutex_lock(mutex);

/ access shared resource */

pthread_mutex_unlock(mutex);

}

However, the code does not check the value returned by pthread_mutex_lock() for errors. If pthread_mutex_lock() cannot acquire the mutex for any reason the function may introduce a race condition into the program and result in undefined behavior.

In order to avoid data races correctly written programs must check the result of thread synchronization functions and appropriately handle all errors, either by attempting to recover from them or reporting it to higher levels.

good C

int f(pthread_mutex_t mutex) {

int result;

result = pthread_mutex_lock(mutex);
if (0 != result)

return result;

/ access shared resource */

return pthread_mutex_unlock(mutex);

}

This Java example shows a simple BankAccount class with deposit and withdraw methods.

bad Java

public class BankAccount {


// variable for bank account balance

private double accountBalance;

// constructor for BankAccount

public BankAccount() {

accountBalance = 0;

}

// method to deposit amount into BankAccount

public void deposit(double depositAmount) {


double newBalance = accountBalance + depositAmount;
accountBalance = newBalance;

}

// method to withdraw amount from BankAccount

public void withdraw(double withdrawAmount) {


double newBalance = accountBalance - withdrawAmount;
accountBalance = newBalance;

}

// other methods for accessing the BankAccount object

...

}

However, the deposit and withdraw methods have shared access to the account balance private class variable. This can result in a race condition if multiple threads attempt to call the deposit and withdraw methods simultaneously where the account balance is modified by one thread before another thread has completed modifying the account balance. For example, if a thread attempts to withdraw funds using the withdraw method before another thread that is depositing funds using the deposit method completes the deposit then there may not be sufficient funds for the withdraw transaction.

To prevent multiple threads from having simultaneous access to the account balance variable the deposit and withdraw methods should be synchronized using the synchronized modifier.

good Java

public class BankAccount {

...
// synchronized method to deposit amount into BankAccount

public synchronized void deposit(double depositAmount) {

...

}

// synchronized method to withdraw amount from BankAccount

public synchronized void withdraw(double withdrawAmount) {

...

}

...

}

An alternative solution is to use a lock object to ensure exclusive access to the bank account balance variable. As shown below, the deposit and withdraw methods use the lock object to set a lock to block access to the BankAccount object from other threads until the method has completed updating the bank account balance variable.

good Java

public class BankAccount {

...
// lock object for thread access to methods

private ReentrantLock balanceChangeLock;

// condition object to temporarily release lock to other threads

private Condition sufficientFundsCondition;

// method to deposit amount into BankAccount

public void deposit(double amount) {


// set lock to block access to BankAccount from other threads

balanceChangeLock.lock();
try {

double newBalance = balance + amount;
balance = newBalance;

// inform other threads that funds are available

sufficientFundsCondition.signalAll();

} catch (Exception e) {...}
finally {

// unlock lock object
balanceChangeLock.unlock();

}

}

// method to withdraw amount from bank account

public void withdraw(double amount) {


// set lock to block access to BankAccount from other threads

balanceChangeLock.lock();
try {

while (balance


// temporarily unblock access

// until sufficient funds are available

sufficientFundsCondition.await();

}
double newBalance = balance - amount;
balance = newBalance;

} catch (Exception e) {...}
finally {

// unlock lock object
balanceChangeLock.unlock();

}

}
...

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

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月5日20:59:33
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  CWE-413 资源加锁不恰当 http://cn-sec.com/archives/612861.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: