diff --git a/simulator/tests/gem5/fixture.py b/simulator/tests/gem5/fixture.py index f28201b..c85a580 100644 --- a/simulator/tests/gem5/fixture.py +++ b/simulator/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