Debian Codegraph @

testsuite/excludefrom-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --exclude-from / --include-from / --files-from / --filter=. file must not
3 # follow planted symlinks at attacker-controlled path components.
4 #
5 # Threat:
6 # privileged rsync reads from a planted symlink → file contents become
7 # filter rules / file list → silent transfer shaping, content leak via
8 # -v / -i / --debug output.
9 #
10 # Signal in this test (--exclude-from variant): the planted file's content
11 # is the basename of a "canary" file we placed in src. With the leak, rsync
12 # parses the planted content as an exclude rule and drops the canary from
13 # the transfer. With the fix, rsync errors on the planted symlink open
14 # (XFLG_FATAL_ERRORS at options.c:1543 + exclude.c:1477).
15 #
16 # Failure condition: returncode == 0 AND canary missing from dest. The only
17 # way that combination arises is if rsync read the planted symlink's target
18 # as filter rules. With the fix, rsync exits non-zero; with no leak at all,
19 # the canary transfers.
20 #
21 # 1.5 (-C $HOME/.cvsignore) is covered by the same fix code path
22 # (parse_filter_file callee shared with 1.1's per-dir merge); we don't
23 # duplicate that test here. The XFLG flag differs (1.5 is silent-skip,
24 # 1.4 is FATAL) but the open primitive is the same.
25 #
26 # Requires root for the cross-uid plant; skips otherwise.
27
28 import os
29 import pwd
30 import subprocess
31
32 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
33
34
35 if os.geteuid() != 0:
36 test_skipped("requires root to plant a symlink owned by a non-self uid "
37 "(the attacker simulation)")
38
39 NOBODY_UID = None
40 for name in ('nobody', 'nfsnobody', 'daemon'):
41 try:
42 u = pwd.getpwnam(name).pw_uid
43 if u != 0 and u != os.geteuid():
44 NOBODY_UID = u
45 break
46 except KeyError:
47 continue
48 if NOBODY_UID is None:
49 test_skipped("no untrusted-uid user available for cross-uid plant")
50
51
52 base = SCRATCHDIR / 'excludefrom'
53 src = base / 'src'
54 dest = base / 'dest'
55 outside = base / 'outside'
56
57 rmtree(base)
58 src.mkdir(parents=True)
59 (src / 'canary_file').write_text("would-be-transferred\n")
60 (src / 'other_file').write_text("noise\n")
61 dest.mkdir(parents=True)
62 outside.mkdir(parents=True)
63
64 # Outside attacker-readable content: a CVS-style pattern that matches
65 # src/canary_file. If rsync follows the planted symlink and parses this as
66 # filter rules, it adds an exclude rule that drops canary_file.
67 (outside / 'exclude_list').write_text("canary_file\n")
68
69
70 def run_excludefrom(plant_path):
71 rmtree(dest)
72 dest.mkdir(parents=True)
73 return subprocess.run(
74 rsync_argv('-a', '--exclude-from=' + str(plant_path),
75 f'{src}/', f'{dest}/'),
76 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
77
78
79 plants = base / 'plants'
80 plants.mkdir()
81
82
83 # ---------------------------------------------------------------------------
84 # LEAF plant
85 # ---------------------------------------------------------------------------
86 leaf_plant = plants / 'exclude_leaf'
87 os.symlink(outside / 'exclude_list', leaf_plant)
88 os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID)
89
90 leaf = run_excludefrom(leaf_plant)
91 canary = dest / 'canary_file'
92 if leaf.returncode == 0 and not canary.exists():
93 test_fail(
94 f"--exclude-from followed a planted LEAF symlink (owned by uid "
95 f"{NOBODY_UID}): rsync read the symlink's target as filter rules and "
96 f"applied the leaked pattern, dropping src/canary_file from the "
97 f"transfer. Fix: refuse planted symlinks at --exclude-from.")
98
99
100 # ---------------------------------------------------------------------------
101 # PARENT-component plant
102 # ---------------------------------------------------------------------------
103 real_target = plants / 'parent_real_target'
104 real_target.mkdir()
105 (real_target / 'exclude_list').write_text("canary_file\n")
106
107 parent_plant = plants / 'parent_link'
108 os.symlink(real_target, parent_plant)
109 os.lchown(parent_plant, NOBODY_UID, NOBODY_UID)
110
111 parent = run_excludefrom(parent_plant / 'exclude_list')
112 canary = dest / 'canary_file'
113 if parent.returncode == 0 and not canary.exists():
114 test_fail(
115 f"--exclude-from followed a planted PARENT-component symlink (owned "
116 f"by uid {NOBODY_UID}): {parent_plant} -> {real_target} was "
117 f"traversed and the leaked exclude_list was applied, dropping "
118 f"canary_file from the transfer. /tmp/somedir/-class parent-flip "
119 "that plain O_NOFOLLOW does NOT defend. Fix: per-component path walk "
120 "with parent-symlink ownership check.")