Skip to content

Commit 2f91952

Browse files
committed
donate-cpu: drop extracted links that end up pointing outside the temp folder
1 parent 1ea1577 commit 2f91952

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,31 @@ def test_unpack_package_duplicate_name(tmpdir):
185185
inc_path = os.path.realpath(os.path.join(temp_path, 'pkg', 'inc.h'))
186186
assert os.path.commonpath([real_temp_path, inc_path]) == real_temp_path
187187
assert os.listdir(outside_path) == ['target.c']
188+
189+
def test_unpack_package_link_redirected_later(tmpdir):
190+
work_path = str(tmpdir)
191+
outside_path = os.path.join(work_path, 'outside')
192+
os.mkdir(outside_path)
193+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
194+
f.write('int secret;\n')
195+
tgz = os.path.join(work_path, 'temp.tgz')
196+
with tarfile.open(tgz, 'w:gz') as tf:
197+
_add_tar_member(tf, 'pkg/src/main.c')
198+
# 'd.h' does not exist yet, so this target is resolved as temp/outside and looks harmless
199+
_add_tar_member(tf, 'pkg/l.h', linkname='d.h/../../outside')
200+
# turns the missing component into a link, which redirects 'l.h' out of temp
201+
_add_tar_member(tf, 'pkg/d.h', linkname='.')
202+
# a link that stays inside the package is still extracted
203+
_add_tar_member(tf, 'pkg/include/main.c', linkname='../src/main.c')
204+
205+
temp_path, source_found = unpack_package(work_path, tgz)
206+
assert source_found
207+
real_temp_path = os.path.realpath(temp_path)
208+
for name in ('l.h', 'd.h'):
209+
path = os.path.join(temp_path, 'pkg', name)
210+
if os.path.lexists(path):
211+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
212+
inc_path = os.path.join(temp_path, 'pkg', 'include', 'main.c')
213+
if os.path.islink(inc_path):
214+
assert os.path.samefile(inc_path, os.path.join(temp_path, 'pkg', 'src', 'main.c'))
215+
assert os.listdir(outside_path) == ['secret.c']

‎tools/donate_cpu_lib.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,33 @@ def __is_inside_path(base_path, path):
353353
return False
354354

355355

356+
def __remove_escaping_links(real_temp_path):
357+
# A link that still pointed inside real_temp_path when it was extracted can be redirected
358+
# by a link that is extracted later, so all links are checked again afterwards. Removing a
359+
# link changes what the remaining ones resolve to, so this is repeated until nothing is left.
360+
removed = 0
361+
while True:
362+
links = []
363+
for root, dirs, files in os.walk(real_temp_path):
364+
for name in dirs + files:
365+
path = os.path.join(root, name)
366+
if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)):
367+
links.append(path)
368+
if not links:
369+
return removed
370+
for path in links:
371+
try:
372+
os.remove(path)
373+
removed += 1
374+
except OSError:
375+
try:
376+
# a directory symlink on Windows
377+
os.rmdir(path)
378+
removed += 1
379+
except OSError:
380+
return removed
381+
382+
356383
def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None):
357384
temp_path = os.path.join(work_path, 'temp')
358385
__remove_tree(temp_path)
@@ -419,6 +446,9 @@ def unpack_package(work_path, tgz, cpp_only=False, c_only=False, skip_files=None
419446
pass
420447
except tarfile.TarError:
421448
pass
449+
removed = __remove_escaping_links(real_temp_path)
450+
extracted -= removed
451+
skipped += removed
422452
print('extracted {} of {} files (skipped {}{})'.format(extracted, total, skipped, (' / only headers' if (extracted and not source_found) else '')))
423453
return temp_path, source_found
424454

0 commit comments

Comments
 (0)