Debian Codegraph @

testsuite/symlink-exclude-component_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon module-exclude bypass via an in-DESTINATION symlink that appears as a
3 # PATH COMPONENT of a transferred file (not as the destination argument).
4 # (Probe for the broader variant of the symlink-exclude finding.)
5 #
6 # The module excludes a subtree (exclude = /secret/) and already contains an
7 # in-module, euid-owned symlink blink -> secret/ plus a protected victim
8 # secret/x. A peer pushes a tree whose file list contains the path "blink/x".
9 # With --keep-dirlinks the receiver treats the existing dest symlink "blink" as
10 # the directory it points at and writes THROUGH it, so "blink/x" lands in the
11 # excluded secret/x -- the per-file daemon filter only ever sees the logical
12 # name "blink/x", never the physical "secret/x".
13 #
14 # RED here = the bypass reproduces (the broader, file-list-component variant is
15 # exploitable). GREEN = rsync refuses it (e.g. replaces the symlink, or the
16 # resolver's exclude check catches the physical target). Runs unprivileged.
17 import os
18 import subprocess
19
20 from rsyncfns import (
21 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail, write_daemon_conf,
22 )
23
24 DAEMON_PORT = 12978
25 SECRET = "PROTECTED-IN-EXCLUDED-SUBTREE\n"
26
27 base = SCRATCHDIR / 'symlink-exclude-component'
28 rmtree(base)
29 base.mkdir()
30
31 mod = base / 'mod'
32 mod.mkdir()
33 secret = mod / 'secret' # excluded subtree
34 secret.mkdir()
35 victim = secret / 'x'
36 victim.write_text(SECRET) # must not be clobbered
37
38 blink = mod / 'blink' # in-module symlink -> excluded dir
39 os.symlink('secret/', blink)
40
41 src = base / 'src'
42 (src / 'blink').mkdir(parents=True) # file list will contain blink/ + blink/x
43 (src / 'blink' / 'x').write_text("NEW\n")
44
45 conf = write_daemon_conf(
46 [('mod', {'path': str(mod), 'read only': 'no', 'exclude': '/secret/'})])
47 url = start_test_daemon(conf, DAEMON_PORT)
48
49 # --keep-dirlinks: treat the dest symlink "blink" as the dir it points at.
50 subprocess.run(
51 rsync_argv('-a', '--keep-dirlinks', f'{src}/', f'{url}mod/'),
52 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
53
54 after = victim.read_text() if victim.exists() else None
55 if after == SECRET:
56 test_fail(
57 "the daemon refused a transferred path through an in-dest symlink that "
58 f"stock rsync (3.2.7) follows: {victim} is still {after!r}. The daemon "
59 "filter is name-based (it sees the logical 'blink/x', not 'secret/x'); it "
60 "must not block this -- `munge symlinks` defends a writable module.")
61 print("daemon exclude is name-based: a transferred path through an in-dest symlink "
62 "lands in the excluded subtree (3.2.7-equivalent)")