Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,13 @@ def _extract_member(self, tarinfo, targetpath, set_attrs=True,
else:
self.makefile(tarinfo, targetpath)

if tarinfo.islnk() and os.path.islink(targetpath):
# makelink_with_filter() fell back to extracting a copy of a
# symlink member, which already has its own attributes set.
# Setting the hard link's attributes here would follow the
# symlink, possibly to a file outside the destination directory.
set_attrs = False

if set_attrs:
self.chown(tarinfo, targetpath, numeric_owner)
if not tarinfo.issym():
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4825,6 +4825,33 @@ 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
def test_sneaky_hardlink_fallback_attrs(self):
# Test that when a hardlink is extracted as a copy of a symlink
# member, the hardlink's attributes are not applied to the file
# the symlink points to.
with ArchiveMaker() as arc:
arc.add("a/s", symlink_to=os.path.join("..", "flag"))
arc.add("s", hardlink_to=os.path.join("a", "s"),
mode='-rwxr-xr-x', mtime=86400, uid=1337, gid=1337)

for filter in 'tar', 'fully_trusted':
with (
self.subTest(filter),
unittest.mock.patch("os.chown", create=True) as mock_chown,
self.check_context(arc.open(), filter) as cc,
):
if not os_helper.can_symlink():
self.expect_file("a/")
else:
self.expect_file("a/s", symlink_to=os.path.join('..', 'flag'))
self.expect_file("s", symlink_to=os.path.join('..', 'flag'))
st = (cc.outerdir / 'flag').stat()
self.assertNotEqual(st.st_mtime, 86400)
if sys.platform != "win32":
self.assertNotEqual(st.st_mode & 0o777, 0o755)
mock_chown.assert_not_called()

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add the CVE.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :meth:`tarfile.TarFile.extractall` and :meth:`tarfile.TarFile.extract`
changing the mode, modification time and owner of a file outside the
destination directory when a hard link member is extracted as a copy of a
symbolic link member, for example with the ``'tar'`` filter.
Loading