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