Mutexes are used to prevent multiple threads from causing a data race by accessing shared resources at the same time. Sometimes, when locking mutexes, multiple threads hold each other's lock, and the program consequently deadlocks. Four conditions are required for deadlock to occur:
- Mutual exclusion
- Hold and wait
- No preemption
- Circular wait
Deadlock needs all four conditions, so preventing deadlock requires preventing any one of the four conditions. One simple solution is to lock the mutexes in a predefined order, which prevents circular wait.
Noncompliant Code Example
The behavior of this noncompliant code example depends on the runtime environment and the platform's scheduler. The program is susceptible to deadlock if thread thr1
attempts to lock ba2
's mutex at the same time thread thr2
attempts to lock ba1
's mutex in the deposit()
function.
#include <mutex> #include <thread> class bank_account { int balance; public: std::mutex balance_mutex; bank_account() = delete; bank_account(int initial_amount) : balance(initial_amount) {} int get_balance() { return balance; } void set_balance(int amount) { balance = amount; } }; int deposit(bank_account *from, bank_account *to, int amount) { std::lock_guard<std::mutex> from_guard(from->balance_mutex); /* Not enough balance to transfer */ if (from->get_balance() < amount) { return -1; /* Indicate error */ } std::lock_guard<std::mutex> to_guard(to->balance_mutex); from->set_balance(from->get_balance() - amount); to->set_balance(to->get_balance() + amount); return 0; } int main(void) { bank_account *ba1 = new bank_account(1000); bank_account *ba2 = new bank_account(1000); /* Perform the deposits */ std::thread thr1(deposit, ba1, ba2, 100); std::thread thr2(deposit, ba2, ba1, 100); thr1.join(); thr2.join(); return 0; }
Compliant Solution
This compliant solution eliminates the circular wait condition by establishing a predefined order for locking in the deposit()
function. Each thread will lock on the basis of the bank_account
ID, which is set when the bank_account
object is initialized.
#include <atomic> #include <mutex> #include <thread> class bank_account { static std::atomic<unsigned int> global_id; const unsigned int id; int balance; public: std::mutex balance_mutex; bank_account() = delete; bank_account(int initial_amount) : id(global_id++), balance(initial_amount) {} unsigned int get_id() { return id; } int get_balance() { return balance; } void set_balance(int amount) { balance = amount; } }; std::atomic<unsigned int> bank_account::global_id(1); int deposit(bank_account *from, bank_account *to, int amount) { int result = -1; std::mutex *first; std::mutex *second; if (from->get_id() == to->get_id()) { return -1; /* Indicate error */ } /* Ensure proper ordering for locking */ if (from->get_id() < to->get_id()) { first = &from->balance_mutex; second = &to->balance_mutex; } else { first = &to->balance_mutex; second = &from->balance_mutex; } std::lock_guard<std::mutex> first_guard(*first); std::lock_guard<std::mutex> second_guard(*second); /* Check for enough balance to transfer */ if (from->get_balance() >= amount) { from->set_balance(from->get_balance() - amount); to->set_balance(to->get_balance() + amount); result = 0; } return result; }
Risk Assessment
Deadlock prevents multiple threads from progressing, halting program execution. A denial-of-service attack is possible if the attacker can create the conditions for deadlock.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
CON53-CPP | Low | Probable | Medium | P4 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
CodeSonar | 8.1p0 | CONCURRENCY.LOCK.ORDER | Conflicting lock order |
Coverity | 6.5 | DEADLOCK | Fully implemented |
Parasoft C/C++test | 9.5 | BD-TRS-DLOCK | Fully implemented |
Related Guidelines
CERT Oracle Secure Coding Standard for Java | LCK07-J. Avoid deadlock by requesting and releasing locks in the same order |
MITRE CWE | CWE-764, Multiple Locks of a Critical Resource |