Debian Codegraph @

testsuite/alt-dest-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/alt-dest-symlink-race.test.
3 #
4 # Regression test for the basedir-confinement gap in
5 # secure_relative_open(): a parent symlink ON basedir is followed
6 # unrestrictedly, then RESOLVE_BENEATH is applied only to relpath,
7 # anchored at the wrong directory. In daemon mode this lets a local
8 # attacker who can write into a module plant module/cd -> /outside and
9 # then use --link-dest=cd / --copy-dest=cd / --compare-dest=cd to make
10 # the receiver's basis-file lookup resolve into /outside, leaking
11 # daemon-readable content via the rsync delta-rolling read-disclosure
12 # primitive.
13 #
14 # Detection: with --link-dest, when basis matches source exactly the
15 # destination is hard-linked to the basis. On a successful escape the
16 # destination shares an inode with /outside/target.txt; on a fix it
17 # doesn't.
18
19 import os
20 import subprocess
21
22 from rsyncfns import (
23 SCRATCHDIR,
24 rsync_argv, get_testuid, get_rootuid, get_rootgid,
25 rmtree, start_test_daemon, test_fail,
26 )
27
28
29 DAEMON_PORT = 12882
30
31
32 mod = SCRATCHDIR / 'module'
33 outside = SCRATCHDIR / 'outside'
34 src_dir = SCRATCHDIR / 'src_files'
35 conf = SCRATCHDIR / 'test-rsyncd.conf'
36
37 for d in (mod, outside, src_dir):
38 rmtree(d)
39 d.mkdir(parents=True)
40
41 # The outside file an attacker wants the daemon to treat as a basis.
42 (outside / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n")
43 os.chmod(outside / 'target.txt', 0o644)
44
45 # Attacker-planted module symlink.
46 os.symlink(str(outside), mod / 'cd')
47
48 # Source: same content + mtime + mode as outside, so --link-dest hard-
49 # links the destination to the basis iff basedir lookup escapes.
50 (src_dir / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n")
51 ref = (outside / 'target.txt').stat()
52 os.utime(src_dir / 'target.txt', (ref.st_atime, ref.st_mtime))
53 os.chmod(src_dir / 'target.txt', 0o644)
54
55 my_uid = get_testuid()
56 root_uid = get_rootuid()
57 root_gid = get_rootgid()
58 uid_line = f"uid = {root_uid}"
59 gid_line = f"gid = {root_gid}"
60 if my_uid != root_uid:
61 uid_line = '#' + uid_line
62 gid_line = '#' + gid_line
63
64 conf.write_text(f"""\
65 use chroot = no
66 {uid_line}
67 {gid_line}
68 log file = {SCRATCHDIR}/rsyncd.log
69 [upload]
70 path = {mod}
71 use chroot = no
72 read only = no
73 """)
74
75 url = start_test_daemon(conf, DAEMON_PORT)
76
77 # Push directly into the module root: pushing into a destination subdir
78 # would make the receiver chdir into it before resolving --link-dest,
79 # making "cd" resolve in the wrong CWD and masking the bug.
80 subprocess.run(
81 rsync_argv('-rtp', '--link-dest=cd',
82 f'{src_dir}/', f'{url}upload/'),
83 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
84 )
85
86 target = mod / 'target.txt'
87 if not target.is_file():
88 test_fail(
89 "destination file was not created -- daemon transfer failed "
90 "before the test could observe the basedir behaviour"
91 )
92
93 if target.stat().st_ino == (outside / 'target.txt').stat().st_ino:
94 test_fail(
95 f"basedir-escape: --link-dest hard-linked module/target.txt to "
96 f"outside/target.txt (inode {target.stat().st_ino}); daemon's "
97 "basis-file lookup followed the parent symlink on the basedir"
98 )