-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
"tarfile" library will lead to "write any content to any file on the host". #88189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
if uncompress file twice to the same dir, attacker can "write any content to any file on the host"". poc code like below:
in this poc code, if one service uncompress tar file which is uploaded by attacker to "dir_name" twice, attacker can create "/tmp/a" and write "it is just poc" string into "/tmp/a" file. |
Can you contact the security team (info at https://www.python.org/dev/security/ ) directly? In general, tarfile (and other Python file functions!) can create files anywhere on the filesystem, provided that the process user has the right permissions. But it seems that you’re talking about an unexpected behaviour leading to unwanted operations, so please send more details about the problem to the team. Thank you for your report! |
TL;DR - A tar file being extracted doesn't check to see if it is overwriting an existing file, which could be a symlink to elsewhere leading to elsewhere's contents being clobbered assuming the elsewhere file exists. doing an unlink before opening the destination file (ignoring either success or FileNotFound) during extract would avoid this _specific_ case. But tarfile is already documented with a warning about untrusted inputs being able to do bad things: https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall fixing this one serialized case doesn't do anything about other cases or race conditions we won't claim protection against, so I'm not sure this issue is serious from a stdlib perspective. |
This looks like a relevant change from OpenBSD pax:
Handling symlinks in general is fraught with danger. See: |
hah, I enjoy the editorial description of what openbsd does in their comment: openbsd/src@6b45b47#diff-8934d8de794095d2f05a1d6ff3354b371ce2d2e01d0fe4ddf43b853ef5a0e077R460 |
PEP-706 (Filter for tarfile.extractall) has been implemented in #102950. See the added docs. Python 3.12, and security updates to some earlier releases, will allow users to avoid this issue by changing their code/settings. For the original report: note that you don't need to extract twice -- |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: