From a23a2e1bbe65798c037e28b1ad531a373b2f47b9 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 7 Oct 2022 20:27:09 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- scripts/datasets/imagenet.py | 42 ++++++++++++++++++++++++++++++++-- scripts/datasets/pascal_voc.py | 42 ++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/scripts/datasets/imagenet.py b/scripts/datasets/imagenet.py index c950666..d1731b1 100644 --- a/scripts/datasets/imagenet.py +++ b/scripts/datasets/imagenet.py @@ -85,7 +85,26 @@ def extract_train(tar_fname, target_dir, with_rec=False, num_thread=1): class_dir = os.path.splitext(class_fname)[0] os.mkdir(class_dir) with tarfile.open(class_fname) as f: - f.extractall(class_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(f, class_dir) os.remove(class_fname) pbar.update(1) pbar.close() @@ -96,7 +115,26 @@ def extract_val(tar_fname, target_dir, with_rec=False, num_thread=1): os.makedirs(target_dir) print('Extracting ' + tar_fname) with tarfile.open(tar_fname) as tar: - tar.extractall(target_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, target_dir) # build rec file before images are moved into subfolders if with_rec: build_rec_process(target_dir, False, num_thread) diff --git a/scripts/datasets/pascal_voc.py b/scripts/datasets/pascal_voc.py index 636e4a1..960b584 100644 --- a/scripts/datasets/pascal_voc.py +++ b/scripts/datasets/pascal_voc.py @@ -35,7 +35,26 @@ def download_voc(path, overwrite=False): filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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(tar, path=path) ##################################################################################### @@ -49,7 +68,26 @@ def download_aug(path, overwrite=False): filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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(tar, path=path) shutil.move(os.path.join(path, 'benchmark_RELEASE'), os.path.join(path, 'VOCaug')) filenames = ['VOCaug/dataset/train.txt', 'VOCaug/dataset/val.txt']