From 792f6a96e45b4d9a86abe449aa56e5ed5dae07f0 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 3 Nov 2022 19:18:45 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- vendor/lowrisc_ip/util/dvsim/LsfLauncher.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/vendor/lowrisc_ip/util/dvsim/LsfLauncher.py b/vendor/lowrisc_ip/util/dvsim/LsfLauncher.py index 4c623433d3..a0e3b1d200 100644 --- a/vendor/lowrisc_ip/util/dvsim/LsfLauncher.py +++ b/vendor/lowrisc_ip/util/dvsim/LsfLauncher.py @@ -54,7 +54,26 @@ def prepare_workspace(project, repo_top, args): log.info("[prepare_workspace]: [pyvenv]: Extracting %s", Launcher.pyvenv) with tarfile.open(Launcher.pyvenv, mode='r') as tar: - tar.extractall(args.scratch_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, args.scratch_root) log.info("[prepare_workspace]: [pyvenv]: Done: %s", path) Launcher.pyvenv = path