Debian Codegraph @

testsuite/daemon-path-root-read_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Functional regression: a daemon module with `path = /` (use chroot = no)
3 # cannot send ANY file in 3.4.3 -- every read fails with "Invalid argument (22)".
4 #
5 # Reported as #897 ("Regression in 3.4.3, Invalid argument (22) on all file
6 # reads when using native protocol"). Works in 3.4.2, works over a remote
7 # shell; only the native (daemon) protocol with an absolute module path breaks.
8 #
9 # Root cause: the 3.4.3 symlink-race hardening routes the sender's file open
10 # through secure_relative_open(module_dir, secure_path, ...) (sender.c), where
11 # secure_path = F_PATHNAME + "/" + f_name.
12 # With `path = /` the module-relative F_PATHNAME is itself ABSOLUTE, so
13 # secure_path starts with '/'. secure_relative_open()'s front door rejects any
14 # absolute relpath with EINVAL *before* it ever calls openat2 (matching the
15 # reporter's strace: the file is stat'd and access()'d but never opened). The
16 # generator then reports "send_files failed to open ...: Invalid argument (22)"
17 # and the whole transfer ends in code 23.
18 #
19 # This is a pure functional regression (no attacker, no symlink): XFAIL until
20 # the sender open is made to tolerate an absolute module-root path (the
21 # accompanying sender.c fix). Runs at any uid.
22
23 import subprocess
24
25 from rsyncfns import (
26 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, test_fail,
27 write_daemon_conf,
28 )
29
30 DAEMON_PORT = 12897
31
32 # A small source tree under the scratch dir: a file at the served-subdir root
33 # and one nested deeper (the bug fails on EVERY file, regardless of depth).
34 served = SCRATCHDIR / 'served'
35 dst = SCRATCHDIR / 'pulldst'
36 rmtree(served)
37 rmtree(dst)
38 makepath(served / 'sub')
39 makepath(dst)
40 (served / 'README').write_text("readme-contents\n")
41 (served / 'sub' / 'deep.txt').write_text("deep-contents\n")
42
43 # Module rooted at the filesystem root, exactly like the report (path = /,
44 # use chroot = no). We then request the served subtree by its absolute path
45 # with the leading '/' stripped, so the daemon serves $served from "/".
46 conf = write_daemon_conf([
47 ('root', {'path': '/', 'read only': 'yes'}),
48 ])
49 url = start_test_daemon(conf, DAEMON_PORT)
50
51 served_rel = str(served).lstrip('/') # e.g. tmp/.../served
52 proc = subprocess.run(
53 rsync_argv('-a', f'{url}root/{served_rel}/', f'{dst}/'),
54 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
55 )
56 out = proc.stdout or ''
57 print(out)
58
59 # Bug present: the sender refuses to open the files with EINVAL(22).
60 if 'Invalid argument (22)' in out or ('failed to open' in out and proc.returncode != 0):
61 from rsyncfns import test_xfail
62 test_xfail(
63 "#897: daemon module `path = /` (use chroot = no) cannot send files -- "
64 "`send_files failed to open ...: Invalid argument (22)`. The sender's "
65 "secure_relative_open(module_dir, secure_path) gets an ABSOLUTE "
66 "secure_path (F_PATHNAME is absolute when path=/) and the front door "
67 "rejects absolute relpaths with EINVAL before any openat2. To be closed "
68 "by letting the sender open succeed for an absolute module-root path.")
69
70 # Bug fixed (or never present): the files transfer intact.
71 if proc.returncode != 0:
72 test_fail(f"daemon pull failed unexpectedly (rc={proc.returncode}); "
73 f"not the #897 EINVAL symptom:\n{out}")
74 for rel in ('README', 'sub/deep.txt'):
75 got = dst / rel
76 if not got.is_file():
77 test_fail(f"daemon pull did not deliver {rel} (dst={dst})")
78 if got.read_text() != (served / rel).read_text():
79 test_fail(f"delivered {rel} content differs from source")