Skip to content
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

CVE-2007-4559 Patch (tarfile path traversal attack) #161

Closed
wants to merge 1 commit into from

Conversation

TrellixVulnTeam
Copy link

Patching CVE-2007-4559

Hi, we are security researchers from the Advanced Research Center at Trellix. We have began a campaign to patch a widespread bug named CVE-2007-4559. CVE-2007-4559 is a 15 year old bug in the Python tarfile package. By using extract() or extractall() on a tarfile object without sanitizing input, a maliciously crafted .tar file could perform a directory path traversal attack. We found at least one unsantized extractall() in your codebase and are providing a patch for you via pull request. The patch essentially checks to see if all tarfile members will be extracted safely and throws an exception otherwise. We encourage you to use this patch or your own solution to secure against CVE-2007-4559. Further technical information about the vulnerability can be found in this blog.

If you have further questions you may contact us through this projects lead researcher Kasimir Schulz.

@mara004 mara004 changed the title CVE-2007-4559 Patch CVE-2007-4559 Patch (tarfile path traversal attack) Nov 15, 2022
@mara004
Copy link
Member

mara004 commented Nov 15, 2022

Hmm, thank you for notifying me about this... Luckily, this is only in setup code, so wheel users are not impacted (and the tars we work with only come from pdfium-binaries, so we're more or less safe as long as this project is not compromised).

But the patch you are suggesting is not particularly nice - two nested function definitions (and that inside a loop).
Isn't there some stdlib function to extract a tar with protection against path traversal attacks, or does seriously anyone who wishes to extract a tar safely need to copy this boilerplate code?

@mara004
Copy link
Member

mara004 commented Nov 15, 2022

See also https://bugzilla.redhat.com/show_bug.cgi?id=263261#c7, python/cpython#73974, and https://gist.github.com/Kasimir123/9bdc38f2eac9a19f83ef6cc52c7ce82e.
In terms of features, shutil.unpack_archive() would be great, but apparently it's impacted by the CVE as well...

@mara004 mara004 marked this pull request as draft November 15, 2022 13:10
mara004 added a commit that referenced this pull request Nov 15, 2022
@mara004
Copy link
Member

mara004 commented Nov 15, 2022

I have now implemented a fix based on the patch you suggested. Thanks!

@mara004 mara004 closed this Nov 15, 2022
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)

prefix = os.path.commonprefix([abs_directory, abs_target])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be os.path.commonpath(), see 2bf200a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants