]> git.kaiwu.me - haproxy.git/commitdiff
REGTESTS: quic: NEW_CONNECTION_ID must not reference a retired CID
authorFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 16:45:37 +0000 (17:45 +0100)
committerFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 17:00:24 +0000 (18:00 +0100)
A NEW_CONNECTION_ID frame stored pointers to the CID and the stateless
reset token of its quic_connection_id. RETIRE_CONNECTION_ID frees that
object at once, while a frame built for it may still be queued, in flight,
or duplicated later for a retransmission: it was then built from freed
memory.

Retiring sequence numbers 1 and 2 in the same packet is enough to reach
it: the second RETIRE frees the CID 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 same connection, so two frames carry the same CID,
which the client detects. Without it (ASan, DEBUG_DONT_SHARE_POOLS), freed
heap content is sent and the sanitizer reports a use-after-free.

The test drives a real handshake through the QUIC client in
reg-tests/quic/qclient, then retires both sequence numbers in one packet,
and checks two things: no duplicate CID is advertised for two different
sequence numbers, and the connection is closed with a transport error of
type PROTOCOL_VIOLATION, as RFC 9000 19.16 requires for a sequence number
that was never sent. A crash instead of the CONNECTION_CLOSE frame leaves
the test waiting, which also fails it.

It needs the Python client, so it carries '#REGTEST_EXTRA=qclient' and
only runs when requested.

Covers 827354038 and a50bb45ba.

reg-tests/quic/retired_cid_ncid.py [new file with mode: 0755]
reg-tests/quic/retired_cid_ncid.vtc [new file with mode: 0644]

diff --git a/reg-tests/quic/retired_cid_ncid.py b/reg-tests/quic/retired_cid_ncid.py
new file mode 100755 (executable)
index 0000000..878e533
--- /dev/null
@@ -0,0 +1,86 @@
+#!/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()
diff --git a/reg-tests/quic/retired_cid_ncid.vtc b/reg-tests/quic/retired_cid_ncid.vtc
new file mode 100644 (file)
index 0000000..6a81d9e
--- /dev/null
@@ -0,0 +1,71 @@
+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"
+}