From bd907f4c44f9424a9c5817b38bd24e68193d222e Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 19 Oct 2022 18:57:10 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../official/nlp/transformer/data_download.py | 21 ++++++++++++++++++- .../research/deep_speech/data/download.py | 21 ++++++++++++++++++- .../models/research/neural_gpu/wmt_utils.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/mobile_deployment/tensorflow/slim/models/official/nlp/transformer/data_download.py b/mobile_deployment/tensorflow/slim/models/official/nlp/transformer/data_download.py index e5f6668..9cf11b1 100644 --- a/mobile_deployment/tensorflow/slim/models/official/nlp/transformer/data_download.py +++ b/mobile_deployment/tensorflow/slim/models/official/nlp/transformer/data_download.py @@ -208,7 +208,26 @@ def download_and_extract(path, url, input_filename, target_filename): # Extract compressed files logging.info("Extracting %s." % compressed_file) with tarfile.open(compressed_file, "r:gz") as corpus_tar: - corpus_tar.extractall(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(corpus_tar, path) # Return file paths of the requested files. input_file = find_file(path, input_filename) diff --git a/mobile_deployment/tensorflow/slim/models/research/deep_speech/data/download.py b/mobile_deployment/tensorflow/slim/models/research/deep_speech/data/download.py index 5ded037..020dd9f 100644 --- a/mobile_deployment/tensorflow/slim/models/research/deep_speech/data/download.py +++ b/mobile_deployment/tensorflow/slim/models/research/deep_speech/data/download.py @@ -78,7 +78,26 @@ def _progress(count, block_size, total_size): tf.logging.info( "Successfully downloaded %s, size(bytes): %d" % (url, statinfo.st_size)) with tarfile.open(tar_filepath, "r") as tar: - 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(tar, directory) finally: tf.gfile.Remove(tar_filepath) diff --git a/mobile_deployment/tensorflow/slim/models/research/neural_gpu/wmt_utils.py b/mobile_deployment/tensorflow/slim/models/research/neural_gpu/wmt_utils.py index ef83191..dd50561 100644 --- a/mobile_deployment/tensorflow/slim/models/research/neural_gpu/wmt_utils.py +++ b/mobile_deployment/tensorflow/slim/models/research/neural_gpu/wmt_utils.py @@ -84,7 +84,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