| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import os |
| 32 |
import shlex |
| 33 |
import subprocess |
| 34 |
|
| 35 |
from rsyncfns import ( |
| 36 |
RSYNC, RSYNC_PREFIX, SCRATCHDIR, patched_rrsync, rmtree, rsync_argv, |
| 37 |
test_fail, xattr_set, xattrs_supported, rsync_path_arg, |
| 38 |
) |
| 39 |
|
| 40 |
base = SCRATCHDIR / 'rrsync-specials' |
| 41 |
rmtree(base) |
| 42 |
src = base / 'src' |
| 43 |
src.mkdir(parents=True) |
| 44 |
os.mkfifo(src / 'evil_fifo') |
| 45 |
(src / 'ordinary').write_text('PLAIN\n') |
| 46 |
|
| 47 |
|
| 48 |
withd = base / 'with' |
| 49 |
withd.mkdir() |
| 50 |
subprocess.run(rsync_argv('-rlptgo', '--specials', f'{src}/', f'{withd}/'), |
| 51 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 52 |
if not (withd / 'evil_fifo').is_fifo(): |
| 53 |
test_fail("premise check failed: --specials should create the FIFO in the dest") |
| 54 |
without = base / 'without' |
| 55 |
without.mkdir() |
| 56 |
subprocess.run(rsync_argv('-rlptgo', f'{src}/', f'{without}/'), |
| 57 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 58 |
if (without / 'evil_fifo').exists(): |
| 59 |
test_fail("premise check failed: without --specials the FIFO should be skipped") |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
record = base / 'argv' |
| 68 |
stub = base / 'rsync-record' |
| 69 |
stub.write_text('#!/bin/sh\nprintf \'%s\\n\' "$@" >' + str(record) + '\n') |
| 70 |
stub.chmod(0o755) |
| 71 |
rrsync = patched_rrsync(base, rsync_path=str(stub)) |
| 72 |
restricted = base / 'restricted' |
| 73 |
restricted.mkdir(exist_ok=True) |
| 74 |
|
| 75 |
def forwarded_opts(cmd): |
| 76 |
if record.exists(): |
| 77 |
record.unlink() |
| 78 |
env = {**os.environ, 'SSH_ORIGINAL_COMMAND': cmd} |
| 79 |
r = subprocess.run([str(rrsync), str(restricted)], env=env, |
| 80 |
capture_output=True, text=True) |
| 81 |
if r.returncode != 0: |
| 82 |
test_fail(f"restricted rrsync rejected `{cmd}` instead of stripping " |
| 83 |
f"the option (exit {r.returncode}): {r.stderr.strip()}") |
| 84 |
return record.read_text().split('\n') if record.exists() else [] |
| 85 |
|
| 86 |
|
| 87 |
for opt in ('-D', '--specials'): |
| 88 |
|
| 89 |
forwarded = forwarded_opts(f'rsync --server {opt} . .') |
| 90 |
if '--drop-D' not in forwarded: |
| 91 |
test_fail(f"restricted rrsync did not force --drop-D for `{opt}` on " |
| 92 |
f"the receiving side, where creation happens: {forwarded}") |
| 93 |
|
| 94 |
|
| 95 |
if opt not in forwarded: |
| 96 |
test_fail(f"restricted rrsync dropped `{opt}` instead of leaving it " |
| 97 |
f"alongside --drop-D: {forwarded}") |
| 98 |
if '--no-D' in forwarded: |
| 99 |
test_fail(f"restricted rrsync forced --no-D for `{opt}`. That clears " |
| 100 |
'preserve_devices/preserve_specials, which also frame the ' |
| 101 |
f'rdev fields, so the file list desynchronises: {forwarded}') |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
served = base / 'served' |
| 107 |
served.mkdir() |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
pushwrap = base / 'pushwrap' |
| 114 |
pushwrap.mkdir() |
| 115 |
shim = base / 'rsync-shim' |
| 116 |
shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n') |
| 117 |
shim.chmod(0o755) |
| 118 |
rrsync_push = patched_rrsync(pushwrap, rsync_path=str(shim)) |
| 119 |
rsh = base / 'fake-rsh' |
| 120 |
rsh.write_text( |
| 121 |
'#!/bin/sh\n' |
| 122 |
'shift\n' |
| 123 |
'SSH_ORIGINAL_COMMAND="$*"\n' |
| 124 |
'export SSH_ORIGINAL_COMMAND\n' |
| 125 |
'exec %s %s\n' % (shlex.quote(str(rrsync_push)), shlex.quote(str(served)))) |
| 126 |
rsh.chmod(0o755) |
| 127 |
|
| 128 |
proc = subprocess.run( |
| 129 |
rsync_argv('-rlptgoD', '--specials', '-e', str(rsh), f'{src}/', 'dummy:.'), |
| 130 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120) |
| 131 |
ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:300]!r}' |
| 132 |
|
| 133 |
|
| 134 |
landed = served / 'ordinary' |
| 135 |
if not landed.is_file() or landed.read_text() != 'PLAIN\n': |
| 136 |
test_fail(f'control failed: the push did not deliver an ordinary file, so ' |
| 137 |
f'the absence of the FIFO proves nothing ({ctx})') |
| 138 |
if (served / 'evil_fifo').exists(): |
| 139 |
test_fail(f'a push through a restricted rrsync created a special file in ' |
| 140 |
f'the served tree ({ctx})') |
| 141 |
|
| 142 |
|
| 143 |
if proc.returncode != 0: |
| 144 |
test_fail(f'the FIFO was denied, but the transfer itself failed ({ctx})') |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if xattrs_supported(): |
| 159 |
devsrc = base / 'devsrc' |
| 160 |
devsrc.mkdir() |
| 161 |
(devsrc / 'ordinary').write_text('PLAIN\n') |
| 162 |
(devsrc / 'achar').write_bytes(b'') |
| 163 |
try: |
| 164 |
xattr_set(f'{RSYNC_PREFIX}.%stat', '20644 1,3 0:0', devsrc / 'achar') |
| 165 |
made_dev = True |
| 166 |
except Exception: |
| 167 |
made_dev = False |
| 168 |
|
| 169 |
if made_dev: |
| 170 |
devserved = base / 'devserved' |
| 171 |
devserved.mkdir() |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
devwrap = base / 'devwrap' |
| 176 |
devwrap.mkdir() |
| 177 |
devshim = base / 'rsync-shim-fake' |
| 178 |
devshim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' --fake-super "$@"\n') |
| 179 |
devshim.chmod(0o755) |
| 180 |
rrsync_dev = patched_rrsync(devwrap, rsync_path=str(devshim)) |
| 181 |
devrsh = base / 'fake-rsh-dev' |
| 182 |
devrsh.write_text( |
| 183 |
'#!/bin/sh\n' |
| 184 |
'shift\n' |
| 185 |
'SSH_ORIGINAL_COMMAND="$*"\n' |
| 186 |
'export SSH_ORIGINAL_COMMAND\n' |
| 187 |
'exec %s %s\n' % (shlex.quote(str(rrsync_dev)), |
| 188 |
shlex.quote(str(devserved)))) |
| 189 |
devrsh.chmod(0o755) |
| 190 |
proc = subprocess.run( |
| 191 |
rsync_argv('--fake-super', '-rlptgoD', '--specials', '-e', |
| 192 |
str(devrsh), f'{devsrc}/', 'dummy:.'), |
| 193 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 194 |
timeout=120) |
| 195 |
ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:300]!r}' |
| 196 |
landed = devserved / 'ordinary' |
| 197 |
if not landed.is_file() or landed.read_text() != 'PLAIN\n': |
| 198 |
test_fail(f'control failed: pushing a tree containing a device did ' |
| 199 |
f'not deliver the ordinary file beside it, so nothing ' |
| 200 |
f'below proves anything. Clearing preserve_devices on ' |
| 201 |
f'the receiver alone desynchronises the file list at ' |
| 202 |
f'EVERY protocol, which fails the whole transfer ({ctx})') |
| 203 |
if os.path.lexists(devserved / 'achar'): |
| 204 |
test_fail(f'a push through a restricted rrsync recorded a device in ' |
| 205 |
f'the served tree ({ctx})') |
| 206 |
if proc.returncode != 0: |
| 207 |
test_fail(f'the device was denied, but the transfer itself failed ' |
| 208 |
f'({ctx})') |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
for opt in ('-D', '--specials'): |
| 214 |
forwarded = forwarded_opts(f'rsync --server --sender {opt} . .') |
| 215 |
for forced in ('--drop-D', '--no-D'): |
| 216 |
if forced in forwarded: |
| 217 |
test_fail(f"restricted rrsync forced {forced} for `{opt}` on the " |
| 218 |
'SENDING side, where there is nothing to deny: a sender ' |
| 219 |
'creates no device or special entry in the served tree ' |
| 220 |
f'({forwarded})') |
| 221 |
|
| 222 |
print("rrsync-specials: --specials creates special files in the served tree; " |
| 223 |
"a restricted rrsync forces --drop-D on the receiving side (a push can " |
| 224 |
"create neither a special nor a device, and the transfer still " |
| 225 |
"succeeds) and leaves the sending side alone") |