From 2b4e0c6b6060a6710a2f8ae4d29490c2bacca2e7 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 3 Nov 2022 13:02:22 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- gem5/tests/gem5/fixture.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gem5/tests/gem5/fixture.py b/gem5/tests/gem5/fixture.py index f28201b..c85a580 100644 --- a/gem5/tests/gem5/fixture.py +++ b/gem5/tests/gem5/fixture.py @@ -306,7 +306,26 @@ class DownloadedArchive(DownloadedProgram): def _extract(self): import tarfile with tarfile.open(self.filename) as tf: - tf.extractall(self.path) + 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(tf, self.path) def _setup(self, testitem): # Check to see if there is a file downloaded