File TOCTOU Race Condition
Overview
TOCTOU (Time-of-Check/Time-of-Use) vulnerabilities occur when there is a window between checking a file's state and using it. An attacker can replace the file with a symlink between the check and the use, potentially reading/writing arbitrary files.
Classic pattern: if access(path, R_OK) == 0: open(path) — between access() and open(), attacker creates symlink.
Remediation
- Use
O_NOFOLLOWflag to prevent symlink following - Use
openat()with AT_FDCWD to operate atomically - In Python, use
os.open()withos.O_NOFOLLOW - Validate path within a trusted directory after opening