On a backend connection, quic_rx_pkt_parse() copied the server's Source
Connection ID into qc->dcid and set QUIC_FL_CONN_SCID_RECEIVED as soon as
the first Initial header was parsed. Header protection removal and AEAD
decryption happen later. A packet which fails either one is dropped, but
the flag stayed set, so a later Initial which does decrypt could not
replace that CID.

RFC 9000 7.2 says the client switches its Destination Connection ID for
the first received Initial or Retry, and only a valid Initial counts.
Packets sent afterwards, including the ACK of that valid Initial, kept
the CID from the discarded packet. The server cannot route them, and the
initial_source_connection_id transport parameter no longer matches
qc->dcid, which kills the connection. A normal retransmission uses the
same CID, so the usual handshake is unaffected. It shows up when the
first datagram's SCID differs from the one in the first Initial that
decrypts, for instance after corruption of that field or an injected
datagram on the connection socket.

Copy the SCID only once the Initial has been decrypted and its frames
have been parsed, in qc_treat_rx_pkts(). Retry packets still update
qc->dcid after their integrity check and still do not set the flag, so
the first accepted Initial may replace the Retry CID as before.

This should be backported up to 3.3, where it was introduced by 2d076178c
("MINOR: quic-be: Store asap the DCID").

diff --git a/include/haproxy/quic_conn-t.h b/include/haproxy/quic_conn-t.h

index 3b84b2db1..8570b42b1 100644
--- a/include/haproxy/quic_conn-t.h
+++ b/include/haproxy/quic_conn-t.h
@@ -464,7 +464,7 @@ struct quic_conn_closed {
 #define QUIC_FL_CONN_HPKTNS_DCD                  (1U << 16) /* Handshake
packet number space discarded  */
 #define QUIC_FL_CONN_PEER_VALIDATED_ADDR         (1U << 17) /* Peer
address is considered as validated for this connection. */
 #define QUIC_FL_CONN_NO_TOKEN_RCVD               (1U << 18) /* Client dit
not send any token */
-#define QUIC_FL_CONN_SCID_RECEIVED               (1U << 19) /* (client
only: first Initial received. */
+#define QUIC_FL_CONN_SCID_RECEIVED               (1U << 19) /* (client
only: first Initial accepted. */
 #define QUIC_FL_CONN_XPRT_CLOSED                 (1U << 20) /* close
callback of xprt layer already called */
 /* gap here */
 #define QUIC_FL_CONN_TO_KILL                     (1U << 24) /* Unusable
connection, to be killed */
diff --git a/src/quic_rx.c b/src/quic_rx.c
index 83c3fec0f..1bdb98fb7 100644
--- a/src/quic_rx.c
+++ b/src/quic_rx.c
@@ -1468,6 +1468,14 @@ int qc_treat_rx_pkts(struct quic_conn *qc)
                                else {
                                        struct quic_arng ar = { .first =
pkt->pn, .last = pkt->pn };

+                                       if (pkt->type ==
QUIC_PACKET_TYPE_INITIAL && qc_is_back(qc) &&
+                                           !(qc->flags &
QUIC_FL_CONN_SCID_RECEIVED)) {
+                                               /* Adopt the peer CID only
after accepting its Initial. */
+                                               memcpy(qc->dcid.data,
pkt->scid.data, pkt->scid.len);
+                                               qc->dcid.len =
pkt->scid.len;
+                                               qc->flags |=
QUIC_FL_CONN_SCID_RECEIVED;
+                                       }
+
                                        /* RFC 9000 8.1. Address Validation
during Connection Establishment
                                         *
                                         * Connection establishment
implicitly provides address validation for
@@ -2271,12 +2279,6 @@ static int quic_rx_pkt_parse(struct quic_conn *qc,
struct quic_rx_packet *pkt,
                                 * ack-eliciting Initial packets to at
least the smallest allowed maximum
                                 * datagram size of 1200 bytes.
                                 */
-                               if (!(qc->flags &
QUIC_FL_CONN_SCID_RECEIVED)) {
-                                       qc->flags |=
QUIC_FL_CONN_SCID_RECEIVED;
-                                       memcpy(qc->dcid.data,
pkt->scid.data, pkt->scid.len);
-                                       qc->dcid.len = pkt->scid.len;
-                               }
-
                                if (qc_is_back(qc)) {
                                        if (qc->flags &
QUIC_FL_CONN_CLOSING) {
                                                TRACE_PROTO("Packet
dropped",

Revisions were tested locally on both the main development branch and my
updated one.
This was found during LLM-audit, and it was manually tested.

Thanks,
Dean

Reply via email to