| 1 |
|
| 2 |
"""An rrsync restricted dir must bound the merge files the peer can name. |
| 3 |
|
| 4 |
Filter rules travel over the protocol rather than in the argv rrsync validates, |
| 5 |
so a dir-merge rule can name a file outside the restricted dir and have the |
| 6 |
server read it in as rules. Redacting the resulting diagnostics is not enough: |
| 7 |
an exclude-only merge (the "-" modifier) turns every line into a pattern, so |
| 8 |
nothing fails to parse and the client reads the file's contents off its own |
| 9 |
file list instead -- no error, no verbosity, and on a pull no --delete either. |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
import shlex |
| 14 |
import signal |
| 15 |
import subprocess |
| 16 |
import sys |
| 17 |
|
| 18 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 19 |
signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 20 |
if '--shell' in sys.argv: |
| 21 |
i = sys.argv.index('--shell') + 2 |
| 22 |
env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])} |
| 23 |
signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
| 24 |
signal.signal(signal.SIGUSR2, signal.SIG_DFL) |
| 25 |
wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT'] |
| 26 |
os.execve(wrapper, [wrapper, '-ro', '-no-lock', root], env) |
| 27 |
|
| 28 |
from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg |
| 29 |
|
| 30 |
base = SCRATCHDIR / 'rrsync-merge-file-confine' |
| 31 |
rmtree(base) |
| 32 |
root = base / 'root' |
| 33 |
sub = root / 'sub' |
| 34 |
dest = base / 'dest' |
| 35 |
makepath(sub, dest) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
(base / 'outside-secret').write_text('secretword\n') |
| 42 |
(root / 'secretword').write_bytes(b'X') |
| 43 |
(root / 'keep').write_bytes(b'X') |
| 44 |
|
| 45 |
(sub / 'intree-dropped').write_bytes(b'X') |
| 46 |
(sub / 'intree-kept').write_bytes(b'X') |
| 47 |
(sub / '.rsync-filter').write_text('intree-dropped\n') |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
shim = base / 'rsync-shim' |
| 53 |
shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n') |
| 54 |
shim.chmod(0o755) |
| 55 |
wrapper = patched_rrsync(base, rsync_path=str(shim)) |
| 56 |
rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell' |
| 57 |
env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)} |
| 58 |
|
| 59 |
|
| 60 |
def pull(filt, into): |
| 61 |
into.mkdir(parents=True, exist_ok=True) |
| 62 |
return subprocess.run( |
| 63 |
rsync_argv('-r', f'--filter={filt}', '-e', rsh, 'ignored:.', f'{into}/'), |
| 64 |
env=env, capture_output=True, text=True, timeout=20) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
esc = dest / 'escape' |
| 70 |
got = pull(':-s ../outside-secret', esc) |
| 71 |
assert got.returncode == 0, ( |
| 72 |
'confining the merge open must not cost the transfer: refusing outright ' |
| 73 |
f'would hand the peer a denial of service. rc={got.returncode}, ' |
| 74 |
f'stderr={got.stderr!r}') |
| 75 |
assert (esc / 'secretword').exists(), ( |
| 76 |
'the server read a merge file outside the restricted dir: "secretword" was ' |
| 77 |
f'excluded, so its presence in that file is confirmed to the client. ' |
| 78 |
f'rc={got.returncode}, stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 79 |
assert 'secretword' not in got.stderr, ( |
| 80 |
f'the out-of-root merge file\'s text reached the peer: stderr={got.stderr!r}') |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
ctl = dest / 'control' |
| 85 |
got = pull(':-s .rsync-filter', ctl) |
| 86 |
assert got.returncode == 0, ( |
| 87 |
f'the in-tree dir-merge failed the transfer: rc={got.returncode}, ' |
| 88 |
f'stderr={got.stderr!r}') |
| 89 |
assert (ctl / 'sub' / 'intree-kept').exists(), ( |
| 90 |
f'the in-tree dir-merge broke the transfer: rc={got.returncode}, ' |
| 91 |
f'stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 92 |
assert not (ctl / 'sub' / 'intree-dropped').exists(), ( |
| 93 |
'the in-tree .rsync-filter was not applied, so this control does not show ' |
| 94 |
f'that merge files still work: stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
alias_root = base / 'aliased' |
| 103 |
makepath(alias_root) |
| 104 |
(base / 'aliased-secret').write_text('aliased-word\n') |
| 105 |
(alias_root / 'aliased-word').write_bytes(b'X') |
| 106 |
(alias_root / 'plain').write_bytes(b'X') |
| 107 |
os.symlink('.', alias_root / 'self-alias') |
| 108 |
os.symlink('../aliased-secret', alias_root / 'outside-link') |
| 109 |
(alias_root / 'intree-list').write_text('plain\n') |
| 110 |
|
| 111 |
|
| 112 |
def confined_pull(filter_text, into): |
| 113 |
(alias_root / '.rsync-filter').write_text(filter_text + '\n') |
| 114 |
rmtree(into) |
| 115 |
into.mkdir(parents=True, exist_ok=True) |
| 116 |
return subprocess.run( |
| 117 |
rsync_argv('-r', f'--confine-root={alias_root}', |
| 118 |
'--filter=:s .rsync-filter', |
| 119 |
f'{alias_root}/self-alias/', f'{into}/'), |
| 120 |
capture_output=True, text=True, timeout=20) |
| 121 |
|
| 122 |
|
| 123 |
esc2 = dest / 'aliased-escape' |
| 124 |
got = confined_pull('merge,- outside-link', esc2) |
| 125 |
assert not (got.returncode == 0 and not (esc2 / 'aliased-word').exists()), ( |
| 126 |
'a merge file outside the root was read through a symlinked source arg, so ' |
| 127 |
f'"aliased-word" was excluded: rc={got.returncode}, stderr={got.stderr!r}') |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ctl2 = dest / 'aliased-control' |
| 133 |
got = confined_pull('merge,- intree-list', ctl2) |
| 134 |
assert got.returncode == 0 and (ctl2 / 'aliased-word').exists(), ( |
| 135 |
'a symlinked source arg with an in-tree merge file broke the transfer: ' |
| 136 |
f'rc={got.returncode}, stderr={got.stderr!r}') |
| 137 |
assert not (ctl2 / 'plain').exists(), ( |
| 138 |
'the in-tree merge file was not applied through the symlinked source arg, ' |
| 139 |
f'so the escape check above proves nothing: stderr={got.stderr!r}') |