Debian Codegraph @

testsuite/ki62-io-error-mask_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Verifies: SW-REQ-114
3 #
4 # When MSG_IO_ERROR arrives, io.c OR's the wire value into the local io_error.
5 # Without masking, a malicious peer could set arbitrary (undefined) bits, which
6 # would then be stored and re-forwarded upstream in the local io_error. The fix
7 # masks the incoming value to the defined IOERR_* bits (IOERR_VALID_MASK). (The
8 # exit code itself is already safe: cleanup.c maps only the three defined bits
9 # onto RERR_* constants, so undefined bits never become the exit status -- the
10 # mask is about not storing/propagating junk, not about the exit value.)
11 #
12 # This is only an interrupted-transfer exit-code smoke test: it starts a real
13 # daemon (the sender for a download), begins a transfer, then kills the sender
14 # mid-transfer and asserts the receiver exits with a documented RERR_* value.
15 # It does NOT craft a hostile MSG_IO_ERROR frame, so it does not exercise the
16 # mask itself (no test does yet) -- it just guards the normal-peer exit path.
17 #
18 # Exit codes: 0 pass, 1 fail, 77 skip (killable daemon testing not possible --
19 # the test needs --use-tcp so it can SIGKILL the sender process directly).
20
21 import os
22 import re
23 import signal
24 import subprocess
25 import sys
26 import time
27
28 from rsyncfns import (
29 FROMDIR, SCRATCHDIR, TODIR, claim_ports, make_data_file, makepath, rmtree,
30 rsync_argv, start_rsyncd, test_fail, test_skipped, build_rsyncd_conf,
31 USE_TCP,
32 )
33
34 # The complete set of exit codes rsync defines (errcode.h). Whatever path the
35 # interrupted transfer takes, the receiver's exit code MUST be one of these --
36 # never an arbitrary number injected via a polluted io_error.
37 KNOWN_RERR = {
38 0, # RERR_OK
39 1, # RERR_SYNTAX
40 2, # RERR_PROTOCOL
41 3, # RERR_FILESELECT
42 4, # RERR_UNSUPPORTED
43 5, # RERR_STARTCLIENT
44 10, # RERR_SOCKETIO
45 11, # RERR_FILEIO
46 12, # RERR_STREAMIO
47 13, # RERR_MESSAGEIO
48 14, # RERR_IPC
49 15, # RERR_CRASHED
50 16, # RERR_TERMINATED
51 19, # RERR_SIGNAL1
52 20, # RERR_SIGNAL
53 21, # RERR_WAITCHILD
54 22, # RERR_MALLOC
55 23, # RERR_PARTIAL
56 24, # RERR_VANISHED
57 25, # RERR_DEL_LIMIT
58 30, # RERR_TIMEOUT
59 35, # RERR_CONTIMEOUT
60 124, # RERR_CMD_FAILED
61 125, # RERR_CMD_KILLED
62 126, # RERR_CMD_RUN
63 127, # RERR_CMD_NOTFOUND
64 }
65
66 PORT = 12966
67
68 # Killing the sender (the daemon process) mid-transfer requires a handle to
69 # the daemon process, which only exists in --use-tcp mode. In the default
70 # pipe mode the daemon is forked off the client and is not directly killable,
71 # so skip rather than run an inconclusive test.
72 if not USE_TCP:
73 test_skipped("needs --use-tcp to kill the daemon (sender) mid-transfer")
74
75 conf = build_rsyncd_conf()
76
77 # Build a source tree with a file big enough that, throttled by --bwlimit, the
78 # transfer is still in flight when we kill the sender.
79 rmtree(FROMDIR)
80 rmtree(TODIR)
81 makepath(FROMDIR)
82 makepath(TODIR)
83 make_data_file(FROMDIR / 'bigfile', 8 * 1024 * 1024) # 8 MB
84
85 claim_ports(PORT)
86 daemon = start_rsyncd(conf, PORT)
87
88 # --bwlimit keeps the transfer slow enough to guarantee the sender is
89 # mid-stream when we SIGKILL it. --info=progress gives a heartbeat we could
90 # observe, but the bwlimit + short sleep is enough on loopback.
91 client = subprocess.Popen(
92 rsync_argv('-a', '--bwlimit=64', '--timeout=60',
93 f'rsync://127.0.0.1:{PORT}/test-from/', f'{TODIR}/'),
94 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
95 )
96
97 # Let the receiver get past the handshake and into the data stream before we
98 # cut the sender off, so the death is observed as a mid-transfer stream error
99 # (the path that exercises the io_error -> exit-code mapping).
100 time.sleep(2)
101
102 # Kill the sender mid-transfer. rsyncd forks a child per connection, so the
103 # actual sender is NOT `daemon` (the listener) -- killing only the listener
104 # leaves the child streaming and the receiver never sees EOF. The child logs
105 # "[pid] rsync on <module>/" to the daemon log; kill that pid (plus the
106 # listener). The daemon shares this test's process group, so killpg is not an
107 # option (it would kill the test itself).
108 child_pids = []
109 try:
110 logtext = (SCRATCHDIR / 'rsyncd.log').read_text()
111 child_pids = [int(pid) for pid in re.findall(r'\[(\d+)\] rsync on ', logtext)]
112 except OSError:
113 pass
114 for pid in child_pids:
115 try:
116 os.kill(pid, signal.SIGKILL)
117 except (ProcessLookupError, PermissionError):
118 pass
119 if daemon.poll() is None:
120 daemon.kill()
121 try:
122 daemon.wait(timeout=5)
123 except subprocess.TimeoutExpired:
124 pass
125
126 # Wait for the receiver to notice the dead sender and exit. It should do so
127 # promptly (closed socket / EOF), but cap it so a regression that hangs is
128 # caught rather than stalling the suite.
129 try:
130 out, _ = client.communicate(timeout=30)
131 except subprocess.TimeoutExpired:
132 client.kill()
133 client.communicate()
134 test_fail("receiver did not exit after the sender was killed (hung)")
135
136 rc = client.returncode
137 if rc not in KNOWN_RERR:
138 sys.stderr.write(
139 f"receiver exited with {rc}, which is NOT a documented RERR_* value; "
140 f"output:\n{out}\n"
141 )
142 sys.exit(1)
143
144 # An interrupted download should not exit 0; sanity-check that we actually
145 # interrupted a transfer (a clean exit would mean we killed the sender too
146 # early/late and didn't exercise the mid-transfer path). Don't fail on this --
147 # timing-dependent -- just note it.
148 if rc == 0:
149 print("ki62-io-error-mask: receiver exited 0 (transfer may have completed "
150 "before the kill); no out-of-range exit code observed.")
151 else:
152 print(f"ki62-io-error-mask: receiver exited {rc} (a documented RERR_* "
153 f"value) after the sender was killed mid-transfer; no arbitrary "
154 f"io_error bit propagated to the exit code.")
155
156 sys.exit(0)