From 2751beae3e4685a88543c66d54a2e88e4fd23165 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Wed, 28 Oct 2026 02:25:38 +0530 Subject: [PATCH 1/8] donate-cpu: keep extracted package files inside the temp folder --- test/tools/donate_cpu_lib_test.py | 29 +++++++++++++++++++++++++++++ tools/donate_cpu_lib.py | 28 ++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index cc892518b15..d6d76f91660 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -16,7 +16,9 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import io import sys +import tarfile import pytest @@ -126,3 +128,30 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch): "lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \ 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \ 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' + +def _add_tar_member(tf, name, linkname=None): + info = tarfile.TarInfo(name) + data = b'' + if linkname is not None: + info.type = tarfile.SYMTYPE + info.linkname = linkname + else: + data = b'int x;\n' + info.size = len(data) + tf.addfile(info, io.BytesIO(data)) + +def test_unpack_package_outside_temp(tmpdir): + work_path = str(tmpdir) + outside_path = os.path.join(work_path, 'outside') + os.mkdir(outside_path) + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + _add_tar_member(tf, 'pkg/main.c') + _add_tar_member(tf, 'pkg/../../outside/dotdot.c') + _add_tar_member(tf, 'pkg/inc.h', linkname=outside_path) + _add_tar_member(tf, 'pkg/inc.h/symlink.c') + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) + assert os.listdir(outside_path) == [] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index b62a01ff2e3..ee13d452bd1 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -16,7 +16,7 @@ # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic # changes) -CLIENT_VERSION = "1.4.2" +CLIENT_VERSION = "1.4.3" # Timeout for analysis with Cppcheck in seconds CPPCHECK_TIMEOUT = 30 * 60 @@ -345,10 +345,19 @@ def download_package(work_path, package, bandwidth_limit): return destfile +def __is_inside_path(base_path, path): + try: + return os.path.commonpath([base_path, path]) == base_path + except ValueError: + # paths are on different drives + return False + + def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None): temp_path = os.path.join(work_path, 'temp') __remove_tree(temp_path) os.mkdir(temp_path) + real_temp_path = os.path.realpath(temp_path) header_endings = ('.hpp', '.h++', '.hxx', '.hh', '.h') cpp_source_endings = ('.cpp', '.c++', '.cxx', '.cc', '.tpp', '.txx', '.ipp', '.ixx', '.qml') @@ -368,10 +377,19 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None total = 0 extracted = 0 skipped = 0 + # Python versions with extraction filters reject members that leave temp_path as well + extract_args = {'filter': 'data'} if hasattr(tarfile, 'data_filter') else {} for member in tf: total += 1 - if member.name.startswith(('/', '..')): - # Skip dangerous file names + member_path = os.path.realpath(os.path.join(temp_path, member.name)) + link_path = None + if member.issym(): + link_path = os.path.realpath(os.path.join(os.path.dirname(member_path), member.linkname)) + elif member.islnk(): + link_path = os.path.realpath(os.path.join(temp_path, member.linkname)) + if not __is_inside_path(real_temp_path, member_path) or \ + (link_path is not None and not __is_inside_path(real_temp_path, link_path)): + # Skip dangerous file names and links pointing outside of temp_path # print('skipping dangerous file: ' + member.name) skipped += 1 continue @@ -389,7 +407,7 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None skipped += 1 continue try: - tf.extract(member.name, temp_path) + tf.extract(member.name, temp_path, **extract_args) if is_source: source_found = True extracted += 1 @@ -397,6 +415,8 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None pass except AttributeError: pass + except tarfile.TarError: + pass print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else ''))) return temp_path, source_found From 6d6d7d62470b4e0d5dd19a191ffab79090a41873 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Wed, 18 Nov 2026 23:39:13 +0530 Subject: [PATCH 2/8] donate-cpu: extract the checked tar member instead of looking it up by name --- test/tools/donate_cpu_lib_test.py | 28 ++++++++++++++++++++++++++-- tools/donate_cpu_lib.py | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index d6d76f91660..9259e1fbb19 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -129,11 +129,11 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch): 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \ 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' -def _add_tar_member(tf, name, linkname=None): +def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE): info = tarfile.TarInfo(name) data = b'' if linkname is not None: - info.type = tarfile.SYMTYPE + info.type = linktype info.linkname = linkname else: data = b'int x;\n' @@ -155,3 +155,27 @@ def test_unpack_package_outside_temp(tmpdir): assert source_found assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) assert os.listdir(outside_path) == [] + +def test_unpack_package_duplicate_name(tmpdir): + work_path = str(tmpdir) + outside_path = os.path.join(work_path, 'outside') + os.mkdir(outside_path) + outside_file = os.path.join(outside_path, 'target.c') + with open(outside_file, 'w', encoding='utf-8') as f: + f.write('int y;\n') + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + # a harmless member followed by a link with the same name that points outside temp + _add_tar_member(tf, 'pkg/sym.c') + _add_tar_member(tf, 'pkg/sym.c', linkname=outside_file) + _add_tar_member(tf, 'pkg/hard.c') + _add_tar_member(tf, 'pkg/hard.c', linkname='../outside/target.c', linktype=tarfile.LNKTYPE) + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + for name in ('sym.c', 'hard.c'): + path = os.path.join(temp_path, 'pkg', name) + assert os.path.isfile(path) + assert not os.path.islink(path) + assert not os.path.samefile(path, outside_file) + assert os.listdir(outside_path) == ['target.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index ee13d452bd1..33380553a73 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -407,7 +407,7 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None skipped += 1 continue try: - tf.extract(member.name, temp_path, **extract_args) + tf.extract(member, temp_path, **extract_args) if is_source: source_found = True extracted += 1 From 1ea1577ab1298c03979938f68b6bc21127624f47 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Fri, 20 Nov 2026 20:35:34 +0530 Subject: [PATCH 3/8] donate-cpu: resolve symlink targets from the unresolved link location --- test/tools/donate_cpu_lib_test.py | 6 ++++++ tools/donate_cpu_lib.py | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 9259e1fbb19..642e61688fb 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -170,6 +170,9 @@ def test_unpack_package_duplicate_name(tmpdir): _add_tar_member(tf, 'pkg/sym.c', linkname=outside_file) _add_tar_member(tf, 'pkg/hard.c') _add_tar_member(tf, 'pkg/hard.c', linkname='../outside/target.c', linktype=tarfile.LNKTYPE) + # a link inside temp followed by a same-named link that only looks inside when resolved through the first one + _add_tar_member(tf, 'pkg/inc.h', linkname='a/b/c') + _add_tar_member(tf, 'pkg/inc.h', linkname='../../outside') temp_path, source_found = unpack_package(work_path, tgz) assert source_found @@ -178,4 +181,7 @@ def test_unpack_package_duplicate_name(tmpdir): assert os.path.isfile(path) assert not os.path.islink(path) assert not os.path.samefile(path, outside_file) + real_temp_path = os.path.realpath(temp_path) + inc_path = os.path.realpath(os.path.join(temp_path, 'pkg', 'inc.h')) + assert os.path.commonpath([real_temp_path, inc_path]) == real_temp_path assert os.listdir(outside_path) == ['target.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 33380553a73..5de0c556991 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -384,7 +384,9 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None member_path = os.path.realpath(os.path.join(temp_path, member.name)) link_path = None if member.issym(): - link_path = os.path.realpath(os.path.join(os.path.dirname(member_path), member.linkname)) + # resolve from the directory the link is created in, not through an existing link with the same name + link_parent = os.path.realpath(os.path.dirname(os.path.join(temp_path, member.name))) + link_path = os.path.realpath(os.path.join(link_parent, member.linkname)) elif member.islnk(): link_path = os.path.realpath(os.path.join(temp_path, member.linkname)) if not __is_inside_path(real_temp_path, member_path) or \ From 2f91952c65d1340d714898ab6079009509037e03 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Sat, 21 Nov 2026 23:34:19 +0530 Subject: [PATCH 4/8] donate-cpu: drop extracted links that end up pointing outside the temp folder --- test/tools/donate_cpu_lib_test.py | 28 ++++++++++++++++++++++++++++ tools/donate_cpu_lib.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 642e61688fb..df8ec324808 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -185,3 +185,31 @@ def test_unpack_package_duplicate_name(tmpdir): inc_path = os.path.realpath(os.path.join(temp_path, 'pkg', 'inc.h')) assert os.path.commonpath([real_temp_path, inc_path]) == real_temp_path assert os.listdir(outside_path) == ['target.c'] + +def test_unpack_package_link_redirected_later(tmpdir): + work_path = str(tmpdir) + outside_path = os.path.join(work_path, 'outside') + os.mkdir(outside_path) + with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f: + f.write('int secret;\n') + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + _add_tar_member(tf, 'pkg/src/main.c') + # 'd.h' does not exist yet, so this target is resolved as temp/outside and looks harmless + _add_tar_member(tf, 'pkg/l.h', linkname='d.h/../../outside') + # turns the missing component into a link, which redirects 'l.h' out of temp + _add_tar_member(tf, 'pkg/d.h', linkname='.') + # a link that stays inside the package is still extracted + _add_tar_member(tf, 'pkg/include/main.c', linkname='../src/main.c') + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + real_temp_path = os.path.realpath(temp_path) + for name in ('l.h', 'd.h'): + path = os.path.join(temp_path, 'pkg', name) + if os.path.lexists(path): + assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path + inc_path = os.path.join(temp_path, 'pkg', 'include', 'main.c') + if os.path.islink(inc_path): + assert os.path.samefile(inc_path, os.path.join(temp_path, 'pkg', 'src', 'main.c')) + assert os.listdir(outside_path) == ['secret.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 5de0c556991..1345e79a393 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -353,6 +353,33 @@ def __is_inside_path(base_path, path): return False +def __remove_escaping_links(real_temp_path): + # A link that still pointed inside real_temp_path when it was extracted can be redirected + # by a link that is extracted later, so all links are checked again afterwards. Removing a + # link changes what the remaining ones resolve to, so this is repeated until nothing is left. + removed = 0 + while True: + links = [] + for root, dirs, files in os.walk(real_temp_path): + for name in dirs + files: + path = os.path.join(root, name) + if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)): + links.append(path) + if not links: + return removed + for path in links: + try: + os.remove(path) + removed += 1 + except OSError: + try: + # a directory symlink on Windows + os.rmdir(path) + removed += 1 + except OSError: + return removed + + def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None): temp_path = os.path.join(work_path, 'temp') __remove_tree(temp_path) @@ -419,6 +446,9 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None pass except tarfile.TarError: pass + removed = __remove_escaping_links(real_temp_path) + extracted -= removed + skipped += removed print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else ''))) return temp_path, source_found From f5cfa233c5d9a7aa16b9c12bf185de25506fd542 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Mon, 23 Nov 2026 12:29:58 +0530 Subject: [PATCH 5/8] donate-cpu: do not abort unpacking on a hardlink to a missing member --- test/tools/donate_cpu_lib_test.py | 13 +++++++++++++ tools/donate_cpu_lib.py | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index df8ec324808..f4362032eb1 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -213,3 +213,16 @@ def test_unpack_package_link_redirected_later(tmpdir): if os.path.islink(inc_path): assert os.path.samefile(inc_path, os.path.join(temp_path, 'pkg', 'src', 'main.c')) assert os.listdir(outside_path) == ['secret.c'] + +def test_unpack_package_hardlink_missing_target(tmpdir): + work_path = str(tmpdir) + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + _add_tar_member(tf, 'pkg/main.c') + # the target is neither on disk nor in the archive, so tarfile raises KeyError instead of OSError + _add_tar_member(tf, 'pkg/copy.c', linkname='pkg/missing.c', linktype=tarfile.LNKTYPE) + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) + assert not os.path.lexists(os.path.join(temp_path, 'pkg', 'copy.c')) diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 1345e79a393..f0ac459acb1 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -444,7 +444,8 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None pass except AttributeError: pass - except tarfile.TarError: + except (tarfile.TarError, KeyError): + # KeyError: hardlink to a member that is neither on disk nor in the archive pass removed = __remove_escaping_links(real_temp_path) extracted -= removed From 60f785815e73cef6efa6d7da6bb986c08493f3ab Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Mon, 23 Nov 2026 19:22:03 +0530 Subject: [PATCH 6/8] donate-cpu: remove escaping links from read-only folders and fail the unpack otherwise --- test/tools/donate_cpu_lib_test.py | 32 +++++++++++++++++++++++++++++-- tools/donate_cpu_lib.py | 31 +++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index f4362032eb1..55137e79ed0 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -129,10 +129,13 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch): 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \ 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' -def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE): +def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE, dirmode=None): info = tarfile.TarInfo(name) data = b'' - if linkname is not None: + if dirmode is not None: + info.type = tarfile.DIRTYPE + info.mode = dirmode + elif linkname is not None: info.type = linktype info.linkname = linkname else: @@ -226,3 +229,28 @@ def test_unpack_package_hardlink_missing_target(tmpdir): assert source_found assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) assert not os.path.lexists(os.path.join(temp_path, 'pkg', 'copy.c')) + +def test_unpack_package_link_in_readonly_dir(tmpdir): + work_path = str(tmpdir) + outside_path = os.path.join(work_path, 'outside') + os.mkdir(outside_path) + with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f: + f.write('int secret;\n') + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + _add_tar_member(tf, 'pkg/main.c') + # redirected out of temp by the next member, like in test_unpack_package_link_redirected_later + _add_tar_member(tf, 'pkg/x.h/l.h', linkname='d.h/../../../outside') + _add_tar_member(tf, 'pkg/x.h/d.h', linkname='.') + # a directory member with a header-like name makes the folder read-only after the links exist + _add_tar_member(tf, 'pkg/x.h', dirmode=0o555) + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) + real_temp_path = os.path.realpath(temp_path) + for name in ('l.h', 'd.h'): + path = os.path.join(temp_path, 'pkg', 'x.h', name) + if os.path.lexists(path): + assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path + assert os.listdir(outside_path) == ['secret.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index f0ac459acb1..5e7961f450a 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -353,10 +353,20 @@ def __is_inside_path(base_path, path): return False +def __remove_link(path): + try: + os.remove(path) + except OSError: + # a directory symlink on Windows + os.rmdir(path) + + def __remove_escaping_links(real_temp_path): # A link that still pointed inside real_temp_path when it was extracted can be redirected # by a link that is extracted later, so all links are checked again afterwards. Removing a # link changes what the remaining ones resolve to, so this is repeated until nothing is left. + # Raises OSError if a link cannot be removed. + import stat removed = 0 while True: links = [] @@ -369,15 +379,13 @@ def __remove_escaping_links(real_temp_path): return removed for path in links: try: - os.remove(path) - removed += 1 + __remove_link(path) except OSError: - try: - # a directory symlink on Windows - os.rmdir(path) - removed += 1 - except OSError: - return removed + # a directory member extracted after the link can have made its folder read-only + parent = os.path.dirname(path) + os.chmod(parent, stat.S_IMODE(os.lstat(parent).st_mode) | stat.S_IWUSR) + __remove_link(path) + removed += 1 def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None): @@ -447,7 +455,12 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None except (tarfile.TarError, KeyError): # KeyError: hardlink to a member that is neither on disk nor in the archive pass - removed = __remove_escaping_links(real_temp_path) + try: + removed = __remove_escaping_links(real_temp_path) + except OSError as e: + # do not analyse a package with a link pointing outside of temp_path + print('Failed to remove link pointing outside of {}: {}'.format(temp_path, e)) + return temp_path, False extracted -= removed skipped += removed print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else ''))) From 9b6cc75d1f982e950da43ecffa60a39e5f21eb7a Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Thu, 26 Nov 2026 13:26:54 +0530 Subject: [PATCH 7/8] donate-cpu: do not let an unlistable folder hide an escaping link --- test/tools/donate_cpu_lib_test.py | 25 +++++++++++++++++++++ tools/donate_cpu_lib.py | 37 +++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 55137e79ed0..c8bab199261 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -254,3 +254,28 @@ def test_unpack_package_link_in_readonly_dir(tmpdir): if os.path.lexists(path): assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path assert os.listdir(outside_path) == ['secret.c'] + +def test_unpack_package_link_in_unreadable_dir(tmpdir): + work_path = str(tmpdir) + outside_path = os.path.join(work_path, 'outside') + os.mkdir(outside_path) + with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f: + f.write('int secret;\n') + tgz = os.path.join(work_path, 'temp.tgz') + with tarfile.open(tgz, 'w:gz') as tf: + _add_tar_member(tf, 'pkg/main.c') + # redirected out of temp by the next member, like in test_unpack_package_link_redirected_later + _add_tar_member(tf, 'pkg/q.h/l.h', linkname='d.h/../../../outside') + _add_tar_member(tf, 'pkg/q.h/d.h', linkname='.') + # a directory member with a header-like name makes the folder unlistable after the links exist + _add_tar_member(tf, 'pkg/q.h', dirmode=0o311) + + temp_path, source_found = unpack_package(work_path, tgz) + assert source_found + assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) + real_temp_path = os.path.realpath(temp_path) + for name in ('l.h', 'd.h'): + path = os.path.join(temp_path, 'pkg', 'q.h', name) + if os.path.lexists(path): + assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path + assert os.listdir(outside_path) == ['secret.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 5e7961f450a..680675fbd54 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -361,20 +361,43 @@ def __remove_link(path): os.rmdir(path) +def __find_escaping_links(real_temp_path): + # Returns the links below real_temp_path that resolve outside of it, and whether a folder had to + # be made readable to list it. Raises OSError for a folder that cannot be listed even then. + import stat + fixed = False + + def onerror(e): + # a directory member extracted after the links can have made its folder unreadable + nonlocal fixed + mode = stat.S_IMODE(os.lstat(e.filename).st_mode) + missing = (stat.S_IRUSR | stat.S_IXUSR) & ~mode + if not missing: + raise e + os.chmod(e.filename, mode | missing) + fixed = True + + links = [] + for root, dirs, files in os.walk(real_temp_path, onerror=onerror): + for name in dirs + files: + path = os.path.join(root, name) + if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)): + links.append(path) + return links, fixed + + def __remove_escaping_links(real_temp_path): # A link that still pointed inside real_temp_path when it was extracted can be redirected # by a link that is extracted later, so all links are checked again afterwards. Removing a # link changes what the remaining ones resolve to, so this is repeated until nothing is left. - # Raises OSError if a link cannot be removed. + # Raises OSError if a folder cannot be listed or a link cannot be removed. import stat removed = 0 while True: - links = [] - for root, dirs, files in os.walk(real_temp_path): - for name in dirs + files: - path = os.path.join(root, name) - if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)): - links.append(path) + links, fixed = __find_escaping_links(real_temp_path) + if fixed: + # a folder that was skipped by the walk is readable now, so look at its content as well + continue if not links: return removed for path in links: From 69d5fb61b5e576c613a90ac15c39558e0d23bfc2 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Sat, 5 Dec 2026 14:20:12 +0530 Subject: [PATCH 8/8] donate-cpu: also restore the search bit on folders that hide escaping links --- test/tools/donate_cpu_lib_test.py | 14 ++++++++++---- tools/donate_cpu_lib.py | 25 ++++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index c8bab199261..df834ef3a07 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -255,7 +255,9 @@ def test_unpack_package_link_in_readonly_dir(tmpdir): assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path assert os.listdir(outside_path) == ['secret.c'] -def test_unpack_package_link_in_unreadable_dir(tmpdir): +# 0o311 cannot be listed, 0o644 can be listed but its entries cannot be told apart from links +@pytest.mark.parametrize('dirmode', [0o311, 0o644]) +def test_unpack_package_link_in_inaccessible_dir(tmpdir, dirmode): work_path = str(tmpdir) outside_path = os.path.join(work_path, 'outside') os.mkdir(outside_path) @@ -267,15 +269,19 @@ def test_unpack_package_link_in_unreadable_dir(tmpdir): # redirected out of temp by the next member, like in test_unpack_package_link_redirected_later _add_tar_member(tf, 'pkg/q.h/l.h', linkname='d.h/../../../outside') _add_tar_member(tf, 'pkg/q.h/d.h', linkname='.') - # a directory member with a header-like name makes the folder unlistable after the links exist - _add_tar_member(tf, 'pkg/q.h', dirmode=0o311) + # a directory member with a header-like name takes the read or search bit off the folder + # that holds the links, after they have been extracted + _add_tar_member(tf, 'pkg/q.h', dirmode=dirmode) temp_path, source_found = unpack_package(work_path, tgz) assert source_found assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c')) real_temp_path = os.path.realpath(temp_path) + # the folder has to be accessible to see whether an escaping link was left in it + dir_path = os.path.join(temp_path, 'pkg', 'q.h') + os.chmod(dir_path, 0o755) for name in ('l.h', 'd.h'): - path = os.path.join(temp_path, 'pkg', 'q.h', name) + path = os.path.join(dir_path, name) if os.path.lexists(path): assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path assert os.listdir(outside_path) == ['secret.c'] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 680675fbd54..bf4a0400b03 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -363,22 +363,33 @@ def __remove_link(path): def __find_escaping_links(real_temp_path): # Returns the links below real_temp_path that resolve outside of it, and whether a folder had to - # be made readable to list it. Raises OSError for a folder that cannot be listed even then. + # be made accessible to look at its content. Raises OSError for a folder that is inaccessible + # even then. import stat fixed = False - def onerror(e): - # a directory member extracted after the links can have made its folder unreadable + def make_accessible(path): + # A directory member extracted after the links can have taken the owner read or search bit + # off its folder. Without the read bit it cannot be listed, and without the search bit its + # entries cannot be told apart from links. nonlocal fixed - mode = stat.S_IMODE(os.lstat(e.filename).st_mode) + mode = stat.S_IMODE(os.lstat(path).st_mode) missing = (stat.S_IRUSR | stat.S_IXUSR) & ~mode if not missing: - raise e - os.chmod(e.filename, mode | missing) + return False + os.chmod(path, mode | missing) fixed = True + return True + + def onerror(e): + if not make_accessible(e.filename): + raise e links = [] for root, dirs, files in os.walk(real_temp_path, onerror=onerror): + if make_accessible(root): + # the entries were listed without the search bit needed to stat them, so start over + break for name in dirs + files: path = os.path.join(root, name) if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)): @@ -396,7 +407,7 @@ def __remove_escaping_links(real_temp_path): while True: links, fixed = __find_escaping_links(real_temp_path) if fixed: - # a folder that was skipped by the walk is readable now, so look at its content as well + # a folder whose content the walk could not see is accessible now, so look again continue if not links: return removed