| 1 |
|
| 2 |
"""A peer value carrying shell syntax must not reach a shell-executed hook. |
| 3 |
|
| 4 |
Context-aware quoting is only correct for a single level of shell parsing, and |
| 5 |
a hook may re-parse the substituted word in a nested shell: |
| 6 |
|
| 7 |
pre-xfer exec = sh -c 'printf %s %RSYNC_USER_NAME% >out' |
| 8 |
|
| 9 |
There the level-1 quotes are removed before the inner shell sees the value, so |
| 10 |
no amount of escaping keeps it as data. The daemon therefore refuses the value |
| 11 |
outright. This checks the refusal in each quote context, including the nested |
| 12 |
form, and -- so the rule cannot be satisfied by refusing everything -- that an |
| 13 |
ordinary value still runs the hook and arrives intact. |
| 14 |
|
| 15 |
One daemon serves a module per case: a second daemon cannot lock the shared pid |
| 16 |
file, so starting one per case would only work in the stdio-pipe transport. |
| 17 |
""" |
| 18 |
|
| 19 |
import os |
| 20 |
import subprocess |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
from rsyncfns import ( |
| 24 |
FROMDIR, SCRATCHDIR, make_tree, makepath, rmtree, rsync_argv, |
| 25 |
start_test_daemon, test_fail, write_daemon_conf, |
| 26 |
) |
| 27 |
|
| 28 |
PORT = 12943 |
| 29 |
|
| 30 |
src = FROMDIR |
| 31 |
rmtree(src) |
| 32 |
make_tree(src, depth=1) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
TEMPLATES = { |
| 37 |
'unq': 'printf %s {v} >{out}', |
| 38 |
'sq': "printf %s '{v}' >{out}", |
| 39 |
'dq': 'printf %s "{v}" >{out}', |
| 40 |
'dqapos': 'printf %s "it\'s {v}" >{out}', |
| 41 |
'nest': "sh -c 'printf %s {v} >{out}'", |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
MARKER = 'metachar-pwned' |
| 47 |
HOSTILE = f'$(touch${{IFS}}{MARKER})x' |
| 48 |
BENIGN = 'alice.b-1_x' |
| 49 |
|
| 50 |
sentinel = Path.cwd() / MARKER |
| 51 |
if sentinel.exists(): |
| 52 |
sentinel.unlink() |
| 53 |
|
| 54 |
modules = [] |
| 55 |
cases = [] |
| 56 |
for label, template in TEMPLATES.items(): |
| 57 |
for kind, user in (('bad', HOSTILE), ('ok', BENIGN)): |
| 58 |
mod = f'{label}_{kind}' |
| 59 |
dest = SCRATCHDIR / f'metachar-dest-{mod}' |
| 60 |
rmtree(dest) |
| 61 |
makepath(dest) |
| 62 |
out = SCRATCHDIR / f'metachar-out-{mod}' |
| 63 |
if out.exists(): |
| 64 |
out.unlink() |
| 65 |
secrets = SCRATCHDIR / f'metachar-{mod}.secrets' |
| 66 |
secrets.write_text(f'{user}:pw\n') |
| 67 |
secrets.chmod(0o600) |
| 68 |
modules.append((mod, { |
| 69 |
'path': str(dest), |
| 70 |
'read only': 'no', |
| 71 |
'use chroot': 'no', |
| 72 |
'auth users': '*', |
| 73 |
'secrets file': str(secrets), |
| 74 |
'pre-xfer exec': template.format(v='%RSYNC_USER_NAME%', out=out), |
| 75 |
})) |
| 76 |
cases.append((mod, kind, user, out)) |
| 77 |
|
| 78 |
pwfile = SCRATCHDIR / 'metachar.password' |
| 79 |
pwfile.write_text('pw\n') |
| 80 |
pwfile.chmod(0o600) |
| 81 |
|
| 82 |
conf = write_daemon_conf(modules, name='metachar.conf') |
| 83 |
url = start_test_daemon(conf, PORT) |
| 84 |
dlog = SCRATCHDIR / 'rsyncd.log' |
| 85 |
|
| 86 |
os.environ['RSYNC_PASSWORD'] = 'wrong-environment-fallback' |
| 87 |
for mod, kind, user, out in cases: |
| 88 |
before = len(dlog.read_text(errors='replace')) if dlog.exists() else 0 |
| 89 |
user_url = url.replace('rsync://', f'rsync://{user}@', 1) + mod + '/' |
| 90 |
proc = subprocess.run( |
| 91 |
rsync_argv('-r', f'--password-file={pwfile}', f'{src}/', user_url), |
| 92 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 93 |
log = (dlog.read_text(errors='replace')[before:]) if dlog.exists() else '' |
| 94 |
ctx = (f'[{mod}] rc={proc.returncode} stderr={proc.stderr.strip()[:200]!r} ' |
| 95 |
f'log={log[-300:]!r}') |
| 96 |
|
| 97 |
if kind == 'bad': |
| 98 |
if sentinel.exists(): |
| 99 |
sentinel.unlink() |
| 100 |
test_fail(f'peer value executed as shell syntax {ctx}') |
| 101 |
if proc.returncode == 0: |
| 102 |
test_fail(f'hostile value did not fail the transfer {ctx}') |
| 103 |
|
| 104 |
if 'shell metacharacter' not in log: |
| 105 |
test_fail(f'transfer failed but not via the metacharacter refusal {ctx}') |
| 106 |
else: |
| 107 |
if proc.returncode != 0: |
| 108 |
test_fail(f'refusal rule rejected an ordinary value {ctx}') |
| 109 |
if not out.exists(): |
| 110 |
test_fail(f'positive control failed: hook did not run {ctx}') |
| 111 |
got = out.read_text() |
| 112 |
if BENIGN not in got: |
| 113 |
test_fail(f'hook did not receive the value intact: {got!r} {ctx}') |
| 114 |
|
| 115 |
print('shell metacharacters in peer values are refused; ordinary values still run') |