Skip to content

Commit 69d5fb6

Browse files
committed
donate-cpu: also restore the search bit on folders that hide escaping links
1 parent 9b6cc75 commit 69d5fb6

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ def test_unpack_package_link_in_readonly_dir(tmpdir):
255255
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
256256
assert os.listdir(outside_path) == ['secret.c']
257257

258-
def test_unpack_package_link_in_unreadable_dir(tmpdir):
258+
# 0o311 cannot be listed, 0o644 can be listed but its entries cannot be told apart from links
259+
@pytest.mark.parametrize('dirmode', [0o311, 0o644])
260+
def test_unpack_package_link_in_inaccessible_dir(tmpdir, dirmode):
259261
work_path = str(tmpdir)
260262
outside_path = os.path.join(work_path, 'outside')
261263
os.mkdir(outside_path)
@@ -267,15 +269,19 @@ def test_unpack_package_link_in_unreadable_dir(tmpdir):
267269
# redirected out of temp by the next member, like in test_unpack_package_link_redirected_later
268270
_add_tar_member(tf, 'pkg/q.h/l.h', linkname='d.h/../../../outside')
269271
_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+
# a directory member with a header-like name takes the read or search bit off the folder
273+
# that holds the links, after they have been extracted
274+
_add_tar_member(tf, 'pkg/q.h', dirmode=dirmode)
272275

273276
temp_path, source_found = unpack_package(work_path, tgz)
274277
assert source_found
275278
assert os.path.isfile(os.path.join(temp_path, 'pkg', 'main.c'))
276279
real_temp_path = os.path.realpath(temp_path)
280+
# the folder has to be accessible to see whether an escaping link was left in it
281+
dir_path = os.path.join(temp_path, 'pkg', 'q.h')
282+
os.chmod(dir_path, 0o755)
277283
for name in ('l.h', 'd.h'):
278-
path = os.path.join(temp_path, 'pkg', 'q.h', name)
284+
path = os.path.join(dir_path, name)
279285
if os.path.lexists(path):
280286
assert os.path.commonpath([real_temp_path, os.path.realpath(path)]) == real_temp_path
281287
assert os.listdir(outside_path) == ['secret.c']

‎tools/donate_cpu_lib.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,33 @@ def __remove_link(path):
363363

364364
def __find_escaping_links(real_temp_path):
365365
# 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.
366+
# be made accessible to look at its content. Raises OSError for a folder that is inaccessible
367+
# even then.
367368
import stat
368369
fixed = False
369370

370-
def onerror(e):
371-
# a directory member extracted after the links can have made its folder unreadable
371+
def make_accessible(path):
372+
# A directory member extracted after the links can have taken the owner read or search bit
373+
# off its folder. Without the read bit it cannot be listed, and without the search bit its
374+
# entries cannot be told apart from links.
372375
nonlocal fixed
373-
mode = stat.S_IMODE(os.lstat(e.filename).st_mode)
376+
mode = stat.S_IMODE(os.lstat(path).st_mode)
374377
missing = (stat.S_IRUSR | stat.S_IXUSR) & ~mode
375378
if not missing:
376-
raise e
377-
os.chmod(e.filename, mode | missing)
379+
return False
380+
os.chmod(path, mode | missing)
378381
fixed = True
382+
return True
383+
384+
def onerror(e):
385+
if not make_accessible(e.filename):
386+
raise e
379387

380388
links = []
381389
for root, dirs, files in os.walk(real_temp_path, onerror=onerror):
390+
if make_accessible(root):
391+
# the entries were listed without the search bit needed to stat them, so start over
392+
break
382393
for name in dirs + files:
383394
path = os.path.join(root, name)
384395
if os.path.islink(path) and not __is_inside_path(real_temp_path, os.path.realpath(path)):
@@ -396,7 +407,7 @@ def __remove_escaping_links(real_temp_path):
396407
while True:
397408
links, fixed = __find_escaping_links(real_temp_path)
398409
if fixed:
399-
# a folder that was skipped by the walk is readable now, so look at its content as well
410+
# a folder whose content the walk could not see is accessible now, so look again
400411
continue
401412
if not links:
402413
return removed

0 commit comments

Comments
 (0)