Debian Codegraph @

testsuite/daemon-filter-merge-bypass_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon-side --filter merge-file bypasses the module's daemon_filter_list.
3 #
4 # Threat (Mitchell Benjamin report, May 2026):
5 # A daemon module has `filter = - /secret/***` to hide its /secret/
6 # subtree from clients. A client that connects to that module with
7 # -M --filter=._/secret/rules
8 # (i.e. --remote-option=--filter=._<path>) makes the daemon's option
9 # parser run parse_filter_str() on the server side. That goes through
10 # parse_merge_name() (exclude.c:599), which under sanitize_paths=True
11 # prepends `module_dir` to absolute merge-file paths, then calls
12 # parse_filter_file().
13 #
14 # parse_filter_file() (exclude.c:1457) *does* check daemon_filter_list
15 # before fopen(), but against the post-merge-name absolute path
16 # (e.g. /srv/rsync/MOD/secret/rules) — the daemon's anchored rule
17 # `- /secret/***` only matches the module-relative form `/secret/...`,
18 # so the check passes and fopen() succeeds.
19 #
20 # Compare options.c:1522-1544 (OPT_EXCLUDE_FROM / OPT_INCLUDE_FROM)
21 # which strips `module_dirlen` from the front BEFORE check_filter():
22 # dir = cp + (*cp == '/' ? module_dirlen : 0);
23 # clean_fname(dir, CFN_COLLAPSE_DOT_DOT_DIRS);
24 # rej = check_filter(&daemon_filter_list, FLOG, dir, 0) < 0;
25 # The merge-file path lacks this strip. That asymmetry is the bug.
26 #
27 # Oracle: make /secret/rules contain `- *` (exclude everything). When the
28 # daemon's sender parses it, the rule joins filter_list and excludes
29 # public.txt too. So:
30 # * baseline pull (no --filter) -> public.txt arrives.
31 # * attack pull (--filter=._/secret/rules):
32 # - if vulnerable: public.txt is EXCLUDED (rules file was parsed
33 # and its rule applied).
34 # - if fixed: public.txt still arrives (rules file refused
35 # before fopen()).
36 #
37 # Negative control: --remote-option=--exclude-from=/secret/rules is
38 # already rejected by the daemon (the working code path). We don't
39 # require it as a hard assertion (the daemon may reject early before we
40 # can introspect), but it is documented behaviour.
41 #
42 # Suggested fix: in parse_filter_file() (exclude.c:1457), strip
43 # module_dirlen the same way OPT_EXCLUDE_FROM does, OR have
44 # parse_merge_name() expose the module-relative form for the daemon-filter
45 # check and use the absolute form only for fopen().
46
47 import os
48 import subprocess
49
50 from rsyncfns import (
51 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon,
52 test_fail, write_daemon_conf,
53 )
54
55 DAEMON_PORT = 12921
56
57 # Module root and the two files: a public one (the baseline transfer
58 # witness) and the daemon-hidden rules file (the attack target).
59 mod = SCRATCHDIR / 'srvmod'
60 rmtree(mod)
61 makepath(mod / 'secret')
62 (mod / 'public.txt').write_text("PUBLIC\n")
63 # A valid rsync filter rule that, if parsed, will exclude everything ---
64 # including public.txt. The point of the test is that the daemon should
65 # never open this file at all.
66 (mod / 'secret' / 'rules').write_text("- *\n")
67
68 conf = write_daemon_conf([
69 ('mod', {'path': str(mod),
70 'read only': 'yes',
71 # Anchored module-relative rule: matches /secret/anything but
72 # NOT the absolute /<module-path>/secret/anything form that
73 # parse_merge_name() produces. This is exactly the rule
74 # shape Mitchell's report uses.
75 'filter': '- /secret/***'}),
76 ])
77 url = start_test_daemon(conf, DAEMON_PORT)
78
79
80 def pull(args, dest):
81 rmtree(dest)
82 makepath(dest)
83 return subprocess.run(rsync_argv('-a', *args, f'{url}mod/', f'{dest}/'),
84 stdout=subprocess.DEVNULL,
85 stderr=subprocess.PIPE, text=True)
86
87
88 # -- baseline: public.txt is delivered without any client filter -----------
89 base_dest = SCRATCHDIR / 'base'
90 rc = pull([], base_dest)
91 if rc.returncode not in (0, 23):
92 test_fail(f"baseline pull failed (rc={rc.returncode}): {rc.stderr!r}")
93 if not (base_dest / 'public.txt').is_file():
94 test_fail("baseline: public.txt did not transfer; the module is "
95 "mis-configured (daemon filter is hiding it) and the test "
96 "would be vacuous.")
97 if (base_dest / 'secret' / 'rules').exists():
98 test_fail("baseline: /secret/rules WAS delivered -- the module's "
99 "daemon filter is not active and the oracle would be "
100 "meaningless.")
101
102
103 # -- attack: --filter=._/secret/rules pushed to the daemon side via -M -----
104 # The daemon sender re-parses this option, runs parse_filter_str ->
105 # parse_merge_name -> parse_filter_file on "/secret/rules", and (when
106 # vulnerable) fopen()s it despite the anchored module-filter.
107 atk_dest = SCRATCHDIR / 'atk'
108 rc = pull(['-M', '--filter=._/secret/rules'], atk_dest)
109 # rc may legitimately be 0 (everything excluded) or 23 (partial); both
110 # are accepted -- the security signal is what landed in atk_dest.
111 if (atk_dest / 'secret' / 'rules').exists():
112 test_fail("daemon delivered /secret/rules itself -- this should never "
113 "happen given the daemon filter. Test setup error?")
114
115 if not (atk_dest / 'public.txt').is_file():
116 # public.txt is missing -> the hidden rule "- *" reached the daemon
117 # filter list, which means parse_filter_file() did open /secret/rules.
118 # That is exactly the leak we're testing for.
119 test_fail(
120 "daemon parsed the hidden merge-file at /secret/rules: public.txt "
121 "was excluded by the rule inside that file even though the "
122 "module's daemon filter (- /secret/***) should have refused the "
123 "fopen(). parse_filter_file()'s daemon_filter_list check uses "
124 "the post-merge-name absolute path; module_dirlen is not stripped, "
125 "so an anchored module-relative rule never matches. Fix: strip "
126 "module_dirlen in exclude.c:1457 like options.c:1522 does for "
127 "--exclude-from.")
128
129 # Daemon-filter-bypass not triggered: public.txt is intact. Either the
130 # fix is in place, or the daemon found another way to refuse the file.
131 print("daemon-filter-merge-bypass: --filter merge-file refused by "
132 "daemon_filter_list (public.txt survived the attack pull).")