Debian Codegraph @

testsuite/chdir-symlink-race_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/chdir-symlink-race.test.
3 #
4 # Regression test for the symlink-TOCTOU bug at the receiver's chdir().
5 # Post-CVE-2026-29518 an attack remained where the receiver's chdir()
6 # into a destination subdirectory followed an attacker-planted symlink,
7 # escaping the module. Each of four transfer flavours must leave the
8 # outside-the-module sentinel's mode AND content unchanged.
9
10 import filecmp
11 import os
12 import subprocess
13
14 from rsyncfns import (
15 SCRATCHDIR,
16 get_rootgid, get_rootuid, get_testuid,
17 make_data_file, rmtree, rsync_argv, start_test_daemon,
18 test_fail,
19 )
20
21
22 DAEMON_PORT = 12885
23
24 mod = SCRATCHDIR / 'module'
25 outside = SCRATCHDIR / 'outside'
26 src = SCRATCHDIR / 'src_files'
27 conf = SCRATCHDIR / 'test-rsyncd.conf'
28
29 for d in (mod, outside, src):
30 rmtree(d)
31 d.mkdir(parents=True)
32 (src / 'subdir').mkdir()
33
34 # Secret sentinel; keep a pristine copy alongside for cmp(1)-style compares.
35 (outside / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n")
36 os.chmod(outside / 'target.txt', 0o600)
37 outside_pristine = SCRATCHDIR / 'outside-pristine.txt'
38 import shutil
39 shutil.copy2(outside / 'target.txt', outside_pristine)
40
41 # Symlink trap planted by the local attacker.
42 os.symlink(str(outside), mod / 'subdir')
43
44 # Source files: same size as outside target, different content, mode 0666.
45 sz = (outside / 'target.txt').stat().st_size
46 make_data_file(src / 'target.txt', sz)
47 make_data_file(src / 'subdir' / 'target.txt', sz)
48 os.chmod(src / 'target.txt', 0o666)
49 os.chmod(src / 'subdir' / 'target.txt', 0o666)
50
51 conf.write_text(f"""\
52 use chroot = no
53 log file = {SCRATCHDIR}/rsyncd.log
54 [upload]
55 path = {mod}
56 use chroot = no
57 read only = no
58 """)
59
60
61 def reset_outside() -> None:
62 os.chmod(outside / 'target.txt', 0o600)
63 (outside / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n")
64 os.chmod(outside / 'target.txt', 0o600)
65
66
67 def verify_unchanged(label: str) -> None:
68 mode = (outside / 'target.txt').stat().st_mode & 0o777
69 if mode != 0o600:
70 test_fail(
71 f"{label}: outside file mode changed from 600 to {oct(mode)[2:]} "
72 "(chmod escape)"
73 )
74 if not filecmp.cmp(outside / 'target.txt', outside_pristine, shallow=False):
75 test_fail(f"{label}: outside file content changed (write escape)")
76
77
78 url = start_test_daemon(conf, DAEMON_PORT)
79
80
81 def run_attack(label: str, *args) -> None:
82 reset_outside()
83 rc = subprocess.run(
84 rsync_argv(*args),
85 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
86 ).returncode
87 if rc >= 128:
88 test_fail(f"{label}: rsync died from a signal (rc={rc})")
89 verify_unchanged(label)
90
91
92 def positive_control() -> None:
93 """Confirm the receiver writes into an ordinary in-module subdirectory, so
94 the symlink-escape scenarios below genuinely exercise the chdir path rather
95 than passing because the daemon refused (or failed) before reaching it."""
96 real = mod / 'realdir'
97 rmtree(real)
98 real.mkdir()
99 # When this test runs as root the daemon serves as 'nobody' (the module
100 # sets no uid), so make the control target world-writable; push a single
101 # file with no attribute preservation so the write never needs to own/chmod
102 # the dir -- it should land purely on the receiver's normal write path.
103 os.chmod(real, 0o777)
104 rc = subprocess.run(
105 rsync_argv(f'{src}/subdir/target.txt', f'{url}upload/realdir/'),
106 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
107 ).returncode
108 landed = real / 'target.txt'
109 if rc != 0 or not landed.is_file() \
110 or not filecmp.cmp(landed, src / 'subdir' / 'target.txt', shallow=False):
111 test_fail(f"positive control: receiver did not write into an ordinary "
112 f"in-module subdir (rc={rc}); attack scenarios would be vacuous")
113
114
115 positive_control()
116
117
118 # 1. Single file with --size-only -- receiver normally skips basis open and
119 # goes straight to chmod; only the chdir-escape blocks it.
120 run_attack("single-file --size-only",
121 '-tp', '--size-only',
122 f'{src}/target.txt',
123 f'{url}upload/subdir/target.txt')
124
125 # 2. -r push INTO the symlinked subdir -- receiver chdir's into "subdir",
126 # follows the symlink, ends up in outside.
127 run_attack("-r --size-only into subdir/",
128 '-rtp', '--size-only',
129 f'{src}/subdir/',
130 f'{url}upload/subdir/')
131
132 # 3. Same but with delta+rename (read-disclosure + write-escape together).
133 run_attack("-r without --size-only into subdir/",
134 '-rtp',
135 f'{src}/subdir/',
136 f'{url}upload/subdir/')
137
138 # 4. -r into the module root -- already covered by the original CVE fix;
139 # regression-check.
140 run_attack("-r --size-only into upload/ root",
141 '-rtp', '--size-only',
142 f'{src}/',
143 f'{url}upload/')