Debian Codegraph @

testsuite/copy-dest-source-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/copy-dest-source-symlink.test.
3 #
4 # Regression test for codex audit Finding 3a: copy_file()'s source open
5 # in copy_altdest_file() is via do_open_nofollow(), which only refuses
6 # a final-component symlink. A daemon-module attacker who plants a
7 # parent symlink (module/cd -> /outside) then runs --copy-dest=cd
8 # against a source matching the size+mtime of /outside/target.txt
9 # drives the receiver to read /outside/target.txt under the daemon's
10 # authority and copy it into the module.
11 #
12 # Detection: source and outside have identical metadata (size, mtime,
13 # mode) but distinct content. After the transfer, module/target.txt
14 # must contain source's content, not outside's.
15
16 import filecmp
17 import os
18 import subprocess
19
20 from rsyncfns import (
21 SCRATCHDIR,
22 rsync_argv, get_testuid, get_rootuid, get_rootgid,
23 rmtree, start_test_daemon, test_fail,
24 )
25
26
27 DAEMON_PORT = 12883
28
29
30 mod = SCRATCHDIR / 'module'
31 outside = SCRATCHDIR / 'outside'
32 src_dir = SCRATCHDIR / 'src_files'
33 conf = SCRATCHDIR / 'test-rsyncd.conf'
34
35 for d in (mod, outside, src_dir):
36 rmtree(d)
37 d.mkdir(parents=True)
38
39 (outside / 'target.txt').write_text("OUTSIDE_LEAKED_DATA!\n")
40 os.chmod(outside / 'target.txt', 0o644)
41
42 os.symlink(str(outside), mod / 'cd')
43
44 # Source: same size + mtime + mode as outside, different content.
45 (src_dir / 'target.txt').write_text("ATTACKER_KNOWN_DATA!\n")
46 ref = (outside / 'target.txt').stat()
47 os.utime(src_dir / 'target.txt', (ref.st_atime, ref.st_mtime))
48 os.chmod(src_dir / 'target.txt', 0o644)
49
50 my_uid = get_testuid()
51 root_uid = get_rootuid()
52 root_gid = get_rootgid()
53 uid_line = f"uid = {root_uid}"
54 gid_line = f"gid = {root_gid}"
55 if my_uid != root_uid:
56 uid_line = '#' + uid_line
57 gid_line = '#' + gid_line
58
59 conf.write_text(f"""\
60 use chroot = no
61 {uid_line}
62 {gid_line}
63 log file = {SCRATCHDIR}/rsyncd.log
64 [upload]
65 path = {mod}
66 use chroot = no
67 read only = no
68 """)
69
70 url = start_test_daemon(conf, DAEMON_PORT)
71
72 subprocess.run(
73 rsync_argv('-rtp', '--copy-dest=cd',
74 f'{src_dir}/', f'{url}upload/'),
75 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
76 )
77
78 target = mod / 'target.txt'
79 if not target.is_file():
80 test_fail(
81 "destination file was not created -- daemon transfer failed "
82 "before the test could observe the basedir behaviour"
83 )
84
85 if filecmp.cmp(target, outside / 'target.txt', shallow=False):
86 test_fail(
87 "basedir-escape via copy_file source: module/target.txt now "
88 "contains the contents of outside/target.txt -- daemon read "
89 "/outside via the cd symlink and copied it into the module"
90 )
91 if not filecmp.cmp(target, src_dir / 'target.txt', shallow=False):
92 test_fail(
93 "destination doesn't match source content (and isn't outside "
94 "content either): unexpected state"
95 )