The vulnerability is a Time-of-Check-Time-of-Use (TOCTOU) race condition in the filelock library, affecting both Unix and Windows platforms. An attacker with local filesystem access can create a symbolic link between the time the library checks for a lock file's existence and the time it opens the file. This allows the attacker to trick the library into truncating an arbitrary target file to which the symlink points.
The analysis of the patch commit 4724d7f8c3393ec1f048c93933e6e3e6ec321f0e reveals the exact locations of the vulnerable code.
-
On Unix-like systems, the vulnerability was in the _acquire method of the UnixFileLock class in src/filelock/_unix.py. The os.open() call lacked the O_NOFOLLOW flag, which would prevent it from following symlinks. The patch rectifies this by adding the flag, thus closing the race condition window.
-
On Windows, the vulnerability was in the _acquire method of the WindowsFileLock class in src/filelock/_windows.py. The os.open() call would follow reparse points (the Windows equivalent of symlinks). The patch introduces a new helper function, _is_reparse_point, which uses the Windows API (GetFileAttributesW) to detect if the file path is a reparse point. This check is performed before the file is opened, and an error is raised if a reparse point is detected, preventing the attack.
Therefore, the functions UnixFileLock._acquire and WindowsFileLock._acquire are the identified vulnerable functions, as they contain the logic that is directly exploited by this race condition.