tests, resources: CVE-2007-4559 Patch

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.

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

Change-Id: I891ac6652cfbd479aed51d64ef6d4e0fe740e06d
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/65271
Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
This commit is contained in:
Melissa Jost
2022-11-03 09:06:03 -07:00
committed by Bobby Bruce
parent f61a640d30
commit 23d405ea55
2 changed files with 46 additions and 2 deletions

View File

@@ -501,5 +501,28 @@ def get_resource(
)
unpack_to = download_dest[: -len(tar_extension)]
with tarfile.open(download_dest) as f:
f.extractall(unpack_to)
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(
tar, path=".", members=None, *, numeric_owner=False
):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception(
"Attempted Path Traversal in Tar File"
)
tar.extractall(path, members, numeric_owner=numeric_owner)
safe_extract(f, unpack_to)
os.remove(download_dest)