Skip to content

Commit 60f7858

Browse files
committed
donate-cpu: remove escaping links from read-only folders and fail the unpack otherwise
1 parent f5cfa23 commit 60f7858

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
@@ -129,10 +129,13 @@ def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
129129
'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \
130130
'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n'
131131

132-
def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE):
132+
def _add_tar_member(tf, name, linkname=None, linktype=tarfile.SYMTYPE, dirmode=None):
133133
info = tarfile.TarInfo(name)
134134
data = b''
135-
if linkname is not None:
135+
if dirmode is not None:
136+
info.type = tarfile.DIRTYPE
137+
info.mode = dirmode
138+
elif linkname is not None:
136139
info.type = linktype
137140
info.linkname = linkname
138141
else:
@@ -226,3 +229,28 @@ def test_unpack_package_hardlink_missing_target(tmpdir):
226229
assert source_found
227230
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
228231
assert not os.path.lexists(os.path.join(temp_path, 'pkg', 'copy.c'))
232+
233+
def test_unpack_package_link_in_readonly_dir(tmpdir):
234+
work_path = str(tmpdir)
235+
outside_path = os.path.join(work_path, 'outside')
236+
os.mkdir(outside_path)
237+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
238+
f.write('int secret;\n')
239+
tgz = os.path.join(work_path, 'temp.tgz')
240+
with tarfile.open(tgz, 'w:gz') as tf:
241+
_add_tar_member(tf, 'pkg/main.c')
242+
# redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
243+
_add_tar_member(tf, 'pkg/x.h/l.h', linkname='d.h/../../../outside')
244+
_add_tar_member(tf, 'pkg/x.h/d.h', linkname='.')
245+
# a directory member with a header-like name makes the folder read-only after the links exist
246+
_add_tar_member(tf, 'pkg/x.h', dirmode=0o555)
247+
248+
temp_path, source_found = unpack_package(work_path, tgz)
249+
assert source_found
250+
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
251+
real_temp_path = os.path.realpath(temp_path)
252+
for name in ('l.h', 'd.h'):
253+
path = os.path.join(temp_path, 'pkg', 'x.h', name)
254+
if os.path.lexists(path):
255+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
256+
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
@@ -353,10 +353,20 @@ def __is_inside_path(base_path, path):
353353
return False
354354

355355

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

382390

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

0 commit comments

Comments
 (0)