1 #!/usr/bin/env python3 2 """ 3 Regression tests for the chunked request body parser (client_poll_post_data()). 4 5 Covers the out-of-bounds read / ustream_consume() abort() reported by 6 Guy Godfroy: a chunked body whose chunk terminator CRLF arrives split over 7 several packets made the parser search - and then consume - bytes beyond the 8 valid data of the ustream read buffer, which aborted the whole server. 9 10 Also covers the framing the parser has to get right around that: chunk size 11 lines split across packets, chunk extensions (RFC 9112 7.1.1), trailer 12 sections, malformed size lines and keep-alive reuse of the connection. 13 14 Usage: chunked_body.py /path/to/uhttpd [-v] 15 Exit code 0 when all cases pass. 16 """ 17 18 import os 19 import random 20 import shutil 21 import socket 22 import subprocess 23 import sys 24 import tempfile 25 import time 26 27 CGI = """#!/bin/sh 28 cat | wc -c | tr -d ' \\n' > "@BODY_DIR@/${QUERY_STRING}" 29 printf 'Status: 200 OK\\r\\nContent-Type: text/plain\\r\\n\\r\\nok\\r\\n' 30 """ 31 32 PATH = "/cgi-bin/dump.sh" 33 34 35 def free_port(): 36 s = socket.socket() 37 s.bind(("127.0.0.1", 0)) 38 port = s.getsockname()[1] 39 s.close() 40 return port 41 42 43 class Server: 44 def __init__(self, binary, verbose=False): 45 self.verbose = verbose 46 self.root = tempfile.mkdtemp(prefix="uhttpd-test-") 47 self.bodies = os.path.join(self.root, "bodies") 48 os.makedirs(os.path.join(self.root, "cgi-bin")) 49 os.makedirs(self.bodies) 50 cgi = os.path.join(self.root, "cgi-bin", "dump.sh") 51 with open(cgi, "w") as f: 52 f.write(CGI.replace("@BODY_DIR@", self.bodies)) 53 os.chmod(cgi, 0o755) 54 55 self.port = free_port() 56 self.log = os.path.join(self.root, "server.log") 57 self.proc = subprocess.Popen( 58 [binary, "-f", "-p", "127.0.0.1:%d" % self.port, "-h", self.root, 59 "-T", "2", "-t", "3"], 60 stdout=open(self.log, "w"), 61 stderr=subprocess.STDOUT, 62 ) 63 for _ in range(100): 64 if self.alive() and self.can_connect(): 65 return 66 time.sleep(0.05) 67 raise RuntimeError("server did not start:\n%s" % open(self.log).read()) 68 69 def can_connect(self): 70 try: 71 s = socket.create_connection(("127.0.0.1", self.port), timeout=1) 72 s.close() 73 return True 74 except OSError: 75 return False 76 77 def alive(self): 78 return self.proc.poll() is None 79 80 def body_size(self, name): 81 path = os.path.join(self.bodies, name) 82 for _ in range(50): 83 try: 84 with open(path) as f: 85 return int(f.read().strip()) 86 except (FileNotFoundError, ValueError): 87 time.sleep(0.05) 88 return None 89 90 def connect(self): 91 s = socket.create_connection(("127.0.0.1", self.port), timeout=4) 92 s.settimeout(4) 93 return s 94 95 def stop(self): 96 self.proc.kill() 97 self.proc.wait() 98 shutil.rmtree(self.root, ignore_errors=True) 99 100 101 class Case: 102 def __init__(self, name, frags, body=None, status=None, note="", close_after_send=False): 103 self.name = name 104 self.frags = frags # list of (delay, bytes); None means "send the request header" 105 self.body = body # expected bytes received by the CGI, or None 106 self.status = status # expected response status code, or None 107 self.note = note 108 # close our side right after sending instead of waiting for a response: 109 # for framing that can never be completed, waiting would leave a request 110 # with a live CGI around and disturb the following case 111 self.close_after_send = close_after_send 112 113 114 def header(name, body_extra=b""): 115 return (b"POST " + (PATH + "?" + name).encode() + b" HTTP/1.1\r\n" 116 b"Host: localhost\r\nConnection: keep-alive\r\n" 117 b"Transfer-Encoding: chunked\r\n" + body_extra + b"\r\n") 118 119 120 def read_response(s): 121 """Read the status line; returns (status_code or None, closed_by_server)""" 122 data = b"" 123 try: 124 while b"\r\n" not in data: 125 chunk = s.recv(4096) 126 if not chunk: 127 return (parse_status(data), True) 128 data += chunk 129 except socket.timeout: 130 return (None, False) 131 132 return (parse_status(data), False) 133 134 135 def parse_status(data): 136 try: 137 return int(data.split(b" ", 2)[1]) 138 except (IndexError, ValueError): 139 return None 140 141 142 def run_case(srv, case): 143 errors = [] 144 s = srv.connect() 145 try: 146 for delay, frag in case.frags: 147 try: 148 s.sendall(header(case.name) if frag is None else frag) 149 except OSError: 150 errors.append("send failed (connection closed early)") 151 break 152 if delay: 153 time.sleep(delay) 154 155 if case.close_after_send: 156 s.close() 157 if not srv.alive(): 158 errors.append("uhttpd died (signal %d)" % srv.proc.returncode) 159 return errors 160 161 status, closed = read_response(s) 162 163 if case.status is not None and status != case.status: 164 if closed: 165 errors.append("connection closed without a response, expected %d" % case.status) 166 elif status is None: 167 errors.append("no response within timeout (parser stalled?)") 168 else: 169 errors.append("status %s, expected %d" % (status, case.status)) 170 elif case.status is None and status is None and not closed: 171 errors.append("no response within timeout (parser stalled?)") 172 173 if case.body is not None: 174 got = srv.body_size(case.name) 175 if got != case.body: 176 errors.append("body %s bytes, expected %d" % (got, case.body)) 177 finally: 178 s.close() 179 180 if not srv.alive(): 181 errors.append("uhttpd died (signal %d)" % srv.proc.returncode) 182 183 return errors 184 185 186 def build_cases(): 187 cases = [] 188 189 # baseline: a single, well formed chunked body 190 cases.append(Case("simple", [(0, None), (0, b"5\r\nhello\r\n0\r\n\r\n")], 191 body=5, status=200)) 192 193 # chunk data split into several packets 194 frags = [(0, None)] 195 body = b"a\r\n" + b"abcdefghij" + b"\r\n0\r\n\r\n" 196 for i in range(0, len(body), 4): 197 frags.append((0.05, body[i:i + 4])) 198 cases.append(Case("fragmented", frags, body=10, status=200)) 199 200 # a chunk-size line split across packets: the parser must wait for the 201 # rest of it instead of acting on whatever follows in the buffer 202 cases.append(Case("split-size-line", 203 [(0, None), (0, b"a\r\nabcdefghij"), (0.4, b"\r\n0"), 204 (0.4, b"\r\n\r\n")], 205 body=10, status=200)) 206 207 # the reported trigger, form 1: a chunk that ends with a lone CR, so the 208 # two bytes the parser skips are not buffered yet 209 d = b"zzzzzzzz\r\n" * 300 210 cases.append(Case("report-form1", 211 [(0, None), (0, b"%x\r\n" % len(d) + d), 212 (0.4, b"\r\n64\r\n" + b"a" * 100 + b"\r"), 213 (0.4, b"\n0\r\n\r\n")], 214 body=len(d) + 100, status=200)) 215 216 # the same, sent exactly as in the report: header, size line and chunk data 217 # in one write, so the buffer layout matches the one the reporter had 218 cases.append(Case("report-form1-exact", 219 [(0, b"POST " + PATH.encode() + b" HTTP/1.1\r\nHost: x\r\n" 220 b"Transfer-Encoding: chunked\r\n\r\n%x\r\n" % len(d) + d), 221 (0.4, b"\r\n64\r\n" + b"a" * 100 + b"\r"), 222 (0.4, b"\n0\r\n\r\n")], 223 body=None, status=200, 224 note="crash reproducer from the report")) 225 226 # the reported trigger, form 2: a previous request on the same keep-alive 227 # connection leaves a CRLF in the buffer, then a chunk ends with a lone CR 228 s1 = b"POST " + (PATH + "?form2-pre").encode() + b" HTTP/1.1\r\nHost: localhost\r\n" \ 229 b"Content-Length: 8\r\nConnection: keep-alive\r\n\r\nAA\r\nBB\r\n" 230 cases.append(Case("report-form2", 231 [(0, s1), (0.3, None), 232 (0, b"6\r\nabcdef\r"), (0.4, b"\n5\r\ngrqzy\r\n0\r\n\r\n")], 233 body=11, status=200)) 234 235 # chunk terminator CRLF itself split over two packets 236 cases.append(Case("split-chunk-terminator", 237 [(0, None), (0, b"3\r\nabc\r"), (0.4, b"\n"), 238 (0.4, b"5\r\n12345\r\n0\r\n\r\n")], 239 body=8, status=200)) 240 241 # chunk extensions, RFC 9112 7.1.1: they must be skipped, not rejected 242 cases.append(Case("chunk-ext", 243 [(0, None), (0, b"7;name=value\r\nabcdefg\r\n0\r\n\r\n")], 244 body=7, status=200)) 245 cases.append(Case("chunk-ext-ietf-tchar", 246 [(0, None), (0, b"6;a=b-c.d!e\r\nabcdef\r\n0\r\n\r\n")], 247 body=6, status=200)) 248 cases.append(Case("chunk-ext-quoted-string", 249 [(0, None), (0, b'5 ;a="x y";b\r\n12345\r\n0\r\n\r\n')], 250 body=5, status=200)) 251 cases.append(Case("chunk-ext-unterminated-quote", 252 [(0, None), (0, b'5;a="x\r\n12345\r\n0\r\n\r\n')], 253 body=None, status=None, 254 note="malformed extension, connection must just close")) 255 256 # uppercase hex sizes 257 cases.append(Case("uppercase-hex", 258 [(0, None), (0, b"A\r\n0123456789\r\n0\r\n\r\n")], 259 body=10, status=200)) 260 261 # trailer section after the terminal chunk must not be parsed as the 262 # next request: the connection has to stay usable afterwards 263 cases.append(Case("trailer-then-next-request", 264 [(0, None), 265 (0, b"3\r\nabc\r\n0\r\nX-Trailer: 1\r\nAnother: 2\r\n\r\n"), 266 (0.2, None), (0, b"4\r\nwxyz\r\n0\r\n\r\n")], 267 body=4, status=200)) 268 269 # malformed framing must terminate the connection, not the server 270 cases.append(Case("bad-size-line", 271 [(0, None), (0, b"3\r\nabc\r\nzz\r\n0\r\n\r\n")], 272 body=None, status=None)) 273 cases.append(Case("bad-chunk-terminator", 274 [(0, None), (0, b"3\r\nabcX"), (0.3, b"\r\n0\r\n\r\n")], 275 body=None, status=None)) 276 cases.append(Case("lone-cr-then-garbage", 277 [(0, None), (0, b"3\r\nabc\r"), (0.3, b"X\r\n0\r\n\r\n")], 278 body=None, status=None)) 279 cases.append(Case("oversized-size-line", 280 [(0, None), (0, b"f" * 5000 + b"\r\n"), (0.3, b"x" * 100)], 281 body=None, status=None, close_after_send=True, 282 note="buffer-filling size line: must not wedge the server")) 283 284 # deterministic randomized fragmentation of well formed bodies 285 for seed in (1, 2, 3): 286 rnd = random.Random(seed) 287 chunks, body = [], b"" 288 for _ in range(6): 289 n = rnd.choice([1, 7, 200, 1500, 4000]) 290 data = bytes(rnd.choice([97, 98, 99]) for _ in range(n)) 291 chunks.append(b"%x\r\n" % n + data + b"\r\n") 292 body += data 293 framed = b"".join(chunks) + b"0\r\n\r\n" 294 frags, pos = [], 0 295 while pos < len(framed): 296 n = rnd.randint(1, len(framed) - pos) 297 frags.append((0.01, framed[pos:pos + n])) 298 pos += n 299 frags.insert(0, (0, None)) 300 cases.append(Case("random-%d" % seed, frags, body=len(body), status=200)) 301 302 return cases 303 304 305 def main(): 306 if len(sys.argv) < 2: 307 print(__doc__) 308 return 2 309 310 binary = os.path.abspath(sys.argv[1]) 311 verbose = "-v" in sys.argv 312 313 srv = Server(binary) 314 cases = build_cases() 315 failed = 0 316 try: 317 for case in cases: 318 errors = run_case(srv, case) 319 failed += bool(errors) 320 if errors: 321 print("FAIL %-28s %s" % (case.name, "; ".join(errors))) 322 elif verbose: 323 print("ok %-28s %s" % (case.name, case.note)) 324 if not srv.alive(): 325 print("uhttpd terminated with signal %d, aborting test run" 326 % srv.proc.returncode) 327 break 328 finally: 329 log = open(srv.log).read() 330 srv.stop() 331 332 if log.strip(): 333 print("--- server output ---") 334 print(log.rstrip()) 335 336 print("%d case(s), %d failed" % (len(cases), failed)) 337 return 1 if failed else 0 338 339 340 if __name__ == "__main__": 341 sys.exit(main())
This page was automatically generated by LXR 0.3.1. • OpenWrt