Debian Codegraph @

testsuite/malicious-dot-file-delete-scope_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A malicious sender must not reinterpret the destination root as a file.
3
4 The receiver permits the synthetic "." entry as a top-level directory even
5 when an implied-file filter restricts the requested source. This server sends
6 that special name with regular-file mode. Under --force, replacing it must not
7 recursively empty the receiver's destination root. No --delete is requested.
8 """
9
10 import socket
11 import subprocess
12 import threading
13
14 from rsyncfns import (
15 SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, rsync_argv,
16 test_fail,
17 )
18 import rsync_proto as rp
19
20 PORT = 13012
21
22 require_tcp("the malicious sender needs a real TCP socket; run with --use-tcp")
23 claim_ports(PORT)
24
25 base = SCRATCHDIR / 'malicious-dot-file-delete-scope'
26 rmtree(base)
27 dest = base / 'dest'
28 makepath(dest / 'private')
29 sentinel = dest / 'private' / 'must-survive'
30 sentinel.write_text('receiver-owned file outside the requested leaf\n')
31
32 # Native control: the same scoped single-file transfer with --force does not
33 # touch receiver-owned siblings when the sender encodes the requested leaf
34 # honestly. This distinguishes the attack from documented --force semantics.
35 honest_src = base / 'honest-src'
36 makepath(honest_src)
37 (honest_src / 'requested').write_text('honest source\n')
38 control = subprocess.run(
39 rsync_argv('-r', '--force', '--no-inc-recursive',
40 str(honest_src / 'requested'), str(dest) + '/'),
41 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
42 if control.returncode or not sentinel.exists():
43 test_fail('native --force single-file control unexpectedly touched the '
44 f'destination scope:\n{control.stdout}{control.stderr}')
45
46 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47 lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
48 lsock.bind(('127.0.0.1', PORT))
49 lsock.listen(1)
50 lsock.settimeout(30)
51 state = {}
52
53
54 def serve():
55 try:
56 csock, _ = lsock.accept()
57 s = rp.DaemonReceiver(csock)
58 s.handshake()
59
60 # "." is the receiver's synthetic transfer-root entry and is exempted
61 # from its requested-name filter. Forge it as a regular file instead
62 # of the only legitimate representation: a directory.
63 s.send_data(
64 rp.FileEntry('.', mode=rp.S_IFREG | 0o644, length=0).encode()
65 + rp.end_of_flist(0, s.protocol)
66 + s.w_ndx(rp.NDX_DONE) * 3
67 )
68 s.drain(timeout=5)
69 s.close()
70 except Exception as exc: # noqa: BLE001 - reported below
71 state['err'] = repr(exc)
72
73
74 t = threading.Thread(target=serve, daemon=True)
75 t.start()
76 try:
77 proc = subprocess.run(
78 rsync_argv('-r', '--force', '--no-inc-recursive',
79 f'rsync://127.0.0.1:{PORT}/mod/requested', str(dest) + '/'),
80 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
81 finally:
82 t.join(timeout=10)
83 lsock.close()
84
85 if not sentinel.exists():
86 test_fail(
87 'malicious sender encoded the special "." root as a regular file and '
88 'made --force recursively erase the destination root')
89 if state.get('err'):
90 test_fail(f"Python sender failed before exercising receiver: {state['err']}\n"
91 f"receiver output:\n{proc.stdout}{proc.stderr}")
92
93 print('malicious-dot-file-delete-scope: receiver rejected a non-directory '
94 'encoding of its synthetic transfer-root entry')