Debian Codegraph @

testsuite/filter-merge-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Per-directory filter merge files (.cvsignore under -C, dir-merge rules)
3 # must not follow a planted symlink at an attacker-controlled path.
4 #
5 # Threat (the "root runs `rsync -a /home /backup`" disclosure class):
6 # during a recursive scan the sender opens a per-directory filter file from
7 # EACH source directory (exclude.c:1464 fopen via parse_filter_file, called
8 # from push_local_filters at exclude.c:811-814). The path is composed as
9 # "<scanned source dir>/<merge pattern>", so an unprivileged user who
10 # controls a source directory can plant `.cvsignore` (or whatever the
11 # dir-merge filename is) as a symlink to ANY file the privileged rsync can
12 # read. rsync then parses that file's bytes as filter rules -- with two
13 # observable effects:
14 #
15 # (a) Content shaping. Lines of the planted-target file become filter
16 # patterns, which affect what does or doesn't transfer; the operator
17 # cannot easily detect this. (Demonstrated below.)
18 # (b) Direct disclosure under --debug=FILTER. The parsed rules are
19 # printed verbatim. (Not exercised here; demonstrated by manual
20 # inspection.)
21 #
22 # The fix refuses symlinks anywhere in the path unless owned by uid 0 or
23 # our effective uid (the trusted set). Cross-uid setup is the
24 # realistic threat shape (privileged user runs rsync over an unprivileged
25 # user's tree): the runner is in the trusted set, the planted symlink is
26 # not, so the fix refuses it; parse_filter_file silently skips the file
27 # (XFLG_ANCHORED2ABS, not XFLG_FATAL_ERRORS), no exclude is added, and
28 # the canary transfers normally.
29 #
30 # Requires root to plant a symlink owned by a non-self uid (the attacker
31 # simulation); skips otherwise -- without cross-uid the runner trusts
32 # their own symlinks and the fix has nothing to refuse.
33
34 import os
35 import pwd
36 import subprocess
37
38 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
39
40
41 if os.geteuid() != 0:
42 test_skipped("requires root to plant a symlink owned by a non-self uid "
43 "(simulates the 'root runs `rsync -a /home /backup`' case)")
44
45 NOBODY_UID = None
46 for name in ('nobody', 'nfsnobody', 'daemon'):
47 try:
48 u = pwd.getpwnam(name).pw_uid
49 if u != 0 and u != os.geteuid():
50 NOBODY_UID = u
51 break
52 except KeyError:
53 continue
54 if NOBODY_UID is None:
55 test_skipped("no untrusted-uid user available for cross-uid plant")
56
57
58 base = SCRATCHDIR / 'filtermerge'
59 src = base / 'src'
60 dest = base / 'dest'
61 outside = base / 'outside'
62
63 rmtree(base)
64 (src / 'sub').mkdir(parents=True)
65 # A canary the test will look for in the destination. It will be excluded by
66 # rsync IFF a planted .cvsignore symlink is followed and its content (the
67 # pattern below) is applied as a filter rule.
68 (src / 'sub' / 'canary_file').write_text("would-be-transferred\n")
69
70 # The attacker-readable "secret" outside the source tree. Its contents are a
71 # single CVS-style pattern that exactly matches the canary's basename.
72 outside.mkdir(parents=True)
73 (outside / 'attacker_target').write_text("canary_file\n")
74
75 # Plant: src/sub/.cvsignore is a symlink to the outside file, OWNED BY THE
76 # ATTACKER (NOBODY_UID). With -C, rsync normally reads .cvsignore as filter
77 # rules per directory; a privileged rsync over an unprivileged-user-controlled
78 # source thus reads the symlink's target unless defended.
79 plant = src / 'sub' / '.cvsignore'
80 os.symlink('../../outside/attacker_target', plant)
81 os.lchown(plant, NOBODY_UID, NOBODY_UID)
82
83 dest.mkdir(parents=True)
84
85 # -aC -- preserve everything + cvs-exclude (reads .cvsignore per dir).
86 # The transfer itself succeeds either way; the question is whether the planted
87 # .cvsignore was followed and its content applied as filter rules.
88 subprocess.run(
89 rsync_argv('-aC', f'{src}/', f'{dest}/'),
90 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
91
92 if not (dest / 'sub' / 'canary_file').exists():
93 test_fail(
94 f"per-dir merge file .cvsignore (planted as a symlink owned by uid "
95 f"{NOBODY_UID}) was followed: rsync read the symlink's target outside "
96 f"the source tree and applied its content as a filter rule, dropping "
97 f"src/sub/canary_file from the transfer. The operator-controlled "
98 f"path was bypassed via an unprivileged-user-planted symlink. Fix: "
99 f"refuse symlinks owned by untrusted uids at the per-dir merge open."
100 )