Skip to content

Commit 2fffd85

Browse files
committed
BUG/MEDIUM: quic: prevent EMSGSIZE with GSO for larger bufsize
A UDP datagram cannot be greater than 65535 bytes, as UDP length header field is encoded on 2 bytes. As such, sendmsg() will reject a bigger input with error EMSGSIZE. By default, this does not cause any issue as QUIC datagrams are limited to 1.252 bytes and sent individually. However, with GSO support, value bigger than 1.252 bytes are specified on sendmsg(). If using a bufsize equal to or greater than 65535, syscall could reject the input buffer with EMSGSIZE. As this value is not expected, the connection is immediately closed by haproxy and the transfer is interrupted. This bug can easily reproduced by requesting a large object on loopback interface and using a bufsize of 65535 bytes. In fact, the limit is slightly less than 65535, as extra room is also needed for IP + UDP headers. Fix this by reducing the count of datagrams encoded in a single GSO invokation via qc_prep_pkts(). Previously, it was set to 64 as specified by man 7 udp. However, with 1252 datagrams, this is still too many. Reduce it to a value of 52. Input to sendmsg will thus be restricted to at most 65.104 bytes if last datagram is full. If there is still data available for encoding in qc_prep_pkts(), they will be written in a separate batch of datagrams. qc_send_ppkts() will then loop over the whole QUIC Tx buffer and call sendmsg() for each series of at most 52 datagrams. This does not need to be backported.
1 parent 3cee8d7 commit 2fffd85

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

include/haproxy/quic_tx-t.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
#define QUIC_DGRAM_HEADLEN (sizeof(uint16_t) + sizeof(void *))
66
#define QUIC_MAX_CC_BUFSIZE (2 * (QUIC_MIN_CC_PKTSIZE + QUIC_DGRAM_HEADLEN))
77

8+
/* Sendmsg input buffer cannot be bigger than 65535 bytes. This comes from UDP
9+
* header which uses a 2-bytes length field. QUIC datagrams are limited to 1252
10+
* bytes for now so this does not cause any issue for serialized emission.
11+
*
12+
* However when using GSO large buffer can be transferred. By default, no more
13+
* than 64 datagrams can be emitted via a single GSO call (man 7 udp). This is
14+
* still too much with 1252 bytes datagram. Use a 52 datagrams max value, which
15+
* ensures sendmsg input will be limited to 65104 bytes.
16+
*/
17+
#define QUIC_MAX_GSO_DGRAMS 52
18+
819
#include <import/eb64tree.h>
920
#include <haproxy/list-t.h>
1021

src/quic_tx.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
570570
int total = 0;
571571
struct quic_enc_level *qel, *tmp_qel;
572572
int dgram_cnt = 0;
573+
/* Restrict GSO emission to comply with sendmsg limitation. See QUIC_MAX_GSO_DGRAMS for more details. */
573574
uchar gso_dgram_cnt = 0;
574575

575576
TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
@@ -763,19 +764,15 @@ static int qc_prep_pkts(struct quic_conn *qc, struct buffer *buf,
763764
!(HA_ATOMIC_LOAD(&qc->li->flags) & LI_F_UDP_GSO_NOTSUPP) &&
764765
dglen == qc->path->mtu &&
765766
(char *)end < b_wrap(buf) &&
766-
gso_dgram_cnt < 64) {
767+
++gso_dgram_cnt < QUIC_MAX_GSO_DGRAMS) {
768+
767769
/* A datagram covering the full MTU has been
768770
* built, use GSO to built next entry. Do not
769771
* reserve extra space for datagram header.
770772
*/
771773
prv_pkt = cur_pkt;
772774
dglen = 0;
773775

774-
/* man 7 udp UDP_SEGMENT
775-
* The segment size must be chosen such that at
776-
* most 64 datagrams are sent in a single call
777-
*/
778-
++gso_dgram_cnt;
779776
}
780777
else {
781778
/* Finalize current datagram if not all frames sent. */

0 commit comments

Comments
 (0)