Debian Codegraph @

testsuite/filter-merge-recursion_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for unbounded merge-file include recursion in the filter
3 # parser (exclude.c parse_filter_file <-> parse_filter_str).
4 #
5 # A '. file' (merge) directive inside a filter file makes parse_filter_str()
6 # call parse_filter_file() on the named file, which feeds each of its lines
7 # back through parse_filter_str(). Nothing bounded the nesting depth, so a
8 # merge file whose only line re-includes itself recurses until the stack
9 # guard page is hit -> SIGSEGV.
10 #
11 # Threat (code-scanner daemon-protocol fuzzing, June 2026): a malicious client of a
12 # writable daemon module uploads a '.merge' file containing '. .merge' and
13 # drives a transfer with --filter=': .merge'; the generator's per-dir merge
14 # handling in push_local_filters() then re-parses that file forever and the
15 # forked daemon child crashes. The recursion lives in shared filter code, so
16 # we reproduce it with a plain local transfer: a per-dir merge rule
17 # (': .merge') plus a source '.merge' that merges itself.
18 #
19 # Oracle:
20 # * pre-fix: the sender recurses to stack exhaustion and dies with SIGSEGV
21 # (the client sees the peer vanish / a signal exit).
22 # * fixed: parse_filter_file() caps nesting at MAX_MERGE_DEPTH (32) and,
23 # on this operator/sender-supplied (XFLG_FATAL_ERRORS) merge,
24 # exits cleanly with RERR_FILEIO and a depth-limit diagnostic.
25
26 import subprocess
27
28 from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail
29
30 SRC = SCRATCHDIR / 'mergerec-src'
31 DST = SCRATCHDIR / 'mergerec-dst'
32 rmtree(SRC)
33 rmtree(DST)
34 makepath(SRC, DST)
35
36 # A per-dir merge file that includes itself: '. .merge' re-merges .merge.
37 (SRC / '.merge').write_text(". .merge\n")
38 (SRC / 'file.txt').write_text("hello\n")
39
40 proc = subprocess.run(
41 rsync_argv('-a', '--filter=: .merge', f'{SRC}/', f'{DST}/'),
42 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
43
44 # A process killed by a signal shows up as a negative returncode in Python, or
45 # as 128+signal when a child died and the parent reported it. RERR_STREAMIO
46 # (12) with no depth diagnostic means the peer crashed mid-stream.
47 crashed = (proc.returncode < 0 or proc.returncode >= 128
48 or (proc.returncode == 12
49 and 'merge-file include depth limit' not in proc.stdout))
50 if crashed:
51 test_fail(
52 "filter merge-file recursion was not capped: rsync died abnormally "
53 f"(rc={proc.returncode}) on a self-including '.merge'. "
54 f"output:\n{proc.stdout}")
55
56 if 'merge-file include depth limit' not in proc.stdout:
57 test_fail(
58 "expected the MAX_MERGE_DEPTH diagnostic; the self-including merge "
59 f"file did not hit the cap (rc={proc.returncode}).\n{proc.stdout}")
60
61 print("filter-merge-recursion: self-including merge file is capped at "
62 "MAX_MERGE_DEPTH (clean RERR_FILEIO, no stack-exhaustion crash).")