FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-157190: Follow symlinks when extracting `tarfile` hard links by StanFromIreland · Pull Request #157191 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion Lib/tarfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
if os.path.lexists(targetpath):
# Avoid FileExistsError on following os.link.
os.unlink(targetpath)
os.link(tarinfo._link_target, targetpath)
# Resolve the target so the hard link points to the file
# itself. Otherwise os.link() may duplicate a symlink to a
# shallower location, where its relative target escapes the
# destination directory. (CVE-2026-82049)
os.link(os.path.realpath(tarinfo._link_target), targetpath)
return
except symlink_exception:
keyerror_to_extracterror = True
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_tarfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4642,6 +4642,24 @@ def test_sneaky_hardlink_fallback_deep(self):
self.expect_file("a/b/s", symlink_to=os.path.join('..', 'escape'))
self.expect_file("s", symlink_to=os.path.join('..', 'escape'))

@symlink_test
@os_helper.skip_unless_hardlink
def test_sneaky_hardlink_relocation(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do we want to test also a symlink pointing to a directory?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm not sure what that would be testing, a hard link to a directory isn't possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I meant a hard link to the symlink, where the symlink points to a directory.

Something like that:

2026-09-09T17:26:21.505759000+0200 maurycy@gimel /tmp/tarararara  % tar tvf evil.tar
drw-r--r--  0 0      0           0 Jan  1  1970 a/d/
-rw-r--r--  0 0      0           5 Jan  1  1970 a/d/f
lrw-r--r--  0 0      0           0 Jan  1  1970 a/b/s -> ../d
hrw-r--r--  0 0      0           0 Jan  1  1970 s link to a/b/s

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

os.link() raises on a directory, so it falls through to the fallback branch, which is the path test_sneaky_hardlink_fallback_deep covers.

with ArchiveMaker() as arc:
arc.add("a/escape", content="decoy")
arc.add("a/b/s", symlink_to=os.path.join("..", "escape"))
arc.add("s", hardlink_to=os.path.join("a", "b", "s"))

for filter in 'data', 'tar':
with self.subTest(filter), self.check_context(arc.open(), filter):
self.expect_file("a/escape", content="decoy")
if os_helper.can_symlink():
self.expect_file("a/b/s", symlink_to=os.path.join('..', 'escape'))
else:
self.expect_file("a/b/s", content="decoy")
self.expect_file("s", content="decoy")
self.assertFalse((self.destdir / "s").is_symlink())

@symlink_test
def test_exfiltration_via_symlink(self):
# (CVE-2025-4138)
Expand Down
Loading

Back | FazBrowse Home | New Git URL