| 1 |
|
| 2 |
"""RSYNC_CONNECT_PROG host quoting composes unsafely with single quotes. |
| 3 |
|
| 4 |
%H is substituted into a command string that a shell runs; a program of the |
| 5 |
form `sh -c '... %H ...'` re-parses the word in a NESTED shell, where the |
| 6 |
quoting rsync added has already been consumed by the outer one. A host of |
| 7 |
`localhost;touch$IFS<marker>;#` therefore executes in the inner shell. |
| 8 |
|
| 9 |
Asserting only that the marker is absent is not enough: it is equally absent |
| 10 |
when rsync failed to parse its arguments, when socketpair_tcp is blocked in the |
| 11 |
environment, or when `touch` could not be found. Each refusal below is checked |
| 12 |
by its specific message, and the accepted-host cases prove the allow-list has |
| 13 |
not simply refused everything -- which would satisfy an absence-only test |
| 14 |
perfectly. |
| 15 |
""" |
| 16 |
|
| 17 |
import os |
| 18 |
import shlex |
| 19 |
import subprocess |
| 20 |
|
| 21 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped |
| 22 |
|
| 23 |
REFUSAL = 'unsafe host characters for RSYNC_CONNECT_PROG' |
| 24 |
|
| 25 |
base = SCRATCHDIR / 'connect-prog-nested' |
| 26 |
rmtree(base) |
| 27 |
base.mkdir(parents=True) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
seen = base / 'seen' |
| 32 |
probe = base / 'probe.sh' |
| 33 |
|
| 34 |
|
| 35 |
probe.write_text(f'#!/bin/sh\nprintf %s "$1" > {shlex.quote(str(seen))}\n') |
| 36 |
probe.chmod(0o755) |
| 37 |
|
| 38 |
|
| 39 |
def run(host, prog): |
| 40 |
env = os.environ.copy() |
| 41 |
env['RSYNC_CONNECT_PROG'] = prog |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
return subprocess.run( |
| 48 |
rsync_argv(f'rsync://{host}/m/', str(base / 'out')), |
| 49 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env, |
| 50 |
cwd=str(base)) |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
marker = base / 'pwned' |
| 55 |
proc = run('localhost;touch${IFS}pwned;#', "sh -c 'printf %H >/dev/null'") |
| 56 |
if 'socketpair_tcp failed' in proc.stdout: |
| 57 |
test_skipped('RSYNC_CONNECT_PROG socketpair is blocked in this environment') |
| 58 |
if marker.exists(): |
| 59 |
test_fail('RSYNC_CONNECT_PROG host escaped the template quote context and ' |
| 60 |
'ran a command in the nested shell') |
| 61 |
if proc.returncode == 0: |
| 62 |
test_fail(f'the malicious host completed a transfer: {proc.stdout!r}') |
| 63 |
if REFUSAL not in proc.stdout: |
| 64 |
test_fail('the shell-active host was not refused for BEING shell-active; ' |
| 65 |
'something else stopped the transfer, so this check would pass ' |
| 66 |
f'with the guard removed: {proc.stdout!r}') |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
for host, why in (('-c', "looks like an option to the connect program"), |
| 72 |
('+x', "looks like an option too: `sh +x` is as real as `sh -x`"), |
| 73 |
('~root', "is tilde-expanded by the nested shell"), |
| 74 |
('%self', "is expanded by a nested fish, where %self is its pid"), |
| 75 |
('', "vanishes when a nested shell re-splits, shifting later args")): |
| 76 |
seen.unlink(missing_ok=True) |
| 77 |
proc = run(host, f'{probe} %H') |
| 78 |
if REFUSAL not in proc.stdout: |
| 79 |
got = seen.read_text() if seen.exists() else '(program not reached)' |
| 80 |
test_fail(f'host {host!r} was accepted, and it {why}; ' |
| 81 |
f'the connect program received {got!r}') |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
for host, expect in (('example.com', 'example.com'), |
| 88 |
('example.com.', 'example.com.'), |
| 89 |
('host_name', 'host_name'), |
| 90 |
('host+alias', 'host+alias'), |
| 91 |
('host~alias', 'host~alias'), |
| 92 |
('[fe80::1%eth0]', 'fe80::1%eth0')): |
| 93 |
seen.unlink(missing_ok=True) |
| 94 |
proc = run(host, f'{probe} %H') |
| 95 |
if REFUSAL in proc.stdout: |
| 96 |
test_fail(f'host {host!r} is legitimate but was refused: {proc.stdout!r}') |
| 97 |
if not seen.exists(): |
| 98 |
test_fail(f'host {host!r} never reached the connect program: {proc.stdout!r}') |
| 99 |
got = seen.read_text() |
| 100 |
if got != expect: |
| 101 |
test_fail(f'host {host!r} reached the connect program as {got!r}, ' |
| 102 |
f'expected {expect!r}') |
| 103 |
|
| 104 |
print('RSYNC_CONNECT_PROG refuses a shell-active host and an option- or ' |
| 105 |
'tilde-active first character, and still passes ordinary ones through') |