From 0cfda62f4dd72aec0d7bc1cd17161f5f7dcfa4e9 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 1 Oct 2026 13:55:43 -0500 Subject: [PATCH 01/10] http: bound the REU body sink to its own region (adv-l5 on #266) http_sink_blit STASHed each bounce to http_reu_body_base + cursor with no bound, and REU addresses wrap. On a 512 KB / 1 MB REU the default base $10:0000 is bank 0; on 16 MB the carry out of the bank byte was dropped. A server's body then reached the multiply rows (banks 0-1), comb's Lim-Lee table and the cold-code bank (bank 2): adv-l5's PoC runs server code at the next typed-target prompt, with the cold bank's marker and 8-bit XOR forged. The XOR is not authentication; bounding the writes is the fix. - reu_probe_size (boot, UCI, before anything else writes the REU) finds the REU's size by write/read-back/alias at bank offset 0; no REU or an expired DMA confirm leaves it unknown. - sink_room_init (per body): the region is [base, end), end = the probed size, or bank 6 below the P-384 banks; a base in banks 0-2 or 6-7, past the end, or with no size gets an empty region. - sink_fits: a blit that would cross the end (or wrap 24 bits) writes nothing and latches http_sink_full; every later blit is refused. http_recv_body stops at once and returns C=1; the timeout verdict says C=1; the menu prints BODY TOO BIG FOR THE REU instead of the viewer. - Assemble-time asserts: HTTP_REU_BODY_BASE outside banks 0-2 and 6-7; the cold bank and the reserved overlay slots inside them. - tools/test_reu_body_sink.py (6502 interpreter, 512 KB/1 MB/16 MB/no REU, adv-l5's attack); test_http gains a VICE over-region vector; the banner rig's body oracle reads http_sink_full. - uci-m3: HTTP_SINK_GUARD gets a home in its cfg, and the M3 receive loop stops on a refusal and is C=1 at completion when one happened. Co-Authored-By: Claude Opus 5.5 (1M context) --- cfg/c64-https-uci-m3.cfg | 1 + cfg/c64-https-uci-onchip-cold.cfg | 3 + cfg/c64-https-uci-onchip.cfg | 3 + cfg/c64-https-uci.cfg | 3 + src/boot.s | 23 ++- src/http.s | 217 +++++++++++++++++++++++- tools/_reu_model.py | 13 +- tools/http_body_checks.py | 27 +++ tools/test_http.py | 40 +++++ tools/test_http_body_checks_unit.py | 17 +- tools/test_reu_body_sink.py | 246 ++++++++++++++++++++++++++++ tools/uci/_prg_load.py | 8 +- tools/uci/rig_https_banner.py | 13 +- 13 files changed, 595 insertions(+), 19 deletions(-) create mode 100644 tools/test_reu_body_sink.py diff --git a/cfg/c64-https-uci-m3.cfg b/cfg/c64-https-uci-m3.cfg index 10ae84b5..fc4cb08a 100644 --- a/cfg/c64-https-uci-m3.cfg +++ b/cfg/c64-https-uci-m3.cfg @@ -28,6 +28,7 @@ SEGMENTS { HTTP_AUX_CODE: load = MAIN, type = ro; HTTP_AUX_CODE2: load = MAIN, type = ro; HTTP_SINK_CODE: load = MAIN, type = ro; + HTTP_SINK_GUARD: load = MAIN, type = ro; # the sink's REU-region bound (src/http.s) TARGET_PROMPT_CODE: load = MAIN, type = ro; UCI_CODE: load = MAIN, type = ro; CRYPTO_CODE: load = MAIN, type = ro, optional = yes; diff --git a/cfg/c64-https-uci-onchip-cold.cfg b/cfg/c64-https-uci-onchip-cold.cfg index 5038c6bb..64931a00 100644 --- a/cfg/c64-https-uci-onchip-cold.cfg +++ b/cfg/c64-https-uci-onchip-cold.cfg @@ -313,6 +313,9 @@ SEGMENTS { # here breaks a rig loudly rather than corrupting crypto; it is still # the first thing to check when one starts failing. HTTP_SINK_CODE: load = CRYPTO_OVERLAY, type = ro, optional = yes; + # The sink's REU-region guard (src/http.s: reu_probe_size, sink_fits): + # resident, CRYPTO_HOT has the room CRYPTO_OVERLAY and LOADER lack. + HTTP_SINK_GUARD: load = CRYPTO_HOT, type = ro, optional = yes; # jsr-only helper slice split out of HTTP_AUX_CODE for ip65's sake # (see c64-https-ip65.cfg); under UCI it shares CRYPTO_OVERLAY. HTTP_AUX_CODE2: load = CRYPTO_OVERLAY, type = ro, optional = yes; diff --git a/cfg/c64-https-uci-onchip.cfg b/cfg/c64-https-uci-onchip.cfg index b52aba5d..df62ddfa 100644 --- a/cfg/c64-https-uci-onchip.cfg +++ b/cfg/c64-https-uci-onchip.cfg @@ -284,6 +284,9 @@ SEGMENTS { # here breaks a rig loudly rather than corrupting crypto; it is still # the first thing to check when one starts failing. HTTP_SINK_CODE: load = CRYPTO_OVERLAY, type = ro, optional = yes; + # The sink's REU-region guard (src/http.s: reu_probe_size, sink_fits): + # resident, CRYPTO_HOT has the room CRYPTO_OVERLAY and LOADER lack. + HTTP_SINK_GUARD: load = CRYPTO_HOT, type = ro, optional = yes; # jsr-only helper slice split out of HTTP_AUX_CODE for ip65's sake # (see c64-https-ip65.cfg); under UCI it shares CRYPTO_OVERLAY. HTTP_AUX_CODE2: load = CRYPTO_OVERLAY, type = ro, optional = yes; diff --git a/cfg/c64-https-uci.cfg b/cfg/c64-https-uci.cfg index 8af2f16c..2208b458 100644 --- a/cfg/c64-https-uci.cfg +++ b/cfg/c64-https-uci.cfg @@ -228,6 +228,9 @@ SEGMENTS { # build target is broken and TLS-level P-384 is stubbed (the same # trade the other overlay residents already make). HTTP_SINK_CODE: load = CRYPTO_OVERLAY, type = ro, optional = yes; + # The sink's REU-region guard (src/http.s: reu_probe_size, sink_fits): + # resident, CRYPTO_HOT has the room CRYPTO_OVERLAY and LOADER lack. + HTTP_SINK_GUARD: load = CRYPTO_HOT, type = ro, optional = yes; # jsr-only helper slice split out of HTTP_AUX_CODE for ip65's sake # (see c64-https-ip65.cfg); under UCI it shares CRYPTO_OVERLAY. HTTP_AUX_CODE2: load = CRYPTO_OVERLAY, type = ro, optional = yes; diff --git a/src/boot.s b/src/boot.s index f43ec232..746a1901 100644 --- a/src/boot.s +++ b/src/boot.s @@ -25,6 +25,9 @@ .if .defined(HTTPS_PIN_SPKI) .and (.not .defined(BACKEND_UCI_M3)) .import cert_pin_banner .endif +.ifdef BACKEND_UCI + .import reu_probe_size +.endif .ifdef COLD_BANK ; uci-comb cold-code bank (src/net/uci/cold_bank.s) .include "cold_bank.inc" @@ -44,7 +47,7 @@ .ifdef VIEWER_LINKED ; #155 phase 2: see Makefile KEEP_VIEWER .import viewer_enter .endif - .import http_body_sink + .import http_body_sink, http_sink_full .endif ; ---- exports: REU multiply table routines ---- @@ -278,6 +281,12 @@ start: inc @zbss_store+2 dex bne @zbss_page +.ifdef BACKEND_UCI + ; The REU's size bounds the HTTP body sink's region (src/http.s). + ; Probed before anything else writes the REU: it writes offset 0 + ; of banks 0, 1, 2, 4 ... 128, which boot rewrites afterwards. + jsr reu_probe_size +.endif .ifdef COLD_BANK ; Before anything else can touch the image's copy at $C000 (the ; TCP ring's range): stash it into the REU. @@ -793,8 +802,15 @@ do_https_get: ; zeroed at boot), so the hook would be a dead branch. lda http_body_sink beq @print_body + lda http_sink_full ; the body outgrew its REU region: the + bne @sink_full ; sink stopped it, and wrote nothing past jsr viewer_enter jmp @close +@sink_full: + lda #sink_full_msg + jsr print_string + jmp @close @print_body: .endif ; display response body @@ -812,6 +828,11 @@ do_https_get: .include "m3_https_get.inc" ; src/net/uci-m3/: do_https_get .endif ; BACKEND_UCI_M3 +.if .defined(BACKEND_UCI) .and .defined(VIEWER_LINKED) +sink_full_msg: + .byte "BODY TOO BIG FOR THE REU", $0d, 0 +.endif + ; ============================================================================= ; print_resp_body - print up to 200 bytes of http_resp_buf to screen. ; Bytes originate from the network as ASCII, so they are fed through diff --git a/src/http.s b/src/http.s index ac73f5bd..013a0e14 100644 --- a/src/http.s +++ b/src/http.s @@ -33,10 +33,15 @@ .ifdef BACKEND_UCI .export http_reu_body_base .export http_sink_blit + .export reu_probe_size, reu_top_bank, reu_size_ok + .export http_sink_room, http_sink_full .endif ; Every REU execute goes through src/reu_exec.s (#191, SPEC §8.2). .import reu_execute +.ifdef BACKEND_UCI + .import reu_dma_timeout +.endif ; ---- imports: data.asm BSS (HTTP I/O + parser state) ---- .import http_host_ptr @@ -262,6 +267,10 @@ http_recv_loop: sta http_in_mode ; input mode 1: parser reads this span jsr http_recv_response bcc @recv_complete ; C=0 means parsing complete +.ifdef BACKEND_UCI + lda http_sink_full ; the body outgrew its REU region: stop + bne @recv_complete ; now (C=1 below), not at the framing +.endif ; Reset timeout counter on progress lda #0 sta http_recv_ticks @@ -281,7 +290,12 @@ http_recv_loop: ; this call covers the poll-timeout fallback so a sink body cut ; short still gets its final blit + first-512 restore. jsr http_body_finish +.ifdef BACKEND_UCI + lda http_sink_full ; C=1 iff the sink refused a write + cmp #1 +.else clc +.endif rts .else ; ============================================================================= @@ -329,6 +343,8 @@ http_recv_body: jsr net_poll jsr http_recv_response bcc @m3_complete + lda http_sink_full ; the body outgrew its REU region: stop + bne @m3_dead ; now, C=1 lda net_tcp_state cmp #NET_TCP_CONNECTED bne @m3_ended @@ -359,7 +375,8 @@ http_recv_body: rts @m3_complete: jsr http_body_finish - clc + lda http_sink_full ; C=1 iff the sink refused a write + cmp #1 rts @m3_tail: .word 0 .endif ; BACKEND_UCI_M3 @@ -425,8 +442,12 @@ http_recv_timeout_verdict: sec ; C=1: no clean end (budget / stall) php ; http_body_finish clobbers C jsr http_body_finish ; idempotent — http_sink_flushed latch - plp ; C = 1 iff no clean end (kept: AND/LDA - lda http_parse_state ; leave C alone, unlike CMP) + plp ; C = 1 iff no clean end (kept: LDA/AND +.ifdef BACKEND_UCI ; below leave C alone, unlike CMP) + lda http_sink_full ; a body the sink refused is never complete + bne @to_short +.endif + lda http_parse_state and #2 ; 2 = body; 0/1 = status line / headers beq @to_short ; never reached the body: not a response lda http_cl_valid @@ -950,7 +971,11 @@ http_body_begin: sta http_reu_cursor+1 sta http_reu_cursor+2 sta http_sink_flushed +.ifdef BACKEND_UCI + jmp sink_room_init ; this body's REU region (and clear the latch) +.else rts +.endif ; ----------------------------------------------------------------------------- ; http_body_done_check - is the identity body complete? @@ -1446,6 +1471,8 @@ http_sink_blit: lda http_resp_len ora http_resp_len+1 beq @rts + jsr sink_fits ; C=1: past the region -- write nothing + bcs @rts jsr sink_reu_setup clc ; REU addr = 24-bit base + cursor lda http_reu_body_base @@ -1551,6 +1578,190 @@ http_body_finish: rts .endif ; BACKEND_UCI (REU sink) +.ifdef BACKEND_UCI +; ============================================================================= +; The sink's REU region. http_sink_blit used to STASH to base + cursor with +; no bound, and REU addresses wrap: on a 512 KB or 1 MB REU the default +; base $10:0000 IS bank 0, and on 16 MB the 24-bit sum's carry out of the +; bank byte is dropped. A server's body then lands on the REU-profile +; multiply rows (banks 0-1), comb's Lim-Lee table and the cold-code bank +; (bank 2) -- all executed or trusted later. Now every body has a region: +; +; [http_reu_body_base, end), end = the REU's size as probed at boot, +; or bank SINK_P384_BANK when the base sits below it, +; +; and a base in banks 0..SINK_FLOOR_BANK-1 or the P-384 banks, past the +; REU's end, or with no REU size established, gets an EMPTY region. A +; blit that would cross the end writes nothing and latches +; http_sink_full; http_recv_body then stops and every verdict is C=1. +; ============================================================================= + .include "reu_layout.inc" ; REU_OVERLAY_* (reserved slots) + .include "cold_bank.inc" ; COLD_REU (comb's cold-code bank) +SINK_FLOOR_BANK = 3 ; banks 0-1: multiply rows; 2: comb Lim-Lee + ; + the cold bank (src/cold_bank.inc) +SINK_P384_BANK = 6 ; banks 6-7: the parked P-384 overlays +.assert HTTP_REU_BODY_BASE >= SINK_FLOOR_BANK * $10000, error, "HTTP_REU_BODY_BASE overlaps REU banks 0-2 (multiply rows, Lim-Lee, cold bank)" +.assert HTTP_REU_BODY_BASE < SINK_P384_BANK * $10000 .or HTTP_REU_BODY_BASE >= (SINK_P384_BANK + 2) * $10000, error, "HTTP_REU_BODY_BASE is in the P-384 banks 6-7" +.assert HTTP_REU_BODY_BASE < $1000000, error, "HTTP_REU_BODY_BASE past a 16 MB REU" +.assert REU_OVERLAY_P384_SHA384 >= SINK_P384_BANK * $10000 .and REU_OVERLAY_P384_CURVE + OVERLAY_SIZE <= (SINK_P384_BANK + 2) * $10000, error, "P-384 overlays left banks 6-7" +.assert ^(COLD_REU + COLD_IMAGE_MAX - 1) < SINK_FLOOR_BANK, error, "the cold bank left banks 0-2" +.assert REU_OVERLAY_STORE_BASE + OVERLAY_SIZE <= SINK_FLOOR_BANK * $10000 .and REU_OVERLAY_P256 + OVERLAY_SIZE <= SINK_FLOOR_BANK * $10000, error, "REU overlays left banks 0-2" + + .segment "HTTP_SINK_GUARD" + +; ----------------------------------------------------------------------------- +; reu_probe_size - boot, BEFORE anything else writes the REU (it writes +; offset 0 of banks 0, 1, 2, 4, ... 128, which reu_mul_init / the comb +; precompute / nobody rewrite after it). +; Out: reu_top_bank = banks - 1 ($FF = 16 MB) and reu_size_ok = 1; or +; reu_size_ok = 0 when a DMA did not confirm. Bank N exists iff a byte +; written there reads back AND bank 0 did not change (no alias): a +; 1750/1764/U64 REU wraps modulo its size. With no REU nothing reads +; back, so the size is one bank, below every region: fail closed. +; ----------------------------------------------------------------------------- +reu_probe_size: + lda #0 + sta reu_size_ok + sta reu_probe_bank + sta reu_probe_byte + lda #$90 ; STASH: bank 0 := 0 + jsr reu_probe_dma + ldx #1 +@bank: stx reu_probe_bank + stx reu_probe_byte + lda #$90 ; STASH: bank X := X + jsr reu_probe_dma + txa + eor #$FF + sta reu_probe_byte ; not X, so a FETCH that never ran shows + lda #$91 ; FETCH bank X + jsr reu_probe_dma + cpx reu_probe_byte + bne @size ; bank X does not hold a byte + lda #0 + sta reu_probe_bank + lda #$91 ; FETCH bank 0 + jsr reu_probe_dma + lda reu_probe_byte + bne @size ; bank X is bank 0 + txa + asl + tax + bne @bank ; 1, 2, 4 ... 128; then all 256 banks +@size: dex + stx reu_top_bank + lda reu_dma_timeout ; sticky: a confirm that ever expired + bne @out ; makes the size unknown + inc reu_size_ok +@out: rts + +; reu_probe_dma - A = command: one byte between reu_probe_byte and +; reu_probe_bank:$0000. X, Y preserved (reu_execute preserves them). +reu_probe_dma: + pha + lda #reu_probe_byte + sta reu_c64_hi + lda #0 + sta reu_reu_lo + sta reu_reu_hi + sta reu_len_hi + sta reu_addr_ctrl + lda reu_probe_bank + sta reu_reu_bank + lda #1 + sta reu_len_lo + pla + jmp reu_execute + +; ----------------------------------------------------------------------------- +; sink_room_init - from http_body_begin: http_sink_room = this body's +; region size (0 = no region), http_sink_full = 0. Clobbers A, X. +; ----------------------------------------------------------------------------- +sink_room_init: + lda #0 + sta http_sink_full + sta http_sink_room + sta http_sink_room+1 + sta http_sink_room+2 + lda reu_size_ok + beq @none + ldx reu_top_bank + inx ; X = banks (0 = 256) + lda http_reu_body_base+2 + cmp #SINK_FLOOR_BANK + bcc @none ; banks 0-2 + cmp #SINK_P384_BANK + bcs @high + cpx #0 ; below P-384: end = min(banks, 6) + beq @six ; (X = 0 is 256 banks) + cpx #SINK_P384_BANK+1 + bcc @below +@six: ldx #SINK_P384_BANK + bne @end ; always +@high: cmp #SINK_P384_BANK+2 + bcc @none ; banks 6-7 + cpx #0 + beq @end ; 16 MB: end is $1000000 +@below: stx reu_probe_bank ; (scratch) base bank must be < banks + cmp reu_probe_bank + bcs @none +@end: sec ; room = end - base (bit 24 dropped: 16 MB) + lda #0 + sbc http_reu_body_base + sta http_sink_room + lda #0 + sbc http_reu_body_base+1 + sta http_sink_room+1 + txa + sbc http_reu_body_base+2 + sta http_sink_room+2 +@none: rts + +; ----------------------------------------------------------------------------- +; sink_fits - C=0: cursor + http_resp_len <= http_sink_room. C=1 otherwise, +; and http_sink_full latched (every later blit is refused too). +; Clobbers A. +; ----------------------------------------------------------------------------- +sink_fits: + lda http_sink_full + bne @no + clc ; end = cursor + len, 24-bit + lda http_reu_cursor + adc http_resp_len + sta reu_sink_end + lda http_reu_cursor+1 + adc http_resp_len+1 + sta reu_sink_end+1 + lda http_reu_cursor+2 + adc #0 + sta reu_sink_end+2 + bcs @full + lda http_sink_room ; room - end: a borrow means end > room + cmp reu_sink_end + lda http_sink_room+1 + sbc reu_sink_end+1 + lda http_sink_room+2 + sbc reu_sink_end+2 + bcc @full + clc + rts +@full: lda #1 + sta http_sink_full +@no: sec + rts + +; Sink region state. Not BSS: CRYPTO_COLD_SHADOW has no room on comb. +reu_size_ok: .byte 0 ; 1: reu_top_bank is the probed size +reu_top_bank: .byte 0 ; banks - 1 +reu_probe_bank: .byte 0 +reu_probe_byte: .byte 0 +http_sink_room: .byte 0, 0, 0 ; this body's region, bytes (24-bit LE) +http_sink_full: .byte 0 ; 1: a blit was refused; the body failed +reu_sink_end: .byte 0, 0, 0 +.endif + ; ============================================================================= ; http_get_plain - perform a plain HTTP (no TLS) GET request ; Input: http_host_ptr/http_host_len = hostname diff --git a/tools/_reu_model.py b/tools/_reu_model.py index 61af2145..7d93c85c 100644 --- a/tools/_reu_model.py +++ b/tools/_reu_model.py @@ -2,8 +2,9 @@ Enough of the register file for src/reu_exec.s and src/net/uci/cold_bank.s: STASH / FETCH with both addresses incrementing, the execute bit with the -$FF00 trigger disabled, and status bit 6 (END OF BLOCK), which a read -clears. Nothing else (no SWAP/VERIFY, no autoload, no fixed addresses); +$FF00 trigger disabled, autoload, and status bit 6 (END OF BLOCK), which a +read clears. Addresses wrap modulo the REU's size, as a 1750/1764/U64 REU +does. Nothing else (no SWAP/VERIFY, no fixed addresses); asking for it raises rather than doing something plausible. `present=False` models a machine with no REU: commands do nothing. What @@ -12,7 +13,8 @@ DMA that never happened -- the case only a check of the fetched bytes catches. -Used by tools/test_cold_bank.py and tools/test_trust_store_6502.py. +Used by tools/test_cold_bank.py, tools/test_trust_store_6502.py and +tools/test_reu_body_sink.py. """ from __future__ import annotations @@ -52,10 +54,9 @@ def write(self, addr: int, value: int) -> None: def _execute(self, cmd: int) -> None: if not cmd & 0x10: raise NotImplementedError("REU: $FF00-triggered execute") - if cmd & 0x20: - raise NotImplementedError("REU: autoload") if self.regs[0x0A]: raise NotImplementedError("REU: fixed-address transfer") + saved = bytes(self.regs) # autoload restores them afterwards op = cmd & 0x03 c64 = self.regs[2] | self.regs[3] << 8 reu = (self.regs[4] | self.regs[5] << 8 | self.regs[6] << 16) % len(self.mem) @@ -70,6 +71,8 @@ def _execute(self, cmd: int) -> None: self.log.append(("fetch", c64, reu, n)) else: raise NotImplementedError(f"REU: transfer type {op}") + if cmd & 0x20: + self.regs[2:9] = saved[2:9] self.status |= 0x40 # END OF BLOCK diff --git a/tools/http_body_checks.py b/tools/http_body_checks.py index d7c96fc5..269dc847 100644 --- a/tools/http_body_checks.py +++ b/tools/http_body_checks.py @@ -38,6 +38,10 @@ claimed the two "mirror branch for branch". They do not, and the claim was wrong in a way that reads as reassuring, so it is spelled out instead: + sink refused MATCHES (UCI). 6502: `lda http_sink_full / bne + @to_short`, first: a body the REU sink stopped at its + region's end is never complete, whatever its framing + says. The symbol is UCI-only (OPTIONAL_SYMBOLS). parse_state < 2 MATCHES. 6502: `and #2 / beq @to_short` -> `sec`. Content-Length MATCHES, including the PRECEDENCE: both test `http_cl_valid` before `http_chunked`, and both then @@ -166,6 +170,11 @@ "http_chunked": 1, "http_chunk_state": 1, } +#: Read when the build has them (labels.txt), 0 otherwise: UCI-only state. +#: http_sink_full: 1 = the REU body sink refused a write past its region. +OPTIONAL_SYMBOLS = { + "http_sink_full": 1, +} @dataclass @@ -178,6 +187,7 @@ class BodyState: body_total: int chunked: int chunk_state: int + sink_full: int = 0 def summary(self) -> str: framing = ("Content-Length" if self.cl_valid @@ -214,9 +224,19 @@ def decode_body_state(raw: dict) -> BodyState: body_total=_le(raw["http_body_total"]), chunked=_le(raw["http_chunked"]), chunk_state=_le(raw["http_chunk_state"]), + sink_full=_optional(raw, "http_sink_full"), ) +def _optional(raw: dict, name: str) -> int: + if name not in raw: + return 0 + if len(bytes(raw[name])) != OPTIONAL_SYMBOLS[name]: + raise ValueError(f"{name}: read {len(bytes(raw[name]))} bytes, " + f"expected {OPTIONAL_SYMBOLS[name]}") + return _le(raw[name]) + + def expected_body_size(state: BodyState): """The body size the RESPONSE ITSELF declares, or None. @@ -264,9 +284,16 @@ def check_body_complete(state: BodyState) -> Verdict: "body_total": state.body_total, "chunked": state.chunked, "chunk_state": state.chunk_state, + "sink_full": state.sink_full, "expected": expected_body_size(state), } + if state.sink_full: + return Verdict(False, + f"REFUSED: the body outgrew its REU region after " + f"{state.body_total:,} B consumed; the sink stopped it " + "and wrote nothing past the region (http_sink_full)", ev) + if state.parse_state < PARSE_STATE_BODY: return Verdict(False, f"the response never reached the body " diff --git a/tools/test_http.py b/tools/test_http.py index d2b0d15d..f2ef66e4 100755 --- a/tools/test_http.py +++ b/tools/test_http.py @@ -749,6 +749,46 @@ def test_reu_body_sink(transport, labels): f"reu_ok={bytes(reu) == body2}") failed += 1 + # --- vector 3: a body past its REU region (adv-l5 on #266) --- + # Boot probed VICE's 512 KB REU; a base 1 KB below bank 6 (the P-384 + # banks) gets a 1 KB region. A 1,500 B body: the first two bounces + # fit exactly, the last is refused, and bank 6 is never written. + size_ok = labels.address("reu_size_ok") + if size_ok is not None: + top = read_bytes(transport, labels.address("reu_top_bank"), 1)[0] + ok = read_bytes(transport, size_ok, 1)[0] + if ok == 1 and top == 7: + print(" PASS: boot probed the 512 KB REU (8 banks)") + passed += 1 + else: + print(f" FAIL: REU probe: reu_size_ok={ok} reu_top_bank={top} " + "(want 1, 7 for -reusize 512)") + failed += 1 + bank6 = (0x00, 0x00, 0x06) + before = bytes(_reu_fetch_to_ram(transport, labels, 512, reu_base=bank6)) + write_bytes(transport, base_var, [0x00, 0xFC, 0x05]) + body3 = bytes((i * 29 + 11) & 0xFF for i in range(1500)) + wire3 = (b"HTTP/1.1 200 OK\r\nContent-Length: 1500\r\n\r\n" + body3) + _run_span_response(transport, labels, [wire3]) + full = read_bytes(transport, labels.address("http_sink_full"), 1)[0] + room = _read_u24(transport, labels, "http_sink_room") + after = bytes(_reu_fetch_to_ram(transport, labels, 512, reu_base=bank6)) + head = b"".join(bytes(_reu_fetch_to_ram(transport, labels, 512, + reu_base=(0x00, hi, 0x05))) + for hi in (0xFC, 0xFE)) # 512 B: the stub is at $C380 + if full == 1 and room == 1024 and after == before and head == body3[:1024]: + print(" PASS: body past its 1 KB region: the bounces that fit " + "are written, the rest refused (http_sink_full), bank 6 " + "untouched") + passed += 1 + else: + print(f" FAIL: over-region body: full={full} room={room} " + f"bank6_untouched={after == before} " + f"head_ok={head == body3[:1024]}") + failed += 1 + else: + print(" !! REU-region vector NOT RUN: no reu_size_ok label") + # Cleanup: buffer mode, shipped default base (bank 16), ring input. write_bytes(transport, sink_flag, [0]) write_bytes(transport, base_var, [0x00, 0x00, 0x10]) diff --git a/tools/test_http_body_checks_unit.py b/tools/test_http_body_checks_unit.py index 750d11bf..f5a5b388 100644 --- a/tools/test_http_body_checks_unit.py +++ b/tools/test_http_body_checks_unit.py @@ -86,6 +86,16 @@ def test_body_complete_red_green() -> None: v = hbc.check_body_complete(state(body_total=125_704)) assert not v.ok and "OVER-READ" in v.reason, v.reason + # A body the REU sink refused past its region (adv-l5 on #266) fails + # even when its Content-Length framing is satisfied, and the field is + # optional: a build without it decodes as 0. + v = hbc.check_body_complete(state(sink_full=1)) + assert not v.ok and "REFUSED" in v.reason, v.reason + raw = {n: bytes(w) for n, w in hbc.SYMBOLS.items()} + assert hbc.decode_body_state(raw).sink_full == 0 + raw["http_sink_full"] = b"\x01" + assert hbc.decode_body_state(raw).sink_full == 1 + # --- chunked framing --------------------------------------------------- chunked = dict(cl_valid=0, content_length=0, chunked=1) v = hbc.check_body_complete(state(**chunked, @@ -877,9 +887,10 @@ def test_the_6502_verdict_keeps_the_shape_this_mirrors() -> None: body = src[start:src.index('.segment "CODE"', start)] order = [ln.split()[1] for ln in body.splitlines() if ln.strip().startswith("lda http_")] - assert order[:3] == ["http_parse_state", "http_cl_valid", "http_chunked"], ( - f"http_recv_timeout_verdict now tests {order[:3]}; this module " - "mirrors parse_state -> cl_valid -> chunked and its " + assert order[:4] == ["http_sink_full", "http_parse_state", "http_cl_valid", + "http_chunked"], ( + f"http_recv_timeout_verdict now tests {order[:4]}; this module " + "mirrors sink_full -> parse_state -> cl_valid -> chunked and its " "cl_valid-before-chunked precedence would no longer match the PRG") # And the documented divergence is still the divergence we documented: # the 6502 chunked arm rejects unconditionally and never reads diff --git a/tools/test_reu_body_sink.py b/tools/test_reu_body_sink.py new file mode 100644 index 00000000..95fdd30c --- /dev/null +++ b/tools/test_reu_body_sink.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python3 +"""test_reu_body_sink.py -- the HTTP body sink stays inside its REU region. + +WHAT THIS TESTS + +http_sink_blit (src/http.s) used to STASH each 512 B bounce to +http_reu_body_base + cursor with no bound. REU addresses wrap: on a 512 KB +or 1 MB REU the default base $10:0000 IS bank 0, and on 16 MB the carry out +of the bank byte is dropped. A server's body then reached the multiply +rows (banks 0-1), comb's Lim-Lee table and the cold-code bank (bank 2), and +the next typed-target prompt ran the server's bytes. + +On a uci-comb HTTPS_BODY_TO_REU=1 TRUST_STORE=1 image, on the repo's 6502 +interpreter with a modelled REU (tools/_reu_model.py), at 512 KB, 1 MB and +16 MB and with no REU: + + * reu_probe_size finds the REU's size (and nothing, without one); + * sink_room_init gives each base the region the probe allows: empty in + banks 0-2 and 6-7, past the REU's end, or with no size established; + * a blit that would cross the region's end writes NOTHING, latches + http_sink_full, and every later blit is refused; one that ends exactly + at the end is written; + * adv-l5's attack (a body whose bytes, at the wrap offsets, are a forged + cold-bank image with the marker and the XOR fixed): the REU's bank 2 is + unchanged byte for byte -- Lim-Lee table and cold image -- and the next + prompt runs the real prompt, not the server's code; + * http_recv_timeout_verdict says C=1 for a refused body even when its + Content-Length framing is otherwise satisfied. + +The integrity check on the cold bank (marker + 8-bit XOR) is NOT what stops +this: a writer who knows the image can satisfy it. Bounding the writes is. + +Usage: + python3 tools/test_reu_body_sink.py # make clean && make + C64_SKIP_BUILD=1 python3 tools/test_reu_body_sink.py + +Exit 0 pass, 1 fail, 2 could not run. +""" +from __future__ import annotations + +import hashlib +import os +import subprocess +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import test_cold_bank as tc # noqa: E402 +import test_trust_store_6502 as t6 # noqa: E402 +from test_trust_store_6502 import Env # noqa: E402 +from _reu_model import REU, Bus # noqa: E402 +from _skip_policy import cannot_run, verdict # noqa: E402 + +REPO = HERE.parent +PROFILE = ["BACKEND=uci", "USE_NISTCURVES_ONCHIP_COMB=1", + "HTTPS_HOST=en.wikipedia.org", "HTTPS_PATH=/wiki/Commodore_64", + "HTTPS_BODY_TO_REU=1", "TRUST_STORE=1"] +CERTIFIES = "the HTTP body sink's REU-region bound (adv-l5 on #266)" +KB, MB = 1024, 1024 * 1024 +PASSED = 0 +FAILED: list[str] = [] + + +def check(ok, msg): + global PASSED + if ok: + PASSED += 1 + else: + FAILED.append(msg) + + +class Box(tc.Rig): + """test_cold_bank's machine, booted like boot.s: probe, then the bank.""" + + def __init__(self, env, size=16 * MB, present=True, absent_status=0x00): + super().__init__(env, init=False) + self.reu = REU(self.ram, size=size, present=present, + absent_status=absent_status) + self.m.reu = self.reu + self.m.mem.uci = Bus(self.reu, self.dos) + self.m.cpu.call(self.L["reu_probe_size"]) + self.m.cpu.call(self.L["cold_bank_init"]) + + def u24(self, name): + a = self.L[name] + return self.ram[a] | self.ram[a + 1] << 8 | self.ram[a + 2] << 16 + + def put24(self, name, v): + a = self.L[name] + self.ram[a:a + 3] = bytes([v & 0xFF, (v >> 8) & 0xFF, v >> 16]) + + def body_begin(self, base): + self.put24("http_reu_body_base", base) + self.m.cpu.call(self.L["http_body_begin"]) + return self.u24("http_sink_room") + + def blit(self, cursor, data): + """One bounce of `data` at `cursor`. -> the REU writes it made.""" + buf, ln = self.L["http_resp_buf"], self.L["http_resp_len"] + self.ram[buf:buf + len(data)] = data + self.ram[ln], self.ram[ln + 1] = len(data) & 0xFF, len(data) >> 8 + self.put24("http_reu_cursor", cursor) + n = len(self.reu.log) + self.m.cpu.call(self.L["http_sink_blit"]) + return [x for x in self.reu.log[n:] if x[0] == "stash"] + + +def case_probe(env): + for size in (128 * KB, 256 * KB, 512 * KB, 1 * MB, 2 * MB, 16 * MB): + b = Box(env, size=size) + banks = size // (64 * KB) + check(b.r8("reu_size_ok") == 1 and b.r8("reu_top_bank") == (banks - 1) & 0xFF, + f"{size // KB} KB: probe says ok={b.r8('reu_size_ok')} " + f"top={b.r8('reu_top_bank')}, want top {banks - 1}") + for status in (0x00, 0xFF): + b = Box(env, present=False, absent_status=status) + room = b.body_begin(0x100000) + check(room == 0, f"no REU (open bus ${status:02X}): region {room:#x}, want 0") + + +def case_regions(env): + for size, base, want in [ + (512 * KB, 0x100000, 0), # aliases bank 0: refused + (1 * MB, 0x100000, 0), + (16 * MB, 0x100000, 0xF00000), # to the REU's end, no wrap + (512 * KB, 0x030000, 0x030000), # banks 3-5 (VICE tests) + (512 * KB, 0x05F000, 0x001000), + (16 * MB, 0x030000, 0x030000), # stops below the P-384 banks + (16 * MB, 0x080000, 0xF80000), + (16 * MB, 0x020000, 0), (16 * MB, 0x02A000, 0), # bank 2 + (16 * MB, 0x000000, 0), (16 * MB, 0x010000, 0), # banks 0-1 + (16 * MB, 0x060000, 0), (16 * MB, 0x07FFFF, 0), # banks 6-7 + (2 * MB, 0x200000, 0), # past the end + ]: + b = Box(env, size=size) + room = b.body_begin(base) + check(room == want, f"{size // KB} KB base ${base:06X}: region {room:#x}, want {want:#x}") + check(b.r8("http_sink_full") == 0, "body_begin left the latch set") + + +def case_bound(env): + b = Box(env, size=512 * KB) + room = b.body_begin(0x05F000) # 4 KB region + data = bytes(range(256)) * 2 + w = b.blit(room - 512, data) + check(len(w) == 1 and bytes(b.reu.mem[0x05F000 + room - 512:0x060000]) == data, + f"a bounce ending exactly at the region's end was not written: {w}") + p6 = bytes(b.reu.mem[0x060000:0x060200]) + w = b.blit(room - 511, data) + check(not w and b.r8("http_sink_full") == 1, + f"a bounce crossing the end by 1 B was written or not latched: {w}") + check(bytes(b.reu.mem[0x060000:0x060200]) == p6, "bank 6 was written") + w = b.blit(0, data) + check(not w, "after the latch, a bounce inside the region was still written") + b = Box(env, size=512 * KB) # fresh: no latch + b.body_begin(0x05F000) + w = b.blit(0xFFFF00, data) + check(not w and b.r8("http_sink_full") == 1, + "a cursor + length that wraps 24 bits was written") + + +def case_attack(env): + """adv-l5's PoC: the forged cold image at the wrap offsets.""" + L = env.labels + n = L["cold_marker_ui"] + 1 - L["__COLD_RUN_UI_START__"] + for size, cursor in ((512 * KB, 0x2A000), (1 * MB, 0x2A000), + (16 * MB, 0xF2A000), (16 * MB, 0xF20000)): + b = Box(env, size=size) + bank2 = bytes(b.reu.mem[0x020000:0x030000]) + b.body_begin(0x100000) + want = b.ram[L["cold_g_sum"]] + code = bytes([0xA9, 0x42, 0x8D, 0x34, 0x03, 0x18, 0x60]) + body = bytearray(code + bytes(n - len(code))) + body[-1] = tc.COLD_MARK + x = 0 + for v in body[:-2]: + x ^= v + body[-2] = x ^ tc.COLD_MARK ^ want + tag = f"{size // KB} KB cursor ${cursor:X}" + w = b.blit(cursor, bytes(body)) + check(not w, f"{tag}: the forged image was written: {w}") + check(bytes(b.reu.mem[0x020000:0x030000]) == bank2, + f"{tag}: REU bank 2 (Lim-Lee + cold bank) changed") + b.ram[0x0334] = 0 + b.set_states(0, 0) + c, _ = b.prompt([]) + check(b.ram[0x0334] != 0x42 and not c, + f"{tag}: the prompt ran server code (C={c}, $0334=${b.ram[0x0334]:02X})") + + +def case_verdict(env): + """A refused body fails even when its Content-Length is satisfied.""" + b = Box(env, size=512 * KB) + b.body_begin(0x05F000) + b.blit(0x1000, bytes(512)) # past the 4 KB region + L = env.labels + b.ram[L["http_parse_state"]] = 2 + b.ram[L["http_cl_valid"]] = 1 + b.put24("http_content_length", 5000) + b.put24("http_body_total", 5000) + c = b.m.cpu.call(L["http_recv_timeout_verdict"]) + check(c, "http_recv_timeout_verdict: C=0 for a body the sink refused") + b.ram[L["http_sink_full"]] = 0 # control: same framing + c = b.m.cpu.call(L["http_recv_timeout_verdict"]) + check(not c, "control: C=1 for a complete Content-Length body") + + +CASES = [case_probe, case_regions, case_bound, case_attack, case_verdict] + + +def main() -> int: + if os.environ.get("C64_SKIP_BUILD") != "1": + subprocess.run(["make", "clean"], cwd=REPO, capture_output=True) + p = subprocess.run(["make", *PROFILE], cwd=REPO, capture_output=True, text=True) + if p.returncode != 0: + return cannot_run(f"make {' '.join(PROFILE)} failed:\n{p.stdout[-1500:]}" + f"{p.stderr[-1500:]}", executed=0, total=len(CASES), + certifies=CERTIFIES) + if not t6.PRG.is_file() or not t6.LABELS.is_file(): + return cannot_run("no build/c64-https.prg + labels.txt", executed=0, + total=len(CASES), certifies=CERTIFIES) + env = Env() + need = ("http_sink_blit", "cold_call", "http_sink_room", "reu_probe_size", + "http_recv_timeout_verdict") + missing = [n for n in need if n not in env.labels] + if missing: + return cannot_run(f"build/ is not a uci-comb sink + cold-bank image " + f"(no {', '.join(missing)})", executed=0, + total=len(CASES), certifies=CERTIFIES) + print(f"PRG sha256 {hashlib.sha256(t6.PRG.read_bytes()).hexdigest()}") + for case in CASES: + before = len(FAILED) + try: + case(env) + except (t6.CPUError, t6.Reset) as e: + FAILED.append(f"{case.__name__}: {e}") + print(f"{'PASS' if len(FAILED) == before else 'FAIL'} {case.__name__}") + for f in FAILED: + print(" -", f) + print(f"{PASSED} checks passed, {len(FAILED)} failed") + return verdict(PASSED, len(FAILED), certifies=CERTIFIES) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/uci/_prg_load.py b/tools/uci/_prg_load.py index 6c249a4e..11fc83db 100644 --- a/tools/uci/_prg_load.py +++ b/tools/uci/_prg_load.py @@ -23,8 +23,8 @@ c64-wireguard, independently), and SYS does not read it. * $0801-$9FFF is compared, and so is $C000-$CFFF (plain RAM: the - uci-comb cold bank's image is carried there until boot stashes it in - the REU). The rest of the tail is BSS zero fill or sits under a ROM / + cold-code bank's image, src/net/uci/cold_bank.s, is carried there + until boot stashes it in the REU). The rest of the tail is BSS zero fill or sits under a ROM / I/O, so a host read there does not return what was loaded; the one property of it that IS decidable from the file - all zeros - is checked instead. The report names both spans. @@ -53,8 +53,8 @@ #: First address NOT compared: $A000+ is CRYPTO_COLD_SHADOW, RAM under the #: BASIC ROM, which a host read returns as ROM until boot.s banks it out. VERIFY_END = 0xA000 -#: A second span that IS compared: RAM no ROM shadows. uci-comb carries its -#: cold-code image here (src/net/uci/cold_bank.s); every other image zeros. +#: A second span that IS compared: RAM no ROM shadows. The cold-code bank +#: (src/net/uci/cold_bank.s) carries its image here; other images zeros. RAM_WINDOW = (0xC000, 0xD000) #: The head-zeroing event lands 2-5 s after READY; re-check past it. HEAD_SETTLE_S = 7.0 diff --git a/tools/uci/rig_https_banner.py b/tools/uci/rig_https_banner.py index 64bc1a7c..0ffba2f2 100755 --- a/tools/uci/rig_https_banner.py +++ b/tools/uci/rig_https_banner.py @@ -91,7 +91,7 @@ from http_body_checks import ( # noqa: E402 EXIT_FAIL, EXIT_INCONCLUSIVE, EXIT_PASS, PARSE_STATE_BODY, STALL_ABORT, STALL_GRACE, - SYMBOLS, StallTracker, check_body_complete, check_fetch_settled, + OPTIONAL_SYMBOLS, SYMBOLS, StallTracker, check_body_complete, check_fetch_settled, check_http_status, close_confirmed, decide_exit, decode_body_state, early_stop_step, poll_until, stall_config_error, ) @@ -376,6 +376,13 @@ def main() -> int: # uses as its progress signal too. print("Letting the fetch run to completion (socket must close cleanly)...") addrs = {name: label_addr(name) for name in SYMBOLS} + widths = dict(SYMBOLS) + for name, width in OPTIONAL_SYMBOLS.items(): + try: + addrs[name] = label_addr(name) + widths[name] = width + except KeyError: + pass # not in this build: reads as 0 # ONE DMA read per poll, not seven. The symbols are scattered across # a few hundred bytes of CRYPTO_COLD_SHADOW, and this loop runs every @@ -383,7 +390,7 @@ def main() -> int: # traffic the shared device does not need. Span, then slice; the # bounds come from labels.txt, so they follow the build. span_lo = min(addrs.values()) - span_hi = max(addrs[n] + w for n, w in SYMBOLS.items()) + span_hi = max(addrs[n] + w for n, w in widths.items()) assert span_hi - span_lo <= 8192, ( f"the http parser state now spans {span_hi - span_lo} B " f"(${span_lo:04X}-${span_hi:04X}); read it per symbol instead") @@ -391,7 +398,7 @@ def main() -> int: def read_state(): blob = bytes(client.read_mem(span_lo, span_hi - span_lo)) raw = {} - for name, width in SYMBOLS.items(): + for name, width in widths.items(): off = addrs[name] - span_lo raw[name] = blob[off:off + width] return decode_body_state(raw) From 37f7b2627f80de50bee53f12e9e1ccc1fba4ad25 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 1 Oct 2026 14:27:36 -0500 Subject: [PATCH 02/10] test_reu_body_sink: a benign oversized body stops at its region on a small REU Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/test_reu_body_sink.py | 45 ++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tools/test_reu_body_sink.py b/tools/test_reu_body_sink.py index 95fdd30c..922815d9 100644 --- a/tools/test_reu_body_sink.py +++ b/tools/test_reu_body_sink.py @@ -26,6 +26,11 @@ prompt runs the real prompt, not the server's code; * http_recv_timeout_verdict says C=1 for a refused body even when its Content-Length framing is otherwise satisfied. + * no attacker needed: the 751 KB Wikipedia demo body streamed on a + 512 KB or 1 MB REU stops cleanly at its region's end (nothing at all + with the default base, which aliases bank 0) and no REU byte outside + the region changes. On master the same stream overwrote all 16 KB of + the Lim-Lee table and the cold image. The integrity check on the cold bank (marker + 8-bit XOR) is NOT what stops this: a writer who knows the image can satisfy it. Bounding the writes is. @@ -206,7 +211,45 @@ def case_verdict(env): check(not c, "control: C=1 for a complete Content-Length body") -CASES = [case_probe, case_regions, case_bound, case_attack, case_verdict] +def case_benign_big_body(env): + """No attacker needed: the 751 KB Wikipedia demo body on a small REU. + + Streamed 512 B a bounce, as body_append does. With the default base on + 512 KB / 1 MB (it aliases bank 0) nothing may be written at all; with a + base in bank 3 the region ends at bank 6 and the body stops there. + Before the fix the same stream wrapped onto banks 0-2 (multiply rows, + Lim-Lee table, cold bank) and on into the P-384 banks.""" + total = 751_073 + for size, base, room in ((512 * KB, 0x100000, 0), (1 * MB, 0x100000, 0), + (512 * KB, 0x030000, 0x030000)): + b = Box(env, size=size) + mark = bytes((i * 7 + 1) & 0xFF for i in range(len(b.reu.mem))) + b.reu.mem[:] = mark # every REU byte known + tag = f"{size // KB} KB base ${base:06X}" + check(b.body_begin(base) == room, f"{tag}: region {b.u24('http_sink_room'):#x}") + cursor, refused_at = 0, None + while cursor < total: + n = min(512, total - cursor) + data = bytes([0xEE]) * n + if b.blit(cursor, data): + cursor += n + else: + refused_at = cursor + break + check(refused_at == room, f"{tag}: stopped at {refused_at}, want {room}") + check(b.r8("http_sink_full") == 1, f"{tag}: not latched") + lo = base % size + inside = range(lo, lo + room) + changed = [a for a in range(len(mark)) if b.reu.mem[a] != mark[a] + and a not in inside] + check(not changed, f"{tag}: {len(changed)} REU bytes written outside " + f"the region, first ${changed[0]:06X}" if changed else "") + check(all(b.reu.mem[a] == 0xEE for a in inside), + f"{tag}: the region itself was not filled") + + +CASES = [case_probe, case_regions, case_bound, case_attack, case_verdict, + case_benign_big_body] def main() -> int: From 5d90e6498f89b1b5ee5a287a50a79f280d26f621 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 1 Oct 2026 14:32:40 -0500 Subject: [PATCH 03/10] rig_https_banner: read http_sink_full on its own, resolved before 'G' Folding it into the one-DMA span widened the span to 20 KB on comb ($6113-$AF50) and tripped the span assert AFTER 'G', with a live socket (measured on the U64E, PRG 75be0262). The span is master's again; the optional symbol is resolved before any socket opens and read separately. Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/uci/rig_https_banner.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/uci/rig_https_banner.py b/tools/uci/rig_https_banner.py index 0ffba2f2..1dea838f 100755 --- a/tools/uci/rig_https_banner.py +++ b/tools/uci/rig_https_banner.py @@ -209,6 +209,15 @@ def main() -> int: prompt = True except KeyError: prompt = False + # OPTIONAL_SYMBOLS (UCI-only parser state, e.g. http_sink_full) are + # resolved here, before any socket opens, and read one by one: they are + # nowhere near the span read_state() takes in one DMA. + optional = {} + for name, width in OPTIONAL_SYMBOLS.items(): + try: + optional[name] = (label_addr(name), width) + except KeyError: + pass # not in this build: decodes as 0 if (TYPED_HOST or TYPED_PATH) and not prompt: print("[fatal] TYPED_HOST/TYPED_PATH set, but this PRG has no " "https_target_prompt (not a UCI build with #155 phase 2)", @@ -376,13 +385,6 @@ def main() -> int: # uses as its progress signal too. print("Letting the fetch run to completion (socket must close cleanly)...") addrs = {name: label_addr(name) for name in SYMBOLS} - widths = dict(SYMBOLS) - for name, width in OPTIONAL_SYMBOLS.items(): - try: - addrs[name] = label_addr(name) - widths[name] = width - except KeyError: - pass # not in this build: reads as 0 # ONE DMA read per poll, not seven. The symbols are scattered across # a few hundred bytes of CRYPTO_COLD_SHADOW, and this loop runs every @@ -390,7 +392,7 @@ def main() -> int: # traffic the shared device does not need. Span, then slice; the # bounds come from labels.txt, so they follow the build. span_lo = min(addrs.values()) - span_hi = max(addrs[n] + w for n, w in widths.items()) + span_hi = max(addrs[n] + w for n, w in SYMBOLS.items()) assert span_hi - span_lo <= 8192, ( f"the http parser state now spans {span_hi - span_lo} B " f"(${span_lo:04X}-${span_hi:04X}); read it per symbol instead") @@ -398,9 +400,11 @@ def main() -> int: def read_state(): blob = bytes(client.read_mem(span_lo, span_hi - span_lo)) raw = {} - for name, width in widths.items(): + for name, width in SYMBOLS.items(): off = addrs[name] - span_lo raw[name] = blob[off:off + width] + for name, (addr, width) in optional.items(): + raw[name] = bytes(client.read_mem(addr, width)) return decode_body_state(raw) # Everything above lives in CRYPTO_COLD_SHADOW, RAM under the BASIC From 883bd28f8e2e16b9ec928d810cb597230ead051a Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 1 Oct 2026 14:34:38 -0500 Subject: [PATCH 04/10] cold bank: assert the REU home against the linked library's tables (adv-l5 LOW 2) The old asserts compared COLD_REU with a literal floor, so they could not fail. They now import the code-read symbols reu_config.o exports (LIB_NISTCURVES_REU_BANK_COMB, _OFFSET_COMB_P256/_P384, _BANK_MUL) and the body sink's SINK_FLOOR_BANK, and check the images, both Lim-Lee tables and the multiply rows are disjoint and below the sink's banks. Red-green: rebuilding the comb archive with LIB_NISTCURVES_REU_OFFSET_COMB_P384=0xA000 (the P-384 table onto the cold home) links silently with the old asserts and fails this link with 'cold bank: images overlap the linked library's P-384 Lim-Lee table'. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/cold_bank.inc | 6 +++--- src/http.s | 1 + src/net/uci/cold_bank.s | 25 +++++++++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/cold_bank.inc b/src/cold_bank.inc index 02bfdc2d..41833dab 100644 --- a/src/cold_bank.inc +++ b/src/cold_bank.inc @@ -26,10 +26,10 @@ COLD_E_TS_SAVE = 3 ; trust_store_save (COLD_G_TRUST) ; table and $4000-$9F9F the P-384 one (parked, not deleted: libs/nistcurves ; API.md's REU map, "$9FA0-$FFFF safe for consumer use"). reu_layout.inc's ; $20100/$22100/$24100 overlay slots sit inside the P-256 table and are -; never used here. cold_bank.s asserts the images stay in -; [COLD_REU_FLOOR, bank 3) and below HTTP_REU_BODY_BASE. +; never used here. cold_bank.s asserts at link time that the images miss +; the LINKED library's tables (its exported bank/offset symbols) and stay +; below the HTTP body sink's banks. COLD_REU = $02A000 -COLD_REU_FLOOR = $029FA0 COLD_IMAGE_MAX = $1000 ; the cfg's COLD_IMAGE size: every group COLD_MARK = $5C ; each group's last byte diff --git a/src/http.s b/src/http.s index 013a0e14..65779823 100644 --- a/src/http.s +++ b/src/http.s @@ -35,6 +35,7 @@ .export http_sink_blit .export reu_probe_size, reu_top_bank, reu_size_ok .export http_sink_room, http_sink_full + .export SINK_FLOOR_BANK:abs .endif ; Every REU execute goes through src/reu_exec.s (#191, SPEC §8.2). diff --git a/src/net/uci/cold_bank.s b/src/net/uci/cold_bank.s index 64274b9f..48860242 100644 --- a/src/net/uci/cold_bank.s +++ b/src/net/uci/cold_bank.s @@ -94,11 +94,28 @@ LEN_TRUST = cold_marker_trust + 1 - COLD_RUN .assert LEN_TRUST <= CERT_BUF_SIZE, lderror, "cold bank: TRUST group larger than cert_buf" .endif ; The REU home (cold_bank.inc), bounded by COLD_IMAGE_MAX so these hold -; for any image that links. +; for any image that links. Checked against what the LINKED library says +; its tables are (the code-read values reu_config.o exports), not against +; a copy of the numbers: a library that moves its comb tables onto the +; cold home fails this link. +.import LIB_NISTCURVES_REU_BANK_COMB, LIB_NISTCURVES_REU_BANK_MUL +.import LIB_NISTCURVES_REU_OFFSET_COMB_P256, LIB_NISTCURVES_REU_OFFSET_COMB_P384 +.import SINK_FLOOR_BANK +COMB_P256_LO = LIB_NISTCURVES_REU_BANK_COMB * $10000 + LIB_NISTCURVES_REU_OFFSET_COMB_P256 +COMB_P384_LO = LIB_NISTCURVES_REU_BANK_COMB * $10000 + LIB_NISTCURVES_REU_OFFSET_COMB_P384 +COMB_P256_LEN = 256 * 64 ; 256 anchors x (X, Y), API.md "REU map" +COMB_P384_LEN = 256 * 96 +COLD_HI = COLD_REU + COLD_IMAGE_MAX .assert __COLD_IMAGE_SIZE__ = COLD_IMAGE_MAX, lderror, "cold bank: COLD_IMAGE_MAX is not the cfg's COLD_IMAGE size" -.assert COLD_REU >= COLD_REU_FLOOR, error, "cold bank: images overlap the Lim-Lee tables in REU bank 2" -.assert COLD_REU + COLD_IMAGE_MAX <= $030000, error, "cold bank: images leave REU bank 2" -.assert COLD_REU + COLD_IMAGE_MAX <= HTTP_REU_BODY_BASE, error, "cold bank: images overlap the HTTP body area" +.assert COLD_HI <= COMB_P256_LO .or COLD_REU >= COMB_P256_LO + COMB_P256_LEN, lderror, "cold bank: images overlap the linked library's P-256 Lim-Lee table" +.assert COLD_HI <= COMB_P384_LO .or COLD_REU >= COMB_P384_LO + COMB_P384_LEN, lderror, "cold bank: images overlap the linked library's P-384 Lim-Lee table" +.assert COLD_HI <= LIB_NISTCURVES_REU_BANK_MUL * $10000 .or COLD_REU >= (LIB_NISTCURVES_REU_BANK_MUL + 2) * $10000, lderror, "cold bank: images overlap the multiply-row banks" +; The HTTP body sink's region starts at bank SINK_FLOOR_BANK (src/http.s), +; so everything here, and the library's tables, must sit below it. +.assert COLD_HI <= SINK_FLOOR_BANK * $10000, lderror, "cold bank: images reach the HTTP body sink's banks" +.assert COMB_P384_LO + COMB_P384_LEN <= SINK_FLOOR_BANK * $10000 .and COMB_P256_LO + COMB_P256_LEN <= SINK_FLOOR_BANK * $10000, lderror, "the Lim-Lee tables reach the HTTP body sink's banks" +.assert LIB_NISTCURVES_REU_BANK_MUL + 2 <= SINK_FLOOR_BANK, lderror, "the multiply rows reach the HTTP body sink's banks" +.assert COLD_HI <= HTTP_REU_BODY_BASE, error, "cold bank: images overlap the HTTP body area" .segment "CODE" From cbd804e98d8085f74d28c47b4a9b6cf4fbe314d1 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Thu, 1 Oct 2026 14:36:30 -0500 Subject: [PATCH 05/10] cold bank: refuse until stashed; stash only a whole image (adv-l5 LOW 3) (a) cold_call never read cold_stashed. With no stash this LOAD, cold_g_sum is 0 and stale or planted REU bytes carrying the marker and an XOR of 0 ran: case_no_stash_refuses ran a planted trust_store_load before this commit ($0334=$42) and is refused (cold_err FETCH) after it. (b) cold_bank_init checked only the UI group's marker, so a LOAD that stopped short of the TRUST group was stashed and summed: case_truncated_group_is_not_stashed was red; every group's marker is now checked at its load address first. The re-run case now forges every marker, so the once-per-LOAD rule stays separately tested. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci/cold_bank.s | 30 +++++++++++++++++++++++++---- tools/test_cold_bank.py | 42 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/net/uci/cold_bank.s b/src/net/uci/cold_bank.s index 48860242..bde06163 100644 --- a/src/net/uci/cold_bank.s +++ b/src/net/uci/cold_bank.s @@ -17,7 +17,8 @@ ; in the same call, because every call runs a fresh copy. ; ; Boot (cold_bank_init) stashes the PRG's COLD_IMAGE into the REU at -; COLD_REU in one DMA and keeps each group's XOR. A tenant's public name is +; COLD_REU in one DMA and keeps each group's XOR, once per LOAD and only if +; every group's marker arrived; cold_call refuses until it has. A tenant's public name is ; a resident stub, `ldy #COLD_E_x / jmp cold_call`, so callers are ; unchanged. cold_call: ; 1. refuses unless no connection is open: net_tcp_state is CLOSED or @@ -134,6 +135,8 @@ cold_call: cmp #TLS_STATE_ERROR bne @busy ; a session owns cert_buf @free: + lda cold_stashed ; nothing stashed this LOAD: cold_g_sum is + beq @bad ; not a sum of anything, the REU unknown ldx cold_entry_grp,y stx cold_g lda cold_g_tail_lo,x @@ -192,14 +195,23 @@ cold_call: ; each group's XOR. Once per LOAD: cold_stashed is PRG data, so only a ; fresh LOAD reads 0 here. A re-run without one (RUN after 'Q') skips it ; and keeps the first stash, because $C000 is the TCP ring and has held -; server-chosen bytes since: stashing them would run them. The marker -; check only refuses a PRG whose image did not arrive. +; server-chosen bytes since: stashing them would run them. Every group's +; marker must be present at its load address, so a LOAD that stopped short +; of the image stashes nothing, and cold_call then refuses every call. cold_bank_init: lda cold_stashed bne @out - lda __COLD_TAIL_UI_LOAD__ + ldx #COLD_GROUPS-1 ; every group's marker must have arrived: +@arrived: ; a LOAD cut short is not stashed whole + lda cold_g_tl_lo,x + sta @tail+1 + lda cold_g_tl_hi,x + sta @tail+2 +@tail: lda $FFFF cmp #COLD_MARK bne @out + dex + bpl @arrived inc cold_stashed lda # Date: Mon, 5 Oct 2026 18:37:32 -0500 Subject: [PATCH 06/10] check_m3_client: a body the REU sink refuses is C=1 on uci-m3 too Two sizes: a 2,000 B body refused at its first blit must stop the loop mid-stream; a 100 B body refused at the final flush must not read as complete. Two mutants (stop removed; completion back to CLC), both killed. Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/check_m3_client.py | 28 ++++++++++++++++++++++++++++ tools/mutate_m3_client.py | 10 ++++++++++ 2 files changed, 38 insertions(+) diff --git a/tools/check_m3_client.py b/tools/check_m3_client.py index c73fd831..4cb6799b 100644 --- a/tools/check_m3_client.py +++ b/tools/check_m3_client.py @@ -951,6 +951,33 @@ def test_http_05_unframed_is_short(prg=None, labels=None): m.no_violations() +def test_http_sink_refusal_stops(prg=None, labels=None): + """HTTPS_BODY_TO_REU: a body the REU sink refuses (here: no REU size + established, so the region is empty and every blit is refused) is C=1. + A 2,000 B body is refused at its first 512 B blit, mid-stream: the loop + stops there instead of reading on to the framing's end. A 100 B body is + refused at the final flush, after the framing completed.""" + for size in (2000, 100): + m = _connected(Machine(prg, labels)) + m.poke("http_body_sink", 1) + m.poke("http_reu_body_base", 0x00) + m.poke("http_reu_body_base", 0x00, 1) + m.poke("http_reu_body_base", 0x03, 2) # bank 3: above the floor + body = bytes((i * 7) & 0x7F | 0x20 for i in range(size)) + resp = (b"HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n" % size) + body + events = [("data", resp[i:i + 600]) for i in range(0, len(resp), 600)] + carry, _, total = _http_fetch(m, events + [("end", CLOSED_BY_HOST)]) + _check(m.peek("http_sink_full") == 1, + "%d B: the sink never refused a blit" % size) + _check(carry is True, "%d B: a body the sink refused was reported " + "complete" % size) + if size > 512: + _check(total < size, "read on to the framing's end (%d B) after " + "the sink refused" % total) + m.call("net_tcp_close") + m.no_violations() + + def test_refusal_reaches_the_user(prg=None, labels=None): """The menu path: 'G' with the default target, the Open refused with 94. The user must see TLS HANDSHAKE FAILED and the WHOLE status line, and @@ -1370,6 +1397,7 @@ def test_held_session_message(prg=None, labels=None): test_long_write_is_split_at_892, test_http_content_length_end_to_end, test_http_05_unframed_is_short, + test_http_sink_refusal_stops, test_refusal_reaches_the_user, test_no_tls_firmware_reaches_the_user, test_http_unframed_stall_is_short, diff --git a/tools/mutate_m3_client.py b/tools/mutate_m3_client.py index ca3be161..f8c45961 100644 --- a/tools/mutate_m3_client.py +++ b/tools/mutate_m3_client.py @@ -118,6 +118,16 @@ ("05-unframed-trusted", "S 1.6 05: trust only framed data", HTTP, " lda m3_eof_code\n cmp #5\n bne @m3_framed", " jmp @m3_framed", ["test_http_05_unframed_is_short"]), + ("sink-refusal-ignored", "a body the REU sink refused stops, C=1", HTTP, + " lda http_sink_full ; the body outgrew its REU region: stop\n" + " bne @m3_dead ; now, C=1\n", "", + ["test_http_sink_refusal_stops"]), + ("sink-refusal-complete", "a body the REU sink refused is never C=0", HTTP, + "@m3_complete:\n jsr http_body_finish\n" + " lda http_sink_full ; C=1 iff the sink refused a write\n" + " cmp #1\n", + "@m3_complete:\n jsr http_body_finish\n clc\n", + ["test_http_sink_refusal_stops"]), ("refused-not-8d", "$8D UCI_ERR_OPEN_REFUSED on a named refusal", NET, " lda #UCI_ERR_OPEN_REFUSED ; named in m3_status (e.g. 94,...)", " lda #UCI_ERR_CONNECT_FAIL", ["test_refusal_line_is_kept_whole", From f5df8b8021100e2888c7e640564c8723be1037f3 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 20:45:32 -0500 Subject: [PATCH 07/10] sink: pin that each verdict flushes before it reads the refusal latch (adv-273 F1) A refusal can first happen in http_recv_{close,timeout}_verdict's own final flush: every full bounce fit, the last partial one crosses the region's end. Moving the http_sink_full check above php / jsr http_body_finish stayed green everywhere. Now pinned twice: - test_reu_body_sink case_verdict_flush (comb TS demo image): both verdicts, unframed close and a satisfied Content-Length. Red on the mutant (fd62f9f0: C=0 twice), green on 393360be. - check_m3_client: the unframed + 01 variant; mutate_m3_client gains sink-check-before-flush, killed. Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/check_m3_client.py | 16 ++++++++++++++++ tools/mutate_m3_client.py | 16 ++++++++++++++++ tools/test_reu_body_sink.py | 26 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/tools/check_m3_client.py b/tools/check_m3_client.py index 4cb6799b..b9b87e55 100644 --- a/tools/check_m3_client.py +++ b/tools/check_m3_client.py @@ -976,6 +976,22 @@ def test_http_sink_refusal_stops(prg=None, labels=None): "the sink refused" % total) m.call("net_tcp_close") m.no_violations() + # Unframed (Connection: close), clean 01 end: the refusal first happens + # in http_recv_close_verdict's own final flush (adv-273 F1). + m = _connected(Machine(prg, labels)) + m.poke("http_body_sink", 1) + m.poke("http_reu_body_base", 0x00) + m.poke("http_reu_body_base", 0x00, 1) + m.poke("http_reu_body_base", 0x03, 2) + body = bytes((i * 7) & 0x7F | 0x20 for i in range(100)) + resp = b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n" + body + carry, _, _ = _http_fetch(m, [("data", resp), ("end", CLOSED_BY_HOST)]) + _check(m.peek("http_sink_full") == 1, + "unframed: the verdict's final flush was not refused") + _check(carry is True, "unframed + 01: a body whose final flush the sink " + "refused was reported complete") + m.call("net_tcp_close") + m.no_violations() def test_refusal_reaches_the_user(prg=None, labels=None): diff --git a/tools/mutate_m3_client.py b/tools/mutate_m3_client.py index f8c45961..c03c494a 100644 --- a/tools/mutate_m3_client.py +++ b/tools/mutate_m3_client.py @@ -128,6 +128,22 @@ " cmp #1\n", "@m3_complete:\n jsr http_body_finish\n clc\n", ["test_http_sink_refusal_stops"]), + ("sink-check-before-flush", "the verdict reads the latch after its own flush", HTTP, + " php ; http_body_finish clobbers C\n" + " jsr http_body_finish ; idempotent — http_sink_flushed latch\n" + " plp ; C = 1 iff no clean end (kept: LDA/AND\n" + ".ifdef BACKEND_UCI ; below leave C alone, unlike CMP)\n" + " lda http_sink_full ; a body the sink refused is never complete\n" + " bne @to_short\n" + ".endif\n", + ".ifdef BACKEND_UCI\n" + " lda http_sink_full\n" + " bne @to_short\n" + ".endif\n" + " php ; http_body_finish clobbers C\n" + " jsr http_body_finish ; idempotent — http_sink_flushed latch\n" + " plp\n", + ["test_http_sink_refusal_stops"]), ("refused-not-8d", "$8D UCI_ERR_OPEN_REFUSED on a named refusal", NET, " lda #UCI_ERR_OPEN_REFUSED ; named in m3_status (e.g. 94,...)", " lda #UCI_ERR_CONNECT_FAIL", ["test_refusal_line_is_kept_whole", diff --git a/tools/test_reu_body_sink.py b/tools/test_reu_body_sink.py index 922815d9..cabddfd9 100644 --- a/tools/test_reu_body_sink.py +++ b/tools/test_reu_body_sink.py @@ -211,6 +211,31 @@ def case_verdict(env): check(not c, "control: C=1 for a complete Content-Length body") +def case_verdict_flush(env): + """A refusal that first happens in the verdict's OWN final flush: every + full bounce fit, the last partial one crosses the region's end. Both + verdicts must flush first and then read http_sink_full (adv-273 F1).""" + L = env.labels + for entry, framed in (("http_recv_close_verdict", False), + ("http_recv_timeout_verdict", True)): + b = Box(env, size=512 * KB) + check(b.body_begin(0x05F000) == 0x1000, f"{entry}: 4 KB region") + b.ram[L["http_body_sink"]] = 1 + b.put24("http_reu_cursor", 0x1000) # 8 bounces fill it exactly + tail = 100 # so any tail crosses the end + b.ram[L["http_resp_len"]] = tail + b.ram[L["http_resp_len"] + 1] = 0 + b.put24("http_body_total", 0x1000 + tail) + b.ram[L["http_parse_state"]] = 2 + b.ram[L["http_chunked"]] = 0 + b.ram[L["http_cl_valid"]] = int(framed) + if framed: # Content-Length satisfied + b.put24("http_content_length", 0x1000 + tail) + c = b.m.cpu.call(L[entry]) + check(b.r8("http_sink_full") == 1, f"{entry}: the final flush was not refused") + check(c, f"{entry}: C=0 for a body whose final flush the sink refused") + + def case_benign_big_body(env): """No attacker needed: the 751 KB Wikipedia demo body on a small REU. @@ -249,6 +274,7 @@ def case_benign_big_body(env): CASES = [case_probe, case_regions, case_bound, case_attack, case_verdict, + case_verdict_flush, case_benign_big_body] From a281ac05cf7d3df150524af73d30b74b67679a5e Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 20:45:44 -0500 Subject: [PATCH 08/10] uci-m3: a body the sink refused never enters the viewer (adv-273 F2) With HTTPS_BODY_TO_REU, m3_https_get.inc entered the viewer whatever http_sink_full said, so a refused body showed REU bytes it never wrote. It now prints BODY TOO BIG FOR THE REU and closes, as boot.s's TLS arm. check_m3_client gains VIEWER_TESTS, run on a BODY_TO_REU image (and named as not run otherwise): the menu path with a refused 100 B body. Red without the fix (b75fd5fb: do_https_get never returns, the viewer waits for keys); green with it (9e6ab506). The model attaches a response to the next Open (M3Device.on_write) so a menu fetch can carry a body. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci-m3/m3_https_get.inc | 7 ++++++ tools/check_m3_client.py | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/net/uci-m3/m3_https_get.inc b/src/net/uci-m3/m3_https_get.inc index 6e70c2e7..56631032 100644 --- a/src/net/uci-m3/m3_https_get.inc +++ b/src/net/uci-m3/m3_https_get.inc @@ -92,8 +92,15 @@ do_https_get: .ifdef VIEWER_LINKED lda http_body_sink beq @print_body + lda http_sink_full ; the body outgrew its REU region: the + bne @sink_full ; sink stopped it (as boot.s's arm) jsr viewer_enter jmp @verdict +@sink_full: + lda #sink_full_msg + jsr print_string + jmp @close @print_body: .endif jsr print_resp_body diff --git a/tools/check_m3_client.py b/tools/check_m3_client.py index b9b87e55..8ccc39df 100644 --- a/tools/check_m3_client.py +++ b/tools/check_m3_client.py @@ -144,6 +144,7 @@ def __init__(self, clock): self.open_delay = FAST self.read_delay = FAST self.write_result = None # None = ok, else (hdr, status) + self.on_write = None # events for the next Open's session self.never = set() # command bytes that never complete self.slow = {} # command byte -> delay in units self.forced_blocks = None # READ: override the reply blocks @@ -373,6 +374,8 @@ def _execute(self, cmd, now): return [], OK, d h = arg self.sessions[h] = {"rx": [], "claimed": False} + if self.on_write is not None: # the response, for a menu fetch + self.sessions[h]["on_write"] = self.on_write self.gone.discard(h) return [bytes([h, 4, 3, 1, 0x13, 0x1D, 0, 0])], OK, d if op == 0x10: # READ @@ -994,6 +997,31 @@ def test_http_sink_refusal_stops(prg=None, labels=None): m.no_violations() +def test_refused_body_skips_the_viewer(prg=None, labels=None): + """HTTPS_BODY_TO_REU builds only (VIEWER_TESTS): the menu path with a + body the sink refuses (no REU size, so an empty region) prints BODY TOO + BIG FOR THE REU and never enters the viewer, as boot.s's TLS arm does; + the viewer would show REU bytes the body never wrote (adv-273 F2).""" + m = Machine(prg, labels) + m.dev.open_result = ("ok", 5) + m.call("do_net_init") + body = bytes((i * 7) & 0x7F | 0x20 for i in range(100)) + resp = b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n" + body + m.dev.on_write = [("data", resp), ("end", CLOSED_BY_HOST)] + m.mem.screen.clear() + m.mem.keys.extend(b"\r\r") # RETURN, RETURN: the defaults + try: + m.call("do_https_get", budget=200_000_000) + except CPUError as exc: + raise AssertionError("do_https_get did not return (the viewer waits " + "for keys): %s\n%s" % (exc, m.screen_text())) + text = m.screen_text() + _check(m.peek("http_sink_full") == 1, "the sink never refused:\n" + text) + _check("BODY TOO BIG FOR THE REU" in text, "no BODY TOO BIG FOR THE REU " + "on screen:\n" + text) + m.no_violations() + + def test_refusal_reaches_the_user(prg=None, labels=None): """The menu path: 'G' with the default target, the Open refused with 94. The user must see TLS HANDSHAKE FAILED and the WHOLE status line, and @@ -1442,11 +1470,18 @@ def test_held_session_message(prg=None, labels=None): test_held_session_message, ) +# Need `make BACKEND=uci-m3 HTTPS_BODY_TO_REU=1` (viewer_enter linked); main +# runs them on such an image and says so when they are not run. +VIEWER_TESTS = ( + test_refused_body_skips_the_viewer, +) + def run(prg=None, labels=None, only=None, quiet=False): """Run TESTS (or `only`, names) on one image. Returns {name: error|None}.""" results = {} - for fn in TESTS: + tests = TESTS + (VIEWER_TESTS if "viewer_enter" in _labels(labels) else ()) + for fn in tests: if only and fn.__name__ not in only: continue try: @@ -1470,6 +1505,10 @@ def main(): return cannot_run(str(exc), executed=0, total=len(TESTS), certifies=CERTIFIES, opt_out_env=OPT_OUT_ENV) results = run() + if "viewer_enter" not in _labels(): + print("NOT RUN (%d): %s -- they need make BACKEND=uci-m3 " + "HTTPS_BODY_TO_REU=1" % (len(VIEWER_TESTS), ", ".join( + f.__name__ for f in VIEWER_TESTS))) failed = [n for n, e in results.items() if e] print("\n%d/%d passed, %d assertions" % (len(results) - len(failed), len(results), ASSERTIONS_RUN)) From d076a9a653904446d86885af72469d599a356e70 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 20:45:44 -0500 Subject: [PATCH 09/10] uci-m3: probe the REU size at boot only for HTTPS_BODY_TO_REU (adv-273 F3) reu_probe_size ran on every uci-m3 boot; with the REU disabled it spins ~3 s at 1 MHz, and only the REU body sink uses its result. A plain uci-m3 boot does no REU DMA again, as on master; every 6510-TLS UCI image and uci-m3 + HTTPS_BODY_TO_REU still probe. check_m3_client test_boot_reu_dma_only_for_the_sink runs `start` to the menu and counts REU register writes: 0 on plain uci-m3 (master b124ea9f 0, 3bf57344 27, now 0a826f7e 0), > 0 on a BODY_TO_REU image. Mutant boot-probe-ungated killed. (It also caught a .not/.or precedence slip in the first draft of the gate, which dropped the probe from BODY_TO_REU.) Co-Authored-By: Claude Opus 5.5 (1M context) --- src/boot.s | 6 ++++-- tools/check_m3_client.py | 29 +++++++++++++++++++++++++++++ tools/mutate_m3_client.py | 4 ++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/boot.s b/src/boot.s index 746a1901..9b22c53e 100644 --- a/src/boot.s +++ b/src/boot.s @@ -25,7 +25,9 @@ .if .defined(HTTPS_PIN_SPKI) .and (.not .defined(BACKEND_UCI_M3)) .import cert_pin_banner .endif -.ifdef BACKEND_UCI +; uci-m3 fills the sink only under HTTPS_BODY_TO_REU: otherwise no REU DMA +.if .defined(BACKEND_UCI) .and ((.not .defined(BACKEND_UCI_M3)) .or .defined(HTTPS_BODY_TO_REU)) +SINK_PROBE = 1 .import reu_probe_size .endif .ifdef COLD_BANK @@ -281,7 +283,7 @@ start: inc @zbss_store+2 dex bne @zbss_page -.ifdef BACKEND_UCI +.ifdef SINK_PROBE ; The REU's size bounds the HTTP body sink's region (src/http.s). ; Probed before anything else writes the REU: it writes offset 0 ; of banks 0, 1, 2, 4 ... 128, which boot rewrites afterwards. diff --git a/tools/check_m3_client.py b/tools/check_m3_client.py index 8ccc39df..294e529a 100644 --- a/tools/check_m3_client.py +++ b/tools/check_m3_client.py @@ -1022,6 +1022,34 @@ def test_refused_body_skips_the_viewer(prg=None, labels=None): m.no_violations() +def test_boot_reu_dma_only_for_the_sink(prg=None, labels=None): + """Boot (`start`, to the menu) touches the REU registers ($DF00-$DF0A) + only on an HTTPS_BODY_TO_REU image, whose sink needs the REU's size + (reu_probe_size). A plain uci-m3 image does no REU DMA, as before the + sink bound: with the REU disabled the probe spins ~3 s at 1 MHz (adv-273 + F3).""" + m = Machine(prg, labels) + hits = [] + write = m.mem.write + + def spy(addr, value): + if 0xDF00 <= (addr & 0xFFFF) <= 0xDF0A: + hits.append(addr & 0xFFFF) + write(addr, value) + m.mem.write = spy + try: + m.call("start", budget=20_000_000) + except CPUError: + pass # the menu waits for a key + _check("I=INIT" in m.screen_text(), "boot never reached the menu:\n" + + m.screen_text()) + if "viewer_enter" in m.L: + _check(hits, "an HTTPS_BODY_TO_REU image did not probe the REU") + else: + _check(not hits, "a plain uci-m3 boot wrote the REU registers %d " + "times" % len(hits)) + + def test_refusal_reaches_the_user(prg=None, labels=None): """The menu path: 'G' with the default target, the Open refused with 94. The user must see TLS HANDSHAKE FAILED and the WHOLE status line, and @@ -1468,6 +1496,7 @@ def test_held_session_message(prg=None, labels=None): test_only_version_alerts_read_as_no_tls13, test_data_more_wedge_keeps_89, test_held_session_message, + test_boot_reu_dma_only_for_the_sink, ) # Need `make BACKEND=uci-m3 HTTPS_BODY_TO_REU=1` (viewer_enter linked); main diff --git a/tools/mutate_m3_client.py b/tools/mutate_m3_client.py index c03c494a..68d30c30 100644 --- a/tools/mutate_m3_client.py +++ b/tools/mutate_m3_client.py @@ -32,6 +32,7 @@ CMD = "src/net/uci-m3/m3_cmd.s" INC = "src/net/uci-m3/m3.inc" HTTP = "src/http.s" +BOOT = "src/boot.s" UI = "src/net/uci-m3/m3_https_get.inc" # (name, rule, file, old, new, tests that must go red) @@ -144,6 +145,9 @@ " jsr http_body_finish ; idempotent — http_sink_flushed latch\n" " plp\n", ["test_http_sink_refusal_stops"]), + ("boot-probe-ungated", "plain uci-m3 boot does no REU DMA", BOOT, + ".if .defined(BACKEND_UCI) .and ((.not .defined(BACKEND_UCI_M3)) .or .defined(HTTPS_BODY_TO_REU))", + ".ifdef BACKEND_UCI", ["test_boot_reu_dma_only_for_the_sink"]), ("refused-not-8d", "$8D UCI_ERR_OPEN_REFUSED on a named refusal", NET, " lda #UCI_ERR_OPEN_REFUSED ; named in m3_status (e.g. 94,...)", " lda #UCI_ERR_CONNECT_FAIL", ["test_refusal_line_is_kept_whole", From 56e29fd1772515d2ded05abbdfad9f82f7a79837 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Mon, 5 Oct 2026 21:33:41 -0500 Subject: [PATCH 10/10] check_m3_client: tell a BODY_TO_REU image by its flags, not by the viewer (adv-273 nits) viewer_enter is also linked by KEEP_VIEWER=1 and VIEWER_TEST_HELPERS=1, which do not stream the body to the REU: on a KEEP_VIEWER image the boot test demanded a probe and the viewer test ran, 49/51 (adv-273). The image's kind now comes off -D HTTPS_BODY_TO_REU in the flags.stamp beside its labels (KEEP_VIEWER: 50/50, the viewer test named as not run). mutate_m3_client refuses a BODY_TO_REU image (boot-probe-ungated is equivalent there) and copies the stamp into its scratch build. Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/check_m3_client.py | 22 ++++++++++++++++++---- tools/mutate_m3_client.py | 7 +++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tools/check_m3_client.py b/tools/check_m3_client.py index 294e529a..cbf75f31 100644 --- a/tools/check_m3_client.py +++ b/tools/check_m3_client.py @@ -506,6 +506,20 @@ def _check(cond, msg): raise AssertionError(msg) +def _body_to_reu(labels=None): + """True for an HTTPS_BODY_TO_REU image, read off the CA65FLAGS in the + flags.stamp beside its labels. Not `viewer_enter in labels`: KEEP_VIEWER + and VIEWER_TEST_HELPERS link the viewer without the sink.""" + stamp = Path(labels or LABELS).parent / "flags.stamp" + if not stamp.is_file(): + raise Unavailable("%s is missing: cannot tell whether the image " + "streams the body to the REU" % stamp) + for line in stamp.read_text().splitlines(): + if line.startswith("CA65FLAGS="): + return "-D HTTPS_BODY_TO_REU" in line + raise Unavailable("%s has no CA65FLAGS line" % stamp) + + def _labels(path=None): path = Path(path or LABELS) if not path.is_file(): @@ -1043,7 +1057,7 @@ def spy(addr, value): pass # the menu waits for a key _check("I=INIT" in m.screen_text(), "boot never reached the menu:\n" + m.screen_text()) - if "viewer_enter" in m.L: + if _body_to_reu(labels): _check(hits, "an HTTPS_BODY_TO_REU image did not probe the REU") else: _check(not hits, "a plain uci-m3 boot wrote the REU registers %d " @@ -1499,7 +1513,7 @@ def test_held_session_message(prg=None, labels=None): test_boot_reu_dma_only_for_the_sink, ) -# Need `make BACKEND=uci-m3 HTTPS_BODY_TO_REU=1` (viewer_enter linked); main +# Need `make BACKEND=uci-m3 HTTPS_BODY_TO_REU=1` (flags.stamp says so); main # runs them on such an image and says so when they are not run. VIEWER_TESTS = ( test_refused_body_skips_the_viewer, @@ -1509,7 +1523,7 @@ def test_held_session_message(prg=None, labels=None): def run(prg=None, labels=None, only=None, quiet=False): """Run TESTS (or `only`, names) on one image. Returns {name: error|None}.""" results = {} - tests = TESTS + (VIEWER_TESTS if "viewer_enter" in _labels(labels) else ()) + tests = TESTS + (VIEWER_TESTS if _body_to_reu(labels) else ()) for fn in tests: if only and fn.__name__ not in only: continue @@ -1534,7 +1548,7 @@ def main(): return cannot_run(str(exc), executed=0, total=len(TESTS), certifies=CERTIFIES, opt_out_env=OPT_OUT_ENV) results = run() - if "viewer_enter" not in _labels(): + if not _body_to_reu(): print("NOT RUN (%d): %s -- they need make BACKEND=uci-m3 " "HTTPS_BODY_TO_REU=1" % (len(VIEWER_TESTS), ", ".join( f.__name__ for f in VIEWER_TESTS))) diff --git a/tools/mutate_m3_client.py b/tools/mutate_m3_client.py index 68d30c30..dccbd1e7 100644 --- a/tools/mutate_m3_client.py +++ b/tools/mutate_m3_client.py @@ -290,6 +290,11 @@ def _stamp(): raise SystemExit("build/ is not a BACKEND=uci-m3 build (flags.stamp " "says %r): run `make BACKEND=uci-m3` first" % vals.get("BACKEND")) + if "-D HTTPS_BODY_TO_REU" in vals.get("CA65FLAGS", ""): + # boot-probe-ungated is equivalent there (that image probes anyway), + # and the rules here are the plain image's. + raise SystemExit("build/ is an HTTPS_BODY_TO_REU image: the mutants " + "are written against plain `make BACKEND=uci-m3`") return vals @@ -310,6 +315,8 @@ def build(workdir: Path, mutate=None) -> Path: shutil.copytree(REPO / "cfg", workdir / "cfg") (workdir / "build").mkdir() shutil.copy(REPO / "build" / "https_host.inc", workdir / "build") + shutil.copy(REPO / "build" / "flags.stamp", workdir / "build") # the + # same flags; check_m3_client reads the image's kind off it if mutate: path, old, new = mutate f = workdir / path