Debian Codegraph @

testsuite/daemon-deny-dns-failopen_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon: a `hosts deny` hostname token that cannot be resolved must fail CLOSED.
3
4 KI-43 / sibling of CVE-2026-43617 (access.c). match_hostname() does a forward
5 DNS lookup of a config-specified hostname token (gethostbyname) and, on failure,
6 returned "no match" -- indistinguishable from a real non-match -- so allow_access
7 fell through to ALLOW. A daemon with `hosts deny = <hostname>` therefore silently
8 admitted the very host it meant to block whenever the token could not resolve (a
9 resolver-less chroot -- a recommended hardening -- or a transient DNS failure),
10 with no attacker DNS control required.
11
12 The fix makes an unresolvable token on a DENY list fail closed (treated as a
13 match, i.e. deny) while leaving allow-list behaviour unchanged.
14
15 This needs --use-tcp: match_hostname only runs for a real TCP peer (127.0.0.1).
16 """
17
18 import subprocess
19
20 from rsyncfns import (
21 FROMDIR, SCRATCHDIR, RSYNC,
22 make_tree, require_tcp, rmtree, rsync_argv, start_test_daemon, test_fail,
23 )
24
25 DAEMON_PORT = 12894
26 require_tcp("hosts deny DNS-failure matching needs a real TCP peer")
27
28 src = FROMDIR
29 rmtree(src)
30 make_tree(src, depth=2)
31
32 # `.invalid` is reserved (RFC 6761) to never resolve, so gethostbyname() on the
33 # deny token fails deterministically -- exactly the fail-open trigger.
34 conf = SCRATCHDIR / 'deny-dns.conf'
35 conf.write_text(
36 f"pid file = {SCRATCHDIR}/rsyncd.pid\n"
37 "use chroot = no\n"
38 f"log file = {SCRATCHDIR}/rsyncd.log\n"
39 f"\n[plain]\n\tpath = {src}\n\tread only = yes\n"
40 f"\n[deny-match]\n\tpath = {src}\n\tread only = yes\n\thosts deny = 127.0.0.0/8\n"
41 f"\n[deny-nomatch]\n\tpath = {src}\n\tread only = yes\n\thosts deny = 10.0.0.0/8\n"
42 f"\n[deny-unresolvable]\n\tpath = {src}\n\tread only = yes\n\thosts deny = nope.nonexistent.invalid\n"
43 )
44 # The access check is daemon-side, so the daemon must be the binary under test
45 # (matters under --rsync-bin2, where RSYNC_PEER could be a different/old binary).
46 url = start_test_daemon(conf, DAEMON_PORT, rsync_cmd=RSYNC)
47
48
49 def connect(mod):
50 """Return rsync's exit code for listing the module over the daemon."""
51 return subprocess.run(rsync_argv('-r', f'{url}{mod}/'),
52 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
53 text=True).returncode
54
55
56 # Controls (non-vacuity): the daemon answers, the deny mechanism works, and a
57 # non-matching deny still admits.
58 if connect('plain') != 0:
59 test_fail("control: connection to [plain] (no deny) should be ALLOWED but was refused")
60 if connect('deny-match') == 0:
61 test_fail("control: [deny-match] (hosts deny = 127.0.0.0/8) should be DENIED but succeeded")
62 if connect('deny-nomatch') != 0:
63 test_fail("control: [deny-nomatch] (hosts deny = 10.0.0.0/8, non-matching) should be ALLOWED but was refused")
64
65 # The fix: an unresolvable deny-list hostname token must fail CLOSED.
66 if connect('deny-unresolvable') == 0:
67 test_fail("[deny-unresolvable] (hosts deny = nope.nonexistent.invalid) was ALLOWED: "
68 "an unresolvable deny token failed OPEN (KI-43)")
69
70 print("daemon-deny-dns-failopen: unresolvable hosts-deny token fails closed")