From fce79a74f14810c4bbc33125c12a6ef2ba81470e Mon Sep 17 00:00:00 2001 From: Titouan Christophe Date: Mon, 8 Jun 2026 23:20:49 +0200 Subject: [PATCH] package/python3: add patch for CVE-2026-7774 This fixes the following vulnerability: - CVE-2026-7774: tarfile.data_filter could be bypassed using crafted link entries, including symlinks with empty or directory-like names, to redirect later archive members outside the intended extraction directory. This allowed a malicious tar archive to cause tarfile.extractall() to write files outside the destination directory, subject to the permissions of the extracting process. https://www.cve.org/CVERecord?id=CVE-2026-7774 Signed-off-by: Titouan Christophe Signed-off-by: Thomas Perale --- ...le-data_filter-validate-written-link.patch | 178 ++++++++++++++++++ package/python3/python3.mk | 2 + 2 files changed, 180 insertions(+) create mode 100644 package/python3/0014-tarfile-data_filter-validate-written-link.patch diff --git a/package/python3/0014-tarfile-data_filter-validate-written-link.patch b/package/python3/0014-tarfile-data_filter-validate-written-link.patch new file mode 100644 index 0000000000..b6337e9cbd --- /dev/null +++ b/package/python3/0014-tarfile-data_filter-validate-written-link.patch @@ -0,0 +1,178 @@ +From 6df9892a802c124c4223e17c4a05ea7054166ac7 Mon Sep 17 00:00:00 2001 +From: Petr Viktorin +Date: Fri, 8 May 2026 14:16:06 +0200 +Subject: [PATCH] [3.12] gh-149486: tarfile.data_filter: validate written + link target (GH-149487) + +The data filter rewrote linknames with normpath() but ran the +containment check against the un-normalised value, and computed a +symlink's directory before stripping trailing slashes. Both let a +crafted archive create links pointing outside the destination. Also +reject link members that resolve to the destination directory itself, +which could otherwise replace it with a symlink and redirect all +subsequent members. + +(Patch by Greg; Petr's just reviewing & merging.) +(cherry picked from commit 578411982c16f753f4893532510099ef665117da) + +Co-authored-by: Petr Viktorin +Co-authored-by: Gregory P. Smith + +Upstream: https://github.com/python/cpython/pull/149556 +CVE: CVE-2026-7774 +Signed-off-by: Titouan Christophe +--- + Lib/tarfile.py | 16 ++-- + Lib/test/test_tarfile.py | 87 ++++++++++++++++++- + ...-05-03-21-00-00.gh-issue-149486.tarflt.rst | 5 ++ + 3 files changed, 99 insertions(+), 9 deletions(-) + create mode 100644 Misc/NEWS.d/next/Security/2026-05-03-21-00-00.gh-issue-149486.tarflt.rst + +diff --git a/Lib/tarfile.py b/Lib/tarfile.py +index 59d3f6e5cce1650..fcb1040f2aefe29 100755 +--- a/Lib/tarfile.py ++++ b/Lib/tarfile.py +@@ -816,16 +816,22 @@ def _get_filtered_attrs(member, dest_path, for_data=True): + if member.islnk() or member.issym(): + if os.path.isabs(member.linkname): + raise AbsoluteLinkError(member) ++ # A link member that resolves to the destination directory itself ++ # would replace it with a (sym)link, redirecting the destination ++ # for all subsequent members. ++ if target_path == dest_path: ++ raise OutsideDestinationError(member, target_path) + normalized = os.path.normpath(member.linkname) + if normalized != member.linkname: + new_attrs['linkname'] = normalized + if member.issym(): +- target_path = os.path.join(dest_path, +- os.path.dirname(name), +- member.linkname) ++ # The symlink is created at `name` with trailing separators ++ # stripped, so its target is relative to the directory ++ # containing that path. ++ link_dir = os.path.dirname(name.rstrip('/' + os.sep)) ++ target_path = os.path.join(dest_path, link_dir, normalized) + else: +- target_path = os.path.join(dest_path, +- member.linkname) ++ target_path = os.path.join(dest_path, normalized) + target_path = os.path.realpath(target_path, + strict=os.path.ALLOW_MISSING) + if os.path.commonpath([target_path, dest_path]) != dest_path: +diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py +index 759fa03ead70b04..fefae4b64c6182c 100644 +--- a/Lib/test/test_tarfile.py ++++ b/Lib/test/test_tarfile.py +@@ -3701,10 +3701,19 @@ def test_parent_symlink(self): + + "which is outside the destination") + + with self.check_context(arc.open(), 'data'): +- self.expect_exception( +- tarfile.LinkOutsideDestinationError, +- """'parent' would link to ['"].*outerdir['"], """ +- + "which is outside the destination") ++ if self.dotdot_resolves_early: ++ # 'current/../..' normalises to '..', which is rejected. ++ self.expect_exception( ++ tarfile.LinkOutsideDestinationError, ++ """'parent' would link to ['"].*outerdir['"], """ ++ + "which is outside the destination") ++ else: ++ # 'current/..' normalises to '.'; the rewritten link is ++ # created and 'parent/evil' lands harmlessly inside the ++ # destination. ++ self.expect_file('current', symlink_to='.') ++ self.expect_file('parent', symlink_to='.') ++ self.expect_file('evil') + + else: + # No symlink support. The symlinks are ignored. +@@ -3978,6 +3987,76 @@ def test_sly_relative2(self): + + """['"].*moo['"], which is outside the """ + + "destination") + ++ @symlink_test ++ @os_helper.skip_unless_symlink ++ def test_normpath_realpath_mismatch(self): ++ # The link-target check must validate the value that will actually ++ # be written to disk (the normalised linkname), not the original. ++ # Here 'a' is a symlink to a deep nonexistent path, so realpath() ++ # of 'a/../../...' stays inside the destination while normpath() ++ # collapses 'a/..' lexically and escapes. ++ depth = len(self.destdir.parts) + 5 ++ deep = '/'.join(f'p{i}' for i in range(depth)) ++ sneaky = 'a/' + '../' * depth + 'flag' ++ for kind in 'symlink_to', 'hardlink_to': ++ with self.subTest(kind): ++ with ArchiveMaker() as arc: ++ arc.add('a', symlink_to=deep) ++ arc.add('escape', **{kind: sneaky}) ++ with self.check_context(arc.open(), 'data'): ++ self.expect_exception( ++ tarfile.LinkOutsideDestinationError) ++ ++ @symlink_test ++ @os_helper.skip_unless_symlink ++ def test_symlink_trailing_slash(self): ++ # A trailing slash on a symlink member's name must not cause the ++ # link target to be resolved relative to the wrong directory. ++ with ArchiveMaker() as arc: ++ t = tarfile.TarInfo('x/') ++ t.type = tarfile.SYMTYPE ++ t.linkname = '..' ++ arc.tar_w.addfile(t) ++ arc.add('x/escaped', content='hi') ++ ++ with self.check_context(arc.open(), 'data'): ++ self.expect_exception(tarfile.LinkOutsideDestinationError) ++ ++ @symlink_test ++ @os_helper.skip_unless_symlink ++ def test_link_at_destination(self): ++ # A link member whose name resolves to the destination directory ++ # itself must be rejected: otherwise the destination is replaced ++ # by a symlink and later members can be redirected through it. ++ for name in '', '.', './': ++ with ArchiveMaker() as arc: ++ t = tarfile.TarInfo(name) ++ t.type = tarfile.SYMTYPE ++ t.linkname = '.' ++ arc.tar_w.addfile(t) ++ ++ with self.check_context(arc.open(), 'data'): ++ self.expect_exception(tarfile.OutsideDestinationError) ++ ++ @symlink_test ++ @os_helper.skip_unless_symlink ++ def test_empty_name_symlink_chain(self): ++ # Regression test for a chain of empty-named symlinks that ++ # incrementally redirects the destination outwards. ++ with ArchiveMaker() as arc: ++ for name, target in [('', ''), ('a/', '..'), ++ ('', 'dummy'), ('', 'a'), ++ ('b/', '..'), ++ ('', 'dummy'), ('', 'a/b')]: ++ t = tarfile.TarInfo(name) ++ t.type = tarfile.SYMTYPE ++ t.linkname = target ++ arc.tar_w.addfile(t) ++ arc.add('escaped', content='hi') ++ ++ with self.check_context(arc.open(), 'data'): ++ self.expect_exception(tarfile.FilterError) ++ + @symlink_test + def test_deep_symlink(self): + # Test that symlinks and hardlinks inside a directory +diff --git a/Misc/NEWS.d/next/Security/2026-05-03-21-00-00.gh-issue-149486.tarflt.rst b/Misc/NEWS.d/next/Security/2026-05-03-21-00-00.gh-issue-149486.tarflt.rst +new file mode 100644 +index 000000000000000..7c69edb683cf80a +--- /dev/null ++++ b/Misc/NEWS.d/next/Security/2026-05-03-21-00-00.gh-issue-149486.tarflt.rst +@@ -0,0 +1,5 @@ ++:func:`tarfile.data_filter` now validates link targets using the same ++normalised value that is written to disk, strips trailing separators from ++the member name when resolving a symlink's directory, and rejects link ++members that would replace the destination directory itself. This closes ++several path-traversal bypasses of the ``data`` extraction filter. diff --git a/package/python3/python3.mk b/package/python3/python3.mk index 0881625ea3..96be4bd651 100644 --- a/package/python3/python3.mk +++ b/package/python3/python3.mk @@ -15,6 +15,8 @@ PYTHON3_CPE_ID_PRODUCT = python # 0013-Fix-O-n-2-canonical-ordering-in-unicodedata-normalize.patch PYTHON3_IGNORE_CVES += CVE-2026-3276 +# 0014-tarfile-data_filter-validate-written-link.patch +PYTHON3_IGNORE_CVES += CVE-2026-7774 # This host Python is installed in $(HOST_DIR), as it is needed when # cross-compiling third-party Python modules.