| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for a stale-dir_flist use-after-free on a sub-flist marker |
| 3 | # sent after every flist has been freed (flist.c recv_file_list). |
| 4 | # |
| 5 | # In inc_recurse mode the receiver frees each flist as it finishes. When the |
| 6 | # last goes, flist_free() sets first_flist = NULL and pool_destroy()s the shared |
| 7 | # file pool, but the global dir_flist is left pointing into the destroyed pool. |
| 8 | # A malicious sender that then sends an NDX_FLIST_OFFSET sub-flist marker made |
| 9 | # recv_file_list() write FLAG_GOT_DIR_FLIST into the freed dir_flist entry (a |
| 10 | # use-after-free) and re-create dir_flist, before the existing dirname !d guard |
| 11 | # turned the follow-on uninitialised deref into a clean RERR_UNSUPPORTED exit. |
| 12 | # The fix refuses the sub-flist up front when first_flist is NULL. |
| 13 | # |
| 14 | # This drives a real daemon receiver with the pure-Python sender, in |
| 15 | # inc_recurse mode (-r + an -e string containing 'i'): push a tiny top-level |
| 16 | # flist (two directories, so there is nothing to transfer and every flist is |
| 17 | # freed promptly), then send a sub-flist marker. |
| 18 | # |
| 19 | # Oracle: fixed -> the receiver logs "refusing sub-flist after final flist was |
| 20 | # freed" (RERR_PROTOCOL at the up-front guard). pre-fix -> that guard is absent; |
| 21 | # the receiver instead performs the use-after-free and exits later via the !d |
| 22 | # backstop ("requested action not supported"), so the refuse message never |
| 23 | # appears (and under ASan the UAF is a hard report). Normal-build oracle; only |
| 24 | # needs a real TCP daemon. |
| 25 | |
| 26 | import time |
| 27 | |
| 28 | from rsyncfns import ( |
| 29 | SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail, |
| 30 | ) |
| 31 | import rsync_proto as rp |
| 32 | |
| 33 | PORT = 12958 |
| 34 | require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp") |
| 35 | claim_ports(PORT) |
| 36 | |
| 37 | mod = SCRATCHDIR / 'sf-mod' |
| 38 | mod.mkdir(parents=True, exist_ok=True) |
| 39 | conf = SCRATCHDIR / 'sf.conf' |
| 40 | log = SCRATCHDIR / 'sf.log' |
| 41 | conf.write_text(f"""\ |
| 42 | pid file = {SCRATCHDIR}/sf-rsyncd.pid |
| 43 | use chroot = no |
| 44 | log file = {log} |
| 45 | |
| 46 | [mod] |
| 47 | path = {mod} |
| 48 | read only = no |
| 49 | """) |
| 50 | start_rsyncd(conf, PORT) |
| 51 | |
| 52 | before = log.read_text(errors='replace') if log.exists() else '' |
| 53 | s = rp.DaemonSender('127.0.0.1', PORT) |
| 54 | s.handshake('mod', ['--server', '-re.iLsfxCIu', '.', 'mod/'], greeting_version=30) |
| 55 | # Two top-level directories: nothing to transfer. |
| 56 | d = rp.FileEntry('d', mode=rp.S_IFDIR | 0o755, length=0, extra_flags=rp.XMIT_TOP_DIR) |
| 57 | s.send_data(d.encode() + d.encode() + rp.end_of_flist(0, s.protocol)) |
| 58 | # One NDX_DONE frees the (single) top flist -> first_flist becomes NULL; then a |
| 59 | # sub-flist marker hits the "after final flist freed" guard. (More than one |
| 60 | # would advance phase past max_phase and break recv_files before the marker.) |
| 61 | s.send_ndx_done() |
| 62 | s.send_subflist_marker(0) |
| 63 | s.drain(timeout=2.0) |
| 64 | s.close() |
| 65 | |
| 66 | refused = False |
| 67 | for _ in range(50): |
| 68 | new = log.read_text(errors='replace')[len(before):] |
| 69 | if 'refusing sub-flist after final flist was freed' in new: |
| 70 | refused = True |
| 71 | break |
| 72 | time.sleep(0.1) |
| 73 | |
| 74 | if not refused: |
| 75 | new = log.read_text(errors='replace')[len(before):] |
| 76 | test_fail( |
| 77 | "receiver did not refuse a sub-flist marker sent after all flists were " |
| 78 | "freed -- it used the stale dir_flist (use-after-free).\n" |
| 79 | f"daemon log:\n{new}") |
| 80 | |
| 81 | print("proto-subflist-freed: receiver refused a sub-flist after the final flist " |
| 82 | "was freed (no stale-dir_flist use-after-free).") |