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)thenfstat(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, checkS_ISREGon thefstatresult, not onlstat(path). O_NOFOLLOWfails the open if the final component is a symlink;O_EXCLmakesO_CREATfail if the name already exists — together they close the classic/tmpsymlink-squatting bug where an attacker precreates/tmp/predictableas a link to a victim file.mkstempexists becausemktempwas the bug.mktempreturns a name; you still have to create it — another check-then-use window, which is why it's deprecated and compilers warn on it.mkstempcreates the file withO_CREAT|O_EXCLin one step and hands you the fd.openat2(2)(Linux 5.6, 2020) takes astruct open_howwithRESOLVE_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+openprivilege check has a dedicated replacement: open the file and check the fd's ownership/permissions, or useseteuidto 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
/tmphas the sticky bit (t) for a reason — it limits who can unlink entries, but it doesn't stop symlink-squatting.O_EXCLdoes.- Same bug, different filesystem:
mkdirthen write,unlinkthen 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?
Keep reading
Related articles from the SudoSecurity blog.
Implementing UART on the ATmega328P
USART on the AtMega328P Needed Documentation * Arduino Nano PCB design * Atmega328P datasheet Materials used * Arduino Nano * Arduino Uno R3 * Relevant cable * Computer Software Used * make (for easy of compiling and building) * avr-gcc (our compiler for avr ISA) *…
Hello World, Embedded Edition: Blink an LED
Embedded C: Hello WorldNeeded DocumentationArduino Nano PCB designAtmega328P datasheetMaterials usedArduino NanoArduino Uno R3Relevant cableComputerSoftware Usedmake (for easy of compiling and building)avr-gcc (our compiler for avr ISA)binutils-avravr-libcavrdude (used to flash board)The…
Build Your Own Stack-Based Bytecode Virtual Machine in C
Ever wonder how Python's CPython, the JVM, or the Lua VM actually execute code under the hood? In this article, we build a stack-based bytecode virtual machine from scratch in C.
CPPFront Project is Aiming to Modernize the C++ Language
The highly vaunted C++ language would become simpler and safer under a proposal dubbed Cppfront, an experimental C++ front end being floated by prominent C++ developer Herb Sutter. Intended as a major C++ evolution, Cppfront is an experimental “syntax 1 to syntax 2” compiler designed to explore…