| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for a receiver NULL-deref on the delta DISCARD path. |
| 3 | # |
| 4 | # In receiver.c receive_data(), a block-MATCH token that arrives while the |
| 5 | # receiver is DISCARDING a file (discard_receive_data() -> receive_data() with |
| 6 | # fname==NULL, fd==-1, hence mapbuf==NULL) reached |
| 7 | # rprintf(FERROR, "...%s...", full_fname(fname), ...) |
| 8 | # with fname==NULL. full_fname() dereferences its argument unconditionally |
| 9 | # (util1.c: `if (*fn == '/')`), so the receiver SIGSEGVs. The faulty error |
| 10 | # branch was added in 31fbb17d ("receiver: fix absolute --partial-dir delta |
| 11 | # resume"); the fix discriminates on fd (not mapbuf) and, on the discard path |
| 12 | # (fd==-1), absorbs the matched bytes benignly instead of erroring. |
| 13 | # |
| 14 | # This is a NORMAL-operation crash, not adversarial: a stock cooperating sender |
| 15 | # triggers it. The generator sends real block sums (basis readable, delta mode); |
| 16 | # the receiver then has to discard because its output mkstemp() fails -- here |
| 17 | # because the destination directory is not writable. A block MATCH against the |
| 18 | # shared leading block reaches the discard path and crashes the pre-fix binary. |
| 19 | # |
| 20 | # We drive a real sender<->receiver pair (client sender -> daemon receiver) so |
| 21 | # the receiver actually takes the recv_files discard path; a local `rsync a b` |
| 22 | # does not. In the default (pipe) daemon transport both ends are the binary |
| 23 | # under test. |
| 24 | # |
| 25 | # Skipped (exit 77) when running as root (root bypasses DAC), or when the |
| 26 | # directory mode is not enforced (e.g. a non-root process holding |
| 27 | # CAP_DAC_OVERRIDE in an unprivileged container): in both cases the receiver's |
| 28 | # mkstemp() would succeed despite chmod 0555, the discard path would not be |
| 29 | # taken, and the test would silently pass against a buggy binary. The |
| 30 | # post-chmod writability probe converts that silent false-pass into an honest |
| 31 | # skip and subsumes the root check. |
| 32 | |
| 33 | import os |
| 34 | import shlex |
| 35 | import subprocess |
| 36 | import tempfile |
| 37 | |
| 38 | from rsyncfns import ( |
| 39 | SCRATCHDIR, RSYNC, TMPDIR, |
| 40 | get_testuid, get_rootuid, makepath, start_test_daemon, write_daemon_conf, |
| 41 | test_fail, test_skipped, split_rsync_cmd, |
| 42 | ) |
| 43 | |
| 44 | DAEMON_PORT = 12895 |
| 45 | |
| 46 | if get_testuid() == get_rootuid(): |
| 47 | test_skipped("root bypasses DAC: the unwritable dest dir wouldn't make " |
| 48 | "the receiver's mkstemp fail, so the discard path (and the " |
| 49 | "bug) is never reached") |
| 50 | |
| 51 | os.chdir(TMPDIR) |
| 52 | |
| 53 | MODDIR = SCRATCHDIR / 'recvdiscard-mod' # daemon module root (writable) |
| 54 | BASISDIR = MODDIR / 'd' # made read-only -> mkstemp fails |
| 55 | SRCDIR_ = SCRATCHDIR / 'recvdiscard-src' # client source tree |
| 56 | makepath(MODDIR, BASISDIR, SRCDIR_) |
| 57 | |
| 58 | # Basis and source share a leading block (2000 'A's) so the generator emits |
| 59 | # real sums and the receiver gets a block MATCH; the tails differ and the |
| 60 | # source is larger so a delta (not a no-op) is sent. |
| 61 | basis = BASISDIR / 'f' |
| 62 | basis.write_bytes(b'A' * 2000 + b'C' * 1000) |
| 63 | src = SRCDIR_ / 'f' |
| 64 | src.write_bytes(b'A' * 2000 + b'B' * 3000) |
| 65 | |
| 66 | # A read/write daemon module rooted at MODDIR. |
| 67 | conf = write_daemon_conf([('recvdiscard', {'path': str(MODDIR), |
| 68 | 'read only': 'no'})]) |
| 69 | url = start_test_daemon(conf, DAEMON_PORT, rsync_cmd=RSYNC) |
| 70 | |
| 71 | # Make the destination directory unwritable so the receiver's output mkstemp() |
| 72 | # fails and it falls back to discarding the delta stream. Restore in finally so |
| 73 | # the per-test scratch tree can be cleaned up. |
| 74 | os.chmod(BASISDIR, 0o555) |
| 75 | |
| 76 | # Probe that the chmod actually denies writes for *this* process. A non-root |
| 77 | # user holding CAP_DAC_OVERRIDE bypasses the directory write bit, so mkstemp |
| 78 | # would succeed in the daemon receiver too, the discard path would never be |
| 79 | # taken, and the test would silently pass on a buggy binary. Better to skip |
| 80 | # explicitly. (Root takes this path too: its probe succeeds → skip, which |
| 81 | # subsumes the uid==0 check.) |
| 82 | try: |
| 83 | _fd, _probe = tempfile.mkstemp(dir=BASISDIR) |
| 84 | os.close(_fd) |
| 85 | os.unlink(_probe) |
| 86 | os.chmod(BASISDIR, 0o755) |
| 87 | test_skipped("destination dir is writable despite chmod 0555 " |
| 88 | "(CAP_DAC_OVERRIDE?); cannot force the receiver discard path") |
| 89 | except OSError: |
| 90 | pass # EACCES -- good, the precondition is enforced |
| 91 | |
| 92 | try: |
| 93 | argv = split_rsync_cmd(RSYNC) + [ |
| 94 | '--no-whole-file', '-a', |
| 95 | str(src), f'{url}recvdiscard/d/f', |
| 96 | ] |
| 97 | print('Running:', ' '.join(argv)) |
| 98 | proc = subprocess.run(argv, stdout=subprocess.PIPE, |
| 99 | stderr=subprocess.STDOUT, text=True) |
| 100 | print(proc.stdout, end='') |
| 101 | finally: |
| 102 | os.chmod(BASISDIR, 0o755) |
| 103 | |
| 104 | rc = proc.returncode |
| 105 | |
| 106 | # A receiver SIGSEGV manifests to the client as a protocol error (the daemon's |
| 107 | # receiver child crashes mid-stream and the connection drops): exit code 12. |
| 108 | # With the fix the receiver drains the delta and, because the forced-unwritable |
| 109 | # destination leaves the file untransferred, the run reports the benign "some |
| 110 | # files were not transferred" -- exit code 23. |
| 111 | # |
| 112 | # 23 is the ONLY non-crash outcome here: the writability probe above guarantees |
| 113 | # the receiver's mkstemp() fails, so the file is always discarded. An exit 0 |
| 114 | # would mean the file actually transferred -- the discard path was NOT exercised |
| 115 | # and the run proves nothing -- so require exactly 23 (and call out 12 as the |
| 116 | # pre-fix crash). |
| 117 | if rc == 12: |
| 118 | test_fail(f"receiver crashed on the discard path (rsync exited {rc}: " |
| 119 | "error in rsync protocol data stream -- the receiver child " |
| 120 | "SIGSEGV'd in full_fname(NULL))") |
| 121 | if rc != 23: |
| 122 | test_fail(f"expected rsync exit 23 (the forced discard leaves the file " |
| 123 | f"untransferred); got {rc} -- the discard path was not exercised, " |
| 124 | "so this run validates nothing (12 would be the pre-fix crash)") |
| 125 | |
| 126 | print(f"OK: receiver discarded the delta without crashing (rsync exit {rc})") |