Skip to content

Commit 2f16f01

Browse files
TrellixVulnTeampre-commit-ci[bot]monai-bot
authored
# Patching CVE-2007-4559 Hi, we are security researchers from the Advanced Research Center at [Trellix](https://www.trellix.com). 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](https://www.trellix.com/en-us/about/newsroom/stories/research/tarfile-exploiting-the-world.html). If you have further questions you may contact us through this projects lead researcher [Kasimir Schulz](mailto:[email protected]). Signed-off-by: monai-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: monai-bot <[email protected]>
1 parent e4a0de9 commit 2f16f01

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

monai/networks/nets/transchex.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,26 @@ def from_pretrained(
7272
else:
7373
tempdir = tempfile.mkdtemp()
7474
with tarfile.open(resolved_archive_file, "r:gz") as archive:
75-
archive.extractall(tempdir)
75+
76+
def is_within_directory(directory, target):
77+
78+
abs_directory = os.path.abspath(directory)
79+
abs_target = os.path.abspath(target)
80+
81+
prefix = os.path.commonprefix([abs_directory, abs_target])
82+
83+
return prefix == abs_directory
84+
85+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
86+
87+
for member in tar.getmembers():
88+
member_path = os.path.join(path, member.name)
89+
if not is_within_directory(path, member_path):
90+
raise Exception("Attempted Path Traversal in Tar File")
91+
92+
tar.extractall(path, members, numeric_owner=numeric_owner)
93+
94+
safe_extract(archive, tempdir)
7695
serialization_dir = tempdir
7796
model = cls(num_language_layers, num_vision_layers, num_mixed_layers, bert_config, *inputs, **kwargs)
7897
if state_dict is None and not from_tf:

0 commit comments

Comments
 (0)