Skip to main content
Vulnerability Database/CVE-2026-12345

CVE-2026-12345: Python tempfile Race Condition Vulnerability

CVE-2026-12345 is a race condition flaw in Python's tempfile.TemporaryDirectory that allows attackers to manipulate file cleanup via symbolic links. 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-2026-12345 Overview

CVE-2026-12345 is a race condition vulnerability in the cleanup routine of Python's tempfile.TemporaryDirectory. An attacker who can modify the directory tree during cleanup can replace a directory with a symbolic link. This causes files outside the temporary directory to be deleted, or have their permissions and file flags reset, using the privileges of the process performing the cleanup. The flaw is classified under [CWE-59] (Improper Link Resolution Before File Access, or symlink following). Platforms where shutil.rmtree.avoids_symlink_attacks evaluates to false remain affected, and file flags may still be reset outside the tree on all platforms.

Critical Impact

A local attacker can leverage a symlink race during temporary directory cleanup to delete arbitrary files or reset permissions and file flags outside the intended tree with the privileges of the cleaning process.

Affected Products

  • CPython tempfile.TemporaryDirectory cleanup routine
  • shutil.rmtree on platforms where avoids_symlink_attacks is false
  • Python applications relying on tempfile.TemporaryDirectory for isolation

Discovery Timeline

Technical Details for CVE-2026-12345

Vulnerability Analysis

The vulnerability resides in the recursive cleanup logic used by tempfile.TemporaryDirectory, which delegates directory removal to shutil.rmtree. During traversal, the cleanup routine walks the tree and issues removal operations on entries it previously enumerated. If the tree is mutated between enumeration and removal, the path a syscall operates on may no longer refer to the original inode. An attacker with write access to any subdirectory of the temporary tree can replace a directory entry with a symbolic link pointing outside the tree. The cleanup process then follows that link and applies destructive operations to unintended targets.

Root Cause

The root cause is a time-of-check to time-of-use (TOCTOU) gap between directory enumeration and the syscalls that unlink entries or reset metadata. The cleanup path did not consistently pass a parent directory file descriptor to constrain resolution, allowing symbolic link substitution to redirect operations. File flag reset operations remain vulnerable on all platforms even after the primary fix, because those calls do not universally use AT_SYMLINK_NOFOLLOW semantics.

Attack Vector

Exploitation requires local access and the ability to write into the temporary directory tree while cleanup is in progress. An attacker races the cleanup loop by removing an enumerated subdirectory and replacing it with a symlink to an external target such as /etc or a user home directory. When cleanup dereferences the crafted entry, files outside the temporary tree are deleted or have their permissions and BSD file flags reset with the privileges of the process performing the cleanup, which may be root in daemon or CI contexts.

python
# Excerpt from the upstream patch to Lib/shutil.py
#   save a call to os.lstat() when walking subdirectories.
func, dirfd, path, orig_entry = stack.pop()
name = path if orig_entry is None else orig_entry.name
parent_fd = None if func is os.close else dirfd
try:
    if func is os.close:
        os.close(dirfd)
# Source: https://github.com/python/cpython/commit/5c20517a4fc56683efe63a7751020db9573f538d

The patch introduces a parent_fd derived from the traversal dirfd, enabling subsequent syscalls to resolve entries relative to an open directory descriptor rather than a full path subject to symlink substitution.

Detection Methods for CVE-2026-12345

Indicators of Compromise

  • Unexpected deletion of files outside temporary directories owned by processes that use tempfile.TemporaryDirectory.
  • Permission or BSD file flag resets on sensitive paths such as /etc, /var, or user home directories correlated with Python process activity.
  • Presence of symbolic links inside /tmp, /var/tmp, or application temp trees that point to system paths.

Detection Strategies

  • Audit filesystem telemetry for unlinkat, rmdir, and chmod syscalls issued by Python interpreters against paths outside the current working temporary directory.
  • Monitor for symlink creation events inside temporary directories immediately followed by removal syscalls from another process ID.
  • Compare pre- and post-run permission baselines on sensitive directories for services that spawn Python worker processes.

Monitoring Recommendations

  • Enable Linux auditd rules on symlink, symlinkat, and renameat syscalls within temporary directory paths.
  • Log all Python process invocations that run with elevated privileges and inspect their use of tempfile APIs.
  • Alert on any process that resets file flags via chflags on paths outside its expected working set.

How to Mitigate CVE-2026-12345

Immediate Actions Required

  • Apply the upstream CPython fix from GitHub Pull Request 157580 or the corresponding backport for your Python version.
  • Avoid running processes that use tempfile.TemporaryDirectory with elevated privileges when untrusted users share the filesystem namespace.
  • Restrict permissions on parent temporary directories so that only the owning user can create entries within them.

Patch Information

The fix is committed to CPython in commit 5c20517a, tracked under GitHub Issue 157579. It hardens shutil.rmtree by using a parent directory file descriptor during traversal so that subsequent operations resolve relative to an open dirfd rather than a symlink-substitutable path. Users should upgrade to the patched Python release for their branch.

Workarounds

  • Place temporary directories under a per-user path such as ~/.cache/app with mode 0700 to prevent other local users from racing cleanup.
  • Use private mount namespaces or containers to isolate temporary filesystem operations from the host.
  • Perform cleanup as an unprivileged user by dropping privileges before invoking TemporaryDirectory.cleanup().
bash
# Create a private, mode 0700 temp root to reduce exposure to local races
export TMPDIR="$HOME/.cache/app-tmp"
install -d -m 0700 "$TMPDIR"

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.