--- /dev/null
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+"""Trigger the NEW_CONNECTION_ID-after-RETIRE bug from haproxy.
+
+A NEW_CONNECTION_ID frame used to store pointers to the CID and the
+stateless reset token of its quic_connection_id. A RETIRE_CONNECTION_ID
+for that sequence number frees the object immediately, while the frame may
+still be queued or be duplicated later for a retransmission: it was then
+built from freed memory.
+
+Retiring sequence numbers 1 and 2 in the same packet is enough: the second
+RETIRE frees the quic_connection_id the first NEW_CONNECTION_ID frame still
+points to. With the pool cache, the freed object is handed back to the next
+CID of the connection, so two frames carry the same CID. Without the cache
+(ASan, DEBUG_DONT_SHARE_POOLS), the freed heap content is sent instead.
+
+The script reports "RESULT OK" on success, or "RESULT <reason>" otherwise.
+"""
+import argparse
+import os
+import sys
+
+sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
+ "qclient"))
+
+from qclient import QClient, varint
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--addr", required=True)
+ ap.add_argument("--port", type=int, required=True)
+ args = ap.parse_args()
+
+ c = QClient((args.addr, args.port), timeout=1.0)
+ print("READY", flush=True)
+
+ if not c.handshake(wait=2.0):
+ print("RESULT handshake failed events=%r closed=%r"
+ % (c.events, c.closed), flush=True)
+ return
+
+ # Let haproxy advertise its CID, then stop as soon as one is received.
+ c.recv_all(2.0, until=lambda: any(e[0] == "ncid" for e in c.events))
+ cids = [e[1] for e in c.events if e[0] == "ncid"]
+ c.events.clear()
+
+ # RETIRE seq 1 then 2 in the same packet: the second RETIRE frees the
+ # quic_connection_id the NEW_CONNECTION_ID frame built for the first one
+ # still points to. seq 2 was never sent, hence PROTOCOL_VIOLATION.
+ c.send_1rtt(c.ack_frame(3) + b"\x19" + varint(1) + b"\x19" + varint(2))
+ c.recv_all(2.0, until=lambda: c.closed is not None)
+ cids += [e[1] for e in c.events if e[0] == "ncid"]
+
+ # Bug: the frame built after the first RETIRE references the CID freed
+ # by the second one, so the same CID ends up advertised under two
+ # different sequence numbers. The same frame retransmitted for the same
+ # sequence number is normal and must not be reported.
+ # ncid events are (seq, retire_prior_to, cid, stateless_reset_token).
+ by_seq = {}
+ for seq, _rpt, cid, _tok in cids:
+ by_seq.setdefault(seq, set()).add(cid)
+
+ seen = {}
+ for seq, seq_cids in sorted(by_seq.items()):
+ for cid in seq_cids:
+ if cid in seen:
+ print("RESULT duplicate CID %s in seq %d and %d"
+ % (cid.hex(), seen[cid], seq), flush=True)
+ return
+ seen[cid] = seq
+
+ # A CONNECTION_CLOSE frame is only received if haproxy survived the
+ # RETIRE storm: a crash would leave nothing but silence.
+ if not c.closed or c.closed[0] != 10:
+ print("RESULT expected PROTOCOL_VIOLATION after RETIRE seq 1 and 2, "
+ "got %r" % (c.closed,), flush=True)
+ return
+
+ print("RESULT OK (alive, PROTOCOL_VIOLATION, %d CID(s) seen)"
+ % len(cids), flush=True)
+
+
+if __name__ == "__main__":
+ main()
--- /dev/null
+varnishtest "QUIC: NEW_CONNECTION_ID must not reference a retired CID"
+
+#REGTEST_TYPE=bug
+#REGTEST_EXTRA=qclient
+
+feature ignore_unknown_macro
+feature cmd "python3 --version"
+feature cmd "python3 -c 'import cryptography'"
+
+# QUIC is not supported with USE_QUIC_OPENSSL_COMPAT nor with wolfSSL.
+feature cmd "$HAPROXY_PROGRAM -cc 'feature(QUIC) && !feature(QUIC_OPENSSL_COMPAT) && !feature(OPENSSL_WOLFSSL)'"
+
+# A NEW_CONNECTION_ID frame used to store pointers to the CID and the
+# stateless reset token of its quic_connection_id. A RETIRE_CONNECTION_ID
+# frees that object at once, while a NEW_CONNECTION_ID frame built for it
+# may still be queued, in flight, or duplicated later for a retransmission:
+# the frame was then built from freed memory.
+#
+# A client triggers it by retiring sequence numbers 1 and 2 in the same
+# packet: the second RETIRE frees the quic_connection_id allocated for the
+# first one, whose NEW_CONNECTION_ID frame is not sent yet. In a default
+# build the pool cache hands the object back to the next CID of the same
+# connection, so two frames carry the same CID, which the client detects
+# here. Without the pool cache (ASan, DEBUG_DONT_SHARE_POOLS), freed heap
+# memory is sent instead and the sanitizer reports a use-after-free.
+
+haproxy h1 -conf {
+ global
+ .if feature(THREAD)
+ thread-groups 1
+ nbthread 1
+ .endif
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ frontend fe
+ bind "quic+fd@${fe}" ssl crt ${testdir}/certs/common.pem alpn h3
+ http-request return status 200 content-type text/plain string "ok"
+} -start
+
+process p1 "python3 ${testdir}/retired_cid_ncid.py --addr ${h1_fe_addr} --port ${h1_fe_port}" -start
+process p1 -expect-text 0 0 "READY"
+
+# The script retires seq 1 and 2 in a single packet, which used to make
+# haproxy build a NEW_CONNECTION_ID frame from the quic_connection_id freed
+# by the second RETIRE. It must survive (a crash would leave only silence),
+# and close the connection with PROTOCOL_VIOLATION since seq 2 was never
+# sent (RFC 9000 19.16).
+process p1 -expect-text 0 0 "RESULT"
+
+# Surface the driver's own message when it did not succeed, instead of
+# leaving only a bare exit status behind.
+shell {
+ if ! grep -q "RESULT OK" "${p1_out}"; then
+ echo "--- p1 stdout ---"; cat "${p1_out}"
+ echo "--- p1 stderr ---"; cat "${p1_err}"
+ exit 1
+ fi
+}
+
+process p1 -wait
+
+# haproxy handled the RETIRE storm and is still alive and serving.
+haproxy h1 -cli {
+ send "show info"
+ expect ~ "Name: HAProxy"
+}