Files
moloapp/audit/MOLO-H1-001/reproduce.py
T

74 lines
2.4 KiB
Python

#!/usr/bin/env python3
# MOLO-H1-001: chunked-body cap bypass via chunk-size addition overflow.
# See README.md in this directory. Drives build/bin/runtime_interop_server
# over a real socket; nothing here is a canned unit test.
import os
import sys
import time
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
import molo_audit as m
BODY_CAP_DEFAULT = 10 * 1024 * 1024 # runtime::caps::kBodyMaxDefault
PUSH_BYTES = BODY_CAP_DEFAULT + 4 * 1024 * 1024 # comfortably over the cap
def main():
m.arm_watchdog()
srv, _tmp = m.start_interop()
port = srv.port
s = m.connect(port)
s.sendall(b"POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\n")
# One ordinary 1-byte chunk, then a chunk-size whose declared length is
# 2**64-1. body_.size()(=1) + size overflows size_t and wraps to 0, so
# the "body_.size() + size > body_cap_" cap check at parser.cpp passes
# when it should reject with 413.
s.sendall(b"1\r\nA\r\n")
s.sendall(b"ffffffffffffffff\r\n")
sent = 0
chunk = b"B" * (1024 * 1024)
rejected_or_closed = False
s.setblocking(False)
import select
while sent < PUSH_BYTES:
try:
s.sendall(chunk)
sent += len(chunk)
except (BlockingIOError, InterruptedError):
time.sleep(0.02)
continue
except OSError:
rejected_or_closed = True
break
r, _, _ = select.select([s], [], [], 0)
if r:
data = s.recv(4096)
if not data or b" 413 " in data or b" 400 " in data:
rejected_or_closed = True
break
if not srv.alive():
m.verdict_skip("server process died (unrelated to this check)")
s.close()
rss = srv.rss_kb()
srv.stop()
if rejected_or_closed:
m.verdict_pass(
"server rejected or closed before %d bytes crossed the %d-byte body cap"
% (sent, BODY_CAP_DEFAULT)
)
if sent >= PUSH_BYTES:
m.verdict_fail(
"sent %d bytes of chunk body (cap is %d) with no 413/close: the chunk-size "
"overflow in FeedChunkSize (body_.size()+size wraps) bypasses kBodyMaxDefault "
"entirely; peak RSS ~%d KiB" % (sent, BODY_CAP_DEFAULT, rss)
)
m.verdict_fail("loop ended ambiguously after %d bytes" % sent)
if __name__ == "__main__":
main()