Skip to content

Commit e521de5

Browse files
committed
donate-cpu: remove escaping links from read-only folders and fail the unpack otherwise
1 parent 407f7f3 commit e521de5

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ def test_match_multiple_time(tmpdir):
7070
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
7171
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
7272

73-
def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE):
73+
def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE, dirmode=None):
7474
info = tarfile.TarInfo(name)
7575
data = b''
76-
if linkname is not None:
76+
if dirmode is not None:
77+
info.type = tarfile.DIRTYPE
78+
info.mode = dirmode
79+
elif linkname is not None:
7780
info.type = linktype
7881
info.linkname = linkname
7982
else:
@@ -167,3 +170,28 @@ def test_unpack_package_hardlink_missing_target(tmpdir):
167170
assert source_found
168171
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
169172
assert not os.path.lexists(os.path.join(temp_path, 'pkg', 'copy.c'))
173+
174+
def test_unpack_package_link_in_readonly_dir(tmpdir):
175+
work_path = str(tmpdir)
176+
outside_path = os.path.join(work_path, 'outside')
177+
os.mkdir(outside_path)
178+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
179+
f.write('int secret;\n')
180+
tgz = os.path.join(work_path, 'temp.tgz')
181+
with tarfile.open(tgz, 'w:gz') as tf:
182+
_add_tar_member(tf, 'pkg/main.c')
183+
# redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
184+
_add_tar_member(tf, 'pkg/x.h/l.h', linkname='d.h/../../../outside')
185+
_add_tar_member(tf, 'pkg/x.h/d.h', linkname='.')
186+
# a directory member with a header-like name makes the folder read-only after the links exist
187+
_add_tar_member(tf, 'pkg/x.h', dirmode=0o555)
188+
189+
temp_path, source_found = unpack_package(work_path, tgz)
190+
assert source_found
191+
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
192+
real_temp_path = os.path.realpath(temp_path)
193+
for name in ('l.h', 'd.h'):
194+
path = os.path.join(temp_path, 'pkg', 'x.h', name)
195+
if os.path.lexists(path):
196+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
197+
assert os.listdir(outside_path) == ['secret.c']

‎tools/donate_cpu_lib.py‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,20 @@ def __is_inside_path(base_path, path):
352352
return False
353353

354354

355+
def __remove_link(path):
356+
try:
357+
os.remove(path)
358+
except OSError:
359+
# a directory symlink on Windows
360+
os.rmdir(path)
361+
362+
355363
def __remove_escaping_links(real_temp_path):
356364
# A link that still pointed inside real_temp_path when it was extracted can be redirected
357365
# by a link that is extracted later, so all links are checked again afterwards. Removing a
358366
# link changes what the remaining ones resolve to, so this is repeated until nothing is left.
367+
# Raises OSError if a link cannot be removed.
368+
import stat
359369
removed = 0
360370
while True:
361371
links = []
@@ -368,15 +378,13 @@ def __remove_escaping_links(real_temp_path):
368378
return removed
369379
for path in links:
370380
try:
371-
os.remove(path)
372-
removed += 1
381+
__remove_link(path)
373382
except OSError:
374-
try:
375-
# a directory symlink on Windows
376-
os.rmdir(path)
377-
removed += 1
378-
except OSError:
379-
return removed
383+
# a directory member extracted after the link can have made its folder read-only
384+
parent = os.path.dirname(path)
385+
os.chmod(parent, stat.S_IMODE(os.lstat(parent).st_mode) | stat.S_IWUSR)
386+
__remove_link(path)
387+
removed += 1
380388

381389

382390
def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None):
@@ -446,7 +454,12 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
446454
except (tarfile.TarError, KeyError):
447455
# KeyError: hardlink to a member that is neither on disk nor in the archive
448456
pass
449-
removed = __remove_escaping_links(real_temp_path)
457+
try:
458+
removed = __remove_escaping_links(real_temp_path)
459+
except OSError as e:
460+
# do not analyse a package with a link pointing outside of temp_path
461+
print('Failed to remove link pointing outside of {}: {}'.format(temp_path, e))
462+
return temp_path, False
450463
extracted -= removed
451464
skipped += removed
452465
print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else '')))

0 commit comments

Comments
 (0)