| 1 |
|
| 2 |
"""Protocol 29 must not let a sender enlarge scoped --delete. |
| 3 |
|
| 4 |
The protocol-30 receiver downgrades an implied parent that a malicious sender |
| 5 |
marks as a content directory. The legacy branch has no XMIT_NO_CONTENT_DIR |
| 6 |
bit and currently trusts XMIT_TOP_DIR, even though the receiver's implied |
| 7 |
filter says that the directory is only a parent of the requested leaf. |
| 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 |
|
| 21 |
PORT = 13303 |
| 22 |
|
| 23 |
require_tcp('the malicious protocol-29 server needs TCP; run with --use-tcp') |
| 24 |
claim_ports(PORT) |
| 25 |
|
| 26 |
base = SCRATCHDIR / 'peer-legacy-implied-delete-scope' |
| 27 |
rmtree(base) |
| 28 |
makepath(base) |
| 29 |
|
| 30 |
|
| 31 |
def pull(label, forged_top_dir): |
| 32 |
dest = base / label |
| 33 |
makepath(dest / 'dir') |
| 34 |
sentinel = dest / 'dir' / 'must-survive' |
| 35 |
sentinel.write_text('receiver-owned sibling outside requested leaf\n') |
| 36 |
|
| 37 |
listener = socket.socket() |
| 38 |
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 39 |
listener.bind(('127.0.0.1', PORT)) |
| 40 |
listener.listen(1) |
| 41 |
listener.settimeout(30) |
| 42 |
state = {} |
| 43 |
|
| 44 |
def serve(): |
| 45 |
try: |
| 46 |
sock, _ = listener.accept() |
| 47 |
sock.settimeout(15) |
| 48 |
peer = rp.DaemonReceiver( |
| 49 |
sock, greeting_version=29, protocol=29, |
| 50 |
) |
| 51 |
peer.handshake(seed=0x12345678) |
| 52 |
|
| 53 |
|
| 54 |
while peer._readline(): |
| 55 |
pass |
| 56 |
|
| 57 |
parent_flags = rp.XMIT_TOP_DIR if forged_top_dir else 0 |
| 58 |
flist = ( |
| 59 |
rp.FileEntry( |
| 60 |
'dir', mode=rp.S_IFDIR | 0o755, |
| 61 |
extra_flags=parent_flags, protocol=29, |
| 62 |
).encode() |
| 63 |
+ rp.FileEntry( |
| 64 |
'dir/file', mode=rp.S_IFREG | 0o644, |
| 65 |
length=0, protocol=29, |
| 66 |
).encode() |
| 67 |
+ rp.end_of_flist(0, 29) |
| 68 |
+ rp.w_int(0) |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
peer.send_data(flist + rp.w_int(rp.NDX_DONE) * 4) |
| 74 |
peer.drain(timeout=3) |
| 75 |
peer.close() |
| 76 |
except Exception as exc: |
| 77 |
state['err'] = repr(exc) |
| 78 |
|
| 79 |
thread = threading.Thread(target=serve, daemon=True) |
| 80 |
thread.start() |
| 81 |
try: |
| 82 |
proc = subprocess.run( |
| 83 |
rsync_argv( |
| 84 |
'-rR', '--delete', |
| 85 |
f'rsync://127.0.0.1:{PORT}/mod/dir/file', str(dest) + '/', |
| 86 |
), |
| 87 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, |
| 88 |
timeout=20, |
| 89 |
) |
| 90 |
finally: |
| 91 |
thread.join(timeout=8) |
| 92 |
listener.close() |
| 93 |
return sentinel.exists(), proc, state |
| 94 |
|
| 95 |
|
| 96 |
control_survived, control_proc, control_state = pull('control', False) |
| 97 |
attack_survived, attack_proc, attack_state = pull('attack', True) |
| 98 |
|
| 99 |
if not control_survived: |
| 100 |
test_fail( |
| 101 |
'protocol-29 control without XMIT_TOP_DIR deleted the sibling: ' |
| 102 |
f'rc={control_proc.returncode}, server={control_state!r}, ' |
| 103 |
f'output={control_proc.stdout}{control_proc.stderr}') |
| 104 |
|
| 105 |
if not attack_survived: |
| 106 |
test_fail( |
| 107 |
'malicious protocol-29 sender marked implied parent dir as ' |
| 108 |
'XMIT_TOP_DIR and expanded scoped --delete from dir/file to dir/, ' |
| 109 |
'deleting receiver-owned must-survive; the protocol-30 implied-parent ' |
| 110 |
'downgrade was not applied to the legacy branch; ' |
| 111 |
f'control rc={control_proc.returncode}, attack rc={attack_proc.returncode}, ' |
| 112 |
f'control server={control_state!r}, attack server={attack_state!r}, ' |
| 113 |
f'attack output={attack_proc.stdout}{attack_proc.stderr}') |
| 114 |
|
| 115 |
if attack_state.get('err') and control_state.get('err') != attack_state.get('err'): |
| 116 |
test_fail( |
| 117 |
f'attack server failed differently from control: attack={attack_state!r}, ' |
| 118 |
f'control={control_state!r}, rc={attack_proc.returncode}, ' |
| 119 |
f'output={attack_proc.stdout}{attack_proc.stderr}') |
| 120 |
|
| 121 |
print('peer-legacy-implied-delete-scope: protocol-29 implied parent remained ' |
| 122 |
'non-content under forged XMIT_TOP_DIR') |