This vulnerability occurs when a program verifies a resource's state (like a file's permissions or existence) but then uses it after that state has already changed. The gap between checking and using creates a race window where an attacker can manipulate the resource, causing the program to operate on invalid or malicious data.

Think of this as a classic bait-and-switch attack against your code. Your application acts on a decision that was correct a moment ago but is now dangerously wrong. For example, your code might check that a file is owned by a safe user, but an attacker quickly swaps it with a symbolic link to a sensitive system file before your code opens it. This time-of-check to time-of-use (TOCTOU) gap is especially common in file system operations, but can also affect shared memory, process states, or even security tokens. To defend against this, you must design your operations to be atomic—meaning the check and the use must happen as a single, uninterruptible action. Use file handles or descriptors instead of pathnames after validation, leverage file-locking mechanisms carefully, or employ secure APIs that internally manage state consistency. Always assume the environment can change between any two consecutive operations, and structure your code to minimize or eliminate these risky time windows.
Impact: Alter Execution LogicUnexpected State
The attacker can gain access to otherwise unauthorized resources.
Impact: Modify Application DataModify Files or DirectoriesModify MemoryOther
Race conditions such as this kind may be employed to gain read or write access to resources which are not normally readable or writable by the user in question.
Impact: Other
The resource in question, or other resources (through the corrupted one), may be changed in undesirable ways by a malicious user.
Impact: Hide Activities
If a file or other resource is written in this method, as opposed to in a valid way, logging of the activity may not occur.
Impact: Other
In some cases it may be possible to delete files a malicious user might not otherwise have access to, such as log files.
Impact: Unexpected State
The product may perform invalid actions when the resource is in an unexpected state.
ccphp
//resolve file if its a symbolic link* if(is_link($filename)){ ``` $filename = readlink($filename); } if(fileowner($filename) == $user){ echo file_get_contents($realFile); return; } else{ echo 'Access denied'; return false; } }
cMedium