TOCTOU

Time-of-Check to Time-of-Use

L215 MINRACE CONDITIONS
PREDICT

A setuid program calls access() on /tmp/foo to ask whether the real user may read it, gets OK, then open()s the same path two microseconds later. Can the open() land on a different file?

A program checks a property of a path, then acts on the path. Two syscalls, two lookups. Between them, another process can replace what the path points to: delete the file and drop a symlink in its place. The check saw the old target. The use resolves the new one. The gap is microseconds wide, and an attacker in a loop hits it eventually.

Many Linux systems set fs.protected_symlinks=1, which stops a root process from following another user’s symlink in a sticky world-writable directory like /tmp. That blocks this exact attack there. It does not help outside sticky directories, on systems without the setting, or with hard links when protected_hardlinks is off.

The fix is not a faster check. It is to stop checking the path at all. The usual fix for a setuid program is to drop privileges to the real user before open(), so the kernel does the permission check on the file it actually opens. The sim shows the other fix: open the file once, then check and read through the file descriptor. An fd is bound to an inode. Nothing an attacker does to the path can move it. Note that checking the owner with fstat is a stricter policy than access(): it refuses group- or world-readable files the user could read.

▶ SIMULATION
/tmp/foo →innocent.txt (regular file, uid 1000)
check—
fd—
read()—
✓ the file read is the file that was checked✓ nothing read that the real user may not read
SCHEDULE
01[victim]victim: access("/tmp/foo", R_OK)
02[attacker]attacker: rm /tmp/foo; ln -s /etc/shadow /tmp/foo
03[victim]victim: open("/tmp/foo", O_RDONLY)
04[victim]victim: read(fd, buf, len)
Start of leak scenario. 4 steps.
// setuid root helper; real uid is the caller's
if (access("/tmp/foo", R_OK) == 0) {   // check: the PATH
    fd = open("/tmp/foo", O_RDONLY);     // use: the PATH, resolved again
    read(fd, buf, len);
}

Watch the invariant strip. In LEAK both lines go red at the last step. In FIX: FSTAT THE FD the attacker swaps right after the open and both lines stay green: the fd never moved, and the check happens on the fd. In FIX: SWAP LANDS FIRST the symlink is already there when the victim opens, and O_NOFOLLOW refuses it.

BREAK IT NOT YET

The victim runs its three syscalls in order. You control when the attacker swaps the path. Place the swap so the victim reads /etc/shadow.

GOAL: the victim reads /etc/shadow

FIX IT NOT YET

Rewrite readIfAllowed so it returns the contents of /tmp/foo when the real user owns it and never returns /etc/shadow, no matter when the attacker swaps the path. The grader lets the attacker point the path at a symlink or a hard link to /etc/shadow between any two syscalls, and swap it back later.

Mental model:

a path is a name, and a name can be rebound by anyone with write access to the directory. A file descriptor is a handle to the thing itself. Any check you do on a name is a check on whatever the name meant at that instant. Check the handle, use the handle, and the window closes.

IN PRODUCTION

Any setuid or privileged service that validates a user-supplied path: package managers writing into /tmp, log rotation, backup agents, container runtimes resolving bind mounts, web servers serving from a user directory. The symptom is a security advisory, not a crash.

IN INTERVIEWS

“What is wrong with if (access(p)) open(p)?” Name the window, say what the attacker does inside it (symlink or rename), and give the fix: drop to the real uid before open(), or open with O_NOFOLLOW, fstat the fd, and operate on the fd. Bonus: O_NOFOLLOW only covers the last component. To cover every component, walk the path one name at a time with openat(dirfd, name, O_NOFOLLOW), or use openat2 with RESOLVE_NO_SYMLINKS.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: DATA RACE →