Race Condition within a Thread

Draft Base
Structure: Simple
Description

This vulnerability occurs when two or more threads within the same application access and manipulate a shared resource (like a variable, data structure, or file) without proper synchronization. Because the threads can execute in an unpredictable order, they can corrupt the resource's state, leading to crashes, incorrect calculations, or data loss.

Extended Description

Unlike race conditions between separate processes, this issue happens entirely within a single program's threads. It's a flaw in the program's internal logic where the developer assumed certain operations would complete in a specific sequence, but the operating system's thread scheduler can interleave them arbitrarily. Common triggers include checking a flag or counter in one thread while another is modifying it, or performing non-atomic 'read-modify-write' operations on shared data. To prevent this, developers must use proper synchronization primitives like mutexes, semaphores, or atomic operations. These tools create critical sections that ensure only one thread can access the shared resource at a time, guaranteeing predictable and valid states. Failing to implement synchronization correctly—or incorrectly assuming certain operations are thread-safe—leaves the application's behavior undefined and unreliable.

Common Consequences 1
Scope: IntegrityOther

Impact: Alter Execution LogicUnexpected State

The main problem is that -- if a lock is overcome -- data could be altered in a bad state.

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 2
Phase: Architecture and Design
Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multithreaded environment.
Phase: Architecture and Design
Create resource-locking validation checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.
Demonstrative Examples 1
The following example demonstrates the weakness.

Code Example:

Bad
C
c

Code Example:

Bad
Java
java
Observed Examples 1
CVE-2022-2621Chain: two threads in a web browser use the same resource (Race Condition within a Thread), but one of those threads can destroy the resource before the other has completed (Use After Free).
References 3
The CLASP Application Security Process
Secure Software, Inc.
2005
ID: REF-18
24 Deadly Sins of Software Security
Michael Howard, David LeBlanc, and John Viega
McGraw-Hill
2010
ID: REF-44
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
Likelihood of Exploit

Medium

Applicable Platforms
Languages:
C : UndeterminedC++ : UndeterminedJava : UndeterminedC# : Undetermined
Modes of Introduction
Implementation
Affected Resources
  1. System Process
Taxonomy Mapping
  • CLASP
  • CERT C Secure Coding
  • CERT C Secure Coding
  • CERT C Secure Coding
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • Software Fault Patterns