Debian Codegraph @

testsuite/link-dest-relative-basis_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Functional regression: a RELATIVE alt-basis dir (--link-dest / --copy-dest /
3 # --compare-dest = ../sibling) is silently ignored by a daemon receiver, so the
4 # basis is never used -- every file is re-transferred instead of hard-linked /
5 # copied / skipped. No error is printed; backups silently stop de-duplicating.
6 #
7 # Reported as #915 ("Security fix breaks --link-dest via rsync daemon": a
8 # `use chroot = no` daemon with `--link-dest=../01` re-transfers everything and
9 # fills the backup disk). The closely-related #928 is the same family over a
10 # remote shell with a relative `--link-dest=../snap.1`.
11 #
12 # Root cause: the 3.4.x symlink-race hardening resolves the receiver's basis
13 # through the confined resolver, which rejects the `..` that climbs from the
14 # destination (00) to its sibling basis (01); no basis is found, so the file is
15 # treated as new. Works in 3.4.1 (basis honoured).
16 #
17 # We exercise all three alt-basis forms because they are NOT obviously identical
18 # even though they share check_alt_basis_dirs():
19 # * --link-dest=../01 : the matched file must be HARD-LINKED to the basis.
20 # * --copy-dest=../01 : the matched file is COPIED from the basis, so its
21 # data is NOT sent over the wire (literal data ~ 0).
22 # * --compare-dest=../01 : a matched file is skipped entirely -- NOT created
23 # in the destination at all.
24 # Each signal cleanly separates "basis honoured" (fixed/3.4.1) from "basis
25 # ignored" (the regression).
26 #
27 # Fixed (#915/#930): the secure resolver honours an in-module relative `..` climb
28 # to a sibling basis on every platform (the per-component walk pops `..` to the
29 # held parent), so all three relative alt-basis forms use the basis. Runs at any uid.
30
31 import re
32 import subprocess
33
34 from rsyncfns import (
35 SCRATCHDIR, make_data_file, makepath, rmtree, rsync_argv, start_test_daemon,
36 test_fail, write_daemon_conf,
37 )
38
39 DAEMON_PORT = 12915
40 DATA_SIZE = 40000
41
42 mod = SCRATCHDIR / 'bakmod' # daemon module root: holds basis 01 and dest 00
43 src = SCRATCHDIR / 'src915'
44 rmtree(mod)
45 rmtree(src)
46 makepath(mod / '01', src)
47 make_data_file(src / 'f.dat', DATA_SIZE)
48 # Basis 01 holds a byte-identical copy of the file (same name/size/mtime so the
49 # quick-check treats it as a match and the basis is eligible).
50 import shutil
51 shutil.copy2(src / 'f.dat', mod / '01' / 'f.dat')
52
53 conf = write_daemon_conf([
54 ('bak', {'path': str(mod), 'read only': 'no'}),
55 ])
56 url = start_test_daemon(conf, DAEMON_PORT)
57
58
59 def push(opt):
60 """Fresh dest 00, push src/ into bak/00/ with the given alt-basis option.
61 Returns (rc, stdout)."""
62 rmtree(mod / '00')
63 proc = subprocess.run(
64 rsync_argv('-a', '--stats', opt, f'{src}/', f'{url}bak/00/'),
65 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
66 return proc.returncode, (proc.stdout or '')
67
68
69 def same_inode(a, b):
70 sa, sb = a.stat(), b.stat()
71 return (sa.st_dev, sa.st_ino) == (sb.st_dev, sb.st_ino)
72
73
74 def literal_bytes(out):
75 m = re.search(r'Literal data:\s*([\d,]+)', out)
76 return int(m.group(1).replace(',', '')) if m else -1
77
78
79 regressions = []
80 basis = mod / '01' / 'f.dat'
81
82 # --- 1. --link-dest=../01 : matched file must be hard-linked to the basis ----
83 rc, out = push('--link-dest=../01')
84 if rc != 0:
85 test_fail(f"--link-dest push failed unexpectedly (rc={rc}):\n{out}")
86 dest = mod / '00' / 'f.dat'
87 if not dest.is_file():
88 test_fail(f"--link-dest: destination file missing ({dest})")
89 if not same_inode(dest, basis):
90 regressions.append("--link-dest=../01 did not hard-link to the basis "
91 "(file re-transferred)")
92
93 # --- 2. --copy-dest=../01 : matched file copied locally, NOT sent on the wire -
94 rc, out = push('--copy-dest=../01')
95 if rc != 0:
96 test_fail(f"--copy-dest push failed unexpectedly (rc={rc}):\n{out}")
97 dest = mod / '00' / 'f.dat'
98 if not dest.is_file():
99 test_fail(f"--copy-dest: destination file missing ({dest})")
100 lit = literal_bytes(out)
101 if lit > DATA_SIZE // 2:
102 regressions.append(f"--copy-dest=../01 re-sent the data over the wire "
103 f"(Literal data={lit}, basis not used)")
104
105 # --- 3. --compare-dest=../01 : matched file skipped, NOT created in dest ------
106 rc, out = push('--compare-dest=../01')
107 if rc != 0:
108 test_fail(f"--compare-dest push failed unexpectedly (rc={rc}):\n{out}")
109 if (mod / '00' / 'f.dat').is_file():
110 regressions.append("--compare-dest=../01 created the file in the dest "
111 "(basis not matched, so the file was transferred)")
112
113 if regressions:
114 test_fail(
115 "#915/#930: a daemon receiver ignored a RELATIVE alt-basis dir (../01) -- "
116 "the secure resolver must honour an in-module `..` climb to the sibling "
117 "basis on every platform:\n - " + "\n - ".join(regressions))
118 # No regressions -> all three relative alt-basis forms honoured the basis.