HTTP/2: account for queued frames when validating WINDOW_UPDATE

When a stream is terminated, its unsent payload length is refunded back
to the connection flow control.  Previously, this could increase the
flow control beyond NGX_HTTP_V2_MAX_WINDOW, if it was increased by the
client after the stream frames were queued and before the stream was
terminated.  This eventually resulted in broken buffer size control in
ngx_http_v2_send_chain() followed by memory corruption and a possible
segmentation fault.

The fix is to account for the queued frames when validating a client
WINDOW_UPDATE.  From the client's point of view, the queued frames
still belong to the flow control window.

Reported by Xint.io.
This commit is contained in:
Roman Arutyunyan 2026-10-29 21:37:05 +04:00 • committed by Roman Arutyunyan
parent e39708edc6
commit e27f5fa84f
3 changed files with 6 additions and 2 deletions

View file

@ -2561,13 +2561,13 @@ ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,
return ngx_http_v2_state_complete(h2c, pos, end);
}
if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window) {
if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window - h2c->send_queued) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated connection flow control: "
"received WINDOW_UPDATE frame "
"with window increment %uz "
"not allowed for window %uz",
window, h2c->send_window);
window, h2c->send_window + h2c->send_queued);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
}

View file

@ -135,6 +135,7 @@ struct ngx_http_v2_connection_s {
ngx_uint_t refused_streams;
size_t send_window;
size_t send_queued;
size_t recv_window;
size_t init_window;

View file

@ -1242,6 +1242,7 @@ ngx_http_v2_send_chain(ngx_connection_t *fc, ngx_chain_t *in, off_t limit)
ngx_http_v2_queue_frame(fn, frame);
h2c->send_window -= frame_size;
h2c->send_queued += frame_size;
stream->send_window -= frame_size;
stream->queued++;
@ -1634,6 +1635,7 @@ done:
stream->request->header_size += NGX_HTTP_V2_FRAME_HEADER_SIZE;
h2c->payload_bytes += frame->length;
h2c->send_queued -= frame->length;
ngx_http_v2_handle_frame(stream, frame);
@ -1770,6 +1772,7 @@ ngx_http_v2_filter_cleanup(void *data)
}
h2c->send_window += window;
h2c->send_queued -= window;
}