| 1 | #!/usr/bin/env python3 |
| 2 | # Python rewrite of testsuite/rename-mixed-parent-transfer.test. |
| 3 | # |
| 4 | # End-to-end (real daemon transfer) companion to the unit-level |
| 5 | # rename-mixed-parent-symlink-race / rename-mixed-parent-escape-poc tests. |
| 6 | # |
| 7 | # Those tests exercise do_rename_at() directly. This one drives the escape |
| 8 | # through an actual rsync transfer against a no-chroot daemon, to prove the |
| 9 | # 3.4.3 do_rename_at() mixed-parent fallback is reachable and exploitable |
| 10 | # from the wire, not just at the function boundary. |
| 11 | # |
| 12 | # Background: do_rename_at() in 3.4.3 only confined parent resolution under |
| 13 | # secure_relative_open() when *both* the source and destination contained a |
| 14 | # slash: |
| 15 | # |
| 16 | # if (!old_slash || !new_slash) |
| 17 | # return do_rename(old_path, new_path); /* plain rename() */ |
| 18 | # |
| 19 | # So a rename where one side is a bare top-of-module name (resolved in the |
| 20 | # module-root CWD, which is safe) and the other side is slashed (whose parent |
| 21 | # the kernel resolves freely) fell back to plain rename() -- letting an |
| 22 | # attacker-planted parent symlink on the slashed side escape the module. |
| 23 | # |
| 24 | # Trigger: --backup --backup-dir=bdir. When the daemon overwrites an existing |
| 25 | # top-of-module file "foo", it backs it up via make_backup() -> link_or_rename(), |
| 26 | # ending in do_rename_at("foo", "bdir/foo"): source bare, destination slashed. |
| 27 | # With "bdir" planted as a symlink pointing outside the module, the 3.4.3 |
| 28 | # fallback renames the in-module file out through the symlink. With the |
| 29 | # per-side fix, the slashed destination parent is opened under |
| 30 | # secure_relative_open(), the symlink is refused, and the file stays inside. |
| 31 | # (do_link_at() is tried first but already has the per-side hardening, so it |
| 32 | # refuses the symlinked backup dir and falls through to do_rename_at().) |
| 33 | # |
| 34 | # The vulnerable code path is the same on every platform (including the |
| 35 | # per-component fallback on systems without RESOLVE_BENEATH), so this test is |
| 36 | # not platform-gated. |
| 37 | |
| 38 | import os |
| 39 | import subprocess |
| 40 | |
| 41 | from rsyncfns import ( |
| 42 | SCRATCHDIR, find_attacker_uid, |
| 43 | rmtree, rsync_argv, start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 44 | ) |
| 45 | |
| 46 | # Operator paths follow a uid0/euid-owned symlink and refuse a foreign-owned one, |
| 47 | # so the escape vector is an ATTACKER-owned backup-dir symlink and a root daemon. |
| 48 | if os.geteuid() != 0: |
| 49 | test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid") |
| 50 | ATT_UID = find_attacker_uid() |
| 51 | if ATT_UID is None: |
| 52 | test_skipped("no untrusted-uid user available for cross-uid plant") |
| 53 | |
| 54 | |
| 55 | DAEMON_PORT = 12903 |
| 56 | OLD = "INSIDE_MODULE_DATA\n" |
| 57 | NEW = "NEW_PUSHED_DATA\n" |
| 58 | |
| 59 | mod = SCRATCHDIR / 'module' |
| 60 | outside = SCRATCHDIR / 'outside' |
| 61 | # NB: 'src' is reserved by runtests.py (a symlink to the source tree), so use |
| 62 | # a distinct name for our push source. |
| 63 | src = SCRATCHDIR / 'src_files' |
| 64 | for d in (mod, outside, src): |
| 65 | rmtree(d) |
| 66 | d.mkdir(parents=True) |
| 67 | |
| 68 | # The existing in-module file the daemon will overwrite (and thus back up). |
| 69 | # Its content is what an attacker would relocate outside the module. |
| 70 | (mod / 'foo').write_text(OLD) |
| 71 | |
| 72 | # A different source so the push genuinely overwrites foo and triggers backup. |
| 73 | (src / 'foo').write_text(NEW) |
| 74 | |
| 75 | # Serve the module as root so the daemon euid differs from the attacker uid; the |
| 76 | # ownership walk must then refuse the foreign-owned backup-dir symlink. Use |
| 77 | # numeric uid/gid 0, not the name "root": the root *group* is "wheel" on the BSDs |
| 78 | # and macOS, so `gid = root` is rejected there. |
| 79 | conf = write_daemon_conf([ |
| 80 | ('upload', {'path': str(mod), 'use chroot': 'no', 'read only': 'no', |
| 81 | 'uid': '0', 'gid': '0'}), |
| 82 | ]) |
| 83 | daemon_url = start_test_daemon(conf, DAEMON_PORT).rstrip('/') |
| 84 | |
| 85 | |
| 86 | def push_with_backup(): |
| 87 | return subprocess.run( |
| 88 | rsync_argv('-t', '--backup', '--backup-dir=bdir', |
| 89 | f'{src}/foo', f'{daemon_url}/upload/'), |
| 90 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 91 | |
| 92 | |
| 93 | # Positive control: a real backup-dir must receive the original file, proving |
| 94 | # this transfer shape exercises --backup-dir and the mixed-parent backup rename. |
| 95 | (mod / 'bdir').mkdir() |
| 96 | proc = push_with_backup() |
| 97 | if proc.returncode != 0: |
| 98 | test_fail("positive control: daemon --backup-dir transfer failed " |
| 99 | f"(rc={proc.returncode}):\n{proc.stdout or ''}") |
| 100 | backup = mod / 'bdir' / 'foo' |
| 101 | if not backup.is_file(): |
| 102 | test_fail("positive control: --backup-dir=bdir did not create " |
| 103 | f"{backup}; the test would not exercise the backup rename path") |
| 104 | if backup.read_text() != OLD: |
| 105 | test_fail("positive control: backup content differs from the original " |
| 106 | f"module/foo content: {backup.read_text()!r}") |
| 107 | if (mod / 'foo').read_text() != NEW: |
| 108 | test_fail("positive control: module/foo was not overwritten by the " |
| 109 | "daemon transfer") |
| 110 | |
| 111 | rmtree(mod / 'bdir') |
| 112 | (mod / 'foo').write_text(OLD) |
| 113 | |
| 114 | # The attacker-planted backup-dir symlink, pointing outside the module, owned by |
| 115 | # the attacker uid (not root, the uid the module is served as). |
| 116 | os.symlink(str(outside), mod / 'bdir') |
| 117 | os.lchown(mod / 'bdir', ATT_UID, ATT_UID) |
| 118 | |
| 119 | # Push the changed file straight into the module root with backups enabled and |
| 120 | # a backup-dir relative to the module root. The transfer may report an error on |
| 121 | # the fixed binary (the secure backup rename is refused), so the security oracle |
| 122 | # is where the files end up. |
| 123 | proc = push_with_backup() |
| 124 | attack_out = proc.stdout or '' |
| 125 | |
| 126 | # The escape: the original in-module file was renamed out through the bdir |
| 127 | # symlink into the outside-the-module directory. |
| 128 | if os.path.lexists(outside / 'foo'): |
| 129 | try: |
| 130 | got = (outside / 'foo').read_text().strip() |
| 131 | except OSError: |
| 132 | got = '<unreadable>' |
| 133 | test_fail("mixed-parent rename escaped the module: backup of module/foo " |
| 134 | f"landed in {outside}/foo (content: {got}); do_rename_at " |
| 135 | "followed the planted backup-dir symlink") |
| 136 | |
| 137 | # Sanity: nothing else should have leaked outside the module either. |
| 138 | leaked = os.listdir(outside) |
| 139 | if leaked: |
| 140 | test_fail(f"unexpected files escaped the module into {outside}: {leaked}") |
| 141 | |
| 142 | try: |
| 143 | final = (mod / 'foo').read_text() |
| 144 | except (OSError, UnicodeError) as exc: |
| 145 | test_fail("foreign-owned --backup-dir symlink transfer left module/foo " |
| 146 | f"unreadable ({exc!r}; rc={proc.returncode}):\n{attack_out}") |
| 147 | if final == NEW: |
| 148 | test_fail("foreign-owned --backup-dir symlink transfer overwrote module/foo " |
| 149 | "without taking or refusing the backup through bdir; the " |
| 150 | f"--backup-dir path may not have been exercised " |
| 151 | f"(rc={proc.returncode}):\n{attack_out}") |