| 1 | #!/usr/bin/env python3 |
| 2 | # --partial-dir symlink policy (absolute, parent-component), create and reuse. |
| 3 | # |
| 4 | # With --delay-updates the receiver stages every file in the --partial-dir before |
| 5 | # the final rename, so handle_partial_dir() must resolve (and, on first use, |
| 6 | # create) the partial dir for each file. An ABSOLUTE --partial-dir whose parent |
| 7 | # component is a symlink was followed by the bare do_*_at() path syscalls (an |
| 8 | # absolute path is operator-trusted), so a foreign-owned parent symlink |
| 9 | # redirected the staging dir -- and the staged file -- out of the tree. |
| 10 | # |
| 11 | # The fix routes handle_partial_dir() through the operator-path ownership walk: |
| 12 | # it refuses a foreign-owned parent symlink for both the do_mkdir_at() that |
| 13 | # creates the partial dir and the do_lstat_at() that reuses an existing one, so |
| 14 | # nothing is staged out of tree. --insecure-links opts out locally. Restricted |
| 15 | # to {abs} x {parent}: a relative partial dir is already confined by |
| 16 | # robust_rename()'s strict resolver, and the leaf form is replaced by a real |
| 17 | # in-tree dir (the leaf is opened O_NOFOLLOW), so neither models this escape. |
| 18 | # |
| 19 | # escape is the partial dir itself: opt's parent component (opd) is the planted |
| 20 | # symlink, so opt=<plant>/opd/sub resolves to <outside>/sub. Two cells, because |
| 21 | # the gate guards two distinct syscalls on that path: |
| 22 | # create -- the partial dir does not exist yet, so the receiver must mkdir it |
| 23 | # through the parent symlink; followed == the out-of-tree leaf appears. |
| 24 | # reuse -- the partial dir already exists (the common case for a reserved |
| 25 | # absolute partial-dir reused across runs), so the receiver lstats and |
| 26 | # stages into it; followed == the staged file advances the leaf's mtime. |
| 27 | |
| 28 | import os |
| 29 | import subprocess |
| 30 | |
| 31 | from rsyncfns import rsync_argv, run_symlink_matrix, plant_operator_symlink |
| 32 | |
| 33 | PINNED = 1000000000 # 2001-09-09; any later mtime means it was touched |
| 34 | |
| 35 | |
| 36 | def _setup(ctx): |
| 37 | src = ctx.base / 'src' |
| 38 | dest = ctx.base / 'dest' |
| 39 | src.mkdir() |
| 40 | dest.mkdir() |
| 41 | (src / 'f0').write_text("PAYLOAD-DATA\n") |
| 42 | return plant_operator_symlink(ctx, dest) |
| 43 | |
| 44 | |
| 45 | def _run(ctx, opt): |
| 46 | extra = ['--insecure-links'] if ctx.insecure else [] |
| 47 | subprocess.run( |
| 48 | rsync_argv('-a', '--delay-updates', f'--partial-dir={opt}', *extra, |
| 49 | 'src/', 'dest/'), |
| 50 | cwd=str(ctx.base), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 51 | |
| 52 | |
| 53 | def case_create(ctx): |
| 54 | # Leaf absent: the receiver must mkdir the partial dir through the parent |
| 55 | # symlink. Followed == that mkdir landed out of tree (an absolute partial-dir |
| 56 | # is not rmdir'd at the end, so the leaf survives to be observed). |
| 57 | opt, escape = _setup(ctx) |
| 58 | _run(ctx, opt) |
| 59 | return escape.exists() |
| 60 | |
| 61 | |
| 62 | def case_reuse(ctx): |
| 63 | # Leaf present: the receiver lstats the existing partial dir and stages into |
| 64 | # it. Followed == the staged file advances the out-of-tree leaf's mtime. |
| 65 | opt, escape = _setup(ctx) |
| 66 | escape.mkdir(parents=True, exist_ok=True) |
| 67 | # Pinned epoch rather than a sub-second delta: on a 1-second-granularity |
| 68 | # filesystem such as HFS+ a same-second change is invisible, and the delta |
| 69 | # test then reports a followed symlink as refused. |
| 70 | os.utime(escape, (PINNED, PINNED)) |
| 71 | pinned = escape.stat().st_mtime # what the fs actually stored |
| 72 | _run(ctx, opt) |
| 73 | return escape.stat().st_mtime != pinned |
| 74 | |
| 75 | |
| 76 | run_symlink_matrix('--partial-dir', case_create, paths=('abs',), wheres=('parent',), |
| 77 | label='create') |
| 78 | run_symlink_matrix('--partial-dir', case_reuse, paths=('abs',), wheres=('parent',), |
| 79 | label='reuse') |
| 80 | print("--partial-dir symlink policy (abs, parent; create + reuse): enforced") |