| •1 |
|
| 2 |
"""A confined fd pin must remain usable inside a Linux user namespace.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import shlex |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
import tempfile |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
from rsyncfns import makepath, rmtree, rsync_argv, test_fail, test_skipped |
| 13 |
|
| 14 |
|
| 15 |
if not sys.platform.startswith('linux'): |
| 16 |
test_skipped('rrsync-userns-procfs is Linux-specific') |
| 17 |
|
| 18 |
if not os.environ.get('RSYNC_USERNS_PROCFS'): |
| 19 |
unshare = shutil.which('unshare') |
| 20 |
if unshare is None: |
| 21 |
test_skipped('unshare is unavailable') |
| 22 |
env = os.environ.copy() |
| 23 |
env['RSYNC_USERNS_PROCFS'] = '1' |
| 24 |
launch_dir = Path(tempfile.mkdtemp(prefix='rsync-userns-launch-')) |
| 25 |
launch_dir.chmod(0o755) |
| 26 |
testdir = Path(__file__).resolve().parent |
| 27 |
child_test = launch_dir / Path(__file__).name |
| 28 |
for source in (Path(__file__), testdir / 'rsyncfns.py', |
| 29 |
testdir / 'exitcodes.py'): |
| 30 |
shutil.copy2(source, launch_dir / source.name) |
| 31 |
rsync_cmd = shlex.split(env['RSYNC']) |
| 32 |
for i, arg in enumerate(rsync_cmd): |
| 33 |
if Path(arg).name in ('rsync', 'rsync.exe') and Path(arg).is_file(): |
| 34 |
staged_rsync = launch_dir / Path(arg).name |
| 35 |
shutil.copy2(arg, staged_rsync) |
| 36 |
staged_rsync.chmod(0o755) |
| 37 |
rsync_cmd[i] = str(staged_rsync) |
| 38 |
break |
| 39 |
else: |
| 40 |
rmtree(launch_dir) |
| 41 |
test_fail(f'cannot locate the rsync executable in {env["RSYNC"]!r}') |
| 42 |
env['RSYNC'] = shlex.join(rsync_cmd) |
| 43 |
launcher = [] |
| 44 |
if os.geteuid() == 0: |
| 45 |
setpriv = shutil.which('setpriv') |
| 46 |
if setpriv is None: |
| 47 |
test_skipped('setpriv is unavailable for the root-run testsuite') |
| 48 |
launcher = [setpriv, '--reuid=65534', '--regid=65534', '--clear-groups'] |
| 49 |
unshare_argv = [unshare, '--user', '--map-root-user', '--mount', '--pid', |
| 50 |
'--fork', '--mount-proc'] |
| 51 |
probe = subprocess.run( |
| 52 |
launcher + unshare_argv + ['true'], |
| 53 |
stdin=subprocess.DEVNULL, |
| 54 |
stdout=subprocess.DEVNULL, |
| 55 |
stderr=subprocess.DEVNULL, |
| 56 |
) |
| 57 |
if probe.returncode != 0: |
| 58 |
rmtree(launch_dir) |
| 59 |
print(f'user namespaces unavailable (rc={probe.returncode})') |
| 60 |
raise SystemExit(0) |
| 61 |
try: |
| 62 |
proc = subprocess.run( |
| 63 |
launcher + unshare_argv |
| 64 |
+ [sys.executable, str(child_test)], |
| 65 |
env=env, |
| 66 |
timeout=30, |
| 67 |
) |
| 68 |
except subprocess.TimeoutExpired: |
| 69 |
test_fail('user-namespace regression test timed out') |
| 70 |
finally: |
| 71 |
rmtree(launch_dir) |
| 72 |
if proc.returncode != 0: |
| 73 |
test_fail(f'user-namespace regression test failed (rc={proc.returncode})') |
| 74 |
print('rrsync fd pin works inside a user namespace') |
| 75 |
raise SystemExit(0) |
| 76 |
|
| 77 |
proc_uid = os.lstat('/proc/self').st_uid |
| 78 |
if proc_uid in (0, os.geteuid()): |
| 79 |
test_skipped('/proc/self does not expose an overflow uid in this namespace') |
| 80 |
|
| 81 |
base = Path(tempfile.mkdtemp(prefix='rsync-userns-procfs-')) |
| 82 |
src = base / 'src' |
| 83 |
dest = base / 'dest' |
| 84 |
outside = base / 'outside' |
| 85 |
makepath(src, dest, outside) |
| 86 |
(src / 'file').write_text('content\n') |
| 87 |
|
| 88 |
fd_roots = ['/proc/self/fd'] |
| 89 |
if Path('/dev/fd').exists(): |
| 90 |
fd_roots.append('/dev/fd') |
| 91 |
|
| 92 |
dest_fd = os.open(dest, os.O_RDONLY | os.O_DIRECTORY) |
| 93 |
try: |
| 94 |
for index, fd_root in enumerate(fd_roots): |
| 95 |
log_file = dest / f'rsync-{index}.log' |
| 96 |
proc = subprocess.run( |
| 97 |
rsync_argv('-a', f'--confine-root={dest}', |
| 98 |
f'--log-file={fd_root}/{dest_fd}/{log_file.name}', |
| 99 |
str(src) + '/', str(dest) + '/'), |
| 100 |
pass_fds=(dest_fd,), |
| 101 |
capture_output=True, |
| 102 |
text=True, |
| 103 |
) |
| 104 |
ctx = (f'fd_root={fd_root!r}, rc={proc.returncode}, ' |
| 105 |
f'stderr={proc.stderr.strip()[:300]!r}') |
| 106 |
if proc.returncode != 0: |
| 107 |
test_fail(f'confined transfer through an fd pin failed ({ctx})') |
| 108 |
if not log_file.is_file(): |
| 109 |
test_fail(f'confined log path through an fd pin was rejected ({ctx})') |
| 110 |
if (dest / 'file').read_text() != 'content\n': |
| 111 |
test_fail(f'confined transfer did not deliver the file ({ctx})') |
| 112 |
finally: |
| 113 |
os.close(dest_fd) |
| 114 |
|
| 115 |
outside_list = outside / 'files-from' |
| 116 |
outside_list.write_text('file\n') |
| 117 |
for fd_root in fd_roots: |
| 118 |
outside_fd = os.open(outside_list, os.O_RDONLY) |
| 119 |
try: |
| 120 |
proc = subprocess.run( |
| 121 |
rsync_argv('-a', f'--confine-root={dest}', |
| 122 |
f'--files-from={fd_root}/{outside_fd}', |
| 123 |
str(src) + '/', str(dest) + '/'), |
| 124 |
pass_fds=(outside_fd,), |
| 125 |
capture_output=True, |
| 126 |
text=True, |
| 127 |
) |
| 128 |
finally: |
| 129 |
os.close(outside_fd) |
| 130 |
if proc.returncode == 0 or 'failed to open files-from file' not in proc.stderr: |
| 131 |
test_fail(f'outside {fd_root} pin was not observably refused: ' |
| 132 |
f'rc={proc.returncode}, stderr={proc.stderr!r}') |
| 133 |
rmtree(base) |