Debian Codegraph @

testsuite/link-dest-module-escape_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Security guard for the #915 re-anchor: a daemon receiver must NOT honour an
3 # alt-basis dir whose `..` climbs OUT of the module.
4 #
5 # Honouring a relative --link-dest=../01 again (#915) deliberately re-permits an
6 # in-module `..` climb (dest 00 -> sibling basis 01). This test pins the other
7 # side of that boundary: a client-supplied --link-dest=../../OUTSIDE that points
8 # at a file OUTSIDE the module root must be refused, so the basis is never used
9 # and the dest file is re-transferred rather than hard-linked to the outside
10 # file (which would be an info-leak / cross-module hard-link).
11 #
12 # The re-anchor confines resolution beneath module_dir with RESOLVE_BENEATH, so
13 # the escaping climb is rejected in-kernel; on platforms without
14 # openat2/O_RESOLVE_BENEATH the portable resolver rejects the `..` outright.
15 # Either way the escape is blocked, so this test must PASS on every platform.
16 # Runs at any uid.
17
18 import shutil
19 import subprocess
20
21 from rsyncfns import (
22 SCRATCHDIR, make_data_file, makepath, rmtree, rsync_argv, start_test_daemon,
23 test_fail, write_daemon_conf,
24 )
25
26 DAEMON_PORT = 12916
27 DATA_SIZE = 40000
28
29 mod = SCRATCHDIR / 'escmod' # daemon module root (holds dest 00)
30 src = SCRATCHDIR / 'escsrc'
31 outside = SCRATCHDIR / 'OUTSIDE' # sibling of the module root -- OUTSIDE it
32 for d in (mod, src, outside):
33 rmtree(d)
34 makepath(mod / '00', src, outside)
35
36 # Source file, plus a byte-identical secret OUTSIDE the module with the same
37 # name/size/mtime (so a followed basis would quick-check as a match).
38 make_data_file(src / 'f.dat', DATA_SIZE)
39 shutil.copy2(src / 'f.dat', outside / 'f.dat')
40
41 conf = write_daemon_conf([
42 ('bak', {'path': str(mod), 'read only': 'no'}),
43 ])
44 url = start_test_daemon(conf, DAEMON_PORT)
45
46 # Dest is bak/00 (cwd = module/00). --link-dest=../../OUTSIDE climbs
47 # module/00 -> module -> SCRATCHDIR/OUTSIDE, i.e. out of the module.
48 proc = subprocess.run(
49 rsync_argv('-a', '--link-dest=../../OUTSIDE', f'{src}/', f'{url}bak/00/'),
50 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
51 out = proc.stdout or ''
52 if proc.returncode not in (0, 23): # 23: a basis rejection is non-fatal here
53 test_fail(f"escape push failed unexpectedly (rc={proc.returncode}):\n{out}")
54
55 dest = mod / '00' / 'f.dat'
56 secret = outside / 'f.dat'
57 if not dest.is_file():
58 test_fail(f"destination file missing ({dest})")
59
60 ds, ss = dest.stat(), secret.stat()
61 if (ds.st_dev, ds.st_ino) == (ss.st_dev, ss.st_ino):
62 test_fail(
63 "MODULE ESCAPE: the dest was hard-linked to a file OUTSIDE the module "
64 f"via --link-dest=../../OUTSIDE -- the confined resolver let a `..` "
65 f"climb escape the module root.\n{out}")
66 # Escape blocked: the basis was refused, so the file was re-transferred and the
67 # dest is its own inode, not the outside secret's.