| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import glob |
| 22 |
import os |
| 23 |
import subprocess |
| 24 |
|
| 25 |
from rsyncfns import ( |
| 26 |
SCRATCHDIR, claim_ports, makepath, require_asan, require_tcp, rsync_argv, |
| 27 |
start_rsyncd, test_fail, |
| 28 |
) |
| 29 |
|
| 30 |
PORT = 12954 |
| 31 |
require_tcp("a real network rsync daemon is needed (am_server==0 per-module); " |
| 32 |
"run with --use-tcp") |
| 33 |
require_asan("the 1-byte heap overflow in add_implied_include is only observable " |
| 34 |
"under AddressSanitizer") |
| 35 |
claim_ports(PORT) |
| 36 |
|
| 37 |
base = SCRATCHDIR / 'implied-bslash' |
| 38 |
mod = base / 'mod' |
| 39 |
dest = base / 'dest' |
| 40 |
makepath(mod, dest) |
| 41 |
(mod / 'file.txt').write_text("hello\n") |
| 42 |
|
| 43 |
|
| 44 |
ff = base / 'files-from.txt' |
| 45 |
ff.write_text('a\\b\\\n') |
| 46 |
|
| 47 |
conf = base / 'implied.conf' |
| 48 |
conf.write_text(f"""\ |
| 49 |
pid file = {SCRATCHDIR}/implied-rsyncd.pid |
| 50 |
use chroot = no |
| 51 |
|
| 52 |
[mod] |
| 53 |
path = {mod} |
| 54 |
read only = yes |
| 55 |
""") |
| 56 |
|
| 57 |
asan_log = base / 'implied-asan' |
| 58 |
for stale in glob.glob(f"{asan_log}.*"): |
| 59 |
os.unlink(stale) |
| 60 |
prev = os.environ.get('ASAN_OPTIONS', '') |
| 61 |
os.environ['ASAN_OPTIONS'] = ( |
| 62 |
(prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}') |
| 63 |
|
| 64 |
start_rsyncd(conf, PORT) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
proc = subprocess.run( |
| 69 |
rsync_argv('-r', f'--files-from={ff}', f'rsync://127.0.0.1:{PORT}/mod/', |
| 70 |
str(dest) + '/'), |
| 71 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=30) |
| 72 |
|
| 73 |
|
| 74 |
def asan_report(): |
| 75 |
text = ''.join(open(r, errors='replace').read() |
| 76 |
for r in glob.glob(f"{asan_log}.*")) |
| 77 |
return text if 'AddressSanitizer' in text else ( |
| 78 |
proc.stdout if 'AddressSanitizer' in (proc.stdout or '') else '') |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
import time |
| 84 |
report = '' |
| 85 |
for _ in range(30): |
| 86 |
report = asan_report() |
| 87 |
if report: |
| 88 |
break |
| 89 |
time.sleep(0.1) |
| 90 |
|
| 91 |
if report: |
| 92 |
test_fail( |
| 93 |
"add_implied_include() overflowed its heap buffer on a files-from name " |
| 94 |
"with a trailing backslash -- the trailing '\\' was doubled but not " |
| 95 |
"counted in the allocation:\n" + report[:1500]) |
| 96 |
|
| 97 |
print("exclude-implied-trailing-backslash: a trailing backslash in a files-from " |
| 98 |
"name is counted; no heap overflow in add_implied_include.") |