Files
STM32F4-Base/MDK-ARM/ftp_abort_probe.py
wandering1 4f2cc8bc90 fix(net): B3 只清本 Socket 中断;B10 对账认领孤儿监听修永久拒连
B3(net_listen_locked / net_relisten_locked 两处):
原先对全部 8 个 Socket 做 GET_INT_STATUS_SN(读即清)并丢弃式读全局中断
状态。依据三条:§5.15 明文全局寄存器整体读即清且无按位清除手段;net_poll
只在全局位置位时才读该 Socket 的 SINT(位图门控);SINT 粘滞到被读
(Trap 14 实证 close/open 都不清)→ 读走即永久销毁其它在用 Socket 的
CONNECT/DISCONNECT/TIM_OUT/SENBUF_FREE。git 考古定性为多连接时代调试遗留
(e862009bc → 319d00e3 → db64e6acf),不属于任何已登记 Trap 的修复;官方
SDK 21 个例程一律"位图门控 + 单 Socket 读 + 立刻转存",open/listen 前后
不清任何中断,无一例全量读 8 个 SINT。现收窄为只清本 Socket 的 SINT,
全局交 net_poll 集中读清,与 AGENTS 模块约定一致。

B10(孤儿监听 → 端口永久拒连):
断开时 handle_disconnect_event 释放分支置 CLOSED 并注释"等待应用重新监
听",而 ui_net_sink 只在 net_recv 返 0(EOF)时才清 g_client,EOF 已被该
事件分支消费 → net 层等应用、应用等 EOF、对账只裁 ESTABLISHED/LISTENING
直接跳过、芯片不会自己回 LISTEN,四方互等。实测孤儿态 state=0 /
chip=0x05/0x00 持续 12s+,:7003 300ms×10 为 0/10 且 +30s 仍拒连。
net_reconcile_tick 新增类型④认领孤儿,复用 net_relisten_try(自带"已在
重试队列不重复登记"保护,与 D10 队列、Trap 23 沉降隔离兼容)。门槛中
auto_relisten 是决定性一条:缺它则出向连接(板级 Phase 7 TC701~703 绑本
地口)会被误判孤儿并被 relisten 成监听器。

工具:ftp_abort_probe.py 仍用 anonymous 登录,N-19 后 530 拒 → Trap 23 两
条腿假 0/5(round1 PASV failed,且半认证会话占死 FTP 唯一会话)。改
admin/admin 后 RST 5/5、FIN 5/5。

回归::7003 300ms×10 由 0/10 永久死 → 10/10(+20s 存活);:21 0ms×50 与
100ms×50 均 50/50;:8888 0ms×20 为 20/20;ftp_regression 11/11×2;Trap 23
RST 5/5、FIN 5/5;@test PASS(600ms、回读无差异);@build 0/0,
Code=197084。

残余:B10 生产者未根治,释放分支仍把状态停在 CLOSED 等应用重建,类型④
是兜底;根治涉及 Trap 25 closed-with-data 契约,单列评估。
2026-10-12 10:35:55 +08:00

138 lines
5.8 KiB
Python

# -*- coding: utf-8 -*-
# Trustworthy abort/robustness probe for the production FTP path (no firmware probes).
#
# Scenario: client starts a STOR upload, sends part of the data, then ABORTS by
# resetting the data connection (SO_LINGER 0 -> RST), which is the common real-world
# pattern (user cancels an upload, client crashes, link drops). Then it checks:
# 1. the control channel still answers (NOOP on a fresh connection),
# 2. a FOLLOW-UP normal upload+download still passes the SHA-256 check,
# 3. the device's relisten actually happened (observable via a successful follow-up).
#
# Usage: ftp_abort_probe.py [--ip ..] [--port 21] [--rounds 10] [--send 8192] [--gap 2]
# [--graceful] [--check-abort]
# Output: TS <epoch> / ABORT <n> sent=<b> ctrl=<reply> alive=<yes|no> followup=<OK|FAIL:x>
# abort5xx=<OK|FAIL|-> (only with --check-abort: RST expects 4xx/5xx on the
# aborted STOR = no false 226 (Trap 23 hardening); graceful FIN expects 2xx
# = protocol boundary, still reported as success)
# SUMMARY rounds=<n> pass=<n> fail=<n>
# Exit 0 all rounds pass, 1 otherwise.
import argparse, hashlib, io, random, re, socket, struct, sys, time
from ftplib import FTP, all_errors
def log(m):
sys.stdout.write(m + "\n"); sys.stdout.flush()
def rd(sock):
try:
return sock.recv(4096).decode("latin1").strip()
except Exception:
return ""
def raw_login(ip, port, timeout=10.0):
s = socket.create_connection((ip, port), timeout=timeout)
rd(s) # 220 banner
# N-19: anonymous login is rejected (530) -> use the factory default account
s.sendall(b"USER admin\r\n"); rd(s)
s.sendall(b"PASS admin\r\n"); rd(s)
s.sendall(b"TYPE I\r\n"); rd(s)
return s
def pasv_data_socket(ctl):
ctl.sendall(b"PASV\r\n")
r = rd(ctl)
m = re.search(r"\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)", r)
if not m:
raise RuntimeError("PASV failed: " + r)
host = ".".join(m.group(i) for i in (1, 2, 3, 4))
port = (int(m.group(5)) << 8) | int(m.group(6))
return socket.create_connection((host, port), timeout=10.0)
def followup_ok(ip, port, size=4096):
"""normal upload + download + sha256 -> True/False"""
data = bytes(random.getrandbits(8) for _ in range(size))
want = hashlib.sha256(data).hexdigest()
try:
f = FTP(); f.connect(ip, port, timeout=15.0); f.login("admin", "admin")
f.storbinary("STOR /ab_ok.bin", io.BytesIO(data))
buf = bytearray(); f.retrbinary("RETR /ab_ok.bin", buf.extend)
try:
f.delete("/ab_ok.bin")
except all_errors:
pass
f.quit()
return hashlib.sha256(bytes(buf)).hexdigest() == want
except all_errors:
return False
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--ip", default="192.168.1.100")
ap.add_argument("--port", type=int, default=21)
ap.add_argument("--rounds", type=int, default=10)
ap.add_argument("--send", type=int, default=8192, help="bytes sent before the RST")
ap.add_argument("--gap", type=float, default=2.0)
ap.add_argument("--graceful", action="store_true",
help="close the data connection with FIN instead of RST")
ap.add_argument("--check-abort", action="store_true",
help="also assert the aborted STOR's control reply: RST -> 4xx/5xx "
"(no false 226, Trap 23 hardening); graceful FIN -> 2xx (boundary)")
a = ap.parse_args()
npass = nfail = 0
for n in range(1, a.rounds + 1):
log("TS %d" % int(time.time()))
ctl_reply = "NA"
alive = "no"
try:
ctl = raw_login(a.ip, a.port)
ds = pasv_data_socket(ctl)
ctl.sendall(("STOR /ab%04d.bin\r\n" % n).encode())
ctl_reply = rd(ctl) # 150
chunk = bytes(random.getrandbits(8) for _ in range(a.send))
ds.sendall(chunk)
if a.graceful:
ds.shutdown(socket.SHUT_RDWR) # graceful FIN (client cancel)
else:
# abort: RST the data connection (linger 0)
ds.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
ds.close()
time.sleep(0.5)
ctl_reply = rd(ctl) # 426/550/226
try:
ctl.sendall(b"QUIT\r\n"); rd(ctl)
except Exception:
pass
try:
ctl.close()
except Exception:
pass
except Exception as e:
ctl_reply = "EXC:%s" % type(e).__name__
# control-channel liveness on a fresh connection
try:
c2 = raw_login(a.ip, a.port)
c2.sendall(b"NOOP\r\n")
alive = "yes" if rd(c2).startswith("2") else "no"
c2.sendall(b"QUIT\r\n"); c2.close()
except Exception:
alive = "no"
fu = followup_ok(a.ip, a.port)
abort_ok = True
if a.check_abort:
code = ctl_reply[0] if (ctl_reply and ctl_reply[0].isdigit()) else "?"
# RST: device must report 4xx/5xx on the aborted STOR (450); graceful
# FIN still gets 226 (STOR has no length declaration - protocol boundary)
abort_ok = (code == "2") if a.graceful else (code in ("4", "5"))
ok = (alive == "yes" and fu and abort_ok)
npass += 1 if ok else 0
nfail += 0 if ok else 1
log("ABORT %d sent=%d ctrl=%s alive=%s followup=%s abort5xx=%s" %
(n, a.send, ctl_reply.replace("\r\n", "|")[:40], alive,
"OK" if fu else "FAIL", "OK" if abort_ok else "FAIL" if a.check_abort else "-"))
if a.gap > 0 and n < a.rounds:
time.sleep(a.gap)
log("SUMMARY rounds=%d pass=%d fail=%d" % (a.rounds, npass, nfail))
return 0 if nfail == 0 else 1
sys.exit(main())