Debian Codegraph @

testsuite/checksum-zero-blocklen_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A malicious RECEIVER (its generator) can put a count>0 / blength=0 checksum
3 # header on the receiver->sender path; the sender's read_sum_head() (io.c) then
4 # computes with a zero block length. read_sum_head() guards this with
5 # "Invalid zero block length" before the bad value is used.
6 #
7 # Unlike the other rsync_proto tests, the malicious bytes here flow the *other*
8 # way -- from the receiver/generator to the sender -- so we drive it with the
9 # SERVER side of rsync_proto (DaemonReceiver): stand up a tiny Python "daemon"
10 # that a real rsync client PUSHES to (client = sender, us = receiver/generator),
11 # run the handshake, and -- as the generator -- send a transfer request for the
12 # pushed file with a sum header of count=1, blength=0. The two stream directions
13 # are independent, so we never parse the client's file list; we just request
14 # index 0 (the single pushed file) and let the client's send_files() read it.
15 #
16 # Oracle: the real sender exits non-zero with "Invalid zero block length". Needs
17 # a real TCP daemon endpoint (raw socket), so it is skipped without --use-tcp.
18
19 import socket
20 import subprocess
21 import threading
22
23 from rsyncfns import (
24 SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, rsync_argv, test_fail,
25 )
26 import rsync_proto as rp
27
28 PORT = 12972
29 require_tcp("the pure-Python receiver needs a real TCP socket; run with --use-tcp")
30 claim_ports(PORT)
31
32 base = SCRATCHDIR / 'checksum-zero-blocklen'
33 rmtree(base)
34 src = base / 'src'
35 makepath(src)
36 (src / 'f').write_text('new data\n' * 4096)
37
38 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
39 lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
40 lsock.bind(('127.0.0.1', PORT))
41 lsock.listen(1)
42 lsock.settimeout(30)
43
44 state = {}
45
46
47 def serve():
48 """Accept the pushing client and, as the generator, send a zero-blocklen
49 sum header for the single pushed file (index 0)."""
50 try:
51 csock, _ = lsock.accept()
52 r = rp.DaemonReceiver(csock)
53 r.handshake()
54 r.send_sum_request(0, count=1, blength=0, s2length=0, remainder=0)
55 r.drain()
56 r.close()
57 except Exception as exc: # noqa: BLE001 - reported below
58 state['err'] = repr(exc)
59
60
61 t = threading.Thread(target=serve, daemon=True)
62 t.start()
63 try:
64 proc = subprocess.run(
65 rsync_argv('--no-whole-file', f'{src}/f', f'rsync://127.0.0.1:{PORT}/mod/'),
66 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
67 finally:
68 t.join(timeout=10)
69 lsock.close()
70
71 out = (proc.stdout or '') + (proc.stderr or '')
72 if state.get('err'):
73 test_fail(f"the Python receiver failed before/while feeding the sender: {state['err']}\n"
74 f"sender output:\n{out}")
75 if proc.returncode == 0:
76 test_fail("malicious receiver sent a count>0/blength=0 checksum header "
77 f"but the sender completed successfully:\n{out}")
78 if 'Invalid zero block length' not in out:
79 test_fail("sender rejected the malicious receiver, but not via the zero-block "
80 f"checksum guard. Output:\n{out}")
81
82 print("checksum-zero-blocklen: sender rejects count>0 checksum headers with blength=0")