CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')

Description:
The product contains a concurrent code sequence that requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence operating concurrently.

Extended Description:
A race condition occurs within concurrent environments, and it is effectively a property of a code sequence. Depending on the context, a code sequence may be in the form of a function call, a small number of instructions, a series of program invocations, etc. 
             A race condition violates these properties, which are closely related: 
                
                   Exclusivity - the code sequence is given exclusive access to the shared resource, i.e., no other code sequence can modify properties of the shared resource before the original sequence has completed execution. 
                   Atomicity - the code sequence is behaviorally atomic, i.e., no other thread or process can concurrently execute the same sequence of instructions (or a subset) against the same resource. 
                
             A race condition exists when an "interfering code sequence" can still access the shared resource, violating exclusivity. 
             The interfering code sequence could

Affected languages: C, C++, Java
Affected technologies: Mobile, ICS/OT
Parent weaknesses: CWE-662

Consequences:
  - Availability: DoS: Resource Consumption (CPU)
  - Availability: DoS: Crash, Exit, or Restart
  - Confidentiality: Read Files or Directories
  - Access Control: Execute Unauthorized Code or Commands

Detection methods:
  - Black Box: Black box methods may be able to identify evidence of race conditions via methods such as multiple simultaneous connections, which may cause the software to become instable or crash. However, race con
  - White Box: Common idioms are detectable in white box analysis, such as time-of-check-time-of-use (TOCTOU) file operations (CWE-367), or double-checked locking (CWE-609).
  - Automated Dynamic Analysis: This weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, a
  - Automated Static Analysis - Binary or Bytecode: According to SOAR [REF-1479], the following detection techniques may be useful: 
                   
                      Highly cost effective: 
                         
                           

Mitigations:
  - [Architecture and Design] In languages that support it, use synchronization primitives. Only wrap these around critical code to minimize the impact on performance.
  - [Architecture and Design] Use thread-safe capabilities such as the data access abstraction in Spring.
  - [Architecture and Design] Minimize the usage of shared resources in order to remove as much complexity as possible from the control flow and to reduce the likelihood of unexpected conditions occurring. 
                   Additionally, this will minimize the amount of synchronization necessary and may even help to reduce the
  - [Implementation] When using multithreading and operating on shared variables, only use thread-safe functions.

Demonstrative examples:
This code could be used in an e-commerce application that supports transfers between accounts. It takes the total amount of the transfer, sends it to the new account, and deducts the amount from the original account.
A race condition could occur between the calls to GetBalanceFromDatabase() and SendNewBalanceToDatabase().
$transfer_amount = GetTransferAmount(); $balance = GetBalanceFromDatabase(); 
                      if ($transfer_amount < 0) { FatalError("Bad Transfer Amount"); } $newbalance = $balance - $transfer_amount; if (($balance - $transfer_amount) < 0) { FatalError("Insufficient Funds"); } SendNewBalanceToDatabase($newbalance); NotifyUser("Transfer of $transfer_amount succeeded."); NotifyUser("New balance: $newbalance");
In the following pseudocode, the attacker makes two simul

The following function attempts to acquire a lock in order to perform operations on a shared resource.
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.
void f(pthread_mutex_t *mutex) { 
                         pthread_mutex_lock(mutex); 
                            
                            /* access shared resource */ 
                            
                            
                            pthread_mutex_unlock(mutex); 
                      }
int f(pthread_mutex_t *mutex) { 
                         int result; 
                            result = pthread_mutex_lock(mutex); 

Real-world examples:
  - CVE-2024-38106: OS kernel stores sensitive data in improperly locked memory, allowing local users to gain privileges by winning a race condition
  - CVE-2022-29527: Go application for cloud management creates a world-writable sudoers file that allows local attackers to inject sudo rules and escalate privileges to root by winning a race condition.
  - CVE-2021-1782: Chain: improper locking (CWE-667) leads to race condition (CWE-362), as exploited in the wild per CISA KEV.
  - CVE-2021-0920: Chain: mobile platform race condition (CWE-362) leading to use-after-free (CWE-416), as exploited in the wild per CISA KEV.
  - CVE-2020-6819: Chain: race condition (CWE-362) leads to use-after-free (CWE-416), as exploited in the wild per CISA KEV.