Debian Codegraph @

testsuite/log-file-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --log-file=PATH must not follow planted symlinks at attacker-controlled
3 # path components.
4 #
5 # Threat: a privileged user (root or
6 # sysadmin) runs rsync with --log-file=PATH where PATH, or a parent component
7 # of PATH, lives in an attacker-influenceable directory (canonical case:
8 # /tmp/somedir/rsync.log). An unprivileged user plants a symlink there →
9 # rsync appends its log to an attacker-chosen file. The trivially exploitable
10 # landing is /root/.ssh/authorized_keys (append an SSH key → privesc).
11 #
12 # Both variants exercised:
13 #
14 # LEAF plant: --log-file=$plant where $plant is a symlink to a victim
15 # file. Refused by the leaf O_NOFOLLOW + the path walk's
16 # ownership check.
17 #
18 # PARENT plant: --log-file=$plant/rsync.log where $plant is a symlink to a
19 # victim directory. Refused by the path walk's parent-
20 # component ownership check (the /tmp/somedir/ case --
21 # plain O_NOFOLLOW alone is leaf-only and does NOT defend
22 # this; the path walk does).
23 #
24 # Defense model (the fix on pr-symlink-fix-privopens, per the audit):
25 # walk the operator-supplied path component-by-component using
26 # openat(O_PATH|O_NOFOLLOW), and for each component that IS a symlink,
27 # fstat() the symlink itself and allow only if st_uid is 0 or geteuid().
28 # Symlinks owned by an untrusted uid → ELOOP.
29 #
30 # Requires root (to lchown a symlink to a non-self uid -- the only way to
31 # simulate a planted attacker-owned symlink in-process). Skipped otherwise.
32
33 import os
34 import pwd
35 import subprocess
36
37 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
38
39
40 if os.geteuid() != 0:
41 test_skipped("requires root to plant a symlink owned by a non-root, "
42 "non-self uid (the attacker simulation)")
43
44 NOBODY_UID = None
45 for name in ('nobody', 'nfsnobody', 'daemon'):
46 try:
47 NOBODY_UID = pwd.getpwnam(name).pw_uid
48 if NOBODY_UID != 0 and NOBODY_UID != os.geteuid():
49 break
50 except KeyError:
51 continue
52 if NOBODY_UID is None or NOBODY_UID == 0 or NOBODY_UID == os.geteuid():
53 test_skipped("no untrusted-uid user available for cross-uid plant "
54 "(tried nobody, nfsnobody, daemon)")
55
56
57 base = SCRATCHDIR / 'logfile'
58 src = base / 'src'
59 dest = base / 'dest'
60 plants = base / 'plants'
61
62 rmtree(base)
63 src.mkdir(parents=True)
64 (src / 'f').write_text("payload\n")
65 dest.mkdir(parents=True)
66 plants.mkdir(parents=True)
67
68
69 # ---------------------------------------------------------------------------
70 # LEAF plant
71 # ---------------------------------------------------------------------------
72 # A victim file the attacker wants rsync to append to. Owned by root (us),
73 # mode 0600, content we'll check is unchanged.
74 sentinel = plants / 'sentinel_leaf'
75 sentinel.write_text("LEAF_SENTINEL_INITIAL\n")
76 os.chmod(sentinel, 0o600)
77
78 # Plant: --log-file path is a symlink to the victim. The symlink itself is
79 # owned by NOBODY_UID (the attacker simulation).
80 leaf_path = plants / 'log_leaf'
81 os.symlink(sentinel, leaf_path)
82 os.lchown(leaf_path, NOBODY_UID, NOBODY_UID)
83
84 subprocess.run(
85 rsync_argv('-a', '--log-file=' + str(leaf_path), f'{src}/', f'{dest}/'),
86 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
87
88 leaf_after = sentinel.read_text()
89 if leaf_after != "LEAF_SENTINEL_INITIAL\n":
90 test_fail(
91 f"--log-file followed a planted LEAF symlink (owned by uid "
92 f"{NOBODY_UID}): {sentinel} changed from 'LEAF_SENTINEL_INITIAL' to "
93 f"{leaf_after!r}. The privileged rsync appended its log into the "
94 f"attacker-chosen file. Fix: refuse planted symlinks at --log-file.")
95
96
97 # ---------------------------------------------------------------------------
98 # PARENT-component plant (the /tmp/somedir/ case)
99 # ---------------------------------------------------------------------------
100 # A victim directory the attacker wants rsync to create files in. Owned by
101 # us, normally empty.
102 real_target = plants / 'parent_real_target'
103 real_target.mkdir()
104
105 # Plant: a directory-symlink owned by NOBODY_UID, pointing at the victim
106 # directory. --log-file=$plant/rsync.log resolves the parent (symlink) →
107 # kernel writes the log into the victim directory. The leaf "rsync.log"
108 # does not exist yet so plain O_NOFOLLOW on the leaf wouldn't reject anything;
109 # only the parent-component ownership check (the audit's owned-by-trusted-uid
110 # walk) defends here.
111 parent_plant = plants / 'parent_link'
112 os.symlink(real_target, parent_plant)
113 os.lchown(parent_plant, NOBODY_UID, NOBODY_UID)
114
115 log_via_parent = parent_plant / 'rsync.log'
116 subprocess.run(
117 rsync_argv('-a', '--log-file=' + str(log_via_parent), f'{src}/', f'{dest}/'),
118 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
119
120 # Was a log file created in the victim directory via the planted symlink?
121 outside_log = real_target / 'rsync.log'
122 if outside_log.exists():
123 test_fail(
124 f"--log-file followed a planted PARENT-component symlink (owned by "
125 f"uid {NOBODY_UID}): {parent_plant} -> {real_target} was traversed "
126 f"and rsync's log was created at {outside_log}. This is the "
127 "/tmp/somedir/-class parent-flip case that plain O_NOFOLLOW does NOT "
128 "defend (O_NOFOLLOW is leaf-only). Fix: per-component path walk that "
129 "refuses parent symlinks owned by an untrusted uid.")