From: Christopher Faulet Date: Thu, 19 Nov 2026 08:00:00 +0000 (+0100) Subject: BUG/MEDIUM: stconn: Only forward kip hint on streams committed to forward data X-Git-Url: https://git.kaiwu.me/http/ngx_http_access_module.html?a=commitdiff_plain;h=a32e3c8bb84e19a3b3b3e1530e3ad8b8f2d7e3f9;p=haproxy.git BUG/MEDIUM: stconn: Only forward kip hint on streams committed to forward data The hint of a sedesc is the payload length the producer endpoint estimates it will emit. It is forwarded to the hint of the opposite sedesc by sc_ep_fwd_kip(), so the consumer endpoint can use it as an optimization, for instance the H1 mux to announce a chunk size covering the whole payload at once. But could be set by the producer on the data it demultiplexes, before the app layer has a chance to see it. In particular, when data filters are registered on the channel, flt_http_payload() resets after each pass, because the filters may alter or drop any part of the payload. But in the meantime, an I/O callback may already have moved the hint to the opposite side: the consumer then uses a stale value, and the data it emits may not match what this value announces. This is how the H1 mux could be led to announce a chunk smaller than the data sent with it, and to emit the remaining bytes without any chunk framing, making an HTTP/1 server process a part of the request payload as another request (request smuggling). The root of the issue is that / are hints on data which were not evaluated by the app layer yet, but they may be consumed as if they were. is a minimal guarantee on the output payload: when it is set, at least this size will be forwarded. It is only safe to feed it with once the app layer committed to forward the data as-is. This patch introduced a safe version of sc_ep_fwd_kip(), sc_ep_fwd_kip_when_committed(), which only forwards the hint when the producer channel is configured to forward data indefinitely (to_forward == CHN_INFINITE_FORWARD). This is the only case where the app layer committed to forward everything, without any alteration: http_wait_request_body() and http_wait_for_response_body() set the infinite forwarding only when no data filter is registered on the channel. Otherwise stays null and the consumer endpoints fall back to their conservative behavior (the H1 mux announces the size of the data it emits). When the app layer is not committed yet, is reset instead of being kept for later. Until the infinite forwarding is reached, data may be forwarded and emitted by the consumer with a null , while still accounts for them, because it is only decremented when forwarded. Moving it later would then set higher than the remaining payload, and the H1 mux would announce a chunk bigger than the data left to send. Resetting it keeps conservative: lower than the remaining payload, never higher. Thanks to Hyunho Seo for the issue analysis. This patch must be backported as far as 3.3. --- diff --git a/include/haproxy/stconn.h b/include/haproxy/stconn.h index 50db0c0bd..2034d4679 100644 --- a/include/haproxy/stconn.h +++ b/include/haproxy/stconn.h @@ -44,6 +44,7 @@ struct stconn *sc_new(struct sedesc *sedesc); void sedesc_free(struct sedesc *sedesc); void se_shutdown(struct sedesc *sedesc, enum se_shut_mode mode); +void sc_ep_fwd_kip_when_committed(struct stconn *sc_in, struct stconn *sc_out); struct stconn *sc_new_from_endp(struct sedesc *sedesc, struct session *sess, struct buffer *input); struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags); diff --git a/src/applet.c b/src/applet.c index 94494d17c..e85103701 100644 --- a/src/applet.c +++ b/src/applet.c @@ -844,7 +844,7 @@ struct task *task_run_applet(struct task *t, void *context, unsigned int state) channel_check_idletimer(ic); - sc_ep_fwd_kip(sco, sc); + sc_ep_fwd_kip_when_committed(sco, sc); input = applet_output_data(app); output = co_data(oc); diff --git a/src/stconn.c b/src/stconn.c index 327218edd..67d15a0c9 100644 --- a/src/stconn.c +++ b/src/stconn.c @@ -39,6 +39,31 @@ struct task *hld_strm_task(struct task *t, void *context, unsigned int state) static int sc_conn_recv(struct stconn *sc); static int sc_conn_send(struct stconn *sc); +/* Forwards the hint from the producer to the consumer side, but only + * when the app layer committed to forward the data as-is. and + * are hints on the data the producer will emit, possibly before the app layer + * has seen them, so they must not be trusted for anything else than an + * optimization: here, announcing a chunk size that covers the whole payload + * at once. When data filters are registered on the channel, they may alter or + * drop any part of the payload after was accounted for, so could + * become a stale or wrong value. It is only safe to forward once the + * app layer committed to forward everything (infinite forwarding). + * + * Before that, data may be forwarded and emitted by the consumer while + * still accounts for them. Forwarding later would then lead to a + * value higher than the remaining payload. So, when the app layer has not + * committed yet, is reset instead. It is conservative: may be + * lower than the remaining payload, but never higher. + */ +void sc_ep_fwd_kip_when_committed(struct stconn *sc_in, struct stconn *sc_out) +{ + if (sc_ic(sc_in)->to_forward != CHN_INFINITE_FORWARD) { + sc_in->sedesc->kip = 0; + return; + } + sc_ep_fwd_kip(sc_in, sc_out); +} + /* Initializes an endpoint */ void sedesc_init(struct sedesc *sedesc) { @@ -1157,7 +1182,7 @@ int sc_conn_recv(struct stconn *sc) flags |= CO_RFL_BUF_FLUSH; goto abort_fastfwd; } - sc_ep_fwd_kip(sc, sc_opposite(sc)); + sc_ep_fwd_kip_when_committed(sc, sc_opposite(sc)); ret = CALL_MUX_WITH_RET(conn->mux, fastfwd(sc, ic->to_forward, flags)); if (ret < 0) goto abort_fastfwd; @@ -1457,7 +1482,7 @@ int sc_conn_send(struct stconn *sc) if (!conn->mux) return 0; - sc_ep_fwd_kip(sco, sc); + sc_ep_fwd_kip_when_committed(sco, sc); if (sc_ep_have_ff_data(sc)) { unsigned int send_flag = 0; @@ -1851,7 +1876,7 @@ int sc_applet_recv(struct stconn *sc) flags |= CO_RFL_BUF_FLUSH; goto abort_fastfwd; } - sc_ep_fwd_kip(sc, sc_opposite(sc)); + sc_ep_fwd_kip_when_committed(sc, sc_opposite(sc)); ret = appctx_fastfwd(sc, ic->to_forward, flags); if (ret < 0) goto abort_fastfwd; @@ -2081,7 +2106,7 @@ int sc_applet_send(struct stconn *sc) if (se_fl_test(sc->sedesc, SE_FL_ORPHAN)) return 0; - sc_ep_fwd_kip(sco, sc); + sc_ep_fwd_kip_when_committed(sco, sc); /* TODO: Splicing is not supported, so it is not possible to have FF data stuck into the I/O buf */ BUG_ON(sc_ep_have_ff_data(sc));