| 1 | #!/usr/bin/env python3 |
| 2 | # Source-tree parent-component symlink race (TOCTOU) -- scenarios 1 & 3. |
| 3 | # |
| 4 | # An unprivileged user who controls a subtree of the SOURCE races a parent |
| 5 | # directory component between a real directory and a symlink pointing OUTSIDE the |
| 6 | # source tree. A higher-authority rsync (a root nightly backup, or a victim user |
| 7 | # copying the attacker's tree) that saw the component as a directory at scan time |
| 8 | # but follows the flipped symlink when it opens a file under it then reads a file |
| 9 | # it should not and copies its contents into the output. |
| 10 | # |
| 11 | # This is the source-side counterpart of nondaemon-symlink-race, which uses |
| 12 | # --backup-dir as a deterministic, race-free RECEIVER primitive. The pure |
| 13 | # source-read path has no operator-config component the attacker controls: |
| 14 | # rsync's directory descent lstat()s every component with O_NOFOLLOW, so a STATIC |
| 15 | # symlink parent is detected and copied as a symlink, never followed. The escape |
| 16 | # needs a genuine TOCTOU -- the component must be a directory when rsync scans it |
| 17 | # (so its files enter the file list) and a symlink when rsync opens those files |
| 18 | # -- so we run a flipping thread and retry for up to RACE_TIMEOUT seconds. |
| 19 | # |
| 20 | # Status: FIXED. For default symlink handling the sender now opens each file's |
| 21 | # content through the confined held-ancestor-dirfd stack anchored at the transfer |
| 22 | # root, so a parent component flipped to a symlink->outside is refused. The |
| 23 | # symlink-following modes (-L/--copy-links/--copy-unsafe-links/-k) still follow, |
| 24 | # and --insecure-links restores the legacy open. The test asserts the secret |
| 25 | # does NOT leak: a leak is a hard FAIL. |
| 26 | |
| 27 | import os |
| 28 | import platform |
| 29 | import subprocess |
| 30 | import time |
| 31 | |
| 32 | from rsyncfns import ( |
| 33 | SCRATCHDIR, race_budget, rmtree, rsync_argv, rsync_supports, |
| 34 | start_path_flipper, stop_flipper, test_fail, test_xfail, |
| 35 | ) |
| 36 | |
| 37 | _CYGWIN = platform.system().startswith('CYGWIN') |
| 38 | |
| 39 | SECRET = "OUTSIDE-SECRET-do-not-copy" |
| 40 | NFILES = 64 |
| 41 | |
| 42 | base = SCRATCHDIR / 'srcrace' |
| 43 | src = base / 'src' |
| 44 | outside = base / 'outside' |
| 45 | dest = base / 'dest' |
| 46 | |
| 47 | rmtree(base) |
| 48 | # The real-directory state of the flipping component, with in-tree dummy files. |
| 49 | realsub = src / '.realsub' |
| 50 | realsub.mkdir(parents=True) |
| 51 | for i in range(NFILES): |
| 52 | (realsub / f'loot{i}').write_text("in-tree dummy\n") |
| 53 | # The out-of-tree target the attacker wants rsync to read, under the same names. |
| 54 | outside.mkdir(parents=True) |
| 55 | for i in range(NFILES): |
| 56 | (outside / f'loot{i}').write_text(SECRET + "\n") |
| 57 | # 'sub' starts as the real directory; 'evil' is the escaping symlink. The flipper |
| 58 | # swaps them so 'sub' alternates directory <-> symlink->../outside. |
| 59 | os.rename(realsub, src / 'sub') |
| 60 | os.symlink('../outside', src / 'evil') |
| 61 | dest.mkdir(parents=True) |
| 62 | |
| 63 | |
| 64 | def leaked(): |
| 65 | d = dest / 'sub' |
| 66 | if d.is_symlink() or not d.is_dir(): |
| 67 | return False |
| 68 | for f in d.iterdir(): |
| 69 | try: |
| 70 | if f.read_text().strip() == SECRET: |
| 71 | return True |
| 72 | except OSError: |
| 73 | pass |
| 74 | return False |
| 75 | |
| 76 | |
| 77 | # --no-inc-recursive widens the TOCTOU window: rsync scans the whole tree |
| 78 | # first (recording sub/ as a directory) and only then opens the files under |
| 79 | # it, so the flipper has the entire read phase -- 64 separate opens of |
| 80 | # sub/* -- to flip sub to a symlink. The disclosure exists with incremental |
| 81 | # recursion too; this just makes the race reliably winnable within the |
| 82 | # budget rather than depending on lucky scan/read interleaving. |
| 83 | # |
| 84 | # Pass the flag only when the rsync binary accepts it: implementations |
| 85 | # without incremental recursion (gokrazy/rsync targets protocol 27, no |
| 86 | # inc-recursive at all) reject --no-inc-recursive as an unknown option, |
| 87 | # but they already default to the safer pre-inc-recursive behaviour. |
| 88 | extra = ['--no-inc-recursive'] if rsync_supports('--no-inc-recursive') else [] |
| 89 | |
| 90 | flip = start_path_flipper(src / 'sub', src / 'evil') |
| 91 | won = False |
| 92 | deadline = time.monotonic() + race_budget() |
| 93 | try: |
| 94 | while time.monotonic() < deadline: |
| 95 | rmtree(dest) |
| 96 | dest.mkdir(parents=True) |
| 97 | subprocess.run(rsync_argv('-a', *extra, f'{src}/', f'{dest}/'), |
| 98 | stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 99 | if leaked(): |
| 100 | won = True |
| 101 | break |
| 102 | finally: |
| 103 | stop_flipper(flip) |
| 104 | |
| 105 | if won: |
| 106 | if _CYGWIN: |
| 107 | # Cygwin emulates symlinks as special files and its open()/path |
| 108 | # resolution does not enforce the per-component O_NOFOLLOW walk the |
| 109 | # confined sender open relies on, so the parent-flip leak is still |
| 110 | # observed here. The native POSIX symlink-race tests are likewise not |
| 111 | # enforced on Cygwin (see cygwin-build.yml); track this as a documented |
| 112 | # Cygwin platform residual rather than a hard failure. |
| 113 | test_xfail( |
| 114 | f"cygwin: source-tree parent-flip race still leaks ({SECRET!r}); the " |
| 115 | "confined sender open is compiled in but Cygwin's symlink emulation " |
| 116 | "does not enforce it -- documented platform residual.") |
| 117 | test_fail( |
| 118 | f"source-tree TOCTOU: content from outside the source tree ({SECRET!r}) " |
| 119 | "was copied into the output -- the sender followed a parent-component " |
| 120 | "symlink it should have confined.") |
| 121 | # No leak within the race budget -> the source-side resolution was confined. |