Skip to content

Commit a2f33a2

Browse files
committed
donate-cpu: drop extracted links that end up pointing outside the temp folder
1 parent 904ee65 commit a2f33a2

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
@@ -126,3 +126,31 @@ def test_unpack_package_duplicate_name(tmpdir):
126126
inc_path = os.path.realpath(os.path.join(temp_path, 'pkg', 'inc.h'))
127127
assert os.path.commonpath([real_temp_path, inc_path]) == real_temp_path
128128
assert os.listdir(outside_path) == ['target.c']
129+
130+
def test_unpack_package_link_redirected_later(tmpdir):
131+
work_path = str(tmpdir)
132+
outside_path = os.path.join(work_path, 'outside')
133+
os.mkdir(outside_path)
134+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
135+
f.write('int secret;\n')
136+
tgz = os.path.join(work_path, 'temp.tgz')
137+
with tarfile.open(tgz, 'w:gz') as tf:
138+
_add_tar_member(tf, 'pkg/src/main.c')
139+
# 'd.h' does not exist yet, so this target is resolved as temp/outside and looks harmless
140+
_add_tar_member(tf, 'pkg/l.h', linkname='d.h/../../outside')
141+
# turns the missing component into a link, which redirects 'l.h' out of temp
142+
_add_tar_member(tf, 'pkg/d.h', linkname='.')
143+
# a link that stays inside the package is still extracted
144+
_add_tar_member(tf, 'pkg/include/main.c', linkname='../src/main.c')
145+
146+
temp_path, source_found = unpack_package(work_path, tgz)
147+
assert source_found
148+
real_temp_path = os.path.realpath(temp_path)
149+
for name in ('l.h', 'd.h'):
150+
path = os.path.join(temp_path, 'pkg', name)
151+
if os.path.lexists(path):
152+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
153+
inc_path = os.path.join(temp_path, 'pkg', 'include', 'main.c')
154+
if os.path.islink(inc_path):
155+
assert os.path.samefile(inc_path, os.path.join(temp_path, 'pkg', 'src', 'main.c'))
156+
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
@@ -352,6 +352,33 @@ def __is_inside_path(base_path, path):
352352
return False
353353

354354

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

0 commit comments

Comments
 (0)