Debian Codegraph @

testsuite/variety-symlink-traversal_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Companion to variety_test.py for the cases the user specifically called out:
3 # the rsync CLIENT is started with a path that TRAVERSES a symlinked directory
4 # component -- as sender (the source path) and as receiver (the destination
5 # path), both as a trailing-slash symlink transfer-root and as an intermediate
6 # path component, with and without --keep-dirlinks.
7 #
8 # Empirically the current build (with its hardened per-component O_NOFOLLOW path
9 # resolver from the CVE-2026-29518 work) reproduces 3.2.7's result on all of
10 # these STATIC traversal patterns: a pre-existing symlinked directory component
11 # is followed by both, identically. The hardening only changes behaviour in the
12 # TOCTOU race scenario (a component swapped to a symlink mid-transfer), which is
13 # covered by the suite's dedicated symlink-race tests, not here. So this is a
14 # pass test: any future divergence on a legitimate traversal pattern is a
15 # regression (or an intended change to record as xfail in the peer manifest).
16 #
17 # Compares new-client vs old-client for each flavour and fails on any
18 # divergence (different exit code or different resulting tree).
19
20 import os
21 import shlex
22 import subprocess
23 import sys
24
25 from rsyncfns import (
26 RSYNC, RSYNC_PEER, SRCDIR, SCRATCHDIR,
27 make_variety_tree, compare_trees, rmtree,
28 xattrs_supported, acls_supported, devices_supported, owners_supported,
29 test_skipped, test_fail, split_rsync_cmd, rsh_cmd,
30 )
31
32 SSH = rsh_cmd()
33 WITH_X = xattrs_supported()
34 WITH_A = acls_supported()
35
36 os.chdir(SCRATCHDIR)
37
38 # A modest variety tree as the real source (depth 3 keeps it quick; symlink/perm
39 # coverage still present).
40 SRC = SCRATCHDIR / 'trav-src'
41 info = make_variety_tree(SRC, depth=3, with_acls=WITH_A, with_xattrs=WITH_X,
42 with_devices=devices_supported(),
43 with_owners=owners_supported())
44 TR = info['transfer_root']
45
46 WS = SCRATCHDIR / 'trav'
47 rmtree(WS)
48 WS.mkdir()
49
50 findings = [] # (case, description)
51
52
53 def run2(binary_cmd, args):
54 """Run rsync without failing on nonzero (old and new may legitimately
55 differ in success). Returns (returncode, combined_output)."""
56 proc = subprocess.run(shlex.split(binary_cmd) + args,
57 capture_output=True, text=True)
58 return proc.returncode, proc.stdout + proc.stderr
59
60
61 def diff_trees(a, b, label):
62 return compare_trees(a, b, label, with_acls=WITH_A,
63 with_xattrs=WITH_X)
64
65
66 def observe(case, new_dest, old_dest, rc_new, rc_old, out_new, out_old):
67 notes = []
68 if rc_new != rc_old:
69 notes.append(f"exit codes differ: new={rc_new} old={rc_old}")
70 # With --relative the dest CONTAINER dir is not a copy of any source dir, so
71 # rsync leaves its mtime at "now" (not preserved by either version); the new
72 # and old runs stamp it at slightly different instants, which would flake the
73 # listing across a 1-second boundary. It is not meaningful content, so pin
74 # both compared roots to a common mtime before diffing.
75 for d in (new_dest, old_dest):
76 try:
77 os.utime(d, (1000000000, 1000000000))
78 except OSError:
79 pass
80 tdiffs = diff_trees(new_dest, old_dest, case)
81 if tdiffs:
82 notes.append("result trees differ:\n" + "\n".join(tdiffs[:8]))
83 if notes:
84 findings.append((case, "\n".join(notes)))
85
86
87 # --- Case A: sender transfer-root IS a symlink to a directory (trailing slash) ---
88 def case_sender_root():
89 os.symlink(str(TR), WS / 'linkdir')
90 out = {}
91 for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)):
92 d = WS / f'A-{role}'
93 rmtree(d); d.mkdir()
94 rc, o = run2(binc, ['-a', f'{WS}/linkdir/', f'{d}/'])
95 out[role] = (d, rc, o)
96 observe('A-sender-root-symlink',
97 out['new'][0], out['old'][0],
98 out['new'][1], out['old'][1], out['new'][2], out['old'][2])
99
100
101 # --- Case B: sender path has an INTERMEDIATE symlinked component ---
102 def case_sender_intermediate():
103 base = WS / 'B-base'
104 (base / 'realcomp' / 'inner').mkdir(parents=True)
105 # copy a couple of real files under inner
106 (base / 'realcomp' / 'inner' / 'file1').write_text('hello-1\n')
107 (base / 'realcomp' / 'inner' / 'file2').write_text('hello-2\n')
108 os.symlink('realcomp', base / 'linkcomp')
109 for relflag in ([], ['-R']): # plain and --relative (implied dirs)
110 tag = 'B-sender-intermediate' + ('-R' if relflag else '')
111 out = {}
112 for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)):
113 d = WS / f'{tag}-{role}'
114 rmtree(d); d.mkdir()
115 rc, o = run2(binc, ['-a'] + relflag
116 + [f'{base}/linkcomp/inner/', f'{d}/'])
117 out[role] = (d, rc, o)
118 observe(tag, out['new'][0], out['old'][0],
119 out['new'][1], out['old'][1], out['new'][2], out['old'][2])
120
121
122 # --- Case C: receiver destination IS a symlink to a directory ---
123 def case_receiver_root():
124 out = {}
125 for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)):
126 real = WS / f'C-real-{role}'
127 real.mkdir()
128 link = WS / f'C-link-{role}'
129 os.symlink(str(real), link)
130 rc, o = run2(binc, ['-a', '--keep-dirlinks', f'{TR}/', f'{link}/'])
131 out[role] = (real, rc, o)
132 observe('C-receiver-root-symlink', out['new'][0], out['old'][0],
133 out['new'][1], out['old'][1], out['new'][2], out['old'][2])
134
135
136 # --- Case D: receiver path has an INTERMEDIATE symlinked component, with and
137 # without --keep-dirlinks (the pointed "create through a symlinked dest
138 # component" case) ---
139 def case_receiver_intermediate():
140 for kdl in ([], ['--keep-dirlinks']):
141 tag = 'D-receiver-intermediate' + ('-kdl' if kdl else '')
142 out = {}
143 for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)):
144 base = WS / f'{tag}-base-{role}'
145 (base / 'realcomp').mkdir(parents=True)
146 os.symlink('realcomp', base / 'linkcomp')
147 rc, o = run2(binc, ['-a'] + kdl
148 + [f'{TR}/', f'{base}/linkcomp/inner/'])
149 landing = base / 'realcomp' / 'inner'
150 if not landing.exists():
151 landing.mkdir(parents=True, exist_ok=True)
152 out[role] = (landing, rc, o)
153 observe(tag, out['new'][0], out['old'][0],
154 out['new'][1], out['old'][1], out['new'][2], out['old'][2])
155
156
157 if split_rsync_cmd(RSYNC) == split_rsync_cmd(RSYNC_PEER):
158 test_skipped("no old peer selected (RSYNC_PEER == RSYNC); nothing to "
159 "compare for symlink-traversal divergence")
160
161 case_sender_root()
162 case_sender_intermediate()
163 case_receiver_root()
164 case_receiver_intermediate()
165
166 if findings:
167 msg = [f"variety-symlink-traversal: {len(findings)} traversal case(s) "
168 f"diverge between current and peer={RSYNC_PEER!r}:"]
169 for case, desc in findings:
170 msg.append(f"\n========== {case} ==========")
171 msg.append(desc)
172 test_fail("\n".join(msg))
173
174 print("variety-symlink-traversal: current and peer agree on all traversal cases")