Race Condition Exploits
Concurrency & Race Conditions · VR Foundations track
4 min read
In this module
From Bug to Weapon
Race conditions are more than reliability headaches. They are a serious class of vulnerability. Timing dependence makes them harder to weaponise than memory corruption bugs (Stack-Based Buffer Overflows through Integer Overflows & Type Confusion), but a successful race usually walks straight past security checks rather than smashing memory to do it.
Dirty COW (CVE-2016-5195)
One of the most damaging race exploits ever shipped targeted the Linux kernel's copy-on-write (COW) machinery. When a process forks, parent and child share the same physical pages. The moment either side writes to a shared page, the kernel makes them a private copy. That's COW.
Dirty COW raced two operations against each other. One thread triggered a COW fault on a read-only mapping. Another called madvise() to tell the kernel to throw the private copy away. Win the race often enough, and you could write directly into the original read-only page. From there, an unprivileged user could overwrite files like /etc/passwd and waltz to root. The bug had been sitting in the kernel for nine years before anyone noticed.
The public exploit writes through /proc/self/mem into a private, read-only mapping of the target file. Here's one winning iteration. The window is between the kernel finishing the COW and retrying its page lookup:
Lose the race and the write goes harmlessly into the private copy, so both threads just loop until one iteration lands like this.
Attackers don't trust luck to win a race. They engineer the timing window wider: pinning threads to specific CPU cores, manipulating system load, dropping in usleep() calls for precise delays, or flooding the filesystem cache to drag kernel operations to a crawl. With enough fiddling, a race that looks impossible to hit becomes reliable.
Filesystem Races
Filesystem TOCTOU is the canonical version of this bug. A privileged program (a setuid binary, say, from System Calls & Processes) checks whether the caller can access a file, then opens it. An attacker prepares a symlink pointing at a harmless file, waits for the permission check to pass, then repoints the symlink at /etc/shadow before the open lands. The privileged program happily reads the sensitive file, convinced the check covered it.
Web Application Races
The same check-then-act gap turns up in web applications, usually across a database rather than a kernel. The commonest form is a limit overrun: a gift card redeemed twice, a discount code applied five times, or a withdrawal approved twice from one balance, because several requests all passed the check before any of them recorded the use. Network jitter used to make these hard to hit from across the internet. In 2023 James Kettle of PortSwigger published the single-packet attack, which holds back the last part of 20 to 30 HTTP/2 requests and then releases them all in one TCP packet, so they reach the server within about a millisecond of each other. The fix is the same as anywhere else: make the check and the update one atomic step, with a database transaction, a row lock or a conditional update.
Double-Fetch Vulnerabilities
Kernel code is constantly hauling data across the user-kernel boundary. A double-fetch bug shows up when the kernel reads a value from user space, validates it, then reads the same value a second time and assumes nothing's changed. A hostile user thread modifies the memory between the two reads. The kernel marches on with a value that was never actually validated. This pattern crops up all over syscall handlers and device drivers.
Signal Handler Races
Unix signals (Process Management & Scheduling) can interrupt a thread at almost any instruction boundary. If a signal handler and the main thread both poke at shared state, you have a race. Calling non-reentrant functions like malloc or printf from a signal handler is undefined behaviour, because the handler might fire while the main thread is halfway through one of those very functions, leaving the internal bookkeeping mangled.
Defence Strategies
The only real defence is to remove the race outright. Use atomic file operations — open() with O_CREAT | O_EXCL rather than check-then-create. Copy user-space data into kernel memory once, then validate the copy and never re-read. Shrink the window between check and use until there's nothing left for an attacker to slip into. In security-critical code, treat every gap between two operations as a candidate attack window.
Try the Simulations
Race Condition: Lost Update
Two 'threads' increment a shared counter, but interleaving causes a lost update. Thread B reads the old value before Thread A finishes writing, so one increment is lost.
Atomic Increment: No Lost Update
The same two-thread counter scenario, but this time each thread completes its full LOAD-ADD-STORE sequence before the other begins. No interleaving means no lost updates.