Debian Codegraph @

testsuite/acl-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Symlink-race (TOCTOU) LPE in the receiver's ACL-apply path.
3 #
4 # rsync applies a received ACL with the path-based sys_acl_set_file() ->
5 # acl_set_file(), which follows symlinks at the final component (POSIX has no
6 # AT_SYMLINK_NOFOLLOW for ACLs) and -- unlike do_chmod_at()/do_lchown_at() --
7 # has no fd-held/secure wrapper. In a "use chroot = no" daemon there is a
8 # window between do_mknod_at() creating a FIFO and set_acl() applying its ACL.
9 # A local attacker who flips the leaf fifo <-> symlink->privileged-file in that
10 # window gets the source FIFO's ACL written to the symlink target: an LPE, since
11 # the attacker chooses the ACL (e.g. grant themselves rwx on a root-owned file).
12 #
13 # The source FIFO carries a marker entry user:MARKER_UID:rwx; the test asserts
14 # the outside privileged sentinel never gains that entry no matter how the race
15 # lands. RED on the unhardened tree; GREEN once the ACL set goes through a held
16 # fd that can't be redirected by a symlink.
17
18 import os
19 import platform
20 import shutil
21 import subprocess
22 import time
23
24 from rsyncfns import (
25 SCRATCHDIR, race_budget,
26 forced_protocol, get_rootgid, get_rootuid, get_testuid,
27 rmtree, run_rsync, rsync_argv, start_path_flipper, stop_flipper,
28 start_test_daemon, test_fail, test_skipped,
29 )
30
31 DAEMON_PORT = 12896
32 MARKER_UID = 60001 # distinctive; must not appear in the baseline ACL
33
34
35 # ---- gates -----------------------------------------------------------------
36 if platform.system() != 'Linux':
37 test_skipped("POSIX ACL race test is Linux-only (setfacl/getfacl semantics)")
38 if not shutil.which('setfacl') or not shutil.which('getfacl'):
39 test_skipped("setfacl/getfacl not available")
40 # Protocol gate FIRST: this test transfers an ACL over the wire, which requires
41 # protocol 30+ regardless of the host's ACL capability. It MUST precede the
42 # ACL_at check below: on a no-xattrat box that check short-circuits to a PASS,
43 # so if it ran first a proto-29 run would PASS where an xattrat box SKIPs --
44 # diverging from the kernel-independent RSYNC_EXPECT_SKIPPED check29 list and
45 # failing CI on the no-xattrat Ubuntu runners.
46 proto = forced_protocol()
47 if proto is not None and proto < 30:
48 test_skipped(f"ACL transfer requires protocol 30+ (negotiated {proto})")
49 vv = run_rsync('-VV', check=True, capture_output=True)
50 if '"ACLs": true' not in vv.stdout:
51 test_skipped("rsync built without ACL support")
52 if '"ACL_at": true' not in vv.stdout:
53 # No race-safe ACL primitive on this kernel (no *xattrat, pre-6.13 / BSD):
54 # by project policy a received ACL is then applied via the path so --acls
55 # keeps working, which re-opens this race. The race is an accepted residual
56 # on platforms that cannot offer the secure primitive, so there is nothing
57 # to assert here. We PASS (rather than skip) so the proto-30+ skip set stays
58 # kernel-independent (a no-xattrat and an xattrat box behave the same here).
59 print("acl-symlink-race: no race-safe ACL primitive on this platform; "
60 "--acls is functional-but-racy here by policy (race not asserted)")
61 raise SystemExit(0)
62
63
64 def setfacl(spec, path):
65 if subprocess.run(['setfacl', '-m', spec, str(path)]).returncode != 0:
66 test_skipped("filesystem has ACLs disabled (setfacl failed)")
67
68
69 def getfacl(path) -> str:
70 # Plain getfacl only: the suite runs with POSIXLY_CORRECT=1, under which
71 # getfacl rejects -c/-E and accepts just the file argument.
72 return subprocess.run(['getfacl', str(path)],
73 capture_output=True, text=True).stdout
74
75
76 def facl_has_marker(path) -> bool:
77 return f'user:{MARKER_UID}:' in getfacl(path)
78
79
80 # ---- layout ----------------------------------------------------------------
81 mod = SCRATCHDIR / 'module'
82 outside = SCRATCHDIR / 'outside'
83 src = SCRATCHDIR / 'src_files'
84 conf = SCRATCHDIR / 'test-rsyncd.conf'
85
86 for d in (mod, outside, src):
87 rmtree(d)
88 d.mkdir(parents=True)
89
90 # The privileged file the attacker wants to grant themselves access to.
91 priv = outside / 'privileged'
92 priv.write_text("ROOT_OWNED_SECRET\n")
93 os.chmod(priv, 0o600)
94 if facl_has_marker(priv):
95 test_fail("baseline outside file already carries the marker ACL")
96
97 # Source FIFO carrying the attacker-chosen marker ACL.
98 victim_src = src / 'victim'
99 os.mkfifo(victim_src, 0o600)
100 setfacl(f'user:{MARKER_UID}:rwx', victim_src)
101
102 # Attacker's symlink inside the module, pointing at the privileged file.
103 os.symlink(str(priv), mod / '.evil')
104
105 my_uid = get_testuid()
106 root_uid = get_rootuid()
107 root_gid = get_rootgid()
108 uid_line = f"uid = {root_uid}"
109 gid_line = f"gid = {root_gid}"
110 if my_uid != root_uid:
111 uid_line = '#' + uid_line
112 gid_line = '#' + gid_line
113
114 conf.write_text(f"""\
115 use chroot = no
116 {uid_line}
117 {gid_line}
118 log file = {SCRATCHDIR}/rsyncd.log
119 [upload]
120 path = {mod}
121 use chroot = no
122 read only = no
123 """)
124
125 url = start_test_daemon(conf, DAEMON_PORT)
126
127
128 def push():
129 subprocess.run(
130 rsync_argv('-rtpA', '--specials', f'{src}/', f'{url}upload/'),
131 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
132 )
133
134
135 # ---- positive control: a normal push carries the FIFO's ACL into the module,
136 # so the race below isn't vacuous (ACLs really are being applied). ------------
137 push()
138 landed = mod / 'victim'
139 if not facl_has_marker(landed):
140 test_fail("positive control: a normal push did not apply the FIFO's ACL in "
141 "the module -- the daemon isn't preserving special-file ACLs, so "
142 "the race scenario would be vacuous")
143 # Clean the control artifact so the flipper starts from a known state. It is a
144 # FIFO: use unlink, not rmtree -- shutil.rmtree on a pre-3.11 Python does
145 # os.open(path, O_RDONLY) on the target, which blocks forever on a writerless
146 # FIFO (the daemon-side hang seen on Ubuntu 22.04's Python 3.10).
147 landed.unlink()
148
149
150 # ---- the race --------------------------------------------------------------
151 # Flip the destination leaf between the FIFO rsync creates and the symlink to
152 # the privileged file. If set_acl() resolves the leaf while it is the symlink,
153 # the marker ACL lands on the privileged file.
154 flip = start_path_flipper(mod / 'victim', mod / '.evil')
155 deadline = time.monotonic() + race_budget(10.0)
156 try:
157 while time.monotonic() < deadline:
158 push()
159 if facl_has_marker(priv):
160 test_fail(
161 f"ACL-set symlink race: the FIFO's user:{MARKER_UID}:rwx ACL was "
162 "applied to the outside privileged file -- the receiver's "
163 "set_acl() followed the leaf symlink (LPE). "
164 f"getfacl {priv}:\n" + getfacl(priv)
165 )
166 finally:
167 stop_flipper(flip)
168
169 # No escape within the race budget.