Debian Codegraph @

testsuite/symlink-race-dest_test.py

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