From 163319d7285dcbd7b9071eb7d8503a0c66ad1396 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 9 Oct 2022 12:14:34 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- lib/binaryaudit/poky.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/binaryaudit/poky.py b/lib/binaryaudit/poky.py index 01c1d7e..e9bcd67 100644 --- a/lib/binaryaudit/poky.py +++ b/lib/binaryaudit/poky.py @@ -37,7 +37,26 @@ def retrieve_baseline(db_conn, prod_id): os.makedirs(extractdir) with tarfile.open(data_fl.name, "r:gz") as tgz: - tgz.extractall(extractdir) + 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(tgz, extractdir) tgz.close() os.unlink(data_fl.name) # Depends on how we pack, but the first sibling named "buildhistory" should be it.