CAPEC-29: Leveraging Time-of-Check and Time-of-Use (TOCTOU) Race Conditions
This attack targets a race condition occurring between the time of check (state) for a resource and the time of use of a resource. A typical example is file access. The adversary can leverage a file access race condition by "running the race", meaning that they would modify the resource between the first time the target program accesses the file and the time the target program uses the file. During that period of time, the adversary could replace or modify the file, causing the application to behave unexpectedly.
Last updated
Overview
CAPEC-29 (Leveraging Time-of-Check and Time-of-Use (TOCTOU) Race Conditions) is a standard-level attack pattern catalogued by MITRE in the Common Attack Pattern Enumeration and Classification (CAPEC). It describes a recurring method attackers use to exploit software weaknesses.
How the attack works
The phases an attacker typically follows to carry out this attack.
- Step 1Explore
The adversary explores to gauge what level of access they have.
- Step 2Experiment
The adversary confirms access to a resource on the target host. The adversary confirms ability to modify the targeted resource.
- Step 3Exploit
The adversary decides to leverage the race condition by "running the race", meaning that they would modify the resource between the first time the target program accesses the file and the time the target program uses the file. During that period of time, the adversary can replace the resource and cause an escalation of privilege.
What the attacker needs
Prerequisites
- A resource is access/modified concurrently by multiple processes.
- The adversary is able to modify resource.
- A race condition exists while accessing a resource.
Skills required
- Medium skill: This attack can get sophisticated since the attack has to occur within a short interval of time.
Consequences
What a successful CAPEC-29 attack can achieve.
Modify Data
Affects: Integrity
Gain Privileges
Affects: Confidentiality, Access Control, Authorization
Alter Execution Logic
Affects: Confidentiality, Integrity, Availability
Read Data
Affects: Confidentiality
Resource Consumption
Affects: Availability
Denial of Service
How to mitigate it
Defenses that reduce the risk of CAPEC-29.
- Use safe libraries to access resources such as files.
- Be aware that improper use of access function calls such as chown(), tempfile(), chmod(), etc. can cause a race condition.
- Use synchronization to control the flow of execution.
- Use static analysis tools to find race conditions.
- Pay attention to concurrency problems related to the access of resources.
Examples
The Net Direct client for Linux before 6.0.5 in Nortel Application Switch 2424, VPN 3050 and 3070, and SSL VPN Module 1000 extracts and executes files with insecure permissions, which allows local users to exploit a race condition to replace a world-writable file in /tmp/NetClient and cause another user to execute arbitrary code when attempting to execute this client, as demonstrated by replacing /tmp/NetClient/client. See also: CVE-2007-1057
The following code illustrates a file that is accessed multiple times by name in a publicly accessible directory. A race condition exists between the accesses where an adversary can replace the file referenced by the name. include include include define FILE "/tmp/myfile" define UID 100 void test(char *str) { int fd; fd = creat(FILE, 0644); if(fd == -1) return; chown(FILE, UID, -1); /* BAD */ close(fd); } int main(int argc, char **argv) { char *userstr; if(argc > 1) { userstr = argv[1]; test(userstr); } return 0; } [REF-107]
Frequently asked questions
Common questions about CAPEC-29.
- What is CAPEC-29?
- This attack targets a race condition occurring between the time of check (state) for a resource and the time of use of a resource. A typical example is file access. The adversary can leverage a file access race condition by "running the race", meaning that they would modify the resource between the first time the target program accesses the file and the time the target program uses the file. During that period of time, the adversary could replace or modify the file, causing the application to behave unexpectedly.
- How does a Leveraging Time-of-Check and Time-of-Use (TOCTOU) Race Conditions attack work?
- It typically unfolds over 3 phases. It begins with: The adversary explores to gauge what level of access they have.
- How do you prevent CAPEC-29?
- Use safe libraries to access resources such as files.
- What weaknesses does CAPEC-29 target?
- CAPEC-29 exploits 9 CWE weaknesses, including CWE-362 (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')), CWE-366 (Race Condition within a Thread), CWE-367 (Time-of-check Time-of-use (TOCTOU) Race Condition), CWE-368 (Context Switching Race Condition).
- How severe is CAPEC-29?
- MITRE rates CAPEC-29 as High severity with high likelihood of attack.
References
Attack-pattern data is sourced from the MITRE CAPEC catalog (v3.9). Weakness associations link to the corresponding CWE entries on RadicalNotion.AI.
Defend against CAPEC-29
Track the CVEs and weaknesses attackers exploit with this technique, with AI-written analysis and remediation guidance.