Skip to content

Lesson 10 of 14 · 20m

TOCTOU: Check-Then-Use Races on the Filesystem

access() then open() answers two questions about two different files — open atomically with O_NOFOLLOW and O_EXCL, then fstat the fd.

A setuid helper checks access(path, W_OK) — "can the invoking user write this?" — sees permission granted, then calls open(path, O_WRONLY) to do the write. Between the two calls the attacker swaps path from their own file to a symlink pointing at /etc/shadow. The access check answered a question about a file that no longer exists; the open answered a different question about a different file. That's TOCTOU — time of check to time of use — and the access(2) man page has called it a security hole in print for decades: checking then using are two syscalls, and the filesystem can change between them.

The wrong mental model: "the path names a thing." A path names a lookup — a walk through directories that runs again on every syscall. access and open each do that walk independently, so two consecutive calls can resolve the same string to two different files. Every API pair of the form check-then-use on a path has this hole: stat then open, access then open, lstat then open on a symlink you wanted to reject.

The fixes all share one shape: make the open atomic and check the result, never the name.

  • Open first, inspect the fd. open(path, O_RDWR) then fstat(fd) gives you the truth about the file you actually opened — inode, type, owner — immune to path swaps after the fact. If it must be a regular file, check S_ISREG on the fstat result, not on lstat(path).
  • O_NOFOLLOW fails the open if the final component is a symlink; O_EXCL makes O_CREAT fail if the name already exists — together they close the classic /tmp symlink-squatting bug where an attacker precreates /tmp/predictable as a link to a victim file.
  • mkstemp exists because mktemp was the bug. mktemp returns a name; you still have to create it — another check-then-use window, which is why it's deprecated and compilers warn on it. mkstemp creates the file with O_CREAT|O_EXCL in one step and hands you the fd.
  • openat2(2) (Linux 5.6, 2020) takes a struct open_how with RESOLVE_NO_SYMLINKS, RESOLVE_BENEATH, RESOLVE_IN_ROOT — kernel-enforced path resolution rules that make whole TOCTOU families impossible. The modern answer when you're on a new enough kernel.
  • The access+open privilege check has a dedicated replacement: open the file and check the fd's ownership/permissions, or use seteuid to drop to the real user around the open itself — then the kernel does the check and the use in one step.

Real-world receipts: CVE-2018-6954 — systemd-tmpfiles followed attacker-planted symlinks while "cleaning" /tmp, letting unprivileged users point ownership changes at arbitrary files; distro tmpwatch/tmpreaper races shipped variants of the same bug for years. The pattern is identical every time: a privileged process checked a path, then opened it.

Try this

Write the vulnerable pair — if (access(path, W_OK) != 0) exit(1); fd = open(path, O_WRONLY); — and a loop that swaps path between a real file and a symlink to ./victim while it runs. Watch the helper write into victim. Then replace it with open(path, O_WRONLY|O_NOFOLLOW) plus an fstat check and re-run: the swap now fails the open. You've just closed a bug class that has a CVE budget of its own.

Field notes

  • /tmp has the sticky bit (t) for a reason — it limits who can unlink entries, but it doesn't stop symlink-squatting. O_EXCL does.
  • Same bug, different filesystem: mkdir then write, unlink then recreate — any two-step path operation. The cure is always atomicity: one syscall, or fd-relative calls (openat, fchownat, fchmod) on a directory you hold open.
  • If the language offers it (openat2, RESOLVE_BENEATH), prefer kernel enforcement over code review — review misses, the resolver doesn't.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.