From 0f0c8523801af295c34a6eba6e98afee57db2216 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 3 Oct 2022 14:26:30 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- .../frontends/cntk/examples/cifar_prepare.py | 21 ++++++++++++++++++- ngraph/frontends/neon/data/cifar10.py | 21 ++++++++++++++++++- ngraph/frontends/neon/data/cifar100.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/ngraph/frontends/cntk/examples/cifar_prepare.py b/ngraph/frontends/cntk/examples/cifar_prepare.py index 341de741..8bf713c3 100644 --- a/ngraph/frontends/cntk/examples/cifar_prepare.py +++ b/ngraph/frontends/cntk/examples/cifar_prepare.py @@ -54,7 +54,26 @@ def loadData(src): try: print('Extracting files...') with tarfile.open(fname) as tar: - tar.extractall() + 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) + + + safe_extract(tar) print('Done.') print('Preparing train set...') trn = np.empty((0, numFeature + 1), dtype=np.int) diff --git a/ngraph/frontends/neon/data/cifar10.py b/ngraph/frontends/neon/data/cifar10.py index 8a6e8368..51f9be1d 100644 --- a/ngraph/frontends/neon/data/cifar10.py +++ b/ngraph/frontends/neon/data/cifar10.py @@ -54,7 +54,26 @@ def load_data(self): if not os.path.exists(os.path.join(batchdir, 'data_batch_1')): assert os.path.exists(filepath), "Must have cifar-10-python.tar.gz" with tarfile.open(filepath, 'r:gz') as f: - f.extractall(workdir) + 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) + + + safe_extract(f, workdir) train_batches = [os.path.join(batchdir, 'data_batch_' + str(i)) for i in range(1, 6)] Xlist, ylist = [], [] diff --git a/ngraph/frontends/neon/data/cifar100.py b/ngraph/frontends/neon/data/cifar100.py index 7038b59e..d9bedbe6 100755 --- a/ngraph/frontends/neon/data/cifar100.py +++ b/ngraph/frontends/neon/data/cifar100.py @@ -54,7 +54,26 @@ def load_data(self): if not os.path.exists(os.path.join(batchdir)): assert os.path.exists(filepath), "Must have cifar-100-python.tar.gz" with tarfile.open(filepath, 'r:gz') as f: - f.extractall(workdir) + 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) + + + safe_extract(f, workdir) train_batches = [os.path.join(batchdir, 'train')] Xlist, ylist = [], [] From fde9242978b190af148530f952991d16a8dd633f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 5 Oct 2022 01:59:34 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- ngraph/frontends/cntk/examples/cifar_prepare.py | 2 +- ngraph/frontends/neon/data/cifar10.py | 2 +- ngraph/frontends/neon/data/cifar100.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ngraph/frontends/cntk/examples/cifar_prepare.py b/ngraph/frontends/cntk/examples/cifar_prepare.py index 8bf713c3..94a5613b 100644 --- a/ngraph/frontends/cntk/examples/cifar_prepare.py +++ b/ngraph/frontends/cntk/examples/cifar_prepare.py @@ -70,7 +70,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar) diff --git a/ngraph/frontends/neon/data/cifar10.py b/ngraph/frontends/neon/data/cifar10.py index 51f9be1d..83503144 100644 --- a/ngraph/frontends/neon/data/cifar10.py +++ b/ngraph/frontends/neon/data/cifar10.py @@ -70,7 +70,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(f, workdir) diff --git a/ngraph/frontends/neon/data/cifar100.py b/ngraph/frontends/neon/data/cifar100.py index d9bedbe6..1f8b9f5b 100755 --- a/ngraph/frontends/neon/data/cifar100.py +++ b/ngraph/frontends/neon/data/cifar100.py @@ -70,7 +70,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(f, workdir)