Debian Codegraph @

testsuite/connect-prog-nested-singlequote-host-injection_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """RSYNC_CONNECT_PROG host quoting composes unsafely with single quotes.
3
4 %H is substituted into a command string that a shell runs; a program of the
5 form `sh -c '... %H ...'` re-parses the word in a NESTED shell, where the
6 quoting rsync added has already been consumed by the outer one. A host of
7 `localhost;touch$IFS<marker>;#` therefore executes in the inner shell.
8
9 Asserting only that the marker is absent is not enough: it is equally absent
10 when rsync failed to parse its arguments, when socketpair_tcp is blocked in the
11 environment, or when `touch` could not be found. Each refusal below is checked
12 by its specific message, and the accepted-host cases prove the allow-list has
13 not simply refused everything -- which would satisfy an absence-only test
14 perfectly.
15 """
16
17 import os
18 import shlex
19 import subprocess
20
21 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped
22
23 REFUSAL = 'unsafe host characters for RSYNC_CONNECT_PROG'
24
25 base = SCRATCHDIR / 'connect-prog-nested'
26 rmtree(base)
27 base.mkdir(parents=True)
28
29 # Records what %H expanded to, so an accepted host is checked for real rather
30 # than inferred from the transfer having failed for some other reason.
31 seen = base / 'seen'
32 probe = base / 'probe.sh'
33 # Records and exits: anything that keeps reading stdin would hang the
34 # transfer until the harness timeout instead of failing fast.
35 probe.write_text(f'#!/bin/sh\nprintf %s "$1" > {shlex.quote(str(seen))}\n')
36 probe.chmod(0o755)
37
38
39 def run(host, prog):
40 env = os.environ.copy()
41 env['RSYNC_CONNECT_PROG'] = prog
42 # cwd=base so a bare marker name in the payload lands in the scratch dir.
43 # It has to be bare: a URL authority ends at the first '/', so an absolute
44 # path never reaches the shell at all -- the injected `touch` would run
45 # with no operand and could not create anything, which would make the
46 # marker check below unfalsifiable.
47 return subprocess.run(
48 rsync_argv(f'rsync://{host}/m/', str(base / 'out')),
49 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env,
50 cwd=str(base))
51
52
53 # --- the injection itself --------------------------------------------------
54 marker = base / 'pwned'
55 proc = run('localhost;touch${IFS}pwned;#', "sh -c 'printf %H >/dev/null'")
56 if 'socketpair_tcp failed' in proc.stdout:
57 test_skipped('RSYNC_CONNECT_PROG socketpair is blocked in this environment')
58 if marker.exists():
59 test_fail('RSYNC_CONNECT_PROG host escaped the template quote context and '
60 'ran a command in the nested shell')
61 if proc.returncode == 0:
62 test_fail(f'the malicious host completed a transfer: {proc.stdout!r}')
63 if REFUSAL not in proc.stdout:
64 test_fail('the shell-active host was not refused for BEING shell-active; '
65 'something else stopped the transfer, so this check would pass '
66 f'with the guard removed: {proc.stdout!r}')
67
68 # --- first characters that change an argument's meaning --------------------
69 # Quoting keeps these as a single word but cannot stop '-' looking like an
70 # option to the named program, nor '~' being tilde-expanded by the nested shell.
71 for host, why in (('-c', "looks like an option to the connect program"),
72 ('+x', "looks like an option too: `sh +x` is as real as `sh -x`"),
73 ('~root', "is tilde-expanded by the nested shell"),
74 ('%self', "is expanded by a nested fish, where %self is its pid"),
75 ('', "vanishes when a nested shell re-splits, shifting later args")):
76 seen.unlink(missing_ok=True)
77 proc = run(host, f'{probe} %H')
78 if REFUSAL not in proc.stdout:
79 got = seen.read_text() if seen.exists() else '(program not reached)'
80 test_fail(f'host {host!r} was accepted, and it {why}; '
81 f'the connect program received {got!r}')
82
83 # --- hosts that must still work -------------------------------------------
84 # RSYNC_CONNECT_PROG exists for custom transports, so %H is not always a name
85 # the resolver ever sees. Without these, an allow-list that refused far too
86 # much would still look perfect above.
87 for host, expect in (('example.com', 'example.com'),
88 ('example.com.', 'example.com.'),
89 ('host_name', 'host_name'),
90 ('host+alias', 'host+alias'),
91 ('host~alias', 'host~alias'),
92 ('[fe80::1%eth0]', 'fe80::1%eth0')):
93 seen.unlink(missing_ok=True)
94 proc = run(host, f'{probe} %H')
95 if REFUSAL in proc.stdout:
96 test_fail(f'host {host!r} is legitimate but was refused: {proc.stdout!r}')
97 if not seen.exists():
98 test_fail(f'host {host!r} never reached the connect program: {proc.stdout!r}')
99 got = seen.read_text()
100 if got != expect:
101 test_fail(f'host {host!r} reached the connect program as {got!r}, '
102 f'expected {expect!r}')
103
104 print('RSYNC_CONNECT_PROG refuses a shell-active host and an option- or '
105 'tilde-active first character, and still passes ordinary ones through')