From fbec8337d1f5acbaaffbaac8e7cede8cea5b37c6 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 25 Oct 2022 10:04:20 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../torchvision/datasets/cifar.py | 21 ++++++++++++++++++- .../site-packages/torchvision/datasets/sbu.py | 21 ++++++++++++++++++- .../site-packages/torchvision/datasets/voc.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/cifar.py b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/cifar.py index 8441c9f7..1473151f 100644 --- a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/cifar.py +++ b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/cifar.py @@ -151,7 +151,26 @@ def download(self): # extract file with tarfile.open(os.path.join(self.root, self.filename), "r:gz") as tar: - tar.extractall(path=self.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(tar, path=self.root) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n' diff --git a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/sbu.py b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/sbu.py index e1dde981..e4274878 100644 --- a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/sbu.py +++ b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/sbu.py @@ -95,7 +95,26 @@ def download(self): # Extract file with tarfile.open(os.path.join(self.root, self.filename), 'r:gz') as tar: - tar.extractall(path=self.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(tar, path=self.root) # Download individual photos with open(os.path.join(self.root, 'dataset', 'SBU_captioned_photo_dataset_urls.txt')) as fh: diff --git a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/voc.py b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/voc.py index 1ec44b3c..e370053f 100644 --- a/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/voc.py +++ b/venv_vid2vid/lib/python3.7/site-packages/torchvision/datasets/voc.py @@ -241,4 +241,23 @@ def parse_voc_xml(self, node): def download_extract(url, root, filename, md5): download_url(url, root, filename, md5) with tarfile.open(os.path.join(root, filename), "r") as tar: - tar.extractall(path=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(tar, path=root)