Debian Codegraph @

testsuite/operator-path-backup-rmdir_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Non-daemon (local) --backup-dir parent-component symlink-race confinement, for
3 # the case where a backup replaces a pre-existing backup directory (do_rmdir_at's
4 # operator path: removing backup/sub/subN before writing the backed-up file).
5 #
6 # A native C flipper swaps the backup parent component (backup/sub) with an
7 # attacker-owned symlink to an outside directory during a live transfer. The
8 # ownership walk must refuse the foreign-owned component so the rmdir never
9 # escapes to remove a directory outside the backup tree.
10
11 import os
12 import subprocess
13 import time
14
15 from rsyncfns import (
16 SCRATCHDIR, race_budget, find_attacker_uid, rmtree,
17 start_c_flipper, stop_flipper, test_fail, test_skipped,
18 )
19
20 if os.geteuid() != 0:
21 test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid")
22
23 ATT_UID = find_attacker_uid()
24 if ATT_UID is None:
25 test_skipped("no untrusted-uid user available for cross-uid plant")
26
27 # Many files widen the per-file backup window so the flipper has more chances to
28 # land the parent swap during a backup rmdir.
29 NFILES = 50
30 base = SCRATCHDIR / 'bdir-race'
31 src = base / 'src'
32 dest = base / 'dest'
33 backup = base / 'backup'
34 outside = base / 'outside'
35 sub = backup / 'sub'
36 sublink = backup / '.sublink'
37
38
39 def build():
40 """Reset the workspace. Call only while the flipper is stopped."""
41 rmtree(base)
42 for d in (src / 'sub', dest / 'sub', backup, outside):
43 d.mkdir(parents=True, exist_ok=True)
44
45 for i in range(NFILES):
46 # The backup target pre-exists as a directory; backing up the old dest
47 # file over it forces rsync to rmdir backup/sub/subN first.
48 (sub / f'sub{i}').mkdir(parents=True, exist_ok=True)
49 # A sentinel directory outside the tree; if the rmdir follows the flipped
50 # parent symlink, outside/subN gets removed.
51 (outside / f'sub{i}').mkdir(exist_ok=True)
52 (src / 'sub' / f'sub{i}').write_text('payload')
53 (dest / 'sub' / f'sub{i}').write_text('different')
54
55 os.symlink(str(outside), sublink)
56 os.lchown(sublink, ATT_UID, ATT_UID)
57
58
59 def push():
60 """Runs a blocking local rsync push invocation."""
61 return subprocess.run(
62 ['./rsync', '-a', '-b', f'--backup-dir={backup}',
63 f'{src}/', f'{dest}/'],
64 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
65 )
66
67
68 def escaped() -> bool:
69 """True if any outside sentinel directory was removed (escaped rmdir)."""
70 for i in range(NFILES):
71 if not (outside / f'sub{i}').is_dir():
72 return True
73 return False
74
75
76 # Positive control: a clean run (no flipper) must back the old dest file over the
77 # pre-existing backup directory, proving this transfer shape exercises the backup
78 # rmdir path. A broken fixture or an rsync that errors early fails here loudly
79 # instead of passing the race vacuously.
80 build()
81 proc = push()
82 if proc.returncode != 0:
83 test_fail(f"positive control: clean --backup-dir run failed (rc={proc.returncode}):\n{proc.stdout or ''}")
84 if not (sub / 'sub0').is_file() or (sub / 'sub0').read_text() != 'different':
85 test_fail("positive control: the old destination file was not backed up over the "
86 "pre-existing backup directory; the backup rmdir path was not exercised")
87 if escaped():
88 test_fail("positive control: an outside sentinel dir vanished during a no-flipper run")
89
90
91 # ---- THE LIVE RACE ---------------------------------------------------------
92
93 deadline = time.monotonic() + race_budget(10.0)
94 flip = None
95
96 try:
97 while time.monotonic() < deadline:
98 if flip is not None:
99 stop_flipper(flip)
100 flip = None
101 build()
102 flip = start_c_flipper(sub, sublink)
103 push()
104
105 if escaped():
106 test_fail(
107 "--backup-dir parent symlink race: a backup rmdir escaped the tree, "
108 f"removing a sentinel directory under {outside}; rsync followed the "
109 "flipped attacker-owned backup/sub component instead of refusing it."
110 )
111 finally:
112 if flip is not None:
113 stop_flipper(flip)
114
115 print("operator-path-backup-rmdir: backup rmdir confined under parent-swap race")