CVE-2025-68146 TOCTOU in filelock: upgrade to 3.20.1 now

  • Thread Author
filelock, the widely used platform‑independent file‑locking library for Python, is the subject of a newly public vulnerability — CVE‑2025‑68146 — that exposes a classic Time‑of‑Check‑Time‑of‑Use (TOCTOU) race condition in lock file creation. The flaw allows a local attacker who can create symlinks to interpose between the library’s existence check and its subsequent open-with-truncate operation, causing arbitrary files to be truncated or corrupted; the problem affects filelock versions prior to 3.20.1 and has been patched in the 3.20.1 release.

TOCTOU upgrade mitigation: code locks /var/tmp to prevent race conditions.Background​

What is filelock and why it matters​

filelock is a small, portable library that provides file-based locking primitives for Python applications across Unix‑like systems and Windows. Because file locks are a common primitive used by packaging tools, build systems, virtual environments, machine‑learning checkpoints, and other infrastructure, a flaw in the library can cascade widely into the Python ecosystem. Many popular projects depend on filelock either directly or indirectly, so changes here have outsized operational and security impact.

Time‑of‑Check–Time‑of‑Use (TOCTOU) explained​

A TOCTOU race condition arises when code checks a property of a filesystem object (time of check) and then later acts on that object (time of use) under the assumption the property still holds. If an attacker can change the filesystem state between the check and the use — for example by creating or replacing a symlink — the program can be tricked into acting on a different file than intended. In this case, filelock performed a check (does the lock file exist / is the directory writable) and then opened the path with flags that include truncation (O_TRUNC), which can follow a symlink and truncate a totally different file.

The vulnerability: how CVE‑2025‑68146 works​

The check-then-open window​

The vulnerable sequence in filelock’s lock acquisition logic performs an existence/writability check on the intended lock path and then, without an atomic, symlink‑safe open, proceeds to open the file with flags that truncate (on Unix: O_RDWR | O_TRUNC; on Windows: O_RDWR | O_CREAT | O_TRUNC). If an attacker can create a symlink at the lock path between those two operations, the subsequent open follows the symlink and truncates the symlink’s target — which may be an arbitrary file owned by some other process or user. This is the core TOCTOU exploit window.

Practical preconditions for exploitation​

  • Local access: The attacker must have the ability to create a symlink in the directory where the lock file will be created. That requires local filesystem access; this is not remotely exploitable in the network sense.
  • Symlink creation privileges: On Unix and Linux systems, regular users can usually create symlinks in directories where they have write permission. On Windows, creating symlinks historically required elevated privileges, but Developer Mode introduced in recent Windows 10+ versions allows non‑admin users to create symlinks — a condition noted in advisories.
  • Predictable lock paths: The attack is far easier when lock file paths are predictable and located in directories where untrusted users can write. Exploitation has been reported to succeed in as few as 1–3 attempts when path choice is predictable.

What an attacker can do​

If successful, the attacker can cause:
  • Silent truncation of arbitrary files (data loss)
  • Corruption of files that downstream processes rely on (integrity compromise)
  • Denial of service for the application that expects the original content (availability impact)
    The vulnerability does not, by itself, grant remote code execution or read access to secret files, but it can break builds, corrupt checkpoints, overwrite configuration, and generally sabotage integrity and availability of systems using filelock.

Who and what is affected​

Library versions and immediate remediation​

All filelock releases prior to 3.20.1 are affected. The maintainers issued a fix in version 3.20.1; authors and operators are strongly advised to upgrade to 3.20.1 or newer as the primary mitigation.

The ecosystem impact: where the vulnerability cascades​

Because filelock is embedded in or used by many higher‑level projects, the vulnerability can propagate into major tools and products:
  • Virtual environment tooling and packaging (virtualenv, pip‑related tooling)
  • Packaging and environment managers (poetry, tox)
  • Machine‑learning projects that store checkpoints or CPU ISA caches using lock files (e.g., PyTorch toolchains)
  • Build systems and CI pipelines that rely on predictable lock files
    Advisories and trackers note that these dependent projects may be affected indirectly if they rely on vulnerable versions of filelock. Mitigations in such projects require either upgrading bundled/indirectly installed filelock or applying distribution-specific patches.

Technical analysis: why this slipped through and how it was fixed​

Root cause in design and common pitfalls​

The fundamental problem is not a novel class of vulnerability — TOCTOU is well understood — but it frequently resurfaces because it's easy to miss in practical code paths. The common anti‑pattern is:
  • Check file existence or writability with a high‑level API (Path.exists, os.access, etc.
  • Then call os.open or builtins.open with flags that create/truncate the file, without requesting atomic semantics or prohibiting symlink traversal
This pattern assumes the filesystem state is static during the short interval, which is false in multi‑user or adversarial contexts. The correct approach for creating files that must not follow symlinks is to use atomic filesystem primitives that provide both creation and exclusive semantics (for example, open with O_CREAT|O_EXCL on POSIX when appropriate, or use O_NOFOLLOW where available, or create the file via a unique temporary file and rename it atomically). Because Python’s high‑level APIs sometimes obscure these platform‑specific flags, libraries must take care to use the appropriate os.open flags and checks to avoid races. Security scanners and dependency auditors often flag such patterns, which is why this finding triggered the CVE and subsequent patch.

The upstream fix and its limits​

The filelock maintainers merged a remediation and released 3.20.1; the changelog explicitly references CVE‑2025‑68146 and credits the fix. The fix, as described in the release notes, addresses the TOCTOU symlink vulnerability in the lock file creation path. The public advisory recommends upgrading to 3.20.1 as the correct remediation. Where immediate upgrading is impractical, maintainers have suggested alternative mitigations (use SoftFileLock, tighten directory permissions, monitor for symlinks) — but these are partial and context‑dependent. System operators should assume the race is exploitable until the fixed version is deployed. Note: Implementation details of the fix (which flags or atomic operations were changed) are described in the repository commits and PR associated with the release. Those commit diffs should be reviewed by security‑conscious teams that require proof of a symlink‑safe implementation. Public trackers reference the specific pull request (PR #461) and commit(s) that closed the issue.

Practical mitigations and operational guidance​

Immediate actions for operators (priority)​

  • Upgrade filelock to 3.20.1 (or later) in all environments, containers, and build images. This is the single most effective step.
  • Audit your dependency graph to find transitive usages of filelock. Use pipdeptree, safety, or your distro’s package metadata to locate packages that embed or pin filelock. Update those packages or apply distribution patches where appropriate.
  • For Linux/Unix systems, ensure lock directories are not writable by untrusted users. Where possible, set directory permissions to be restrictive (for example, chmod 0700) to prevent unprivileged users from creating symlinks in the target directory. However, note this is only a mitigation and may not be feasible in shared or multi‑tenant setups.

When upgrading is not immediately possible: partial mitigations​

  • Use SoftFileLock instead of UnixFileLock/WindowsFileLock if the application semantics tolerate the change. SoftFileLock uses a different model that avoids creating predictable lock files in the same way, though it has different guarantees and may not be suitable for all workloads.
  • Restrict who can write to lock directories (group ownership, ACLs) and monitor those directories for suspicious symlinks. Automated monitoring coupled with alerting can provide early detection of attempted exploitation.
  • Avoid predictable lock file names. If a process can construct unpredictable or per‑instance lock names (for example, using randomized suffixes or including process IDs in directories not writable by untrusted users), the attack becomes harder to mount. This is a defense‑in‑depth move, not a fix.

Steps for Python package maintainers​

  • Bump downstream dependencies to require filelock >= 3.20.1 and release patched versions of your packages. Communicate clearly with users about the security release and the required action.
  • In CI pipelines, add checks for unsafe file operations, and run static analysis that catches TOCTOU‑like idioms where plausible. Consider integrating SAST and supply‑chain scanners that flag vulnerabilities like CVE‑2025‑68146.

Windows‑specific considerations​

Windows historically restricted symlink creation to administrators, but modern Windows 10/11 versions allow non‑elevated symlink creation when Developer Mode is enabled. That nuance reduces the bar for exploitation on modern developer machines and some CI runners that enable Developer Mode. Advisories explicitly call out Windows Developer Mode as a condition for symlink creation by non‑admin users, and therefore the vulnerability applies to Windows in those contexts as well. Operators running Windows build agents, developer workstations, or shared Windows CI runners should treat them as potentially exploitable unless the patched filelock is installed.

Risk assessment and timeline considerations​

Severity and CVSS​

Multiple trackers list this vulnerability as medium severity; one commonly cited aggregate CVSS v3.1 score is 6.3 (vector: AV:L/AC:H/PR:L/UI:N/S:U/C:N/I:H/A:H), reflecting that the vulnerability requires local access and specific conditions but can cause high integrity and availability impact on successful exploitation. The general assessment is that confidentiality is not directly impacted, while integrity and availability can be significantly affected.

Exploitability​

Public advisories indicate exploitation is feasible and can succeed in a small number of attempts when lock file paths are predictable and writable by untrusted actors. This makes systems with predictable lock placement — CI runners, shared /tmp directories, global caches — the highest risk targets. Local attackers, untrusted users on multi‑user systems, and compromised accounts on CI or build runners are the primary threat actors.

Attack scenarios to prioritize​

  • Supply‑chain and CI compromise: a malicious collaborator on a shared CI runner creates a symlink to a sensitive target before a trusted job runs, leading to truncated artifacts or sabotaged builds.
  • ML pipelines: an attacker with access to a shared training environment corrupts model checkpoints or caches used by teams, causing downtime or silent data corruption.
  • Shared hosting/BYO developers: workstations or shared dev containers with Developer Mode on Windows or permissive /tmp directories on Unix provide fertile ground for this class of attack.

Critical appraisal: strengths and risks of the response​

What the maintainers did well​

  • The maintainers recognized the TOCTOU pattern and pushed a targeted fix, publishing a small point release (3.20.1) that explicitly references the CVE. That rapid patch and clear release note make it straightforward for downstream projects and packagers to act.

Remaining concerns and practical friction​

  • Patch uptake time: many large codebases and Linux distributions may take days to weeks to roll the fixed package into stable channels; transitive dependencies in pinned container images and wheels further delay deployment. Distribution and ecosystem lag is the most realistic operational risk.
  • Partial mitigations: recommendations such as changing permissions or using SoftFileLock are helpful stopgaps but are not universal solutions. SoftFileLock changes semantics and may not be appropriate for all users; permission tightening can be infeasible in shared environments. These mitigation suggestions are useful but insufficient in many production contexts.
  • Lack of remote exploitability: while the vulnerability requires local access (limiting the threat surface), many modern attacks chain local weaknesses with other vectors (CI misconfigurations, compromised credentials) to achieve a local foothold. Treating the "local" requirement as a low risk can lead to complacency.

Verification of fix​

  • Public advisories and release notes reference a specific pull request and commit that remedied the issue; security‑conscious teams should review those diffs to confirm the fix uses atomic or no‑follow semantics. If an organization’s threat model demands it, vendors or integrators should run small test harnesses that attempt to exploit the previous pattern in a controlled environment and verify the new release is resistant in practice. Advisories list the PR and commit identifiers for exact inspection.

Checklist for remediation (quick reference)​

  • Upgrade filelock to 3.20.1 or later (application, containers, system packages).
  • Audit transitive dependencies and rebuild images that include vulnerable versions.
  • Lock down permissions on directories used for lock files where feasible (chmod 0700 or equivalent ACLs).
  • Replace UnixFileLock/WindowsFileLock with SoftFileLock only if application semantics permit; otherwise plan an urgent upgrade path.
  • Monitor lock directories for unexpected symlinks and add alerts to detection tooling.
  • Review and harden CI runners and shared developer machines (avoid Developer Mode on Windows in multi‑user/shared setups unless necessary).

Conclusion​

CVE‑2025‑68146 is an instructive reminder that TOCTOU issues remain a real and practical threat in modern cross‑platform libraries. The filelock maintainers have issued a targeted fix in version 3.20.1, and the published advisories are consistent about the exploitability and the recommended upgrade. The vulnerability’s practical impact — silent truncation or corruption of arbitrary files — is significant in environments where lock file locations are predictable or writable by untrusted parties, and where downstream projects rely on filelock for critical operations such as packaging or model checkpointing. Operators must treat this as a priority patch: upgrade quickly, audit transitive dependencies, and apply the recommended hardening measures for lock file directories and CI runners. Where upgrading is delayed, use the recommended partial mitigations but recognize they are not perfect substitutes for the upstream fix.
Source: MSRC Security Update Guide - Microsoft Security Response Center
 

Back
Top