Visitar URL original
gh-157579: Fix race condition in the cleanup of `tempfile.TemporaryDirectory` by StanFromIreland · Pull Request #157580 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ff74078
Fix `tempfile.TemporaryDirectory` cleanup race condition
StanFromIreland Aug 14, 2026
0fd04ae
Pass descriptors to `onexc` instead of re-walking
StanFromIreland Aug 26, 2026
55c0c1f
Petr's review
StanFromIreland Aug 28, 2026
b4c1865
Add regression check for _rmtree_use_dir_fd=True
encukou Sep 1, 2026
51c9c6a
Avoid infinite recursion if directory removal fails
encukou Sep 1, 2026
bbbb669
Bring back chmod by name (avoid regression); use LBYL
encukou Sep 1, 2026
a9a993e
Bring back chmod(open(nofollow))
encukou Sep 1, 2026
f524182
Add warning for Windows
encukou Sep 1, 2026
d4e5045
Use lstat; check for it
encukou Sep 1, 2026
4a9eb37
Test that cleanup() does not chmod the parent directory
encukou Sep 1, 2026
4a2ab25
Catch OSError on ignore_errors
encukou Sep 1, 2026
99c6f63
Apply suggestion from @StanFromIreland
encukou Sep 8, 2026
ffcf4bb
Match else: to try:, and don't catch AttributeError
encukou Sep 8, 2026
7994121
Use new issue number.
StanFromIreland Sep 15, 2026
10c4541
Merge branch 'main' into tempfile-toctou-2
StanFromIreland Sep 15, 2026
a837624
Add WASI skip
StanFromIreland Sep 15, 2026
4baeeb1
Update Lib/test/test_tempfile.py
StanFromIreland Sep 17, 2026
c12bec3
Revert "Update Lib/test/test_tempfile.py"
StanFromIreland Sep 17, 2026
de46e06
Make dir1 writeable before rename
StanFromIreland Sep 17, 2026
d8aab2b
Add skip for Emscripten
StanFromIreland Sep 22, 2026
9992aae
Fix case
encukou Sep 22, 2026
7568089
Merge branch 'main' into tempfile-toctou-2
encukou Sep 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bring back chmod(open(nofollow))
This reduces the attack surface -- readable files avoid the race condition.
  • Loading branch information
encukou committed Sep 1, 2026
commit a9a993e7b3259c88dfc59e22970580feb2dc318c
20 changes: 19 additions & 1 deletion Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ def _resetperms_fd(dir_fd, path):
_resetflags(path)
_os.chmod(dir_fd, 0o700)

try:
_nofollow_mode = _os.O_RDONLY | _os.O_NONBLOCK | _os.O_NOFOLLOW
except AttributeError:
_nofollow_mode = None

def _resetperms_at(name, dir_fd, path):
# Same as _resetperms(), but name is resolved relative to the directory
# file descriptor dir_fd. path is only used for os.chflags(), which
Expand All @@ -314,7 +319,20 @@ def _resetperms_at(name, dir_fd, path):
_os.chmod(name, 0o700, dir_fd=dir_fd, follow_symlinks=False)
else:
# dir_fd & follow_symlinks is not supported on this platform.
# We change by name, which is subject to a race condition.
# Try chmod opening the file with O_NOFOLLOW.
if _nofollow_mode is not None:
try:
fd = _os.open(name, _nofollow_mode, dir_fd=dir_fd)
except (OSError, AttributeError):
pass
else:
try:
_os.chmod(fd, 0o700)
finally:
_os.close(fd)
return
# If that did not work, we change by name, which is subject to a race
# condition.
stat = _os.stat(name, dir_fd=dir_fd, follow_symlinks=False)
if not _stat.S_ISLNK(stat.st_mode):
_os.chmod(name, 0o700, dir_fd=dir_fd)
Expand Down