Debian Codegraph @

testsuite/sender-flist-symlink-leak_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/sender-flist-symlink-leak.test.
3 #
4 # Regression test for codex re-check finding: the sender-side file-list
5 # generator could still follow an attacker-planted symlink out of the
6 # module via change_pathname() -> change_dir(...,CD_SKIP_CHDIR) ->
7 # change_dir(...,CD_NORMAL). Reach: a daemon module with use chroot =
8 # no, attacker plants module/cd -> /outside, client pulls
9 # rsync://daemon/module/cd/; the daemon would enumerate /outside in
10 # the file list (metadata leak) before the actual content transfer
11 # failed at secure_relative_open.
12
13 import os
14 import subprocess
15
16 from rsyncfns import (
17 SCRATCHDIR,
18 rsync_argv, get_testuid, get_rootuid, get_rootgid,
19 rmtree, start_test_daemon, test_fail,
20 )
21
22
23 DAEMON_PORT = 12881
24
25
26 mod = SCRATCHDIR / 'module'
27 outside = SCRATCHDIR / 'outside'
28 listfile = SCRATCHDIR / 'listed.txt'
29 conf = SCRATCHDIR / 'test-rsyncd.conf'
30
31 rmtree(mod)
32 rmtree(outside)
33 mod.mkdir(parents=True)
34 outside.mkdir(parents=True)
35
36 (outside / 'leak_marker.txt').write_text(
37 "OUTSIDE_PROTECTED_FILE_USED_AS_LEAK_DETECTOR\n"
38 )
39 os.chmod(outside / 'leak_marker.txt', 0o644)
40
41 os.symlink(str(outside), mod / 'cd')
42
43 # A legitimate in-module file, used as a positive control so the leak check
44 # below can't pass simply because the daemon's listing machinery is broken.
45 (mod / 'realdir').mkdir()
46 (mod / 'realdir' / 'in_module.txt').write_text("INSIDE_THE_MODULE\n")
47
48 my_uid = get_testuid()
49 root_uid = get_rootuid()
50 root_gid = get_rootgid()
51 uid_line = f"uid = {root_uid}"
52 gid_line = f"gid = {root_gid}"
53 if my_uid != root_uid:
54 uid_line = '#' + uid_line
55 gid_line = '#' + gid_line
56
57 conf.write_text(f"""\
58 use chroot = no
59 {uid_line}
60 {gid_line}
61 log file = {SCRATCHDIR}/rsyncd.log
62 [upload]
63 path = {mod}
64 use chroot = no
65 read only = no
66 """)
67
68 url = start_test_daemon(conf, DAEMON_PORT)
69
70 # Positive control: a normal recursive listing of an in-module path must
71 # enumerate the in-module file. If this fails, the daemon's flist generation is
72 # broken and the leak check below would be vacuously satisfied.
73 ctl = subprocess.run(
74 rsync_argv('-nrv', f'{url}upload/realdir/', f'{SCRATCHDIR}/dst/'),
75 capture_output=True, text=True,
76 )
77 if ctl.returncode != 0 or 'in_module.txt' not in ctl.stdout:
78 test_fail("positive control: listing an in-module path did not enumerate "
79 f"in_module.txt (rc={ctl.returncode}); leak check would be vacuous"
80 f"\n{ctl.stdout}{ctl.stderr}")
81
82 proc = subprocess.run(
83 rsync_argv('-nrv', f'{url}upload/cd/', f'{SCRATCHDIR}/dst/'),
84 capture_output=True, text=True,
85 )
86 if proc.returncode >= 128:
87 test_fail(f"leak pull: rsync died from a signal (rc={proc.returncode})")
88 listfile.write_text(proc.stdout + proc.stderr)
89
90 if 'leak_marker.txt' in listfile.read_text():
91 import sys
92 sys.stderr.write("----- leaked listing follows\n")
93 for line in listfile.read_text().splitlines():
94 sys.stderr.write(f" {line}\n")
95 sys.stderr.write("----- leaked listing ends\n")
96 test_fail(
97 "sender flist leak: outside/leak_marker.txt was enumerated to "
98 "the client (daemon's chdir followed the cd symlink during flist "
99 "generation)"
100 )