| 1 |
|
| 2 |
"""A peer value with no EXECUTING character still selects a command. |
| 3 |
|
| 4 |
`pre-xfer exec = sh -c '.../%RSYNC_USER_NAME% ...'` re-parses the substituted |
| 5 |
word in a SECOND shell. rsync escapes for the quoting context it sees, which is |
| 6 |
correct for the outer shell only; the inner one gets the value bare. A username |
| 7 |
of `touc?` carries nothing that can execute in one level of parsing -- which |
| 8 |
is why the original character set let it through -- yet the inner shell |
| 9 |
glob-expands `<dir>/touc?` to `<dir>/touch` and runs it. ('?' is refused now, |
| 10 |
which is what this test pins; it was not when the hole was found.) The substitution has stopped being data and has chosen the command. |
| 11 |
|
| 12 |
Two things this test has to avoid. |
| 13 |
|
| 14 |
It must not pass merely because nothing happened: an absent marker plus a |
| 15 |
non-zero exit is equally true when authentication failed, when the daemon could |
| 16 |
not start, or when the globbed command was missing. So the daemon log is |
| 17 |
required to carry the specific refusal. |
| 18 |
|
| 19 |
And it must not pass because the attack was inert here. The control below runs |
| 20 |
the same nested-shell expansion directly and requires it to work, so a pass |
| 21 |
means rsync refused a live attack rather than that the mechanism never fired in |
| 22 |
this environment. The command it globs onto is one this test creates, rather |
| 23 |
than /usr/bin/touch -- a hardcoded system path would make the whole thing hinge |
| 24 |
on a binary the build never guaranteed. |
| 25 |
""" |
| 26 |
|
| 27 |
import os |
| 28 |
import subprocess |
| 29 |
|
| 30 |
from rsyncfns import ( |
| 31 |
SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, test_fail, |
| 32 |
write_daemon_conf, |
| 33 |
) |
| 34 |
|
| 35 |
REFUSAL = 'refusing to run shell hook: %RSYNC_USER_NAME% holds a shell metacharacter' |
| 36 |
|
| 37 |
base = SCRATCHDIR / 'exec-second-shell-argv' |
| 38 |
rmtree(base) |
| 39 |
module = base / 'module' |
| 40 |
makepath(module) |
| 41 |
sentinel = module / 'pwned' |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
bindir = base / 'bin' |
| 47 |
makepath(bindir) |
| 48 |
victim = bindir / 'touch' |
| 49 |
victim.write_text('#!/bin/sh\n: > "$1"\n') |
| 50 |
victim.chmod(0o755) |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
user = 'touc?' |
| 56 |
password = 'known-password' |
| 57 |
secrets = base / 'secrets' |
| 58 |
secrets.write_text(f'{user}:{password}\n') |
| 59 |
secrets.chmod(0o600) |
| 60 |
pwfile = base / 'password' |
| 61 |
pwfile.write_text(password + '\n') |
| 62 |
pwfile.chmod(0o600) |
| 63 |
|
| 64 |
|
| 65 |
control = base / 'control-marker' |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
outer = os.environ.get('RSYNC_SHELL') or '/bin/sh' |
| 70 |
subprocess.run([outer, '-c', f"sh -c '{bindir}/touc? {control}'"], |
| 71 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 72 |
if not control.exists(): |
| 73 |
test_fail(f'the nested-shell glob did not select {victim} in this ' |
| 74 |
'environment, so this test cannot show rsync refusing it; the ' |
| 75 |
'check below would pass no matter what rsync did') |
| 76 |
|
| 77 |
|
| 78 |
conf = write_daemon_conf([ |
| 79 |
('m', { |
| 80 |
'path': str(module), 'read only': 'no', 'auth users': '*', |
| 81 |
'secrets file': str(secrets), |
| 82 |
'pre-xfer exec': f"sh -c '{bindir}/%RSYNC_USER_NAME% {sentinel}'", |
| 83 |
}), |
| 84 |
]) |
| 85 |
url = start_test_daemon(conf, 12983).replace('rsync://', f'rsync://{user}@', 1) + 'm/' |
| 86 |
proc = subprocess.run( |
| 87 |
rsync_argv('-r', f'--password-file={pwfile}', f'{module}/', url), |
| 88 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 89 |
|
| 90 |
if sentinel.exists(): |
| 91 |
test_fail('the nested-shell peer value selected and ran a command: ' |
| 92 |
f'{sentinel} was created') |
| 93 |
if proc.returncode == 0: |
| 94 |
test_fail(f'the daemon accepted a shell-active peer value: {proc.stdout!r}') |
| 95 |
|
| 96 |
log = SCRATCHDIR / 'rsyncd.log' |
| 97 |
log_text = log.read_text(errors='replace') if log.exists() else '' |
| 98 |
if REFUSAL not in log_text: |
| 99 |
test_fail('the transfer failed, but not because the value was refused for ' |
| 100 |
'holding a shell metacharacter -- an authentication or startup ' |
| 101 |
'failure would look exactly like this, and would pass without ' |
| 102 |
f'the guard: client={proc.stdout.strip()[:200]!r}') |
| 103 |
|
| 104 |
print('the daemon refused a peer value that a nested shell would have turned ' |
| 105 |
'into a command') |