From: Christopher Faulet Date: Mon, 30 Nov 2026 13:36:36 +0000 (+0100) Subject: MINOR: htx: never create, leave nor accept an empty HTX block X-Git-Url: https://git.kaiwu.me/http/%22../doc/static/%7B@url%7D?a=commitdiff_plain;h=58ce50527ca672418de17356f7f173d00725d07e;p=haproxy.git MINOR: htx: never create, leave nor accept an empty HTX block An HTX block of any type must never have a size of 0. A zero-sized DATA block especially has a special meaning on the wire: for a chunked message, mux-h1 emits a zero-sized chunk, i.e. the last-chunk, prematurely ending the message for the peer, and any data following in the HTX message would then be interpreted as a new message (request smuggling towards a server, response splitting towards a client). HTX producers are not supposed to leave empty blocks and no way was found to make a client or server create one, but the HTX API itself could create or leave them, and the consequence would be dramatic. So the invariant is now enforced by the HTX API: * __htx_add_blk() is the single entry point for block creation, it now refuses to create a block of size 0. htx_add_data_atonce() and htx_add_last_data() thus also refuse empty data. * htx_replace_blk_value() refuses a replacement that would empty the block, returning NULL as if there was not enough space. The caller must then remove the block itself with htx_remove_blk(). * htx_change_blk_value_len() and htx_set_blk_value_len() now BUG_ON if they would empty a block, except for a header or a trailer whose value may legitimately be empty because the name is never empty. * htx_cut_data_blk() now BUG_ONs if the cut removes the whole block. Special blocks (EOH, EOT, NOOP) are created with a size of 1, and no other block is ever created empty, so no legitimate caller is affected. The two callers that could produce an empty block were fixed in the two previous commits. --- diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index 758bb5f04..017c4d5df 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -397,6 +397,10 @@ static inline enum htx_blk_type htx_get_first_type(const struct htx *htx) /* Changes the size of the value. It is the caller responsibility to change the * value itself, make sure there is enough space and update allocated * value. This function updates the HTX message accordingly. + * + * It must never be used to empty a block. Only headers/trailers can have a + * value of size 0. Otherwise, Such block must be removed with htx_remove_blk() + * instead. */ static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen) { @@ -404,6 +408,8 @@ static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk uint32_t oldlen, sz; int32_t delta; + BUG_ON(!newlen && (type != HTX_BLK_HDR && type != HTX_BLK_TLR)); + sz = htx_get_blksz(blk); switch (type) { case HTX_BLK_HDR: @@ -433,11 +439,17 @@ static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk * value itself, make sure there is enough space and update allocated * value. Unlike the function htx_change_blk_value_len(), this one does not * update the HTX message. So it should be used with caution. + * + * It must never be used to empty a block. Only headers/trailers can have a + * value of size 0. Otherwise, Such block must be removed with htx_remove_blk() + * instead. */ static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen) { enum htx_blk_type type = htx_get_blk_type(blk); + BUG_ON(!vlen && (type != HTX_BLK_HDR && type != HTX_BLK_TLR)); + switch (type) { case HTX_BLK_HDR: case HTX_BLK_TLR: @@ -710,10 +722,14 @@ static inline struct htx_blk *htx_add_all_trailers(struct htx *htx, const struct * updated. This is used to mark that part of some data were transferred * from a DATA block without removing this DATA block. No sanity check is * performed, the caller is responsible for doing this exclusively on DATA - * blocks, and never removing more than the block's size. + * blocks, and never removing more than the block's size. must also never + * be equal to the block's size, a fully consumed block must be removed with + * htx_remove_blk() instead. */ static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n) { + BUG_ON(n >= htx_get_blksz(blk)); + if (blk->addr == htx->end_addr) htx->end_addr += n; blk->addr += n; diff --git a/src/htx.c b/src/htx.c index 68cd003ae..10bd40498 100644 --- a/src/htx.c +++ b/src/htx.c @@ -157,6 +157,11 @@ static void htx_defrag_blks(struct htx *htx) * block is returned and the HTX message is updated. Space for this new block is * reserved in the HTX message. But it is the caller responsibility to set right * info in the block to reflect the stored data. + * + * must never be 0: a block of any type must never be empty. Special + * blocks (EOH, EOT, NOOP) are created with a size of 1 for this reason. The + * single entry point for block creation, __htx_add_blk(), enforces this + * invariant. */ static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz) { @@ -360,12 +365,16 @@ static int htx_prepare_blk_expansion(struct htx *htx, struct htx_blk *blk, int32 * ended. It must only be used by the HTX API itself, to temporarily add a block * after the one carrying the EOM flag, before moving it at the right place. Any * other caller must use htx_add_blk() instead. + * + * A block of any type must never be empty: must never be 0. */ struct htx_blk *__htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz) { struct htx_blk *blk; BUG_ON(blksz >= 256 << 20); + if (unlikely(!blksz)) + return NULL; if (unlikely(type < HTX_BLK_EOH && htx_hdrs_too_big(htx, blksz))) return NULL; blk = htx_reserve_nxblk(htx, blksz); @@ -593,6 +602,9 @@ struct htx_ret htx_drain(struct htx *htx, uint32_t count) * ended. It must only be used by the HTX API itself, to temporarily add a block * after the one carrying the EOM flag, before moving it at the right place. Any * other caller must use htx_add_data_type_atonce() instead. + * + * Nothing is added when is empty: an empty DATA or RAW_DATA block must + * never be created (see __htx_add_blk()). NULL is returned in this case. */ struct htx_blk *__htx_add_data_type_atonce(struct htx *htx, struct ist data, enum htx_blk_type type) { @@ -603,6 +615,9 @@ struct htx_blk *__htx_add_data_type_atonce(struct htx *htx, struct ist data, enu BUG_ON(!htx_is_data_type(type)); + if (!data.len) + return NULL; + if (htx->head == -1) goto add_new_block; @@ -709,6 +724,11 @@ struct htx_blk *htx_add_data_atonce(struct htx *htx, struct ist data) * larger than the old one. This function works for any kind of block with * attached data. It returns the new block on success, otherwise it returns * NULL. + * + * A block of any type must never be emptied: NULL is returned if the + * replacement removes all the data of the block. The caller must remove the + * block with htx_remove_blk(), taking care of the EOM flag, if the removed + * block was carrying it. */ struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, const struct ist old, const struct ist new) @@ -723,6 +743,9 @@ struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk, v = htx_get_blk_value(htx, blk); delta = new.len - old.len; + if (unlikely((int32_t)htx_get_blksz(blk) + delta == 0)) + return NULL; + /* The value length of a header or a trailer is encoded on 20 bits. It * must not overflow on the other fields of the block descriptor. */