TOCTOU (time-of-check to time-of-use) is a problem where the state of something changes between the moment it is checked and the moment it is used. This page explains how it works and how to prevent it (no attack steps).
What TOCTOU is
Programs often work as "check, then use if it looks fine": for example, "check that this file may be written, then open and write it." But there is a small time gap between the check and the use. If another program or request changes the target in that gap, the result of the check no longer holds.
MITRE's CWE-367 describes this as a product that checks the state of a resource before using it, where the state can change between the check and the use in a way that invalidates the check. CWE (Common Weakness Enumeration) is a shared catalog of the weaknesses behind vulnerabilities. TOCTOU is a kind of race condition, where the outcome depends on the timing of things that run at the same time.
Why it happens
According to CWE-367, consequences can include access to resources that should be off limits, resources changed in unwanted ways, and missing logs. The gap may be short, but in systems that run many things at once it is hit repeatedly, not just by chance.
Where it shows up
| Place | Check | Use | What goes wrong |
|---|---|---|---|
| File handling | Existence or permission check | Open or write | The file is swapped after the check |
| Web app stock or balance | SELECT the remaining count | UPDATE to reduce it | Two concurrent requests both pass and exceed the limit |
| Payments and sign-ups | Is it still unprocessed? | Run the action | A double submit runs it twice |
| File uploads | Inspect the content | Store or publish | The content changes after inspection |
For upload inspection, see also file upload vulnerabilities.
Defense: don't separate check from use
Stop checking before using
CWE-367's most basic advice is not to perform a check before the use. Use the resource directly and handle the error if it fails. That removes the gap itself.
Use file descriptors, not paths
Open the file first, then check state and write through the open file descriptor. Looking up the same path name repeatedly can point to a different target each time. Flags such as O_NOFOLLOW (do not follow symbolic links) and calls such as openat (open relative to a directory) help too; what is available depends on the OS.
Use conditional updates and transactions for databases
Instead of "SELECT to check, then UPDATE", use one UPDATE that includes the condition (for example, decrement only when the count is 1 or more). When several statements are involved, use a transaction with a suitable isolation level and row locks.
Lock before the check
If you use locks, take them before the check, as CWE-367 says. Locking after it does not guarantee that the checked target is the one you use.
Stop double execution with idempotency keys
For payments and sign-ups, give each request a unique key (an idempotency key) and run the action only once per key.
How to check your own code
Do this in code you build or manage.
- Find places where the checking code and the using code are separate calls (existence check then open; SELECT then UPDATE)
- For each, ask whether the check can be dropped and the step made a single operation
- Write a test that sends the same request several times at once, and confirm only one is applied
- Never try this against someone else's service; use your own environment
This site's view: put the check inside the same operation as the use
TOCTOU is not "someone forgot the check." The check exists; it is just separated from the use. Adding more checking code does not fix that. The fix is to put the check inside the same atomic operation as the use, not to add it as an extra step. Learning it next to IDOR helps in reviews: both are "a check exists but doesn't do its job" bugs.
Sources (primary)
- MITRE CWE-367: Time-of-check Time-of-use (TOCTOU) Race Condition — cwe.mitre.org (definition, consequences, mitigations)
- The concrete techniques (O_NOFOLLOW, openat, idempotency keys, transactions) are described as general implementation practice; availability varies by OS and database, so check each product's official documentation
Read next
- Glossary: What is path traversal · What is IDOR · What is the OWASP Top 10
- Guide: File upload vulnerabilities
- Glossary: What is a CVE