Debian Codegraph @

testsuite/symlink-race-relative-dest_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Relative-destination chdir symlink defense (cross-uid).
3 #
4 # A non-daemon receiver chdir()s into the operator-named destination. change_dir()
5 # now resolves it (relative or absolute) with open_no_attacker_symlinks: it
6 # follows the operator's/root's OWN symlinked dest (the /backup->/mnt/disk admin
7 # pattern) but REFUSES one owned by another uid, closing the dest-chdir TOCTOU --
8 # an attacker who races the operator-named dest from a directory to a
9 # symlink->outside can no longer redirect the receiver's writes out of the tree.
10 # (The ownership check is timing-independent, so a static attacker-owned plant
11 # subsumes the race.)
12 #
13 # Cross-uid is the real threat: root runs `rsync -a src/ dest/` from a directory
14 # where an unprivileged user planted `dest`. Requires root. This is the
15 # relative-destination variant; symlink-race-dest is the absolute companion.
16 # Found by Omar Elsayed.
17
18 import os
19 import pwd
20 import subprocess
21
22 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
23
24 if os.geteuid() != 0:
25 test_skipped("requires root to plant a symlink owned by a non-self uid "
26 "(simulates 'root runs rsync -a src/ dest/' over an "
27 "unprivileged user's directory)")
28
29 ATT_UID = None
30 for name in ('nobody', 'nfsnobody', 'daemon'):
31 try:
32 u = pwd.getpwnam(name).pw_uid
33 if u != 0 and u != os.geteuid():
34 ATT_UID = u
35 break
36 except KeyError:
37 continue
38 if ATT_UID is None:
39 test_skipped("no untrusted-uid user available for cross-uid plant")
40
41
42 def run(cwd):
43 subprocess.run(rsync_argv('-a', 'src/', 'dest/'), cwd=str(cwd),
44 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45
46
47 # --- attack: a relative dest that is an ATTACKER-owned symlink must be refused -
48 base = SCRATCHDIR / 'destchdir-rel'
49 src = base / 'src'
50 dest = base / 'dest'
51 outside = base / 'outside'
52 rmtree(base)
53 (src / 'sub').mkdir(parents=True)
54 for i in range(4):
55 (src / 'sub' / f'f{i}').write_text("payload\n")
56 outside.mkdir(parents=True)
57 os.symlink(outside, dest) # operator-named relative dest, planted...
58 os.lchown(dest, ATT_UID, ATT_UID) # ...by a non-root attacker
59
60 run(base)
61
62 escaped = sorted(p.name for p in outside.iterdir())
63 if escaped:
64 test_fail(
65 "relative destination chdir followed an ATTACKER-owned symlink: the "
66 f"receiver wrote files OUTSIDE the tree ({escaped}). change_dir() must "
67 "refuse a relative dest symlink not owned by uid 0 or the euid.")
68
69 # --- admin pattern: the operator's own (root-owned) symlinked dest still works -
70 base2 = SCRATCHDIR / 'destchdir-rel-admin'
71 src2 = base2 / 'src'
72 real = base2 / 'realdest'
73 rmtree(base2)
74 (src2 / 'sub').mkdir(parents=True)
75 (src2 / 'sub' / 'f0').write_text("payload\n")
76 real.mkdir(parents=True)
77 os.symlink(real, base2 / 'dest') # root-owned symlinked dest (admin pattern)
78
79 run(base2)
80
81 if not (real / 'sub' / 'f0').is_file():
82 test_fail(
83 "a root-owned symlinked destination (the /backup->/mnt/disk admin "
84 "pattern) was NOT followed -- the cross-uid defense must still follow "
85 "the operator's own symlinked dest.")