Skip to content

Commit a66a729

Browse files
[3.13] Fix symlink escape via tarfile hard link to symlink
(cherry picked from commit 480ea4a) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent a4ab2c1 commit a66a729

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

Lib/tarfile.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
27382738
if os.path.lexists(targetpath):
27392739
# Avoid FileExistsError on following os.link.
27402740
os.unlink(targetpath)
2741-
os.link(tarinfo._link_target, targetpath)
2741+
# Resolve the target so the hard link points to the file
2742+
# itself. Otherwise os.link() may duplicate a symlink to a
2743+
# shallower location, where it's relative target escapes the
2744+
# destination directory. (CVE-2026-82049)
2745+
os.link(os.path.realpath(tarinfo._link_target), targetpath)
27422746
return
27432747
except symlink_exception:
27442748
keyerror_to_extracterror = True

Lib/test/test_tarfile.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,6 +4452,23 @@ def test_sneaky_hardlink_fallback_deep(self):
44524452
self.expect_file("a/b/s", symlink_to=os.path.join('..', 'escape'))
44534453
self.expect_file("s", symlink_to=os.path.join('..', 'escape'))
44544454

4455+
@symlink_test
4456+
def test_sneaky_hardlink_relocation(self):
4457+
with ArchiveMaker() as arc:
4458+
arc.add("a/escape", content="decoy")
4459+
arc.add("a/b/s", symlink_to=os.path.join("..", "escape"))
4460+
arc.add("s", hardlink_to=os.path.join("a", "b", "s"))
4461+
4462+
for filter in 'data', 'tar':
4463+
with self.subTest(filter), self.check_context(arc.open(), filter):
4464+
self.expect_file("a/escape", content="decoy")
4465+
if os_helper.can_symlink():
4466+
self.expect_file("a/b/s", symlink_to=os.path.join('..', 'escape'))
4467+
else:
4468+
self.expect_file("a/b/s", content="decoy")
4469+
self.expect_file("s", content="decoy")
4470+
self.assertFalse((self.destdir / "s").is_symlink())
4471+
44554472
@symlink_test
44564473
def test_exfiltration_via_symlink(self):
44574474
# (CVE-2025-4138)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixed a vulnerability in the :mod:`tarfile` ``data`` and ``tar`` extraction
2+
filters where a crafted archive using a hard link to a symbolic link could
3+
change the permissions and modification time of a file outside the
4+
destination directory, and expose its contents inside the extracted tree.
5+
This addresses :cve:`2026-82049`.

0 commit comments

Comments
 (0)