| 1 | #!/usr/bin/env python3 |
| 2 | # Daemon-side `name converter` empty-line response is treated as success |
| 3 | # with id=0, mapping unknown sender names to uid/gid 0. |
| 4 | # |
| 5 | # Threat (Mitchell Benjamin report, May 2026): |
| 6 | # The documented helper contract (support/nameconvert:14) is |
| 7 | # "An unknown ID_NUM or NAME results in an empty return value." |
| 8 | # But namecvt_call() in clientserver.c:1227 does: |
| 9 | # if (!read_line_old(namecvt_fd_ans, buf, sizeof buf, 0)) |
| 10 | # return False; |
| 11 | # if (*name_p) |
| 12 | # *id_p = (id_t)atol(buf); <-- atol("") == 0 |
| 13 | # else |
| 14 | # *name_p = strdup(buf); |
| 15 | # return True; |
| 16 | # So an empty response is read successfully and atol("") = 0 is treated |
| 17 | # as a valid mapping. user_to_uid()/group_to_gid() then return success |
| 18 | # with *uid_p = 0 / *gid_p = 0. Receiver recv_add_id() (uidlist.c:273) |
| 19 | # uses that as the local id, and with `fake super = yes` the file's |
| 20 | # xattr ends up with "MODE 0,0 0:0" — root-owned setuid metadata that |
| 21 | # a fake-super restore/list view honours as a real root-owned file. |
| 22 | # |
| 23 | # Test design: |
| 24 | # * A stub name-converter that always prints an empty line, no matter |
| 25 | # what the daemon asks. This simulates the "every sender name is |
| 26 | # unknown to the daemon" case (the report did the same thing via an |
| 27 | # NSS shim; the stub is equivalent and far easier). |
| 28 | # * Daemon module: read only=no, fake super=yes, name converter=stub, |
| 29 | # numeric ids=no, so the receiver looks up the sender's user/group |
| 30 | # names through the stub. |
| 31 | # * Client uploads a regular file under the current user's identity |
| 32 | # (a non-root uid/gid). |
| 33 | # * After upload, read user.rsync.%stat xattr on the stored file. |
| 34 | # Format is "MODE 0,0 UID:GID". If UID is 0 (and sender's uid was |
| 35 | # non-zero), the bug is present. |
| 36 | # |
| 37 | # Fix direction: |
| 38 | # In namecvt_call(), for the name-to-id branch, treat empty/non-numeric |
| 39 | # buf as lookup failure (return False). recv_add_id then falls back |
| 40 | # to the sender's numeric id (uidlist.c:276), so unknown names stay |
| 41 | # non-root. |
| 42 | |
| 43 | import os |
| 44 | import re |
| 45 | import subprocess |
| 46 | |
| 47 | from rsyncfns import ( |
| 48 | RSYNC_PREFIX, SCRATCHDIR, _xattr_full, makepath, rmtree, rsync_argv, |
| 49 | start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 50 | xattrs_supported, |
| 51 | ) |
| 52 | |
| 53 | # Skips as root (see the my_uid==0 guard below), so the fleet harness reruns it |
| 54 | # in its non-root pass to get real coverage (testsuite/fleettest.py). |
| 55 | fleet_nonroot = True |
| 56 | |
| 57 | DAEMON_PORT = 12904 |
| 58 | |
| 59 | if not xattrs_supported(): |
| 60 | test_skipped("namecvt-empty-response test requires xattr support to " |
| 61 | "read fake-super metadata") |
| 62 | |
| 63 | if not hasattr(os, 'getxattr'): |
| 64 | # CPython exposes os.getxattr only on Linux; the OS (e.g. Cygwin) may have |
| 65 | # xattrs yet no Python binding to read the fake-super %stat metadata. |
| 66 | test_skipped("this Python build lacks os.getxattr to read fake-super metadata") |
| 67 | |
| 68 | my_uid = os.getuid() |
| 69 | my_gid = os.getgid() |
| 70 | if my_uid == 0: |
| 71 | # Running as root would defeat the test: any namecvt mapping that |
| 72 | # ends in id=0 is indistinguishable from "the sender was already |
| 73 | # root." We need a non-zero sender id so the bug-vs-fix branches |
| 74 | # produce different xattr UIDs. |
| 75 | # NOTE: because every Linux CI job runs the suite as root, this test only |
| 76 | # gets coverage from the dedicated non-root run -- the "check (non-root, |
| 77 | # targeted)" step in .github/workflows/ubuntu-build.yml. Any new non-root- |
| 78 | # only test must be added to that step (and declare `fleet_nonroot = True` |
| 79 | # for the fleet harness's non-root pass) or it will silently never run in CI. |
| 80 | test_skipped("namecvt-empty-response test must run as a non-root user " |
| 81 | "(sender uid/gid must differ from 0 to expose the bug)") |
| 82 | |
| 83 | |
| 84 | # -- stub name-converter that always returns empty ------------------------- |
| 85 | stub = SCRATCHDIR / 'stub_nameconvert' |
| 86 | stub.write_text( |
| 87 | "#!/usr/bin/env python3\n" |
| 88 | "# Always-empty stub: every usr/grp/uid/gid lookup answers ''.\n" |
| 89 | "# The bundled support/nameconvert helper does this only when the\n" |
| 90 | "# lookup fails (KeyError); the namecvt_call() bug treats the empty\n" |
| 91 | "# line as a valid numeric 0.\n" |
| 92 | "import sys\n" |
| 93 | "for _ in sys.stdin:\n" |
| 94 | " print('', flush=True)\n" |
| 95 | ) |
| 96 | stub.chmod(0o755) |
| 97 | |
| 98 | |
| 99 | mod = SCRATCHDIR / 'recvmod' |
| 100 | src = SCRATCHDIR / 'srcdir' # NOT 'src' -- runtests.py:155 plants |
| 101 | # a <scratch>/src symlink to SRCDIR for |
| 102 | # every test, so writing files under it |
| 103 | # would land in the repo working tree. |
| 104 | rmtree(mod) |
| 105 | rmtree(src) |
| 106 | makepath(mod) |
| 107 | makepath(src) |
| 108 | (src / 'f').write_text("DATA\n") |
| 109 | |
| 110 | |
| 111 | conf = write_daemon_conf([ |
| 112 | ('recv', {'path': str(mod), |
| 113 | 'read only': 'no', |
| 114 | 'fake super': 'yes', |
| 115 | 'numeric ids': 'no', |
| 116 | 'name converter': str(stub)}), |
| 117 | ]) |
| 118 | url = start_test_daemon(conf, DAEMON_PORT) |
| 119 | |
| 120 | |
| 121 | # -- positive control: upload a file ------------------------------------- |
| 122 | # `-a` includes owner+group preservation, and the receiver (daemon w/ |
| 123 | # fake-super) writes the metadata into user.rsync.%stat. |
| 124 | proc = subprocess.run(rsync_argv('-a', f'{src}/', f'{url}recv/'), |
| 125 | stdout=subprocess.DEVNULL, |
| 126 | stderr=subprocess.PIPE, text=True) |
| 127 | if proc.returncode not in (0, 23): |
| 128 | test_fail(f"upload to recv module failed (rc={proc.returncode}): " |
| 129 | f"{proc.stderr!r}") |
| 130 | |
| 131 | stored = mod / 'f' |
| 132 | if not stored.is_file(): |
| 133 | test_fail(f"upload did not deliver the file ({stored})") |
| 134 | |
| 135 | |
| 136 | # -- read the fake-super xattr ------------------------------------------ |
| 137 | # Fake-super only stores an xattr when the metadata to remember DIFFERS |
| 138 | # from the filesystem reality (rsync.c:526: |
| 139 | # change_uid = am_root && uid_ndx && sxp->st.st_uid != F_OWNER(file)). |
| 140 | # With the FIX: |
| 141 | # * namecvt empty -> user_to_uid returns 0 (failure) |
| 142 | # * recv_add_id() falls back to the sender's numeric id |
| 143 | # * F_OWNER == filesystem-creation uid (both are `my_uid`) |
| 144 | # * no xattr is written. |
| 145 | # With the BUG: |
| 146 | # * namecvt empty -> user_to_uid returns success with id=0 |
| 147 | # * F_OWNER becomes 0, filesystem uid is `my_uid`, they differ |
| 148 | # * xattr is written carrying "0:0" -- that is the leak we detect. |
| 149 | # |
| 150 | # So we check: if an xattr IS present, parse it and confirm it does not |
| 151 | # encode 0:0. If no xattr is present we infer the namecvt path did not |
| 152 | # fabricate a phantom id=0 -- the metadata simply matches reality. |
| 153 | xkey = _xattr_full(RSYNC_PREFIX + '.%stat') # 'user.rsync.%stat' on Linux |
| 154 | try: |
| 155 | raw = os.getxattr(str(stored), xkey) |
| 156 | xval = raw.decode('utf-8', 'surrogateescape') |
| 157 | except OSError: |
| 158 | xval = None |
| 159 | |
| 160 | if xval is None: |
| 161 | # No fake-super xattr stored: metadata matched the filesystem, which |
| 162 | # is what the fix produces because F_OWNER == my_uid == filesystem |
| 163 | # uid. That is the GREEN signal. |
| 164 | print(f"daemon-namecvt-empty-response: no fake-super xattr written -- " |
| 165 | f"namecvt empty response was correctly treated as a lookup " |
| 166 | f"failure, so recv_add_id() kept sender uid={my_uid} and the " |
| 167 | f"receiver had nothing to remember separately from the " |
| 168 | f"filesystem.") |
| 169 | else: |
| 170 | # An xattr is present. user.rsync.%stat format: |
| 171 | # "MODE DEV_MAJOR,DEV_MINOR UID:GID" |
| 172 | # The leak case is UID:GID == 0:0 . |
| 173 | m = re.match(r'\S+\s+\S+\s+(\d+):(\d+)\s*$', xval) |
| 174 | if not m: |
| 175 | test_fail(f"unrecognised fake-super xattr format: {xval!r}") |
| 176 | stored_uid, stored_gid = int(m.group(1)), int(m.group(2)) |
| 177 | |
| 178 | if stored_uid == 0 and stored_gid == 0: |
| 179 | test_fail( |
| 180 | f"namecvt empty response was treated as a valid id=0 " |
| 181 | f"mapping: file uploaded by uid={my_uid}/gid={my_gid} is " |
| 182 | f"stored with fake-super metadata {xval!r} (uid:gid = 0:0). " |
| 183 | f" This means a daemon-unknown sender name silently becomes " |
| 184 | f"root-owned in preserved metadata. Fix: namecvt_call() " |
| 185 | f"must treat an empty buf as lookup failure (return False) " |
| 186 | f"so recv_add_id() falls back to the sender's numeric id " |
| 187 | f"(clientserver.c:1227, uidlist.c:273-282).") |
| 188 | |
| 189 | print(f"daemon-namecvt-empty-response: xattr {xval!r} present but " |
| 190 | f"non-zero uid/gid -- empty namecvt response did not produce " |
| 191 | f"a phantom root-owned mapping.") |