From 1fad2f41e7b882087037b38b6f0b7db433812ac8 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 7 Oct 2022 19:42:07 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../python3/lib/python3.8/tarfile.py | 21 ++++++++++++++++++- .../python3/lib/python3.8/tarfile.py | 21 ++++++++++++++++++- .../python3/lib/python3.8/tarfile.py | 21 ++++++++++++++++++- .../python3/lib/python3.9/tarfile.py | 21 ++++++++++++++++++- .../python3/lib/python3.9/tarfile.py | 21 ++++++++++++++++++- .../build-tools/common/py3-stdlib/tarfile.py | 21 ++++++++++++++++++- 6 files changed, 120 insertions(+), 6 deletions(-) diff --git a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-bootstrap/python3/lib/python3.8/tarfile.py b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-bootstrap/python3/lib/python3.8/tarfile.py index 7a69e1b1a..3c7d066aa 100755 --- a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-bootstrap/python3/lib/python3.8/tarfile.py +++ b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-bootstrap/python3/lib/python3.8/tarfile.py @@ -2522,7 +2522,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r383902/python3/lib/python3.8/tarfile.py b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r383902/python3/lib/python3.8/tarfile.py index d31b9cbb5..d0b0eaed7 100755 --- a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r383902/python3/lib/python3.8/tarfile.py +++ b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r383902/python3/lib/python3.8/tarfile.py @@ -2520,7 +2520,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r399163b/python3/lib/python3.8/tarfile.py b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r399163b/python3/lib/python3.8/tarfile.py index d31b9cbb5..d0b0eaed7 100755 --- a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r399163b/python3/lib/python3.8/tarfile.py +++ b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r399163b/python3/lib/python3.8/tarfile.py @@ -2520,7 +2520,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183b/python3/lib/python3.9/tarfile.py b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183b/python3/lib/python3.9/tarfile.py index 1d1561261..98dd54b0e 100755 --- a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183b/python3/lib/python3.9/tarfile.py +++ b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183b/python3/lib/python3.9/tarfile.py @@ -2541,7 +2541,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183c/python3/lib/python3.9/tarfile.py b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183c/python3/lib/python3.9/tarfile.py index 1d1561261..98dd54b0e 100755 --- a/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183c/python3/lib/python3.9/tarfile.py +++ b/kernel_platform/prebuilts-master/clang/host/linux-x86/clang-r416183c/python3/lib/python3.9/tarfile.py @@ -2541,7 +2541,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git a/kernel_platform/prebuilts/build-tools/common/py3-stdlib/tarfile.py b/kernel_platform/prebuilts/build-tools/common/py3-stdlib/tarfile.py index 1d1561261..98dd54b0e 100755 --- a/kernel_platform/prebuilts/build-tools/common/py3-stdlib/tarfile.py +++ b/kernel_platform/prebuilts/build-tools/common/py3-stdlib/tarfile.py @@ -2541,7 +2541,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + 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(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src)