Debian Codegraph @

testsuite/daemon-chroot_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: `use chroot = yes` and the `path = /outer/./inner`
3 chroot-with-inner-subdir syntax.
4
5 Every other daemon test in the suite forces `use chroot = no` (via
6 write_daemon_conf()'s globals) so the chroot setup in clientserver.c
7 rsync_module() -- the use_chroot path-split (~866-887), the actual
8 chroot(2) call (~1003-1012), and the `temp dir` module option -- was
9 never reached. Those lines are also where the per-connection child
10 loses the ability to write its .gcda counters (the build-tree paths
11 are outside the chroot), so a companion gcov_flush() lands just before
12 chroot() in clientserver.c; this test exercises that flush point and
13 everything before it.
14
15 Three modules:
16 chr use chroot = yes, plain path -> 880-887, 1003-1012
17 chrinner use chroot = yes, path = /outer/./in -> 868-877 split
18 chrtmp use chroot = yes, temp dir = /tmpd -> 1093-1095 lp_temp_dir
19
20 Requires CAP_SYS_CHROOT (root in the test container); skips cleanly
21 without it.
22 """
23
24 import ctypes
25 import os
26 import subprocess
27
28 from rsyncfns import (
29 SCRATCHDIR, FROMDIR,
30 claim_ports, get_rootuid, get_testuid, make_tree, makepath, require_tcp,
31 rmtree, rsync_argv, start_test_daemon, test_fail, test_skipped,
32 write_daemon_conf,
33 )
34
35 if get_testuid() != get_rootuid():
36 test_skipped("use chroot = yes needs root (CAP_SYS_CHROOT)")
37
38 # Probe chroot() directly so a rootless/seccomp'd container that maps uid 0
39 # but withholds CAP_SYS_CHROOT skips cleanly instead of failing.
40 pid = os.fork()
41 if pid == 0:
42 try:
43 ctypes.CDLL(None).chroot(b"/")
44 os._exit(0)
45 except Exception:
46 os._exit(1)
47 _, st = os.waitpid(pid, 0)
48 if not (os.WIFEXITED(st) and os.WEXITSTATUS(st) == 0):
49 test_skipped("chroot(2) not permitted in this environment")
50
51 require_tcp("daemon chroot path needs the real start_daemon socket flow")
52
53 PORT = 19886
54 claim_ports(PORT)
55
56 src = FROMDIR
57 rmtree(src)
58 make_tree(src, depth=1)
59
60 # Module roots. For the /./ form the OUTER path is what gets chroot()'d
61 # and the INNER path is chdir'd inside it.
62 root_chr = SCRATCHDIR / 'chroot-plain'
63 root_outer = SCRATCHDIR / 'chroot-outer'
64 root_inner = root_outer / 'inner'
65 root_tmp = SCRATCHDIR / 'chroot-tmp'
66 tmpd = root_tmp / 'tmpd' # `temp dir` is interpreted INSIDE the chroot
67 for d in (root_chr, root_inner, root_tmp, tmpd):
68 rmtree(d)
69 makepath(root_chr, root_inner, root_tmp, tmpd)
70
71 mods = [
72 ('chr', {
73 'path': str(root_chr),
74 'use chroot': 'yes',
75 'read only': 'no',
76 }),
77 ('chrinner', {
78 # /outer/./inner: chroot to /outer, chdir to /inner inside.
79 'path': f'{root_outer}/./inner',
80 'use chroot': 'yes',
81 'read only': 'no',
82 }),
83 ('chrtmp', {
84 'path': str(root_tmp),
85 'use chroot': 'yes',
86 'read only': 'no',
87 # Relative to the chroot root (= module path).
88 'temp dir': '/tmpd',
89 }),
90 ]
91 conf = write_daemon_conf(mods, name='chroot.conf')
92 url = start_test_daemon(conf, PORT)
93
94
95 def push(module, dest):
96 r = subprocess.run(rsync_argv('-r', f'{src}/', f'{url}{module}/'),
97 capture_output=True, text=True)
98 if r.returncode != 0:
99 test_fail(f"push to [{module}] (use chroot=yes) failed "
100 f"(rc={r.returncode}):\n{r.stderr}")
101 if not any(dest.iterdir()):
102 test_fail(f"push to [{module}] wrote nothing into {dest}")
103
104
105 push('chr', root_chr)
106 push('chrinner', root_inner)
107 push('chrtmp', root_tmp)
108
109 # Pull from a chrooted module too (sender side under chroot).
110 pull_dst = SCRATCHDIR / 'pull-chr'
111 rmtree(pull_dst); makepath(pull_dst)
112 r = subprocess.run(rsync_argv('-r', f'{url}chr/', f'{pull_dst}/'),
113 capture_output=True, text=True)
114 if r.returncode != 0:
115 test_fail(f"pull from [chr] failed (rc={r.returncode}):\n{r.stderr}")
116 if not any(pull_dst.iterdir()):
117 test_fail("pull from [chr] produced nothing")
118
119 print("daemon-chroot: use chroot=yes (plain + /./inner + temp dir) push+pull ok")