Debian Codegraph @

testsuite/operator-path-backup-symlink_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 the backed-up item is a symlink (do_symlink_at's operator path).
4 #
5 # High-frequency race variant: a native C flipper swaps the backup parent
6 # component (backup/sub) with an attacker-owned symlink to an outside directory
7 # under a live transfer. The ownership walk must refuse the foreign-owned
8 # component so no backup symlink is ever created outside the backup tree.
9
10 import os
11 import subprocess
12 import time
13
14 from rsyncfns import (
15 SCRATCHDIR, race_budget, find_attacker_uid, rmtree,
16 start_c_flipper, stop_flipper, test_fail, test_skipped,
17 )
18
19 if os.geteuid() != 0:
20 test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid")
21
22 ATT_UID = find_attacker_uid()
23 if ATT_UID is None:
24 test_skipped("no untrusted-uid user available for cross-uid plant")
25
26 # Many files widen rsync's per-file backup window so the background flipper has
27 # more chances to land the parent swap during a backup operation.
28 NFILES = 95
29 base = SCRATCHDIR / 'bdir-race'
30 src = base / 'src'
31 dest = base / 'dest'
32 backup = base / 'backup'
33 outside = base / 'outside'
34 sub = backup / 'sub'
35 sublink = backup / '.sublink'
36
37
38 def build():
39 """Reset the workspace. Call only while the flipper is stopped."""
40 rmtree(base)
41 for d in (src / 'sub', dest / 'sub', backup, outside):
42 d.mkdir(parents=True, exist_ok=True)
43
44 # Distinct source and destination symlink values so each transfer overwrites
45 # the destination symlink and thus triggers a backup of the old one.
46 for i in range(NFILES):
47 (src / 'sub' / f'f{i}').symlink_to('test')
48 (dest / 'sub' / f'f{i}').symlink_to('test2')
49
50 # The attacker-owned parent-swap target: a symlink to the outside directory.
51 os.symlink(str(outside), sublink)
52 os.lchown(sublink, ATT_UID, ATT_UID)
53 sub.mkdir(parents=True, exist_ok=True)
54
55
56 def push():
57 """Runs a blocking local rsync push invocation."""
58 return subprocess.run(
59 ['./rsync', '-a', '-b', f'--backup-dir={backup}',
60 f'{src}/', f'{dest}/'],
61 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
62 )
63
64
65 def escaped() -> bool:
66 """True if any symlink was created directly in the outside directory."""
67 with os.scandir(outside) as it:
68 for e in it:
69 if e.is_symlink():
70 return True
71 return False
72
73
74 # Positive control: a clean run (no flipper) must back the old destination
75 # symlink into the backup tree, proving this transfer shape exercises the backup
76 # symlink path. A broken fixture or an rsync that errors early fails here loudly
77 # instead of passing the race vacuously.
78 build()
79 proc = push()
80 if proc.returncode != 0:
81 test_fail(f"positive control: clean --backup-dir run failed (rc={proc.returncode}):\n{proc.stdout or ''}")
82 if not (sub / 'f0').is_symlink() or os.readlink(sub / 'f0') != 'test2':
83 test_fail("positive control: the old destination symlink was not backed up into "
84 f"{sub}; the backup symlink path was not exercised")
85 if escaped():
86 test_fail("positive control: a symlink appeared in outside/ during a no-flipper run")
87
88
89 # ---- THE LIVE RACE ---------------------------------------------------------
90
91 deadline = time.monotonic() + race_budget(10.0)
92 flip = None
93
94 try:
95 while time.monotonic() < deadline:
96 # Reset the workspace only while the flipper is quiet, so build()'s own
97 # rmtree/mkdir can't race the swapper and drop artifacts in outside/.
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 symlink escaped into "
108 f"{outside}; rsync followed the flipped attacker-owned backup/sub "
109 "component instead of refusing it."
110 )
111 finally:
112 if flip is not None:
113 stop_flipper(flip)
114
115 print("operator-path-backup-symlink: backup symlink confined under parent-swap race")