You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks [CVE-2023-6597] #91133
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2022-03-10.17:22:08.506>labels= ['library', '3.9', 'type-crash']
title='tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks'updated_at=<Date2022-03-10.17:25:32.378>user='https://github.com/afeblot'
#!/usr/bin/env python3importosimporttempfiledefcreateUnremovableDir(workdir):
print(workdir)
os.mkdir(f'{workdir}/mydir')
os.symlink('/bin/bash', f'{workdir}/mydir/mylink') # Symlink to a root owned fileos.chmod(f'{workdir}/mydir', 0o555)
withtempfile.TemporaryDirectory() asworkdir:
createUnremovableDir(workdir)
Fails because tempfile.TemporaryDirectory._rmtree tries to execute os.chmod(path, 0o700) on the symlink, which by default tries to change the root owned file symlink target instead of the symlink itself:
/tmp/tmp1_dy42ef
Traceback (most recent call last):
File "/usr/lib/python3.9/shutil.py", line 682, in _rmtree_safe_fd
os.unlink(entry.name, dir_fd=topfd)
PermissionError: [Errno 13] Permission denied: 'mylink'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~/tempfile_demo_bug.py", line 13, in <module>
createUnremovableDir(workdir)
File "/usr/lib/python3.9/tempfile.py", line 969, in __exit__
self.cleanup()
File "/usr/lib/python3.9/tempfile.py", line 973, in cleanup
self._rmtree(self.name)
File "/usr/lib/python3.9/tempfile.py", line 955, in _rmtree
_rmtree(name, onerror=onerror)
File "/usr/lib/python3.9/shutil.py", line 727, in rmtree
_rmtree_safe_fd(fd, path, onerror)
File "/usr/lib/python3.9/shutil.py", line 664, in _rmtree_safe_fd
_rmtree_safe_fd(dirfd, fullname, onerror)
File "/usr/lib/python3.9/shutil.py", line 684, in _rmtree_safe_fd
onerror(os.unlink, fullname, sys.exc_info())
File "/usr/lib/python3.9/tempfile.py", line 941, in onerror
resetperms(path)
File "/usr/lib/python3.9/tempfile.py", line 936, in resetperms
_os.chmod(path, 0o700)
PermissionError: [Errno 1] Operation not permitted: '/tmp/tmp1_dy42ef/mydir/mylink'
#!/usr/bin/env python3importosimporttempfiledefcreateUnremovableDir(workdir):
print(workdir)
os.mkdir(f'{workdir}/mydir')
os.symlink('/bin/bash', f'{workdir}/mydir/mylink') # Symlink to a root owned fileos.chmod(f'{workdir}/mydir', 0o555)
def_rmtree(cls, name, ignore_errors=False):
defonerror(func, path, exc_info):
ifissubclass(exc_info[0], PermissionError):
defresetperms(path):
try:
ifos.chflagsinos.supports_follow_symlinks: # This is the patchos.chflags(path, 0, follow_symlinks=False) # This is the patchelifnotos.path.islink(path): # This is the patchos.chflags(path, 0)
exceptAttributeError:
passifos.chmodinos.supports_follow_symlinks: # This is the patchos.chmod(path, 0o700, follow_symlinks=False) # This is the patchelifnotos.path.islink(path): # This is the patchos.chmod(path, 0o700)
try:
ifpath!=name:
resetperms(os.path.dirname(path))
resetperms(path)
try:
os.unlink(path)
# PermissionError is raised on FreeBSD for directoriesexcept (IsADirectoryError, PermissionError):
cls._rmtree(path, ignore_errors=ignore_errors)
exceptFileNotFoundError:
passelifissubclass(exc_info[0], FileNotFoundError):
passelse:
ifnotignore_errors:
raiseshutil.rmtree(name, onerror=onerror)
# Monkey patch the class method tempfile.TemporaryDirectory._rmtreefromtypesimportMethodTypeimportshutiltempfile.TemporaryDirectory._rmtree=MethodType(_rmtree, tempfile.TemporaryDirectory)
withtempfile.TemporaryDirectory() asworkdir:
createUnremovableDir(workdir)
I think that it is a security issue which allows an unprivileged user to reset permissions of files if they can run Python program that has higher privileges. There are be special conditions for this, but perhaps you can do this by making the program to unpack a special tarfile that contains symlinks out of the tree in temporary directory.
changed the title [-]tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks[/-][+]tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks [CVE-2023-6597][/+]on Dec 8, 2023
Activity
afeblot commentedon Mar 10, 2022
Fails because
tempfile.TemporaryDirectory._rmtreetries to execute os.chmod(path, 0o700) on the symlink, which by default tries to change the root owned file symlink target instead of the symlink itself:and leaves:
This fixes it:
pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug
pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug in cleanup
pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug in cleanup
serhiy-storchaka commentedon Dec 7, 2023
I think that it is a security issue which allows an unprivileged user to reset permissions of files if they can run Python program that has higher privileges. There are be special conditions for this, but perhaps you can do this by making the program to unpack a special tarfile that contains symlinks out of the tree in temporary directory.
11 remaining items
[3.8] pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug in…
[3.12] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in clea…
[3.11] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in clea…
[-]tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks[/-][+]tempfile.TemporaryDirectory fails removing dir in some edge cases related to symlinks [CVE-2023-6597][/+][3.8] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in clean…
[3.9] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in clean…
[3.10] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in clea…
pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug in clean…
gpshead commentedon Feb 21, 2024
the fixes were merged, releases will be going out soon. thanks for the report and PRs everyone!
sparrowt commentedon Apr 3, 2024
The GHSA page for this CVE-2023-6597 GHSA-797f-63wg-8chv states that it affects python "versions 3.12.2, 3.11.8, ..." but AFAICS those versions both contain the fix (see
gh-91133mentioned in 3.12.2 release notes and 3.11.8 release notes).See also this comment on another CVE-fixing-PR where the same thing has happened.
sethmlarson commentedon Apr 3, 2024
As noted here, I have fixed the CVE record metadata: #109858 (comment)
sparrowt commentedon May 15, 2024
Thank you. Sadly GHSA-797f-63wg-8chv is still wrong :(
pythongh-91133: tempfile.TemporaryDirectory: fix symlink bug in clean…