Skip to content

Commit 9b6cc75

Browse files
committed
donate-cpu: do not let an unlistable folder hide an escaping link
1 parent 60f7858 commit 9b6cc75

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
@@ -254,3 +254,28 @@ def test_unpack_package_link_in_readonly_dir(tmpdir):
254254
if os.path.lexists(path):
255255
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
256256
assert os.listdir(outside_path) == ['secret.c']
257+
258+
def test_unpack_package_link_in_unreadable_dir(tmpdir):
259+
work_path = str(tmpdir)
260+
outside_path = os.path.join(work_path, 'outside')
261+
os.mkdir(outside_path)
262+
with open(os.path.join(outside_path, 'secret.c'), 'w', encoding='utf-8') as f:
263+
f.write('int secret;\n')
264+
tgz = os.path.join(work_path, 'temp.tgz')
265+
with tarfile.open(tgz, 'w:gz') as tf:
266+
_add_tar_member(tf, 'pkg/main.c')
267+
# redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
268+
_add_tar_member(tf, 'pkg/q.h/l.h', linkname='d.h/../../../outside')
269+
_add_tar_member(tf, 'pkg/q.h/d.h', linkname='.')
270+
# a directory member with a header-like name makes the folder unlistable after the links exist
271+
_add_tar_member(tf, 'pkg/q.h', dirmode=0o311)
272+
273+
temp_path, source_found = unpack_package(work_path, tgz)
274+
assert source_found
275+
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
276+
real_temp_path = os.path.realpath(temp_path)
277+
for name in ('l.h', 'd.h'):
278+
path = os.path.join(temp_path, 'pkg', 'q.h', name)
279+
if os.path.lexists(path):
280+
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
281+
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
@@ -361,20 +361,43 @@ def __remove_link(path):
361361
os.rmdir(path)
362362

363363

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

0 commit comments

Comments
 (0)