From: Christopher Faulet Date: Wed, 2 Dec 2026 15:11:39 +0000 (+0100) Subject: BUG/MEDIUM: mux-h1: don't swap buffers for a chunk that cannot be framed X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=0c1fcfbf1498e8be8ba3bc85bfedf9a277cd4fc3;p=haproxy.git BUG/MEDIUM: mux-h1: don't swap buffers for a chunk that cannot be framed When a chunked message is sent and a new chunk is started in the zero-copy path of h1_make_data(), the chunk size is taken from the hint of the sedesc when it is set. Since the commit e582abe8a ("BUG/MEDIUM: mux-h1: never forward more data than announced in a chunk"), an error is reported if the DATA block is bigger than the announced chunk size. But this check, like the other ones on the chunk size, was performed after the buffers were swapped and the chunk size was prepended. So the output buffer already contained the chunk size line followed by more data than announced, and it was sent anyway. The response was cut and the client waited for the end of the chunk until the client timeout. However, is only a minimal guarantee. It may be lower than the DATA block, for instance when the end of a chunk and the beginning of the next one are merged in the same block, after some data were forwarded mid-chunk, like when the cache stops storing a response exceeding the maximum object size. It is not an error. The copy path properly handles this case by splitting the block. So now, the conditions to perform the zero-copy are evaluated in a dedicated function, h1_zero_copy_data_allowed(), before swapping the buffers. For chunked messages, it is only performed if the whole block fits in the current chunk or in the new one, and if the end of the message matches the end of the chunk. Otherwise, the copy path is used. It splits the block or reports the error before anything is emitted. The checks on the chunk size performed after the buffers swap are useless now and are removed. Messages with a content-length are not changed: the check on the announced length is still performed after the buffers swap, but an error there is a real protocol error. The commit above was introduced in 3.5-dev14 and was marked to be backported as far as 3.3. This patch must be backported with it, so to 3.5, and as far as 3.3 where the commit above was backported. --- diff --git a/src/mux_h1.c b/src/mux_h1.c index 6abb10dd2..d90f8271a 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -3107,6 +3107,59 @@ static size_t h1_make_eoh(struct h1s *h1s, struct h1m *h1m, struct htx *htx, siz goto end; } +/* Returns 1 if bytes of the HTX message can be sent by swapping + * the caller's buffer with the mux's output buffer in h1_make_data(), 0 + * otherwise. It is only possible if the output buffer is empty and if the HTX + * message contains exactly one DATA block of bytes. The whole block is + * then emitted at once, it cannot be split. So, for a chunked message, it must + * fit in the chunk being sent or in the new one, whose size is announced from + * the hint when set. is a minimal guarantee, it may be lower than + * the block size. In this case, or when the end of message is reached before + * the end of the announced chunk, the copy path must be used instead: it + * splits the block or reports the error before anything is emitted. Here, + * there is no way back once the buffers were swapped. + */ +static inline int h1_zero_copy_data_allowed(const struct h1s *h1s, const struct h1m *h1m, + const struct htx *htx, const struct buffer *buf, + size_t count) +{ + const struct h1c *h1c = h1s->h1c; + const struct htx_blk *blk; + uint64_t chksz; + + if ((!(h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_REQ)) || + ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))) + return 0; + + if (b_data(&h1c->obuf) || b_size(&h1c->obuf) != b_size(buf)) + return 0; + + /* the HTX message must contain exactly one DATA block of bytes */ + if (htx_nbblks(htx) != 1) + return 0; + blk = htx_get_head_blk(htx); + if (htx_get_blk_type(blk) != HTX_BLK_DATA || htx_get_blksz(blk) != count) + return 0; + + if (!(h1m->flags & H1_MF_CHNK)) + return 1; + + if (h1m->state != H1_MSG_CHUNK_CRLF && h1m->state != H1_MSG_CHUNK_SIZE) { + /* the end of the current chunk only */ + return (count == h1m->curr_len); + } + + /* A new chunk is started */ + if (h1m->curr_len) + return 0; + chksz = (h1s->sd->kop ? h1s->sd->kop : count); + if (count > chksz) + return 0; + if ((blk->flags & HTX_BLK_FL_EOM) && count != chksz) + return 0; + return 1; +} + /* Try to send the message payload from the HTX message for the stream * . In this case, we are not in TUNNEL mode. It returns the number of * bytes consumed or zero if nothing was done or if an error occurred. No more @@ -3135,14 +3188,7 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, * end-to-end. This is the situation that happens all the time with * large files. */ - if (((!(h1m->flags & H1_MF_RESP) && !(h1s->flags & H1S_F_BODYLESS_REQ)) || - ((h1m->flags & H1_MF_RESP) && !(h1s->flags & H1S_F_BODYLESS_RESP))) && - !b_data(&h1c->obuf) && - (!(h1m->flags & H1_MF_CHNK) || ((h1m->flags & H1_MF_CHNK) && (!h1m->curr_len || count == h1m->curr_len))) && - htx_nbblks(htx) == 1 && - htx_get_blk_type(blk) == HTX_BLK_DATA && - htx_get_blk_value(htx, blk).len == count && - b_size(&h1c->obuf) == b_size(buf)) { + if (h1_zero_copy_data_allowed(h1s, h1m, htx, buf, count)) { void *old_area; int eom = (blk->flags & HTX_BLK_FL_EOM); @@ -3182,12 +3228,6 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, /* If is a new chunk, prepend the chunk size */ if (h1m->state == H1_MSG_CHUNK_CRLF || h1m->state == H1_MSG_CHUNK_SIZE) { - if (h1m->curr_len) { - COUNT("chunk bigger than announced (0-copy)"); - TRACE_ERROR("chunk bigger than announced", - H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); - goto error; - } h1m->curr_len = (h1s->sd->kop ? h1s->sd->kop : count); h1s_consume_kop(h1s, h1m->curr_len); @@ -3201,12 +3241,7 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, h1m->state = H1_MSG_DATA; } - if (count > h1m->curr_len) { - COUNT("chunk bigger than announced (0-copy)"); - TRACE_ERROR("chunk bigger than announced", - H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); - goto error; - } + /* h1_zero_copy_data_allowed() ensures the block fits in the chunk */ h1m->curr_len -= count; /* It is the end of the chunk, append the CRLF */ @@ -3217,12 +3252,6 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, /* It is the end of the message, add the last chunk with the extra CRLF */ if (eom) { - if (h1m->curr_len) { - COUNT("chunk smaller than announced (0-copy)"); - TRACE_ERROR("chunk smaller than announced", - H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); - goto error; - } /* Emit the last chunk too at the buffer's end */ b_putblk(&h1c->obuf, "0\r\n\r\n", 5); h1m->state = H1_MSG_DONE;