CWE-413: Improper Resource Locking

Description

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

Submission Date :

July 19, 2006, midnight

Modification Date :

2023-10-26 00:00:00+00:00

Organization :

MITRE
Extended Description

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

Example Vulnerable Codes

Example - 1

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


// /* access shared resource */// 
pthread_mutex_lock(mutex);pthread_mutex_unlock(mutex);void f(pthread_mutex_t *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 them to higher levels.


return result;
// /* access shared resource */// 
int result;result = pthread_mutex_lock(mutex);if (0 != result)return pthread_mutex_unlock(mutex);int f(pthread_mutex_t *mutex) {}

Example - 2

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


// // variable for bank account balance// 
// // constructor for BankAccount// 
accountBalance = 0;
// // method to deposit amount into BankAccount// 

double newBalance = accountBalance + depositAmount;accountBalance = newBalance;
// // method to withdraw amount from BankAccount// 

double newBalance = accountBalance - withdrawAmount;accountBalance = newBalance;
// // other methods for accessing the BankAccount object// 
private double accountBalance;public BankAccount() {}public void deposit(double depositAmount) {}public void withdraw(double withdrawAmount) {}...public class BankAccount {}

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.


// // synchronized method to deposit amount into BankAccount// 
...
// // synchronized method to withdraw amount from BankAccount// 
...
...public synchronized void deposit(double depositAmount) {}public synchronized void withdraw(double withdrawAmount) {}...public class BankAccount {}

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.


// // lock object for thread access to methods// 
// // condition object to temporarily release lock to other threads// 
// // method to deposit amount into BankAccount// 

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

// // inform other threads that funds are available// 
double newBalance = balance + amount;balance = newBalance;sufficientFundsCondition.signalAll();

// unlock lock objectbalanceChangeLock.unlock();balanceChangeLock.lock();try {} catch (Exception e) {...}finally {}
// // method to withdraw amount from bank account// 

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

// // temporarily unblock access// 
// // until sufficient funds are available// 
sufficientFundsCondition.await();
while (balance < amount) {}double newBalance = balance - amount;balance = newBalance;

// unlock lock objectbalanceChangeLock.unlock();balanceChangeLock.lock();try {} catch (Exception e) {...}finally {}
...private ReentrantLock balanceChangeLock;private Condition sufficientFundsCondition;public void deposit(double amount) {}public void withdraw(double amount) {}...public class BankAccount {}

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.

© cvefeed.io
Latest DB Update: Nov. 24, 2024 0:18