GHSA-w853-jp5j-5j7f: filelock has a TOCTOU race condition which allows symlink attacks during lock file creation
ImpactA Time-of-Check-Time-of-Use (TOCTOU) race condition allows local attackers to corrupt or truncate arbitrary user files through symlink attacks. The vulnerability exists in both Unix and Windows lock file creation where filelock checks if a file exists before opening it with O_TRUNC. An attacker can create a symlink pointing to a victim file in the time gap between the check and open, causing os.open() to follow the symlink and truncate the target file.**Who is impacted:**All users of filelock on Unix, Linux, macOS, and Windows systems. The vulnerability cascades to dependent libraries:**virtualenv users**: Configuration files can be overwritten with virtualenv metadata, leaking sensitive paths**PyTorch users**: CPU ISA cache or model checkpoints can be corrupted, causing crashes or ML pipeline failures**poetry/tox users**: through using virtualenv or filelock on their own.Attack requires local filesystem access and ability to create symlinks (standard user permissions on Unix; Developer Mode on Windows 10+). Exploitation succeeds within 1-3 attempts when lock file paths are predictable.PatchesFixed in version **3.20.1**.**Unix/Linux/macOS fix:** Added O_NOFOLLOW flag to os.open() in UnixFileLock.\_acquire() to prevent symlink following.**Windows fix:** Added GetFileAttributesW API check to detect reparse points (symlinks/junctions) before opening files in WindowsFileLock.\_acquire().**Users should upgrade to filelock 3.20.1 or later immediately.**WorkaroundsIf immediate upgrade is not possible:Use SoftFileLock instead of UnixFileLock/WindowsFileLock (note: different locking semantics, may not be suitable for all use cases)Ensure lock file directories have restrictive permissions (chmod 0700) to prevent untrusted users from creating symlinksMonitor lock file directories for suspicious symlinks before running trusted applications**Warning:** These workarounds provide only partial mitigation. The race condition remains exploitable. Upgrading to version 3.20.1 is strongly recommended.Technical Details: How the Exploit WorksThe Vulnerable Code Pattern**Unix/Linux/macOS** (`src/filelock/_unix.py:39-44`):**Windows** (`src/filelock/_windows.py:19-28`):The Race WindowThe vulnerability exists in the gap between operations:**Unix variant:****Windows variant:**Step-by-Step Attack Flow**1. Attacker Setup:****2. Attacker Creates Race Condition:****3. Victim Application Runs:****4. What Happens Inside os.open():**On Unix systems, when `os.open()` is called:Without `O_NOFOLLOW` flag, the kernel follows the symlink and truncates the target file.Why the Attack Succeeds Reliably**Timing Characteristics:****Check operation** (Path.exists()): ~100-500 nanoseconds**Symlink creation** (os.symlink()): ~1-10 microseconds**Race window**: ~1-5 microseconds (very small but exploitable)**Thread scheduling quantum**: ~1-10 milliseconds**Success factors:****Tight loop**: Running attack in a loop hits the race window within 1-3 attempts**CPU scheduling**: Modern OS thread schedulers frequently context-switch during I/O operations**No synchronization**: No atomic file creation prevents the race**Symlink speed**: Creating symlinks is extremely fast (metadata-only operation)Real-World Attack Scenarios**Scenario 1: virtualenv Exploitation****Scenario 2: PyTorch Cache Poisoning**Why Standard Defenses Don't Help**File permissions don't prevent this:**Attacker doesn't need write access to victim_fileos.open() with O_TRUNC follows symlinks using the *victim's* permissionsThe victim process truncates its own file**Directory permissions help but aren't always feasible:**Lock files often created in shared /tmp directory (mode 1777)Applications may not control lock file locationMany apps use predictable paths in user-writable directories**File locking doesn't prevent this:**The truncation happens *during* the open() call, before any lock is acquiredfcntl.flock() only prevents concurrent lock acquisition, not symlink attacksExploitation Proof-of-Concept ResultsFrom empirical testing with the provided PoCs:**Simple Direct Attack** (`filelock_simple_poc.py`):Success rate: 33% per attempt (1 in 3 tries)Average attempts to success: 2.1Target file reduced to 0 bytes in \<100ms**virtualenv Attack** (`weaponized_virtualenv.py`):Success rate: ~90% on first attempt (deterministic timing)Information leaked: File paths, Python version, system configurationData corruption: Complete loss of original file contents**PyTorch Attack** (`weaponized_pytorch.py`):Success rate: 25-40% per attemptImpact: Application crashes, model loading failuresRecovery: Requires cache rebuild or model retraining**Discovered and reported by:** George Tsigourakos (@tsigouris007)
CVSS v3.1: 6.3