From 1aabd019194fa99787e7cb8e42504461497e6275 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 29 Oct 2022 16:24:25 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../create_debian_packages.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/linux_dist_support/create_debian_packages.py b/tools/linux_dist_support/create_debian_packages.py index 94c0d53c121..6911c2ad36d 100755 --- a/tools/linux_dist_support/create_debian_packages.py +++ b/tools/linux_dist_support/create_debian_packages.py @@ -68,7 +68,26 @@ def BuildDebianPackage(tarball, out_dir, arch, toolchain): copyfile(tarball, origtarball) with tarfile.open(origtarball) as tar: - tar.extractall(path=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, path=temp_dir) # Build source package. print("Building source package")