Debian Codegraph @

testsuite/backup-dir-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Non-daemon (local) --backup-dir parent-component symlink-race confinement.
3 #
4 # Companion to nondaemon-symlink-race (which pins the DETERMINISTIC leaf case:
5 # the whole --backup-dir is a pre-planted escaping symlink). Here the escaping
6 # symlink is a PARENT component of the backup path that an attacker flips
7 # dir<->symlink UNDER A RACE:
8 #
9 # rsync -a -b --chmod=777 --backup-dir=backup src/ dest/
10 #
11 # Differing files in dest/sub/ are backed up to backup/sub/. A concurrent
12 # flipper swaps backup/sub between a real directory and a symlink -> outside.
13 # If the receiver resolves the backup path while "sub" is the symlink and
14 # renames the old dest file through it, the (0777) backup lands in outside/,
15 # clobbering a sentinel there -- an escape. The static parent-component case
16 # does NOT reproduce (a pre-existing symlink-to-dir is handled without escape);
17 # only the race window exposes it, so this case needs the flipper.
18 #
19 # The operator-path policy follows a symlink owned by uid 0 or the euid (the
20 # operator's OWN backup dir) and refuses a foreign-owned one, so the boundary is
21 # an ATTACKER-owned backup symlink with rsync run as root. The held-dirfd /
22 # ownership-walk resolver must refuse it on every flip, not just statically.
23 #
24 # RED on stock rsync (the pre-resolver code follows the parent symlink under the
25 # race); GREEN once make_backup resolves through the operator-path resolver.
26 # --insecure-links is the documented opt-out and is exercised by the matrix
27 # tests, not here.
28
29 import os
30 import stat
31 import subprocess
32 import time
33
34 from rsyncfns import (
35 SCRATCHDIR, race_budget, find_attacker_uid,
36 rmtree, rsync_argv, start_c_flipper, stop_flipper, test_fail, test_skipped,
37 )
38
39 # Cross-uid plant needs root: only root can own a symlink by a foreign uid and
40 # thereby make it "not the operator's own". At any other uid the boundary
41 # cannot be built, so the race would be vacuous.
42 if os.geteuid() != 0:
43 test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid")
44 ATT_UID = find_attacker_uid()
45 if ATT_UID is None:
46 test_skipped("no untrusted-uid user available for cross-uid plant")
47
48 NFILES = 95
49
50 base = SCRATCHDIR / 'bdir-race'
51 src = base / 'src'
52 dest = base / 'dest'
53 backup = base / 'backup'
54 outside = base / 'outside'
55 sub = backup / 'sub' # the parent the attacker flips dir<->symlink
56 sublink = backup / '.sublink' # symlink -> outside, swapped in for `sub`
57
58
59 def build():
60 rmtree(base)
61 for d in (src / 'sub', dest / 'sub', backup, outside):
62 d.mkdir(parents=True)
63 for i in range(NFILES):
64 (src / 'sub' / f'f{i}').write_text('payload\n')
65 os.chmod(src / 'sub' / f'f{i}', 0o777)
66 (dest / 'sub' / f'f{i}').write_text('different\n')
67 os.chmod(dest / 'sub' / f'f{i}', 0o777)
68 # Out-of-tree sentinels at a distinctive 0600: a 0777 here means a
69 # backed-up dest file escaped through the flipped symlink.
70 (outside / f'f{i}').write_text('do-not-touch\n')
71 os.chmod(outside / f'f{i}', 0o600)
72 sub.mkdir()
73 os.symlink(str(outside), sublink)
74 os.lchown(sublink, ATT_UID, ATT_UID) # foreign-owned: must be refused
75
76
77 def push():
78 subprocess.run(
79 rsync_argv('-a', '-b', '--chmod=777', f'--backup-dir={backup}',
80 f'{src}/', f'{dest}/'),
81 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
82 )
83
84
85 def escaped() -> bool:
86 # Any out-of-tree sentinel turned 0777 == a backup escaped through the symlink.
87 with os.scandir(outside) as it:
88 for e in it:
89 if e.is_file() and stat.S_IMODE(e.stat().st_mode) == 0o777:
90 return True
91 return False
92
93
94 # ---- positive control: with `sub` a plain dir, a normal push really does back
95 # up into backup/sub/, so the race below isn't vacuous. ----------------------
96 build()
97 push()
98 if not (sub / 'f0').is_file():
99 test_fail("positive control: a normal push did not create backup/sub/f0 -- "
100 "backups are not happening, so the race scenario would be vacuous")
101 if escaped():
102 test_fail("positive control: a sentinel was already 0777 before the race")
103
104
105 # ---- the race --------------------------------------------------------------
106 # Flip backup/sub between the real dir and the attacker-owned symlink->outside.
107 # The swap is name-only renames, so the symlink keeps its foreign owner; the
108 # resolver must refuse it on every flip it happens to observe. The compiled
109 # flipper (renameat2 EXCHANGE) flips fast enough to win the window reliably even
110 # on a slow journaled fs; it degrades to the Python flipper where no compiler is.
111 flip = start_c_flipper(sub, sublink)
112 try:
113 deadline = time.monotonic() + race_budget(10.0)
114 while time.monotonic() < deadline:
115 # Re-dirty the dest files so every push triggers fresh backups.
116 for i in range(80):
117 (dest / 'sub' / f'f{i}').write_text('changed\n')
118 push()
119 if escaped():
120 test_fail(
121 "backup-dir parent symlink race: a 0777 dest file was backed up "
122 f"into {outside} through the flipped backup/sub symlink -- "
123 "make_backup followed a foreign-owned parent component (escape).")
124 finally:
125 stop_flipper(flip)
126
127 # No escape within the race budget.