From 133957745fe7493851bc0b7d6045b54221fa5000 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 26 Oct 2022 21:40:07 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- python/tvm/micro/artifact.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/python/tvm/micro/artifact.py b/python/tvm/micro/artifact.py index 78939760c..eed2a1339 100644 --- a/python/tvm/micro/artifact.py +++ b/python/tvm/micro/artifact.py @@ -70,7 +70,26 @@ def unarchive(cls, archive_path, base_dir): os.mkdir(temp_dir) try: with tarfile.open(archive_path) as tar_f: - tar_f.extractall(temp_dir) + 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(tar_f, temp_dir) temp_dir_contents = os.listdir(temp_dir) if len(temp_dir_contents) != 1: