| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for a wild-pointer read via the first inc_recurse flist's |
| 3 | # parent_ndx when dir_flist is empty (flist.c recv_file_list). |
| 4 | # |
| 5 | # Under inc_recurse the first flist (ndx_start == 1) has no parent entry of its |
| 6 | # own: recv_file_list() trusts the peer's "." entry to be the transfer root and |
| 7 | # leaves parent_ndx at the flist_new() default of 0, meaning dir_flist->files[0]. |
| 8 | # Only S_ISDIR entries are appended to dir_flist, so a peer that sends a "." |
| 9 | # entry with a NON-directory mode keeps dir_flist->used at 0 while the basename |
| 10 | # strcmp() still passes -- parent_ndx stays 0 and the consumers (generate_files, |
| 11 | # recv_files, touch_up_dirs) index a never-written slot of the freshly allocated |
| 12 | # dir_flist->files[], i.e. uninitialised heap, and dereference it. |
| 13 | # |
| 14 | # This drives a real daemon receiver with the pure-Python sender: an inc_recurse |
| 15 | # push whose first (and only) flist is a "." REGULAR file plus a second regular |
| 16 | # file "a" -- no directory anywhere, so dir_flist->used stays 0, and "." sorts |
| 17 | # lowest so the basename test passes. ("a" is there so file_total != 1 and the |
| 18 | # receiver doesn't divert into recv_additional_file_list.) |
| 19 | # |
| 20 | # The file list ALONE is what does it: the GENERATOR (the connection parent; |
| 21 | # do_recv() forks the receiver as the child) crashes in generate_files() before |
| 22 | # any transfer phase. The follow-on token for ndx 0 is sent only to exercise |
| 23 | # recv_files()'s parent_ndx path on a build that survives the list; on a |
| 24 | # vulnerable one the generator is already gone by then, so this test does not |
| 25 | # demonstrate the receiver-side consumer. |
| 26 | # |
| 27 | # Three independent layers now stop this, in wire order: recv_file_entry() |
| 28 | # refuses a non-directory transfer-root entry outright; recv_file_list() clears |
| 29 | # parent_ndx to -1 when dir_flist is empty; and the parent_ndx consumers bounds- |
| 30 | # check against dir_flist->used. Only the first fires on a current build, so |
| 31 | # THIS TEST GATES THE ATTACK SHAPE, NOT THE parent_ndx CLAUSE: removing that one |
| 32 | # clause alone leaves the test passing. Isolating it needs the other two layers |
| 33 | # disabled as well (done once by hand: clause out = SIGSEGV, clause in = |
| 34 | # orderly refusal), which is why the clause is still worth backporting alone. |
| 35 | # |
| 36 | # The dereferenced slot is not guaranteed to be NULL -- dir_flist->files[] comes |
| 37 | # from realloc(), not calloc(). It reads as NULL in an ordinary run (si_addr |
| 38 | # 0x14 = file->mode), but a peer that grooms the heap first (a large freed |
| 39 | # filter list) can put its own bytes there. Either way the observed outcome is |
| 40 | # a crash. Normal-build oracle; only needs a real TCP daemon. |
| 41 | |
| 42 | import time |
| 43 | |
| 44 | from rsyncfns import ( |
| 45 | SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail, |
| 46 | ) |
| 47 | import rsync_proto as rp |
| 48 | |
| 49 | PORT = 12967 |
| 50 | require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp") |
| 51 | claim_ports(PORT) |
| 52 | |
| 53 | mod = SCRATCHDIR / 'pn-mod' |
| 54 | mod.mkdir(parents=True, exist_ok=True) |
| 55 | conf = SCRATCHDIR / 'pn.conf' |
| 56 | log = SCRATCHDIR / 'pn.log' |
| 57 | conf.write_text(f"""\ |
| 58 | pid file = {SCRATCHDIR}/pn-rsyncd.pid |
| 59 | use chroot = no |
| 60 | log file = {log} |
| 61 | |
| 62 | [mod] |
| 63 | path = {mod} |
| 64 | read only = no |
| 65 | """) |
| 66 | start_rsyncd(conf, PORT) |
| 67 | |
| 68 | before = log.read_text(errors='replace') if log.exists() else '' |
| 69 | s = rp.DaemonSender('127.0.0.1', PORT) |
| 70 | s.handshake('mod', ['--server', '-re.iLsfxCIu', '.', 'mod/'], greeting_version=30) |
| 71 | # The transfer root "." as a regular file: it never lands in dir_flist, so |
| 72 | # dir_flist->used stays 0 while "." still sorts lowest. |
| 73 | dot = rp.FileEntry('.', mode=rp.S_IFREG | 0o644, length=100) |
| 74 | other = rp.FileEntry('a', mode=rp.S_IFREG | 0o644, length=100) |
| 75 | s.send_data(dot.encode() + other.encode() + rp.end_of_flist(0, s.protocol)) |
| 76 | time.sleep(0.5) |
| 77 | # ndx 0 == first_flist->ndx_start - 1. A FIXED daemon has already refused the |
| 78 | # file list and gone by now, so this send and the drain may hit a closed socket: |
| 79 | # Linux and FreeBSD swallow it, other stacks raise EPIPE/ECONNRESET. That is an |
| 80 | # expected outcome, not a result -- the verdict comes from the daemon log below. |
| 81 | try: |
| 82 | s.send_transfer_ndx(0, rp.ITEM_TRANSFER) |
| 83 | s.drain(timeout=3.0) |
| 84 | except (OSError, rp.ProtocolError): |
| 85 | pass |
| 86 | s.close() |
| 87 | |
| 88 | # Two things must both be true, and a bare "rsync error:" is NOT enough: any |
| 89 | # later wire error would satisfy that, so the test would pass without the |
| 90 | # crafted list ever reaching the parser (proved by sending a bogus file index |
| 91 | # against a build where "." was a valid directory -- it "passed"). |
| 92 | # * positive control: the daemon reached recv_file_list() at all; and |
| 93 | # * the refusal names THIS condition. |
| 94 | reached = refused = False |
| 95 | for _ in range(50): |
| 96 | new = log.read_text(errors='replace')[len(before):] |
| 97 | reached = 'receiving file list' in new |
| 98 | refused = 'rejecting non-directory transfer-root entry' in new |
| 99 | if reached and refused: |
| 100 | break |
| 101 | time.sleep(0.1) |
| 102 | |
| 103 | new = log.read_text(errors='replace')[len(before):] |
| 104 | if not reached: |
| 105 | test_fail("the daemon never logged 'receiving file list', so the crafted " |
| 106 | "transfer root never reached recv_file_list() -- this run proves " |
| 107 | f"nothing either way.\ndaemon log:\n{new}") |
| 108 | if not refused: |
| 109 | test_fail( |
| 110 | "the daemon did not refuse the non-directory transfer root of a first " |
| 111 | "inc_recurse flist. Either it died dereferencing dir_flist->files[0] " |
| 112 | "from an empty dir_flist (no further log output), or it failed for some " |
| 113 | f"unrelated reason.\ndaemon log:\n{new}") |
| 114 | |
| 115 | print("proto-parent-ndx-empty-dirflist: a non-directory transfer root in the " |
| 116 | "first inc_recurse flist is refused, not turned into a dir_flist->files[0] " |
| 117 | "deref.") |