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