Debian Codegraph @

testsuite/daemon-namecvt-newline-token_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon with `numeric ids = no` and a `name converter` maps the user/group
3 # names a sender puts in the file list by piping each name to the converter
4 # helper as a whitespace-and-newline-delimited token. A malicious sender that
5 # sends a user name containing a literal newline could inject an extra token (or
6 # a forged converter response) into that pipe. namecvt_call() (clientserver.c)
7 # guards this with namecvt_safe_token(), which refuses any token containing a
8 # control char (< 0x20 or 0x7F) before writing it to the converter.
9 #
10 # This drives the attack with the pure-Python sender rather than a recompiled
11 # malicious binary: push one file-list entry whose XMIT_USER_NAME_FOLLOWS name
12 # is "bad\nname". recv_user_name() -> recv_add_id() -> user_to_uid() ->
13 # namecvt_call() runs during file-list receipt, so no transfer phase is needed.
14 #
15 # Oracle: the converter helper must NOT receive a split "name" token (the
16 # newline never reached the pipe) AND the daemon must report "invalid
17 # name-converter token" (it refused the control char). Needs a real TCP daemon.
18
19 import time
20
21 from rsyncfns import (
22 SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, start_test_daemon,
23 test_fail, write_daemon_conf, xattrs_supported,
24 )
25 import rsync_proto as rp
26
27 PORT = 12974
28 require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp")
29 claim_ports(PORT)
30
31 base = SCRATCHDIR / 'daemon-namecvt-newline'
32 rmtree(base)
33 dest = base / 'module'
34 makepath(dest)
35
36 cvt_log = base / 'namecvt.log'
37 converter = base / 'nameconvert'
38 converter.write_text(
39 "#!/usr/bin/env python3\n"
40 "import sys\n"
41 f"log = open({str(cvt_log)!r}, 'a', buffering=1)\n"
42 "for line in sys.stdin:\n"
43 " log.write(line)\n"
44 " print('123', flush=True)\n")
45 converter.chmod(0o755)
46
47 daemon_log = base / 'namecvt-daemon.log'
48 params = {
49 'path': str(dest),
50 'read only': 'no',
51 'use chroot': 'no',
52 'numeric ids': 'no',
53 'name converter': str(converter),
54 'log file': str(daemon_log),
55 }
56 if xattrs_supported():
57 params['fake super'] = 'yes'
58 conf = write_daemon_conf([('recv', params)], name='namecvt-newline.conf')
59 url = start_test_daemon(conf, PORT)
60
61 s = rp.DaemonSender('127.0.0.1', PORT)
62 # -o => preserve_uid, so the receiver reads the uid + user name and maps it.
63 s.handshake('recv', ['--server', '-oe.LsfxCIu', '.', 'recv/'], greeting_version=30)
64 # One entry whose owner name carries a literal newline.
65 s.send_flat_flist([rp.FileEntry('f', mode=rp.S_IFREG | 0o644, length=0,
66 uid=1, user_name='bad\nname')])
67 back = s.drain(timeout=3.0)
68 s.close()
69
70 rejected = b'invalid name-converter token' in back
71 for _ in range(50):
72 if daemon_log.exists() and 'invalid name-converter token' in daemon_log.read_text(errors='replace'):
73 rejected = True
74 break
75 if rejected:
76 break
77 time.sleep(0.1)
78
79 cvt_lines = cvt_log.read_text().splitlines() if cvt_log.exists() else []
80 if any(line == 'name' for line in cvt_lines):
81 test_fail("name converter received the newline-split tail of a malicious "
82 f"sender user name. Logged requests: {cvt_lines!r}")
83 if not rejected:
84 dlog = daemon_log.read_text(errors='replace') if daemon_log.exists() else ''
85 test_fail("daemon accepted a sender-supplied user name containing a newline "
86 "without reporting the name-converter token rejection.\n"
87 f"client bytes:\n{back[:400]!r}\ndaemon log:\n{dlog}\n"
88 f"converter requests: {cvt_lines!r}")
89
90 print("daemon-namecvt-newline-token: daemon rejects newline-bearing "
91 "name-converter tokens")