Skip to content

Commit 22829f0

Browse files
committed
donate-cpu: do not let an unlistable folder hide an escaping link
1 parent e521de5 commit 22829f0

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,28 @@ def test_unpack_package_link_in_readonly_dir(tmpdir):
195195
if os.path.lexists(path):
196196
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
197197
assert os.listdir(outside_path) == ['secret.c']
198+
199+
def test_unpack_package_link_in_unreadable_dir(tmpdir):
200+
work_path = str(tmpdir)
201+
outside_path = os.path.join(work_path, 'outside')
202+
os.mkdir(outside_path)
203+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
204+
f.write('int secret;\n')
205+
tgz = os.path.join(work_path, 'temp.tgz')
206+
with tarfile.open(tgz, 'w:gz') as tf:
207+
_add_tar_member(tf, 'pkg/main.c')
208+
# redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
209+
_add_tar_member(tf, 'pkg/q.h/l.h', linkname='d.h/../../../outside')
210+
_add_tar_member(tf, 'pkg/q.h/d.h', linkname='.')
211+
# a directory member with a header-like name makes the folder unlistable after the links exist
212+
_add_tar_member(tf, 'pkg/q.h', dirmode=0o311)
213+
214+
temp_path, source_found = unpack_package(work_path, tgz)
215+
assert source_found
216+
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
217+
real_temp_path = os.path.realpath(temp_path)
218+
for name in ('l.h', 'd.h'):
219+
path = os.path.join(temp_path, 'pkg', 'q.h', name)
220+
if os.path.lexists(path):
221+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
222+
assert os.listdir(outside_path) == ['secret.c']

‎tools/donate_cpu_lib.py‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,43 @@ def __remove_link(path):
360360
os.rmdir(path)
361361

362362

363+
def __find_escaping_links(real_temp_path):
364+
# Returns the links below real_temp_path that resolve outside of it, and whether a folder had to
365+
# be made readable to list it. Raises OSError for a folder that cannot be listed even then.
366+
import stat
367+
fixed = False
368+
369+
def onerror(e):
370+
# a directory member extracted after the links can have made its folder unreadable
371+
nonlocal fixed
372+
mode = stat.S_IMODE(os.lstat(e.filename).st_mode)
373+
missing = (stat.S_IRUSR | stat.S_IXUSR) & ~mode
374+
if not missing:
375+
raise e
376+
os.chmod(e.filename, mode | missing)
377+
fixed = True
378+
379+
links = []
380+
for root, dirs, files in os.walk(real_temp_path, onerror=onerror):
381+
for name in dirs + files:
382+
path = os.path.join(root, name)
383+
if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)):
384+
links.append(path)
385+
return links, fixed
386+
387+
363388
def __remove_escaping_links(real_temp_path):
364389
# A link that still pointed inside real_temp_path when it was extracted can be redirected
365390
# by a link that is extracted later, so all links are checked again afterwards. Removing a
366391
# 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.
392+
# Raises OSError if a folder cannot be listed or a link cannot be removed.
368393
import stat
369394
removed = 0
370395
while True:
371-
links = []
372-
for root, dirs, files in os.walk(real_temp_path):
373-
for name in dirs + files:
374-
path = os.path.join(root, name)
375-
if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)):
376-
links.append(path)
396+
links, fixed = __find_escaping_links(real_temp_path)
397+
if fixed:
398+
# a folder that was skipped by the walk is readable now, so look at its content as well
399+
continue
377400
if not links:
378401
return removed
379402
for path in links:

0 commit comments

Comments
 (0)