Leveraging Time-of-Check and Time-of-Use (TOCTOU) Race Conditions
Attack Pattern ID: 29 (Standard Attack Pattern Completeness: Complete)Typical Severity: HighStatus: Draft
+ Description


This attack targets a race condition occurring between the time of check (state) for a resource and the time of use of a resource. The typical example is the file access. The attacker can leverage a file access race condition by "running the race", meaning that he 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 attacker could do something such as replace the file and cause an escalation of privilege.

Attack Execution Flow

  1. The attacker explores to gauge what level of access he has.

  2. The attacker confirms access to a resource on the target host. The attacker confirms ability to modify the targeted resource.

  3. The attacker decides to leverage the race condition by "running the race", meaning that he 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 attacker can replace the resource and cause an escalation of privilege.

+ Attack Prerequisites

A resource is access/modified concurrently by multiple processes.

The attacker is able to modify resource.

A race condition exists while accessing a resource.

+ Typical Likelihood of Exploit

Likelihood: High

+ Methods of Attack
  • Time and State
  • Modification of Resources
+ Examples-Instances


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.

Related Vulnerabilities



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 attacker can replace the file referenced by the name.

include <sys/types.h>
include <fcntl.h>
include <unistd.h>
define FILE "/tmp/myfile"
define UID 100
void test(char *str)
int fd;
fd = creat(FILE, 0644);
if(fd == -1)
chown(FILE, UID, -1); /* BAD */
int main(int argc, char **argv)
char *userstr;
if(argc > 1) {
userstr = argv[1];
return 0;
//Source : SAMATE.NIST.GOV : http://samate.nist.gov/SRD/view_testcase.php?login=Guest&;tID=1598
+ Attacker Skills or Knowledge Required

Skill or Knowledge Level: Medium

This attack can get sophisticated since the attack has to occur within a short interval of time.

+ Probing Techniques

Vulnerability testing tool can be used to probe for race condition.

The attacker may also look for temporary file creation. The attacker may try to replace them and take advantage of a race condition.

+ Solutions and Mitigations

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.

+ Attack Motivation-Consequences
  • Data Modification
  • Privilege Escalation
  • Run Arbitrary Code
  • Information Leakage
  • Denial of Service
+ Related Weaknesses
CWE-IDWeakness NameWeakness Relationship Type
367Time-of-check Time-of-use (TOCTOU) Race ConditionTargeted
368Context Switching Race ConditionSecondary
366Race Condition within a ThreadSecondary
370Missing Check for Certificate Revocation after Initial CheckSecondary
362Race ConditionSecondary
662Insufficient SynchronizationTargeted
691Insufficient Control Flow ManagementTargeted
663Use of a Non-reentrant Function in an Unsynchronized ContextTargeted
665Improper InitializationSecondary
+ Related Attack Patterns
NatureTypeIDNameDescriptionView(s) this relationship pertains toView\(s\)
ChildOfAttack PatternAttack Pattern26Leveraging Race Conditions 
Mechanism of Attack1000
ChildOfCategoryCategory172Time and State Attacks 
Mechanism of Attack (primary)1000
ParentOfAttack PatternAttack Pattern27Leveraging Race Conditions via Symbolic Links 
Mechanism of Attack1000
+ Related Security Principles
  • Least Privilege

+ Purposes
  • Exploitation
+ CIA Impact
Confidentiality Impact: HighIntegrity Impact: HighAvailability Impact: Low
+ Technical Context
Architectural Paradigms
+ References

J. Viega and G. McGraw. Building Secure Software. Addison-Wesley, 2002.

CWE - Input Validation

+ Content History
Eric DalciCigital, Inc2007-01-25
Sean BarnumCigital, Inc2007-03-07Review and revise
Richard StruseVOXEM, Inc2007-03-26Review and feedback leading to changes in Name, Attack Flow, Attack Prerequisites and Related Attack Patterns
Sean BarnumCigital, Inc2007-04-13Modified pattern content according to review and feedback