| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import glob |
| 20 |
import os |
| 21 |
import socket |
| 22 |
import struct |
| 23 |
import time |
| 24 |
|
| 25 |
from rsyncfns import ( |
| 26 |
SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail, |
| 27 |
) |
| 28 |
import rsync_proto as rp |
| 29 |
|
| 30 |
PORT = 12958 |
| 31 |
require_tcp("the pure-Python client needs a real TCP daemon; run with --use-tcp") |
| 32 |
require_asan("the read_a_msg MSG_NO_SEND recursion is observed via the daemon's " |
| 33 |
"AddressSanitizer stack-overflow report") |
| 34 |
claim_ports(PORT) |
| 35 |
|
| 36 |
mod = SCRATCHDIR / 'nosend-mod' |
| 37 |
mod.mkdir(parents=True, exist_ok=True) |
| 38 |
(mod / 'f').write_text("hello\n") |
| 39 |
|
| 40 |
conf = SCRATCHDIR / 'nosend.conf' |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
conf.write_text(f"""\ |
| 45 |
pid file = {SCRATCHDIR}/nosend-rsyncd.pid |
| 46 |
use chroot = no |
| 47 |
socket options = SO_SNDBUF=2048 |
| 48 |
|
| 49 |
[mod] |
| 50 |
path = {mod} |
| 51 |
read only = no |
| 52 |
""") |
| 53 |
|
| 54 |
asan_log = SCRATCHDIR / 'nosend-asan' |
| 55 |
for stale in glob.glob(f"{asan_log}.*"): |
| 56 |
os.unlink(stale) |
| 57 |
prev = os.environ.get('ASAN_OPTIONS', '') |
| 58 |
os.environ['ASAN_OPTIONS'] = ( |
| 59 |
(prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}') |
| 60 |
|
| 61 |
start_rsyncd(conf, PORT) |
| 62 |
|
| 63 |
c = rp.DaemonClient('127.0.0.1', PORT) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
c.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2048) |
| 69 |
|
| 70 |
|
| 71 |
c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '.', 'mod/'], |
| 72 |
greeting_version=30) |
| 73 |
|
| 74 |
NOSEND = struct.pack('<I', ((rp.MPLEX_BASE + 102) << 24) | 4) + b'\x00\x00\x00\x00' |
| 75 |
try: |
| 76 |
c._send_raw(NOSEND * 60000) |
| 77 |
except OSError: |
| 78 |
pass |
| 79 |
try: |
| 80 |
c.drain(timeout=5.0) |
| 81 |
except OSError: |
| 82 |
pass |
| 83 |
c.close() |
| 84 |
|
| 85 |
reports = '' |
| 86 |
for _ in range(30): |
| 87 |
reports = ''.join(open(r, errors='replace').read() |
| 88 |
for r in glob.glob(f"{asan_log}.*")) |
| 89 |
if 'AddressSanitizer' in reports: |
| 90 |
break |
| 91 |
time.sleep(0.1) |
| 92 |
|
| 93 |
if 'AddressSanitizer' in reports: |
| 94 |
test_fail( |
| 95 |
"daemon sender recursed through read_a_msg on a MSG_NO_SEND flood and hit " |
| 96 |
"an AddressSanitizer stack-overflow -- the MSG_NO_SEND handler re-armed " |
| 97 |
"in_multiplexed before the send_msg_int flush:\n" + reports[:1500]) |
| 98 |
|
| 99 |
print("io-nosend-flood-recursion: a MSG_NO_SEND flood does not re-enter " |
| 100 |
"read_a_msg (no stack exhaustion).") |