Skip to main content

CVE-2025-6075: Python os.path.expandvars() DOS Vulnerability

CVE-2025-6075 is a denial of service flaw in Python's os.path.expandvars() function that causes performance degradation when processing user-controlled input. This post explains its impact, affected versions, and mitigation steps.

Published:

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

CVE-2025-6075 Overview

CVE-2025-6075 is a denial-of-service vulnerability in CPython's os.path.expandvars() function. The function exhibits quadratic algorithmic complexity when expanding environment variables in user-controlled input. An attacker who can supply crafted strings to os.path.expandvars() can trigger performance degradation and consume CPU resources on the host process. The flaw is tracked under CWE-400: Uncontrolled Resource Consumption and is fixed in the CPython tree via multiple backports to supported branches, including 3.11, 3.14, and the pre-release 3.15 alpha series.

Critical Impact

User-controlled values passed to os.path.expandvars() can cause algorithmic complexity denial of service through excessive CPU consumption during variable expansion.

Affected Products

  • Python CPython (posixpath.expandvars and ntpath.expandvars)
  • CPython 3.15.0 alpha1 and earlier affected branches (3.9, 3.11, 3.14 patched)
  • Any application invoking os.path.expandvars() on untrusted input

Discovery Timeline

  • 2025-10-31 - CVE-2025-6075 published to NVD
  • 2026-10-07 - Last updated in NVD database

Technical Details for CVE-2025-6075

Vulnerability Analysis

The vulnerability is an algorithmic complexity flaw in CPython's path utilities. os.path.expandvars() expands shell-style variables of the form $var and ${var} within a given path string. The pre-patch implementation used a regular expression search loop that re-scanned portions of the input on each iteration, producing quadratic O(n²) runtime relative to input length. When an attacker supplies a long, maliciously crafted string containing many $ sigils or unterminated ${ sequences, the function performs disproportionate work and ties up the Python interpreter thread. This maps to CWE-400: Uncontrolled Resource Consumption.

Root Cause

The root cause is the use of an iterative search-and-slice loop over the input path with the regex \$(\w+|\{[^}]*\}). Each unmatched or partial match segment caused the function to advance one character and re-scan, producing nested scans across overlapping windows of the string. The fix replaces this with a single compiled re.sub call using the pattern \$(\w+|\{[^}]*\}?), which performs linear substitution in one pass.

Attack Vector

Exploitation requires the attacker to control a value that reaches os.path.expandvars(). Typical exposure paths include configuration file processing, archive extraction routines, web application file-path handling, and CLI tools that normalize user-supplied paths. The CVSS vector CVSS:4.0/AV:L/AC:L/AT:P/PR:H/UI:N/VC:N/VI:N/VA:L indicates a local attack with limited availability impact. The result is CPU exhaustion in the hosting process, not code execution or data disclosure.

python
# Security patch in Lib/posixpath.py (CPython)
# Fix for quadratic complexity in os.path.expandvars()

-_varprog = None
-_varprogb = None
+_varpattern = r'\$(\w+|\{[^}]*\}?)'
+_varsub = None
+_varsubb = None

 def expandvars(path):
     """Expand shell variables of form $var and ${var}.  Unknown variables
     are left unchanged."""
     path = os.fspath(path)
-    global _varprog, _varprogb
+    global _varsub, _varsubb
     if isinstance(path, bytes):
         if b'$' not in path:
             return path
-        if not _varprogb:
+        if not _varsubb:
             import re
-            _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII)
-        search = _varprogb.search
+            _varsubb = re.compile(_varpattern.encode(), re.ASCII).sub
+        sub = _varsubb

Source: CPython commit 631ba340

Detection Methods for CVE-2025-6075

Indicators of Compromise

  • Sustained high CPU utilization on a single Python process without corresponding I/O or network activity
  • Python worker threads blocked for extended periods inside posixpath.expandvars or ntpath.expandvars
  • Request timeouts or thread-pool exhaustion in Python web services that process user-supplied paths or configuration strings

Detection Strategies

  • Inventory Python applications and identify call sites invoking os.path.expandvars() on externally sourced input via static analysis or grep across repositories
  • Enable Python profiling (cProfile, py-spy) in staging environments to flag long-running calls into expandvars
  • Instrument reverse proxies and WAFs to flag requests containing unusually long strings with repeated $ or unterminated ${ sequences

Monitoring Recommendations

  • Alert on process-level CPU anomalies in Python runtimes, especially for request handlers that normalize file paths
  • Track Python version inventory across production hosts and container images to confirm patched builds are deployed
  • Correlate elevated latency metrics with input sizes recorded at application entry points to detect algorithmic abuse patterns

How to Mitigate CVE-2025-6075

Immediate Actions Required

  • Upgrade to a patched CPython release for your branch (3.9, 3.11, 3.14 backports landed via the referenced commits) once published by the Python Software Foundation
  • Audit all invocations of os.path.expandvars() and remove calls that operate on untrusted input where feasible
  • Enforce input length limits and reject strings containing excessive $ sigils before passing them to path expansion routines

Patch Information

The fix is distributed through multiple commits to the CPython repository, replacing the iterative regex search with a single-pass re.sub substitution. Relevant upstream commits include commit 631ba340 for the 3.14 branch, commit 5dceb934 for the 3.11 branch, and commit 2e6150ad for the 3.9 branch. Tracking discussion is available in CPython issue gh-136065 and the Python Security Announcement Thread.

Workarounds

  • Validate and bound the length of any user-controlled string before passing it to os.path.expandvars()
  • Resolve environment variables through an allowlist lookup against os.environ instead of invoking expandvars on untrusted input
  • Execute path expansion inside a worker process with CPU time limits (for example, resource.setrlimit(RLIMIT_CPU, ...)) to contain runaway execution
bash
# Confirm the installed Python version and verify it includes the fix
python3 --version
python3 -c "import posixpath, inspect; print('_varpattern' in inspect.getsource(posixpath))"

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.