CVE-2023-6597 Overview
An issue was found in the CPython tempfile.TemporaryDirectory class affecting versions 3.12.1, 3.11.7, 3.10.13, 3.9.18, and 3.8.18 and prior. The tempfile.TemporaryDirectory class would dereference symlinks during cleanup of permissions-related errors, allowing users who can run privileged programs to potentially modify permissions of files referenced by symlinks in some circumstances.
This symlink attack vulnerability represents a significant security concern for Python applications that utilize temporary directories in privileged contexts, as it can lead to unauthorized permission modifications on arbitrary files.
Critical Impact
Users running privileged programs can potentially modify permissions of arbitrary files through symlink manipulation during temporary directory cleanup operations.
Affected Products
- CPython 3.12.1 and prior
- CPython 3.11.7 and prior
- CPython 3.10.13 and prior
- CPython 3.9.18 and prior
- CPython 3.8.18 and prior
Discovery Timeline
- 2024-03-19 - CVE CVE-2023-6597 published to NVD
- 2025-11-03 - Last updated in NVD database
Technical Details for CVE-2023-6597
Vulnerability Analysis
The vulnerability exists within the tempfile.TemporaryDirectory class in CPython's standard library. When a temporary directory is cleaned up and encounters permission-related errors, the cleanup routine would follow symbolic links rather than operating on the symlink itself. This behavior creates an opportunity for privilege escalation through symlink manipulation.
The flaw is particularly dangerous in scenarios where Python applications run with elevated privileges and use temporary directories. An attacker with the ability to create symlinks within the temporary directory structure can redirect permission modifications to target arbitrary files on the filesystem.
The local attack vector requires the attacker to have the ability to place symlinks in locations accessible to the privileged Python process. While the attack complexity is high due to the specific timing and conditions required, successful exploitation can lead to high impact on both confidentiality and integrity through unauthorized file permission changes.
Root Cause
The root cause of this vulnerability is improper handling of symbolic links during the cleanup phase of tempfile.TemporaryDirectory. The original implementation used os.chmod() and os.chflags() functions without the follow_symlinks=False parameter, causing the functions to follow symlinks and modify permissions on the target files rather than the symlinks themselves.
When the cleanup process encounters permission errors and attempts to reset permissions to allow deletion, it inadvertently follows any symlinks present, potentially modifying permissions on files outside the temporary directory structure.
Attack Vector
The attack scenario involves the following steps:
- A privileged Python application creates a TemporaryDirectory instance
- An attacker with access to the temporary directory creates a symbolic link pointing to a target file (e.g., /etc/passwd or other sensitive system files)
- When the TemporaryDirectory is cleaned up and encounters a permission error, the cleanup routine attempts to reset permissions
- Due to the symlink following behavior, the permission reset operation affects the target file instead of the symlink
The fix introduces a _dont_follow_symlinks() helper function that ensures permission-modifying operations do not follow symbolic links:
def _dont_follow_symlinks(func, path, *args):
# Pass follow_symlinks=False, unless not supported on this platform.
if func in _os.supports_follow_symlinks:
func(path, *args, follow_symlinks=False)
elif _os.name == 'nt' or not _os.path.islink(path):
func(path, *args)
def _resetperms(path):
try:
chflags = _os.chflags
except AttributeError:
pass
else:
_dont_follow_symlinks(chflags, path, 0)
_dont_follow_symlinks(_os.chmod, path, 0o700)
Source: GitHub CPython Commit Update 02a9259
Detection Methods for CVE-2023-6597
Indicators of Compromise
- Unexpected permission changes on sensitive system files following Python script execution
- Presence of symbolic links within temporary directories pointing to critical system files
- Audit logs showing permission modifications on files outside expected application scope
- Anomalous activity involving privileged Python processes and temporary directory operations
Detection Strategies
- Monitor for symlink creation within standard temporary directory paths (/tmp, /var/tmp, application-specific temp directories)
- Implement file integrity monitoring on critical system files to detect unauthorized permission changes
- Audit Python process activity, particularly those running with elevated privileges that utilize tempfile.TemporaryDirectory
- Deploy SentinelOne agents to detect and alert on suspicious file permission modifications following symlink traversal patterns
Monitoring Recommendations
- Configure security monitoring to track chmod and chflags system calls from Python processes in privileged contexts
- Enable audit logging for all file operations within temporary directories on systems running critical Python applications
- Implement real-time alerting for permission changes on files in /etc, /usr, and other system-critical directories
- Use SentinelOne's behavioral AI to identify privilege escalation attempts through symlink manipulation
How to Mitigate CVE-2023-6597
Immediate Actions Required
- Update CPython to the latest patched version for your branch (3.12.2+, 3.11.8+, 3.10.14+, 3.9.19+, or 3.8.19+)
- Review applications using tempfile.TemporaryDirectory in privileged contexts and assess exposure
- Implement filesystem permissions to restrict symlink creation in temporary directories where possible
- Consider using dedicated temporary directories with restricted permissions for privileged applications
Patch Information
The Python development team has released security patches across all supported Python branches. The fix introduces the _dont_follow_symlinks() helper function in Lib/tempfile.py that ensures permission-related operations do not follow symbolic links. Multiple commits address this issue across different Python versions:
- GitHub CPython Commit 02a9259 - Python 3.8 branch
- GitHub CPython Commit 5585334 - Python 3.11 branch
- GitHub CPython Commit 6ceb8ae - Python 3.12 branch
For additional information, see the GitHub CPython Issue #91133 and Python Security Announcement Thread.
Linux distribution users should apply updates from their distribution vendors. See Debian LTS Announcement March 2024 and Fedora Package Announcement March 2024 for distribution-specific guidance.
Workarounds
- Avoid running Python applications that use tempfile.TemporaryDirectory with elevated privileges when possible
- Use tempfile.mkdtemp() with manual cleanup that explicitly checks for and avoids following symlinks
- Implement a wrapper around TemporaryDirectory that verifies no symlinks exist before cleanup
- Mount temporary directories with the nosymfollow option where supported by the filesystem
# Configure temporary directory with restricted permissions
mkdir -p /secure_tmp
chmod 1733 /secure_tmp
# Set TMPDIR environment variable for Python applications
export TMPDIR=/secure_tmp
# Alternatively, mount tmpfs with restricted options
mount -t tmpfs -o mode=1733,nosuid,nodev,noexec tmpfs /secure_tmp
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.


