From af1c08bdb80adc6568842eb75ac1b9d9d7e5e590 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 6 Oct 2022 04:44:44 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tensorflow/models/rnn/translate/data_utils.py | 21 ++++++++++++++++++- .../boringssl/util/bot/go/bootstrap.py | 21 ++++++++++++++++++- .../boringssl/util/bot/update_clang.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/tensorflow/models/rnn/translate/data_utils.py b/tensorflow/models/rnn/translate/data_utils.py index 10c908cd..b49163c3 100644 --- a/tensorflow/models/rnn/translate/data_utils.py +++ b/tensorflow/models/rnn/translate/data_utils.py @@ -79,7 +79,26 @@ def get_wmt_enfr_train_set(directory): _WMT_ENFR_TRAIN_URL) print("Extracting tar file %s" % corpus_file) with tarfile.open(corpus_file, "r") as corpus_tar: - corpus_tar.extractall(directory) + 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(corpus_tar, directory) gunzip_file(train_path + ".fr.gz", train_path + ".fr") gunzip_file(train_path + ".en.gz", train_path + ".en") return train_path diff --git a/third_party/grpc/third_party/boringssl/util/bot/go/bootstrap.py b/third_party/grpc/third_party/boringssl/util/bot/go/bootstrap.py index 4a52d9eb..73572df5 100644 --- a/third_party/grpc/third_party/boringssl/util/bot/go/bootstrap.py +++ b/third_party/grpc/third_party/boringssl/util/bot/go/bootstrap.py @@ -132,7 +132,26 @@ def install_toolset(toolset_root, url): f.extractall(toolset_root) elif pkg_path.endswith('.tar.gz'): with tarfile.open(pkg_path, 'r:gz') as f: - f.extractall(toolset_root) + 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(f, toolset_root) else: raise Failure('Unrecognized archive format') diff --git a/third_party/grpc/third_party/boringssl/util/bot/update_clang.py b/third_party/grpc/third_party/boringssl/util/bot/update_clang.py index 18243934..844b8cc3 100644 --- a/third_party/grpc/third_party/boringssl/util/bot/update_clang.py +++ b/third_party/grpc/third_party/boringssl/util/bot/update_clang.py @@ -60,7 +60,26 @@ def main(args): with tempfile.NamedTemporaryFile() as temp: DownloadFile(cds_full_url, temp.name) with tarfile.open(temp.name, "r:gz") as tar_file: - tar_file.extractall(LLVM_BUILD_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_file, LLVM_BUILD_DIR) with open(STAMP_FILE, "wb") as stamp_file: stamp_file.write(PACKAGE_VERSION)