Debian Codegraph @

testsuite/xattr-wire-cap_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A malicious DAEMON acting as the SENDER can put an oversized per-value xattr
3 # datum_len in the file list it streams to a pulling client. receive_xattr()
4 # (xattrs.c) guards this: read_varint_size() bounds it to MAX_WIRE_XATTR_DATALEN
5 # and a second check rejects anything over MAX_XATTR_VALUE_BYTES (128 MiB) with
6 # "xattr datum_len exceeds per-value limit", before the value is read.
7 #
8 # The malicious bytes flow daemon(sender) -> client(receiver) in the FILE-LIST
9 # phase (send_xattr() is appended to each entry under -X), so we drive it with
10 # the SERVER side of rsync_proto (DaemonReceiver acting as the sender): stand up
11 # a tiny Python "daemon" that a real rsync client PULLS from, run the handshake,
12 # and stream a one-entry file list whose xattr block declares datum_len =
13 # 128 MiB + 1. receive_xattr() runs during recv_file_list(), so no transfer
14 # phase is needed, and because the receiver rejects at the datum_len check we
15 # needn't send the value itself.
16 #
17 # Oracle: the real receiver exits non-zero with "xattr datum_len exceeds
18 # per-value limit". Needs xattr support and a real TCP socket (--use-tcp).
19
20 import socket
21 import subprocess
22 import threading
23
24 from rsyncfns import (
25 SCRATCHDIR, claim_ports, forced_protocol, makepath, require_tcp, rmtree,
26 rsync_argv, test_fail, test_skipped, xattrs_supported,
27 )
28 import rsync_proto as rp
29
30 if not xattrs_supported():
31 test_skipped("xattr-wire-cap requires rsync and filesystem xattr support")
32 proto = forced_protocol()
33 if proto is not None and proto < 30:
34 test_skipped(f"xattr-wire-cap requires protocol 30+ (xattrs unsupported at negotiated {proto})")
35
36 PORT = 12970
37 require_tcp("the pure-Python daemon needs a real TCP socket; run with --use-tcp")
38 claim_ports(PORT)
39
40 base = SCRATCHDIR / 'xattr-wire-cap'
41 rmtree(base)
42 dest = base / 'dest'
43 makepath(dest)
44
45 # send_xattr() bytes for a new list with one xattr whose declared value length
46 # is 128 MiB + 1 -- it clears the wire cap (read_varint_size, < 2 GiB) but trips
47 # the per-value cap (MAX_XATTR_VALUE_BYTES = 128 MiB). No value follows: the
48 # receiver rejects before reading it.
49 xattr = rp.xattr_list_wire([(b'user.wirecap\0', 128 * 1024 * 1024 + 1, b'')])
50
51 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52 lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
53 lsock.bind(('127.0.0.1', PORT))
54 lsock.listen(1)
55 lsock.settimeout(30)
56
57 state = {}
58
59
60 def serve():
61 """Accept the pulling client and, as the sender, stream a one-entry file
62 list whose xattr block carries the oversized datum length."""
63 try:
64 csock, _ = lsock.accept()
65 r = rp.DaemonReceiver(csock)
66 r.handshake()
67 flist = (rp.FileEntry('f', mode=rp.S_IFREG | 0o644, length=8).encode()
68 + xattr + rp.end_of_flist(0, r.protocol))
69 r.send_data(flist)
70 r.drain()
71 r.close()
72 except Exception as exc: # noqa: BLE001 - reported below
73 state['err'] = repr(exc)
74
75
76 t = threading.Thread(target=serve, daemon=True)
77 t.start()
78 try:
79 proc = subprocess.run(
80 rsync_argv('-aX', f'rsync://127.0.0.1:{PORT}/x/f', str(dest) + '/'),
81 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
82 finally:
83 t.join(timeout=10)
84 lsock.close()
85
86 out = (proc.stdout or '') + (proc.stderr or '')
87 if state.get('err'):
88 test_fail(f"the Python daemon failed before/while streaming the file list: {state['err']}\n"
89 f"receiver output:\n{out}")
90 if proc.returncode == 0:
91 test_fail("malicious daemon sender advertised an oversized xattr datum length "
92 f"but the receiver completed successfully:\n{out}")
93 if 'xattr datum_len exceeds per-value limit' not in out:
94 test_fail("receiver rejected the malicious xattr stream, but not via the per-value "
95 f"wire cap. Output:\n{out}")
96
97 print("xattr-wire-cap: receiver rejects oversized peer-supplied xattr datum lengths")