Failures and antipatterns · Intermediate

TOCTOU races

Trace changes between validation and use, and choose object-bound operations or conditional updates at the required boundary.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. A check does not reserve the future
  2. Trace the gap between path checks and open
  3. Use APIs that bind operations to the intended object
  4. Apply conditions atomically beyond files
  5. Preserve competing schedules in tests
  6. Ask Logeia about this articleCheck your understanding

A check does not reserve the future

A time-of-check/time-of-use race occurs when state changes after validation but before the operation relying on it. The earlier check can be correct while its conclusion no longer applies at use. Closely spaced calls are not necessarily atomic: scheduling and other processes can intervene. Shortening the interval reduces neither the logical gap nor the need for a guarantee. Identify which state can change and who can change it. A mutex used by threads inside one program has a different scope from a filesystem name modified by another process. The design question is whether the condition and its dependent action can be bound within an appropriate operation.

Trace the gap between path checks and open

For a hypothetical file example, inspect path P, decide that its attributes are acceptable, then open the same textual path later. If the name resolves differently between those steps, the opened object may not be the checked one. Equal strings do not prove identical objects. This models a race rather than asks the reader to alter another environment. List the checking API, control of the containing directories, possible symbolic links or renames, and the eventual operation. Reusing the path again after opening introduces another resolution. The investigation should explain the identity relationship across those resolutions instead of assuming that one successful preliminary inspection permanently certifies the name.

Use APIs that bind operations to the intended object

After obtaining a descriptor, operations such as fstat and descriptor-based reads can remain attached to that opened object instead of repeatedly resolving its name. This does not automatically establish that opening the object was authorized. Directory-relative operations and Linux openat2 resolution constraints can help enforce an intended namespace boundary. Check the actual semantics of RESOLVE_BENEATH, symlink restrictions and mount-related constraints for the goal. A no-follow rule affecting the final component is not necessarily a rule covering every intermediate component. Handle unsupported features and errors explicitly rather than silently falling back to the original check-then-use sequence. Object identity and access policy remain related but separate obligations.

Apply conditions atomically beyond files

The same pattern appears in databases and shared memory. Reading that stock is positive and decrementing it in a later independent operation permits another request to consume it between those steps. Choose a conditional update or comparison-and-update boundary that checks the predicate together with the change. A surrounding transaction alone provides different guarantees depending on isolation and locking. An application-local lock cannot constrain a different process that does not participate. If a version condition rejects a conflicting update, treat that as an expected concurrency result and define bounded rereading or retry behavior. The remedy belongs at the boundary that can actually enforce the needed invariant.

Preserve competing schedules in tests

In tests, deliberately order validation, a competing change and use. Verify that the revised operation either remains bound to the intended object or rejects the invalid condition. Test-only synchronization points are more reproducible than hoping a particular sleep interval triggers a race. Adding one more check still leaves a gap after the final check if the dependent operation remains separate. Cover ordinary paths, forbidden resolution, disappearing objects, competing updates and feature errors. The goal is not eliminating all concurrent change. It is specifying the boundary protecting the operation’s assumption and the behavior when that assumption cannot be established, with evidence for both cases.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path