From 92914c6da0129ca377f3a1e80bac43fc37c19e31 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 24 Nov 2022 03:41:28 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- src/python/WMCore/WMRuntime/Unpacker.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/python/WMCore/WMRuntime/Unpacker.py b/src/python/WMCore/WMRuntime/Unpacker.py index 731dd71d16..d3ab5e916d 100644 --- a/src/python/WMCore/WMRuntime/Unpacker.py +++ b/src/python/WMCore/WMRuntime/Unpacker.py @@ -78,7 +78,26 @@ def createWorkArea(sandbox): os.makedirs(os.path.join(jobDir, 'StartupScript')) with tarfile.open(sandbox, "r") as tfile: - tfile.extractall(jobDir) + 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(tfile, jobDir) # need to pull out the startup file from the zipball with zipfile.ZipFile(os.path.join(jobDir, 'WMCore.zip'), 'r') as zfile: