Lesson 2 of 9 · 24m
TOCTOU: Time-of-Check to Time-of-Use
How check-then-use sequences become exploitable gaps across filesystems, permissions, cloud APIs, and application logic.
TOCTOU means time-of-check to time-of-use. You check that something is safe, then later act on the assumption that it is still safe. Between those two operations the object can change. The textbook example is filesystem permissions, but the same shape appears anywhere a decision is separated from the action it authorizes.
The filesystem shape everyone should recognize
def insecure_read(path):
if os.path.islink(path):
raise PermissionError("symlink refused")
return open(path, "rb").read() # path may no longer be the checked objectThis looks cautious and is exploitable. The attacker waits until after islink and before open, then swaps path so it points at /etc/shadow or another target. The code checked one object and used another. On Linux the stronger pattern is to open the final object first and validate the file descriptor:
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
st = os.fstat(fd) # validates the object actually opened
if not stat.S_ISREG(st.st_mode):
raise PermissionError("not a regular file")
data = os.fdopen(fd, "rb").read()fstat examines the object you actually opened, not the name that used to point at it.
Names lie; handles tell the truth
A path is a name. Names can be rebound. File descriptors, database rows locked in a transaction, object versions, and capability handles refer to a specific object or snapshot. In secure design, perform the check against the same handle you will use, or make check and use one atomic operation.
The stale version reads SELECT role ..., decides in application code, then updates later. If the role changes in between, the decision is stale. Better shape: enforce authorization inside the mutation itself.
UPDATE documents SET owner = 42
WHERE id = 'doc-17'
AND EXISTS (SELECT 1 FROM users WHERE id = 42 AND role = 'admin');Cloud and API TOCTOU
The pattern survives when the filesystem disappears. Examples:
- Check an S3 object does not exist, then upload — another writer creates it first.
- Query whether a username is free, then insert — another request inserts it first.
- Validate a container image digest, then run a mutable tag — the tag moves before deployment.
- Look up a security group rule, then assume it is still present at launch time.
Use object versions, conditional writes, unique constraints, immutable references, and transactions. Avoid mutable names in security decisions.
The exploit primitive is replacement
Attackers do not need to win a microscopic race every time. They create many candidates and automate retries: symlink swaps, rapid file recreation, parallel API requests, DNS rebinding between validation and connection, or replacing a temporary file while a privileged process is writing it. If the operation is scriptable, a one-in-a-thousand window is enough.
Field notes
- Never validate a path and then reopen it by name in a privileged process.
- Prefer
openat2withRESOLVE_NO_SYMLINKSwhere available for hostile directory trees. - Temporary files belong in private directories with mode
0700; shared/tmpis adversarial territory. - "Validate then act" is a smell in permissions, billing, inventory, and identity systems.
Was this lesson helpful?