CWE-367OWASP A04:2021CWE-362CWE-840

CWE-367: TOCTOU Race Condition

A program checks a condition and then acts on it, but the state can change in between, so the action runs on a condition that no longer holds.

Also known as: TOCTOU, Time-of-check time-of-use race, Check-then-use race

Risk

Typically medium to high. The gap between validating a condition and acting on it lets an attacker change the state in between, turning a passed check into an unsafe action, including privilege and file operations.

Definition

What it is

CWE-367 is a specific race in which a program checks the state of a resource and then uses it, assuming the state is unchanged, while another actor alters it in the window between the two steps. The check passes, but by the time the action runs the condition is no longer true. It commonly appears with files, permissions, balances, and reservations where the check and the use are separate operations. The weakness is treating a check and a later use as if they were a single atomic step.

Use-cases

How it shows up in real code

File swapped after check

Code verifies a file is safe to write, then writes to it, but between the two steps the path is replaced with a link to a sensitive file, which the write then clobbers.

Balance checked then spent

A payment confirms sufficient funds, then deducts later. A second request spends the balance in the gap, so the deduction overdraws the account.

Permission rechecked too early

An operation checks that a user still has access, then performs a long action, while access is revoked in between, so the action completes under stale permission.

Reservation taken in the gap

A booking checks a slot is free, then books it, but another request claims the same slot between the check and the write, producing a double booking.

AI risk

Why AI-generated code has increased the exposure

  • Assistants naturally emit a check followed by a use as two steps, which reads as correct but leaves the exploitable window open.
  • The model cannot see that another actor may change the resource between the steps, so it assumes the checked state persists.
  • Models optimize for logic that passes a sequential test, where nothing mutates state between check and use, so the gap never surfaces.
  • Check-then-act snippets dominate training data and are reproduced without the atomic guard that would close the window.
  • As the assistant generates more file, balance, and reservation flows, each separates check from use, and concurrent load turns each gap into a bug.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships check-then-use sequences whose vulnerable window is invisible in an unread diff.
  • Testing sequentially, the developer never mutates state between the two steps, so the TOCTOU gap stays hidden.
  • Prompt-and-ship skips the review where someone would ask whether the checked state can change before the action.
  • A prototype that was safe under single-user testing is promoted to production where concurrent access exposes the gap.
  • Rapid growth brings the concurrent load that turns a dormant check-then-use window into a real exploit faster than review catches it.

Detection

How to find it

  • Find places where a check and the action it guards are separate steps and confirm the state cannot change between them.
  • For file operations, prefer operating on an open handle rather than re-resolving a path after checking it.
  • Enforce the condition atomically at the use, for example a conditional update that both checks and acts in one step.
  • Run concurrent tests that mutate the resource between check and use and confirm the action stays safe.

Remediation

How to fix it

  • Collapse the check and the use into a single atomic operation so no state change can slip between them.
  • Use a conditional update or compare-and-set that re-verifies the condition at the moment of action.
  • Hold a lock or transaction across the check and the use when they cannot be combined.
  • For files, act on an opened handle rather than a path that can be re-pointed after the check.
  • Add tests that change the resource between check and use and fail the build if the unsafe action still runs.

Frequently asked questions

What is CWE-367 (time-of-check time-of-use (toctou) race condition)?
CWE-367 is a race in which a program checks a resource's state and then acts on it, while another actor changes that state in the window between the two steps. The check passes but the action runs on a condition that no longer holds, which is common with files, balances, permissions, and reservations.
How have AI assistants and vibe coding increased TOCTOU risk?
Assistants naturally emit a check followed by a separate use and cannot see that another actor may change the resource in between, so the exploitable window stays open. Vibe coding ships that sequence unread, and because sequential testing never mutates state between the steps, the TOCTOU gap stays hidden until concurrent access exposes it.
How do I fix time-of-check time-of-use (toctou) race condition?
Collapse the check and the use into one atomic operation, use a conditional update or compare-and-set that re-verifies at the moment of action, hold a lock or transaction across both when needed, act on open file handles rather than paths, and add tests that change the resource between check and use.

Related weaknesses

Catch CWE-367 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-367 and the rest of the business logic vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit