| 1 | #!/usr/bin/env python3 |
| 2 | # A daemon sender must not let a symlink-following mode (-L / --copy-dirlinks / |
| 3 | # --copy-unsafe-links) escape the served MODULE during the directory scan. |
| 4 | # |
| 5 | # The content open is confined beneath module_dir even in these modes |
| 6 | # (sender_open_copylinks_confined), but the directory ENUMERATION (opendir(fbuf) |
| 7 | # in flist.c send_directory) was only confined in the default no-follow mode. |
| 8 | # So a daemon module containing an in-module symlinked directory that points |
| 9 | # OUTSIDE the module, pulled with --copy-dirlinks, made the sender follow it and |
| 10 | # enumerate the out-of-module directory, leaking its entry names, metadata and |
| 11 | # (preserved) symlink targets to the client -- even though the content open |
| 12 | # refuses the out-of-module file data. |
| 13 | # |
| 14 | # Deterministic: a STATIC in-module symlink, no race. --copy-dirlinks descends |
| 15 | # the symlinked directory (the escape vector) but preserves file symlinks under |
| 16 | # it, so an out-of-module symlink whose target is a unique marker proves the |
| 17 | # enumeration -- not the content open -- escaped the module. FAILS before the |
| 18 | # daemon-follow-mode confinement; PASSES once the enumeration is confined beneath |
| 19 | # module_dir in following modes too (still following in-module symlinks, refusing |
| 20 | # module escapes). |
| 21 | |
| 22 | import os |
| 23 | |
| 24 | from rsyncfns import ( |
| 25 | SCRATCHDIR, get_rootgid, get_rootuid, get_testuid, makepath, rmtree, |
| 26 | run_rsync, start_test_daemon, test_fail, |
| 27 | ) |
| 28 | |
| 29 | DAEMON_PORT = 12947 |
| 30 | MARKER = "XFIL-OUTSIDE-MODULE-SYMLINK-TARGET" |
| 31 | |
| 32 | base = SCRATCHDIR / 'daemon-scan-escape' |
| 33 | rmtree(base) |
| 34 | mod = base / 'mod' # the served module |
| 35 | outside = base / 'outside' # OUTSIDE the module (a sibling of mod/) |
| 36 | dest = base / 'dest' |
| 37 | makepath(mod / 'real', outside, dest) |
| 38 | |
| 39 | # Legitimate in-module content (sanity: the transfer runs and --copy-dirlinks |
| 40 | # is active). |
| 41 | (mod / 'real' / 'in.txt').write_text("in-module\n") |
| 42 | |
| 43 | # The escape vector: an in-module symlink to a DIRECTORY outside the module. |
| 44 | # --copy-dirlinks treats it as a directory and descends it. |
| 45 | os.symlink('../outside', mod / 'escape') |
| 46 | |
| 47 | # Out-of-module sentinels. xfil_link is a FILE symlink (not dereferenced by |
| 48 | # --copy-dirlinks), so its marker target is transferred verbatim via readlinkat |
| 49 | # during the scan -- a pure enumeration signal. The dir/file names are |
| 50 | # secondary signals. |
| 51 | os.symlink(MARKER, outside / 'xfil_link') |
| 52 | (outside / 'xfil_dir').mkdir() |
| 53 | (outside / 'xfil_dir' / 'inner').write_text("x\n") |
| 54 | (outside / 'xfil_file').write_text("x\n") |
| 55 | |
| 56 | # A use-chroot=no daemon drops to nobody under a root test run and can't read a |
| 57 | # root-owned module, so as root we pin it to root (as build_rsyncd_conf does); |
| 58 | # off-root it runs as us and must not set uid/gid, so those lines simply vanish. |
| 59 | root = get_testuid() == get_rootuid() |
| 60 | ids = f"uid = {get_rootuid()}\ngid = {get_rootgid()}" if root else "" |
| 61 | |
| 62 | conf = base / 'daemon-scan.conf' |
| 63 | conf.write_text(f"""\ |
| 64 | pid file = {base}/rsyncd.pid |
| 65 | use chroot = no |
| 66 | {ids} |
| 67 | log file = {base}/rsyncd.log |
| 68 | |
| 69 | [m] |
| 70 | path = {mod} |
| 71 | read only = yes |
| 72 | hosts allow = 127.0.0.1 |
| 73 | """) |
| 74 | url = start_test_daemon(conf, DAEMON_PORT) |
| 75 | |
| 76 | # --copy-dirlinks makes the daemon sender descend the symlinked 'escape' dir. |
| 77 | # check=False: with the fix the escape is refused, which may surface as a |
| 78 | # per-entry error; assert on the resulting tree either way. |
| 79 | run_rsync('-a', '--copy-dirlinks', f'{url}m/', f'{dest}/', check=False) |
| 80 | |
| 81 | esc = dest / 'escape' |
| 82 | leak = None |
| 83 | link = esc / 'xfil_link' |
| 84 | if link.is_symlink() and os.readlink(link) == MARKER: |
| 85 | leak = f"symlink target leaked: dest/escape/xfil_link -> {MARKER}" |
| 86 | else: |
| 87 | for name in ('xfil_dir', 'xfil_file', 'xfil_link'): |
| 88 | if (esc / name).exists() or (esc / name).is_symlink(): |
| 89 | leak = f"out-of-module name leaked: dest/escape/{name}" |
| 90 | break |
| 91 | |
| 92 | if leak is not None: |
| 93 | test_fail( |
| 94 | "daemon directory-enumeration escaped the module in a symlink-following " |
| 95 | f"mode: the sender followed an in-module symlink out of the module and " |
| 96 | f"copied an out-of-module entry to the client ({leak}). send_directory's " |
| 97 | "enumeration must stay confined beneath module_dir in following modes " |
| 98 | "too, like the content open.") |
| 99 | print("daemon-scan-dir-escape: --copy-dirlinks enumeration stayed within the module") |