What it is: Time-of-check Time-of-use (TOCTOU) Race Condition (CWE-367) is a vulnerability where the state of a resource can change between a check and its use, leading to potential security issues.
Why it matters: This race condition allows attackers to manipulate resources before they are used by legitimate processes, compromising integrity and potentially altering application logic.
How to fix it: Ensure atomicity in resource operations or use locking mechanisms to prevent unauthorized changes between checks and uses.
TL;DR: Time-of-check Time-of-use (TOCTOU) Race Condition is a race condition vulnerability where an attacker can change the state of a resource after checking but before using it, leading to security issues. Fix it by ensuring atomic operations or implementing proper locking mechanisms.
| Field | Value |
|---|---|
| CWE ID | CWE-367 |
| OWASP Category | Not directly mapped |
| CAPEC | CAPEC-27, CAPEC-29 |
| Typical Severity | Medium |
| Affected Technologies | N/A |
| Detection Difficulty | Moderate |
| Last Updated | 2026-07-29 |
What is Time-of-check Time-of-use (TOCTOU) Race Condition?
Time-of-check Time-of-use (TOCTOU) Race Condition (CWE-367) is a type of race condition vulnerability that occurs when the state of a resource can change between the time it is checked and the time it is used. As defined by the MITRE Corporation under CWE-367, this issue allows an attacker to manipulate resources before they are accessed by legitimate processes.
Quick Summary
Time-of-check Time-of-use (TOCTOU) Race Condition vulnerabilities arise when a resource’s state can change between being checked and actually used, leading to potential security breaches. This is critical for applications that rely on consistent state management across concurrent operations. Jump to: Overview · How It Works · Business Impact · Attack Scenario · Detection · Fixes · Framework-Specific Fixes
Jump to: Quick Summary · Time-of-check Time-of-use (TOCTOU) Race Condition Overview · How Time-of-check Time-of-use (TOCTOU) Race Condition Works · Business Impact of Time-of-check Time-of-use (TOCTOU) Race Condition · Time-of-check Time-of-use (TOCTOU) Race Condition Attack Scenario · How to Detect Time-of-check Time-of-use (TOCTOU) Race Condition · How to Fix Time-of-check Time-of-use (TOCTOU) Race Condition · Framework-Specific Fixes for Time-of-check Time-of-use (TOCTOU) Race Condition · How to Ask AI to Check Your Code for Time-of-check Time-of-use (TOCTOU) Race Condition · Time-of-check Time-of-use (TOCTOU) Race Condition Best Practices Checklist · Time-of-check Time-of-use (TOCTOU) Race Condition FAQ · Vulnerabilities Related to Time-of-check Time-of-use (TOCTOU) Race Condition · References · Scan Your Own Site
Time-of-check Time-of-use (TOCTOU) Race Condition Overview
What
A TOCTOU race condition occurs when a resource’s state can change between the time it is checked and used.
Why It Matters
This vulnerability allows attackers to manipulate resources before they are accessed, leading to unauthorized access or unexpected execution logic changes.
Where It Occurs
It typically happens in multi-threaded environments where multiple processes or threads interact with shared resources.
Who Is Affected
Applications that rely on consistent state management across concurrent operations are at risk.
Who Is Not Affected
Systems that ensure atomicity in resource operations and prevent interleaving of checks and uses are not affected by TOCTOU race conditions.
How Time-of-check Time-of-use (TOCTOU) Race Condition Works
Root Cause
The root cause is the inability to guarantee a consistent state between checking and using a shared resource.
Attack Flow
- Attacker identifies a check on a resource.
- Attacker changes the state of the resource before it is used.
- The changed state leads to unexpected behavior or unauthorized access.
Prerequisites to Exploit
- Multiple processes interacting with the same resource.
- Lack of proper synchronization mechanisms.
Vulnerable Code
def use_resource(path):
if os.path.exists(path):
# Resource check
f = open(path, 'r')
data = f.read()
f.close()
use_resource('/path/to/resource') # No validation or locking mechanism
This code is vulnerable because it checks the existence of a resource and then uses it without any synchronization.
Secure Code
def use_resource_securely(path):
with open(path, 'r') as f:
data = f.read()
use_resource_securely('/path/to/resource')
The secure version ensures that the file is opened and read in an atomic operation, preventing race conditions.
Business Impact of Time-of-check Time-of-use (TOCTOU) Race Condition
Confidentiality
- Unauthorized access to sensitive information.
Integrity
- Unexpected execution logic changes.
- Data modification without proper authorization.
Availability
- Resource unavailability due to unauthorized modifications.
Business consequences include financial losses, compliance issues, and reputational damage from security breaches.
Time-of-check Time-of-use (TOCTOU) Race Condition Attack Scenario
- Attacker identifies a resource check in the application.
- Attacker modifies the state of the resource before it is used.
- The modified state leads to unexpected behavior or unauthorized access, compromising system integrity and availability.
How to Detect Time-of-check Time-of-use (TOCTOU) Race Condition
Manual Testing
- Review code for checks followed by use operations without proper synchronization.
- Test concurrent access scenarios manually.
Automated Scanners (SAST / DAST)
Static analysis can detect potential race conditions, while dynamic testing simulates multi-threaded environments to identify actual vulnerabilities.
PenScan Detection
PenScan’s scanner engines like ZAP and Nuclei help in identifying TOCTOU race condition vulnerabilities.
False Positive Guidance
A real finding will show a resource check followed by use without proper synchronization. False positives may arise from benign checks that are not exploitable.
How to Fix Time-of-check Time-of-use (TOCTOU) Race Condition
- Ensure atomicity in resource operations.
- Use locking mechanisms before performing any check on shared resources.
- Limit the amount of time between checking and using a resource.
Framework-Specific Fixes for Time-of-check Time-of-use (TOCTOU) Race Condition
Python/Django
Ensure that file operations are performed atomically or use synchronization mechanisms to prevent race conditions.
def secure_file_operation(path):
with open(path, 'r') as f:
data = f.read()
How to Ask AI to Check Your Code for Time-of-check Time-of-use (TOCTOU) Race Condition
Review the following Python code block for potential CWE-367 Time-of-check Time-of-use (TOCTOU) Race Condition vulnerabilities and rewrite it using proper synchronization techniques: [paste code here]
Time-of-check Time-of-use (TOCTOU) Race Condition Best Practices Checklist
✅ Ensure atomicity in resource operations. ✅ Use locking mechanisms before performing any check on shared resources. ✅ Limit the amount of time between checking and using a resource.
Time-of-check Time-of-use (TOCTOU) Race Condition FAQ
How does a TOCTOU race condition occur?
A TOCTOU race condition occurs when the state of a resource is checked before it is used, and an attacker can change that state between the check and use to invalidate the results of the initial check.
What are the common consequences of a TOCTOU race condition?
Common consequences include unauthorized access to resources, unexpected execution logic changes, and potential data modification or deletion without proper authorization.
How can you detect a TOCTOU race condition in your code?
Detecting a TOCTOU race condition involves manual testing for resource checks followed by use operations that are susceptible to race conditions. Automated scanners may also help identify such vulnerabilities.
What is the primary fix technique for preventing TOCTOU race conditions?
The primary fix technique involves ensuring atomicity in resource operations or using locking mechanisms before performing any check on a shared resource.
How can you prevent TOCTOU race conditions when developing software?
Prevent TOCTOU race conditions by avoiding checks before use, limiting interleaving of operations, and implementing robust synchronization techniques to ensure consistent state management.
What are the best practices for mitigating TOCTOU race conditions in existing codebases?
Mitigate TOCTOU race conditions by rechecking resources after use calls, ensuring environmental locking mechanisms protect shared resources effectively, and limiting the time between checks and uses of shared resources.
How can you test your application to ensure it is free from TOCTOU race conditions?
Test for TOCTOU race conditions through manual code review focusing on resource checks before use operations. Use automated tools that simulate concurrent access scenarios to identify potential vulnerabilities.
Vulnerabilities Related to Time-of-check Time-of-use (TOCTOU) Race Condition
| CWE | Name | Relationship | |—|—|—| | CWE-362 | Concurrent Execution using Shared Resource with Improper Synchronization (‘Race Condition’) | ChildOf |
References
Scan Your Own Site
Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find Time-of-check Time-of-use (TOCTOU) Race Condition and other risks before an attacker does.