Skip to content

CVE-2025-68146 Don't follow a symlink at the lock path - #1

Merged
icanhasmath merged 1 commit into
3.1.0.xfrom
cve-2025-68146
Jun 10, 2026
Merged

icanhasmath merged 1 commit into
3.1.0.xfrom
cve-2025-68146

Conversation

@icanhasmath

Copy link
Copy Markdown
Collaborator

filelock opened the lock file with os.O_CREAT | os.O_TRUNC and no O_NOFOLLOW, so a symlink planted at the lock path was followed and its target truncated (TOCTOU symlink attack, CVE-2025-68146 / affects < 3.20.1).

Backport the upstream 3.20.1 mitigation, adapted to this 3.1.0 / Python-2.7 single-module layout:

  • UnixFileLock and SoftFileLock: add os.O_NOFOLLOW to the open flags, guarded by getattr(os, "O_NOFOLLOW", None) for platforms that lack it. A symlinked lock path now fails with ELOOP instead of truncating the target. (SoftFileLock already used O_EXCL, which refuses existing paths; O_NOFOLLOW is added for consistency.)
  • WindowsFileLock: Windows has no O_NOFOLLOW, so add _is_reparse_point() (via GetFileAttributesW + FILE_ATTRIBUTE_REPARSE_POINT) and refuse to open the lock file if it is a reparse point (symlink/junction).

Add a regression test (runs for both lock types) asserting a symlinked lock path leaves its target intact and the lock unheld. Py2.7-safe: %-formatting, getattr guard, lazy ctypes import, path decoded to unicode for the wide WinAPI call.

filelock opened the lock file with os.O_CREAT | os.O_TRUNC and no
O_NOFOLLOW, so a symlink planted at the lock path was followed and its target
truncated (TOCTOU symlink attack, CVE-2025-68146 / affects < 3.20.1).

Backport the upstream 3.20.1 mitigation, adapted to this 3.1.0 / Python-2.7
single-module layout:
- UnixFileLock and SoftFileLock: add os.O_NOFOLLOW to the open flags, guarded
  by getattr(os, "O_NOFOLLOW", None) for platforms that lack it. A symlinked
  lock path now fails with ELOOP instead of truncating the target. (SoftFileLock
  already used O_EXCL, which refuses existing paths; O_NOFOLLOW is added for
  consistency.)
- WindowsFileLock: Windows has no O_NOFOLLOW, so add _is_reparse_point() (via
  GetFileAttributesW + FILE_ATTRIBUTE_REPARSE_POINT) and refuse to open the
  lock file if it is a reparse point (symlink/junction).

Add a regression test (runs for both lock types) asserting a symlinked lock
path leaves its target intact and the lock unheld. Py2.7-safe: %-formatting,
getattr guard, lazy ctypes import, path decoded to unicode for the wide WinAPI
call.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant