Repository navigation
[3.12] gh-157579: Fix race condition in the cleanup of tempfile.TemporaryDirectory (GH-157580) (GH-158430) - #159074
Merged
Merged
Conversation
…e.TemporaryDirectory` (pythonGH-157580) (pythonGH-158430) * pythongh-157579: Fix race condition in the cleanup of `tempfile.TemporaryDirectory` (pythonGH-157580) (cherry picked from commit 5c20517) * Add root user checks to test.support This partially backports commit 86b8617 (pythonGH-146195); making existing tests use the helper is omitted. --------- (cherry picked from commit 58ed025) Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Petr Viktorin <encukou@gmail.com>
Documentation build overview
497 files changed ·
|
StanFromIreland
approved these changes
Oct 21, 2026
This was referenced Oct 28, 2026
Merged
icanhasmath
added a commit
to ActiveState/cpython
that referenced
this pull request
Oct 28, 2026
….TemporaryDirectory (CVE-2026-12345) When TemporaryDirectory cleanup recovers from a PermissionError it resets permissions and retries removal. It did that by resolving paths again, so an attacker who could modify the tree during cleanup could replace a directory with a symlink and make cleanup delete files, or reset their permissions and flags, outside of the temporary directory. Files are now handled relative to the open directory file descriptors that the fd-based shutil.rmtree() walk already holds. Port of the upstream fix (pythonGH-157580, pythonGH-158430), based on the 3.12 backport (python#159074), plus pythongh-134993 (os.lstat in os.supports_dir_fd). 3.10.22.x: the 3.12 fix relies on 3.12's shutil.rmtree() API (onexc, a public dir_fd parameter, and the _onexc_kwargs switch that passes direntry/dir_fd to the callback). 3.10 only has onerror and a recursive _rmtree_safe_fd(), so this adds the private keyword arguments _dir_fd and _onerror_kwargs to rmtree() instead and threads the parent directory fd and os.DirEntry through _rmtree_safe_fd(). With _onerror_kwargs left false, existing onerror callbacks get the same three positional arguments as before. TemporaryDirectory._rmtree() is ported onto the 3.11 shape of the code (bpo-43153 and pythongh-79325 are picked first), using os.lstat(base, dir_fd=...) where 3.12 uses os.path.isdir()/isjunction() on the full path. Tests are adapted: onerror instead of onexc, local root and platform skips instead of support.requires_non_root_user and PY_SUPPORT_TIER. (cherry picked from commit c39b52d) (cherry picked from commit 5c20517) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
icanhasmath
added a commit
to ActiveState/cpython
that referenced
this pull request
Oct 28, 2026
…TemporaryDirectory (CVE-2026-12345) When TemporaryDirectory cleanup recovers from a PermissionError it resets permissions and retries removal. It did that by resolving paths again, so an attacker who could modify the tree during cleanup could replace a directory with a symlink and make cleanup delete files, or reset their permissions and flags, outside of the temporary directory. Files are now handled relative to the open directory file descriptors that the fd-based shutil.rmtree() walk already holds. Port of the upstream fix (pythonGH-157580, pythonGH-158430), based on the 3.12 backport (python#159074), plus pythongh-134993 (os.lstat in os.supports_dir_fd). 3.10.22.x: the 3.12 fix relies on 3.12's shutil.rmtree() API (onexc, a public dir_fd parameter, and the _onexc_kwargs switch that passes direntry/dir_fd to the callback). 3.10 only has onerror and a recursive _rmtree_safe_fd(), so this adds the private keyword arguments _dir_fd and _onerror_kwargs to rmtree() instead and threads the parent directory fd and os.DirEntry through _rmtree_safe_fd(). With _onerror_kwargs left false, existing onerror callbacks get the same three positional arguments as before. TemporaryDirectory._rmtree() is ported onto the 3.11 shape of the code (bpo-43153 and pythongh-79325 are picked first), using os.lstat(base, dir_fd=...) where 3.12 uses os.path.isdir()/isjunction() on the full path. Tests are adapted: onerror instead of onexc, local root and platform skips instead of support.requires_non_root_user and PY_SUPPORT_TIER. (cherry picked from commit c39b52d) (cherry picked from commit 5c20517) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 776b466) 3.9.25.x: tests use test.support instead of test.support.os_helper, and test_parent_mode_preserved only covers the default (no ignore_errors) case because 3.9's TemporaryDirectory has no ignore_cleanup_errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tempfile.TemporaryDirectory#157579: Fix race condition in the cleanup oftempfile.TemporaryDirectory(gh-157579: Fix race condition in the cleanup oftempfile.TemporaryDirectory#157580)(cherry picked from commit 5c20517)
This partially backports commit 86b8617 (GH-146195); making existing tests use the helper is omitted.
(cherry picked from commit 86b8617)
(cherry picked from commit e004cf8)