| 1 |
|
| 2 |
"""Daemon-mode: the server must reject a wire-supplied --max-alloc=0. |
| 3 |
|
| 4 |
max-alloc-zero-rejected_test.py only proves the *local* client refuses |
| 5 |
--max-alloc=0. That alone doesn't protect a daemon: a modified or older client |
| 6 |
still forwards --max-alloc=0 on the wire, and an unpatched daemon honours it and |
| 7 |
disables its my_alloc() allocation cap (the defence behind CVE-2024-12084 and |
| 8 |
friends). This test drives an older rsync client -- which lacks the reject-zero |
| 9 |
check and so forwards the option -- against the current rsync daemon, and |
| 10 |
asserts the *daemon* refuses it. |
| 11 |
|
| 12 |
It uses the in-tree old_versions/rsync_3.2.7 as the client (3.2.7 predates the |
| 13 |
reject-zero fix, so it forwards --max-alloc=0 on the wire). If that binary is |
| 14 |
missing or can't run here (e.g. a non-Linux host that can't run the static |
| 15 |
archive) the test skips. |
| 16 |
""" |
| 17 |
|
| 18 |
import subprocess |
| 19 |
from pathlib import Path |
| 20 |
|
| 21 |
from rsyncfns import ( |
| 22 |
FROMDIR, RSYNC, SCRATCHDIR, |
| 23 |
makepath, rmtree, start_test_daemon, test_fail, test_skipped, |
| 24 |
write_daemon_conf, |
| 25 |
) |
| 26 |
|
| 27 |
DAEMON_PORT = 12932 |
| 28 |
REJECT_MSG = 'max-alloc must be greater than zero' |
| 29 |
|
| 30 |
OLD_CLIENT = Path(__file__).resolve().parents[1] / 'old_versions' / 'rsync_3.2.7' |
| 31 |
|
| 32 |
if not OLD_CLIENT.exists(): |
| 33 |
test_skipped(f"{OLD_CLIENT} not present") |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
try: |
| 40 |
probe = subprocess.run([str(OLD_CLIENT), '--version'], |
| 41 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 42 |
text=True) |
| 43 |
except OSError as e: |
| 44 |
test_skipped(f"cannot run {OLD_CLIENT.name} on this OS/arch: {e}") |
| 45 |
if probe.returncode != 0 or 'version 3.2.7' not in probe.stdout: |
| 46 |
test_skipped(f"{OLD_CLIENT.name} does not run as rsync on this OS/arch") |
| 47 |
|
| 48 |
|
| 49 |
src = FROMDIR |
| 50 |
rmtree(src) |
| 51 |
makepath(src) |
| 52 |
(src / 'file.txt').write_text('hello\n') |
| 53 |
|
| 54 |
conf = write_daemon_conf([('mod', {'path': str(src), 'read only': 'yes'})]) |
| 55 |
url = start_test_daemon(conf, DAEMON_PORT, rsync_cmd=RSYNC) |
| 56 |
|
| 57 |
dest = SCRATCHDIR / 'out.txt' |
| 58 |
|
| 59 |
|
| 60 |
def run_client(*extra): |
| 61 |
argv = [str(OLD_CLIENT), *extra, f'{url}mod/file.txt', str(dest)] |
| 62 |
return subprocess.run(argv, stdout=subprocess.DEVNULL, |
| 63 |
stderr=subprocess.PIPE, text=True) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
dest.unlink(missing_ok=True) |
| 69 |
ctrl = run_client() |
| 70 |
if ctrl.returncode != 0: |
| 71 |
test_fail(f"old client could not talk to the current daemon:\n{ctrl.stderr}") |
| 72 |
|
| 73 |
|
| 74 |
dest.unlink(missing_ok=True) |
| 75 |
proc = run_client('--max-alloc=0') |
| 76 |
if proc.returncode == 0: |
| 77 |
test_fail("daemon accepted a wire-supplied --max-alloc=0") |
| 78 |
if REJECT_MSG not in proc.stderr: |
| 79 |
test_fail("daemon did not reject --max-alloc=0 with the expected message; " |
| 80 |
f"stderr:\n{proc.stderr}") |
| 81 |
|
| 82 |
print("daemon-max-alloc-zero: daemon refuses a wire-supplied --max-alloc=0 " |
| 83 |
f"(client {OLD_CLIENT.name})") |