Skip to content

Commit 74cca9a

Browse files
miss-islingtonencukougpshead
authored
[3.14] gh-149486: tarfile.data_filter: validate written link target (GH-149487) (GH-149554)
* 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 5784119) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 6588ca5 commit 74cca9a

3 files changed

Lines changed: 133 additions & 39 deletions

File tree

Lib/tarfile.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,16 +830,22 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
830830
if member.islnk() or member.issym():
831831
if os.path.isabs(member.linkname):
832832
raise AbsoluteLinkError(member)
833+
# A link member that resolves to the destination directory itself
834+
# would replace it with a (sym)link, redirecting the destination
835+
# for all subsequent members.
836+
if target_path == dest_path:
837+
raise OutsideDestinationError(member, target_path)
833838
normalized = os.path.normpath(member.linkname)
834839
if normalized != member.linkname:
835840
new_attrs['linkname'] = normalized
836841
if member.issym():
837-
target_path = os.path.join(dest_path,
838-
os.path.dirname(name),
839-
member.linkname)
842+
# The symlink is created at `name` with trailing separators
843+
# stripped, so its target is relative to the directory
844+
# containing that path.
845+
link_dir = os.path.dirname(name.rstrip('/' + os.sep))
846+
target_path = os.path.join(dest_path, link_dir, normalized)
840847
else:
841-
target_path = os.path.join(dest_path,
842-
member.linkname)
848+
target_path = os.path.join(dest_path, normalized)
843849
target_path = os.path.realpath(target_path,
844850
strict=os.path.ALLOW_MISSING)
845851
if os.path.commonpath([target_path, dest_path]) != dest_path:

Lib/test/test_tarfile.py

Lines changed: 117 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,6 +3682,39 @@ class TestExtractionFilters(unittest.TestCase):
36823682
# The destination for the extraction, within `outerdir`
36833683
destdir = outerdir / 'dest'
36843684

3685+
@classmethod
3686+
def setUpClass(cls):
3687+
# Posix and Windows have different pathname resolution:
3688+
# either symlink or a '..' component resolve first.
3689+
# Let's see which we are on.
3690+
if os_helper.can_symlink():
3691+
testpath = os.path.join(TEMPDIR, 'resolution_test')
3692+
os.mkdir(testpath)
3693+
3694+
# testpath/current links to `.` which is all of:
3695+
# - `testpath`
3696+
# - `testpath/current`
3697+
# - `testpath/current/current`
3698+
# - etc.
3699+
os.symlink('.', os.path.join(testpath, 'current'))
3700+
3701+
# we'll test where `testpath/current/../file` ends up
3702+
with open(os.path.join(testpath, 'current', '..', 'file'), 'w'):
3703+
pass
3704+
3705+
if os.path.exists(os.path.join(testpath, 'file')):
3706+
# Windows collapses 'current\..' to '.' first, leaving
3707+
# 'testpath\file'
3708+
cls.dotdot_resolves_early = True
3709+
elif os.path.exists(os.path.join(testpath, '..', 'file')):
3710+
# Posix resolves 'current' to '.' first, leaving
3711+
# 'testpath/../file'
3712+
cls.dotdot_resolves_early = False
3713+
else:
3714+
raise AssertionError('Could not determine link resolution')
3715+
else:
3716+
cls.dotdot_resolves_early = False
3717+
36853718
@contextmanager
36863719
def check_context(self, tar, filter, *, check_flag=True):
36873720
"""Extracts `tar` to `self.destdir` and allows checking the result
@@ -3853,10 +3886,19 @@ def test_parent_symlink(self):
38533886
+ "which is outside the destination")
38543887

38553888
with self.check_context(arc.open(), 'data'):
3856-
self.expect_exception(
3857-
tarfile.LinkOutsideDestinationError,
3858-
"""'parent' would link to ['"].*outerdir['"], """
3859-
+ "which is outside the destination")
3889+
if self.dotdot_resolves_early:
3890+
# 'current/../..' normalises to '..', which is rejected.
3891+
self.expect_exception(
3892+
tarfile.LinkOutsideDestinationError,
3893+
"""'parent' would link to ['"].*outerdir['"], """
3894+
+ "which is outside the destination")
3895+
else:
3896+
# 'current/..' normalises to '.'; the rewritten link is
3897+
# created and 'parent/evil' lands harmlessly inside the
3898+
# destination.
3899+
self.expect_file('current', symlink_to='.')
3900+
self.expect_file('parent', symlink_to='.')
3901+
self.expect_file('evil')
38603902

38613903
else:
38623904
# No symlink support. The symlinks are ignored.
@@ -3946,35 +3988,6 @@ def test_parent_symlink2(self):
39463988
# Test interplaying symlinks
39473989
# Inspired by 'dirsymlink2b' in jwilk/traversal-archives
39483990

3949-
# Posix and Windows have different pathname resolution:
3950-
# either symlink or a '..' component resolve first.
3951-
# Let's see which we are on.
3952-
if os_helper.can_symlink():
3953-
testpath = os.path.join(TEMPDIR, 'resolution_test')
3954-
os.mkdir(testpath)
3955-
3956-
# testpath/current links to `.` which is all of:
3957-
# - `testpath`
3958-
# - `testpath/current`
3959-
# - `testpath/current/current`
3960-
# - etc.
3961-
os.symlink('.', os.path.join(testpath, 'current'))
3962-
3963-
# we'll test where `testpath/current/../file` ends up
3964-
with open(os.path.join(testpath, 'current', '..', 'file'), 'w'):
3965-
pass
3966-
3967-
if os.path.exists(os.path.join(testpath, 'file')):
3968-
# Windows collapses 'current\..' to '.' first, leaving
3969-
# 'testpath\file'
3970-
dotdot_resolves_early = True
3971-
elif os.path.exists(os.path.join(testpath, '..', 'file')):
3972-
# Posix resolves 'current' to '.' first, leaving
3973-
# 'testpath/../file'
3974-
dotdot_resolves_early = False
3975-
else:
3976-
raise AssertionError('Could not determine link resolution')
3977-
39783991
with ArchiveMaker() as arc:
39793992

39803993
# `current` links to `.` which is both the destination directory
@@ -4010,7 +4023,7 @@ def test_parent_symlink2(self):
40104023

40114024
with self.check_context(arc.open(), 'data'):
40124025
if os_helper.can_symlink():
4013-
if dotdot_resolves_early:
4026+
if self.dotdot_resolves_early:
40144027
# Fail when extracting a file outside destination
40154028
self.expect_exception(
40164029
tarfile.OutsideDestinationError,
@@ -4130,6 +4143,76 @@ def test_sly_relative2(self):
41304143
+ """['"].*moo['"], which is outside the """
41314144
+ "destination")
41324145

4146+
@symlink_test
4147+
@os_helper.skip_unless_symlink
4148+
def test_normpath_realpath_mismatch(self):
4149+
# The link-target check must validate the value that will actually
4150+
# be written to disk (the normalised linkname), not the original.
4151+
# Here 'a' is a symlink to a deep nonexistent path, so realpath()
4152+
# of 'a/../../...' stays inside the destination while normpath()
4153+
# collapses 'a/..' lexically and escapes.
4154+
depth = len(self.destdir.parts) + 5
4155+
deep = '/'.join(f'p{i}' for i in range(depth))
4156+
sneaky = 'a/' + '../' * depth + 'flag'
4157+
for kind in 'symlink_to', 'hardlink_to':
4158+
with self.subTest(kind):
4159+
with ArchiveMaker() as arc:
4160+
arc.add('a', symlink_to=deep)
4161+
arc.add('escape', **{kind: sneaky})
4162+
with self.check_context(arc.open(), 'data'):
4163+
self.expect_exception(
4164+
tarfile.LinkOutsideDestinationError)
4165+
4166+
@symlink_test
4167+
@os_helper.skip_unless_symlink
4168+
def test_symlink_trailing_slash(self):
4169+
# A trailing slash on a symlink member's name must not cause the
4170+
# link target to be resolved relative to the wrong directory.
4171+
with ArchiveMaker() as arc:
4172+
t = tarfile.TarInfo('x/')
4173+
t.type = tarfile.SYMTYPE
4174+
t.linkname = '..'
4175+
arc.tar_w.addfile(t)
4176+
arc.add('x/escaped', content='hi')
4177+
4178+
with self.check_context(arc.open(), 'data'):
4179+
self.expect_exception(tarfile.LinkOutsideDestinationError)
4180+
4181+
@symlink_test
4182+
@os_helper.skip_unless_symlink
4183+
def test_link_at_destination(self):
4184+
# A link member whose name resolves to the destination directory
4185+
# itself must be rejected: otherwise the destination is replaced
4186+
# by a symlink and later members can be redirected through it.
4187+
for name in '', '.', './':
4188+
with ArchiveMaker() as arc:
4189+
t = tarfile.TarInfo(name)
4190+
t.type = tarfile.SYMTYPE
4191+
t.linkname = '.'
4192+
arc.tar_w.addfile(t)
4193+
4194+
with self.check_context(arc.open(), 'data'):
4195+
self.expect_exception(tarfile.OutsideDestinationError)
4196+
4197+
@symlink_test
4198+
@os_helper.skip_unless_symlink
4199+
def test_empty_name_symlink_chain(self):
4200+
# Regression test for a chain of empty-named symlinks that
4201+
# incrementally redirects the destination outwards.
4202+
with ArchiveMaker() as arc:
4203+
for name, target in [('', ''), ('a/', '..'),
4204+
('', 'dummy'), ('', 'a'),
4205+
('b/', '..'),
4206+
('', 'dummy'), ('', 'a/b')]:
4207+
t = tarfile.TarInfo(name)
4208+
t.type = tarfile.SYMTYPE
4209+
t.linkname = target
4210+
arc.tar_w.addfile(t)
4211+
arc.add('escaped', content='hi')
4212+
4213+
with self.check_context(arc.open(), 'data'):
4214+
self.expect_exception(tarfile.FilterError)
4215+
41334216
@symlink_test
41344217
def test_deep_symlink(self):
41354218
# Test that symlinks and hardlinks inside a directory
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:func:`tarfile.data_filter` now validates link targets using the same
2+
normalised value that is written to disk, strips trailing separators from
3+
the member name when resolving a symlink's directory, and rejects link
4+
members that would replace the destination directory itself. This closes
5+
several path-traversal bypasses of the ``data`` extraction filter.

0 commit comments

Comments
 (0)