| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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: |
| 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") |