| 1 | #!/usr/bin/env python3 |
| 2 | # --relative implied-parent creation escapes the tree via make_path(). |
| 3 | # |
| 4 | # Receiver write escape (scenario 2), in a |
| 5 | # code path the held-dirfd hardening does NOT cover. Under --relative (-R) the |
| 6 | # receiver re-creates the implied parent directories of each transferred path. |
| 7 | # When a needed parent is missing it calls make_path() (util1.c), which creates |
| 8 | # every missing component with a PLAIN do_mkdir() on the full multi-component |
| 9 | # path -- no secure_relative_open, no held dirfd, no O_NOFOLLOW. The per-entry |
| 10 | # leaf mkdir was converted to the hardened do_mkdir_at(), but make_path()'s |
| 11 | # parent-chain creation was not, so it still follows a symlink planted at any |
| 12 | # parent component and creates directories at the symlink's target, OUTSIDE the |
| 13 | # destination tree. |
| 14 | # |
| 15 | # Trigger (generator.c recv_generator, the relative_paths && !implied_dirs site): |
| 16 | # pushing a file whose implied parent is absent makes the generator call |
| 17 | # make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) |
| 18 | # to build the parent chain. We use --no-implied-dirs so the parents are not |
| 19 | # pre-created as their own (hardened do_mkdir_at) entries, forcing the make_path |
| 20 | # fallback. An attacker-planted parent symlink (dest/A -> outside) then makes |
| 21 | # make_path create dest/A/B == outside/B and the file lands at outside/B/file. |
| 22 | # |
| 23 | # Deterministic (no race window): the escaping component is pre-planted, exactly |
| 24 | # like nondaemon-symlink-race. Runs at any uid; root (a nightly `rsync -aR` |
| 25 | # backup over a tree with an attacker-writable component) is the high-severity |
| 26 | # instance. No RESOLVE_BENEATH gate: make_path() is unconfined on every |
| 27 | # platform, so creating a brand-new directory has no legitimate reason to follow |
| 28 | # a symlink component -- the fix should refuse the escape everywhere. |
| 29 | # |
| 30 | # Fixed: make_path()'s parent-chain creation now goes through the held-dirfd |
| 31 | # do_mkdir_at(), which confines each component beneath the destination, so the |
| 32 | # escape is refused. This is a regression guard -- it asserts nothing escapes |
| 33 | # and fails hard if that confinement is ever lost. |
| 34 | |
| 35 | import subprocess |
| 36 | |
| 37 | from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail |
| 38 | |
| 39 | |
| 40 | srcroot = SCRATCHDIR / 'srcroot' |
| 41 | dest = SCRATCHDIR / 'dest' |
| 42 | outside = SCRATCHDIR / 'outside' |
| 43 | for d in (srcroot, dest, outside): |
| 44 | rmtree(d) |
| 45 | d.mkdir(parents=True) |
| 46 | |
| 47 | # A transferred file two levels below the implied-dir boundary (the '/./'). |
| 48 | (srcroot / 'A' / 'B').mkdir(parents=True) |
| 49 | (srcroot / 'A' / 'B' / 'file').write_text("PAYLOAD\n") |
| 50 | |
| 51 | # The attacker-planted parent: dest/A is a symlink pointing OUTSIDE the tree. |
| 52 | (dest / 'A').symlink_to(outside) |
| 53 | |
| 54 | # -R replicates the path after '/./' (A/B/file) under dest; --no-implied-dirs |
| 55 | # forces the make_path() parent-chain fallback rather than per-dir entries. |
| 56 | subprocess.run( |
| 57 | rsync_argv('-aR', '--no-implied-dirs', f'{srcroot}/./A/B/file', f'{dest}/'), |
| 58 | stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 59 | ) |
| 60 | |
| 61 | # The escape: make_path() resolved dest/A/B through the planted dest/A symlink |
| 62 | # and created the directory (and file) in the outside-the-tree directory. |
| 63 | try: |
| 64 | leaked = sorted(p.name for p in outside.iterdir()) |
| 65 | except OSError: |
| 66 | leaked = [] |
| 67 | |
| 68 | if leaked: |
| 69 | test_fail( |
| 70 | "--relative make_path() escaped the destination tree: implied-parent " |
| 71 | f"creation landed in {outside} ({leaked}) by following a planted " |
| 72 | "dest/A -> outside symlink -- make_path()'s parent-chain creation must " |
| 73 | "stay routed through the held-dirfd do_mkdir_at().") |
| 74 | # No escape -> make_path confined the implied-parent creation beneath the dest. |