From: Vadim Zhestikov Date: Tue, 10 Nov 2026 18:52:32 +0000 (-0800) Subject: Validated buffer sizes in send chain callbacks X-Git-Url: https://git.kaiwu.me/http/ngx_http_fastcgi_module.html?a=commitdiff_plain;h=a364c8366b5b7b297bbd432d4c9fba8a862f3c4b;p=nginx.git Validated buffer sizes in send chain callbacks The negative size buffer checks added in f4c70589c (1.15.7) to the write filters, ngx_output_chain() and the chain writer did not cover the send chain callbacks. A buffer malformed after the filters resulted in a bogus size being passed to a system call or ngx_memcpy(). Now the callbacks report such buffers and reject the chain as the filters do: an alert with the buffer dump, a debug point, and NGX_CHAIN_ERROR. ngx_ssl_send_chain() and ngx_wsasend_chain() also check that a buffer is in memory, as ngx_output_chain_to_iovec() does. --- diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c index 8ba0122d4..27635855c 100644 --- a/src/event/ngx_event_openssl.c +++ b/src/event/ngx_event_openssl.c @@ -3065,6 +3065,44 @@ ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) continue; } + if (!ngx_buf_in_memory(in->buf)) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "bad buf in SSL output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + + if (in->buf->last < in->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "negative size buf in SSL output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos); if (n == NGX_ERROR) { @@ -3134,6 +3172,44 @@ ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) break; } + if (!ngx_buf_in_memory(in->buf)) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "bad buf in SSL output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + + if (in->buf->last < in->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "negative size buf in SSL output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + size = in->buf->last - in->buf->pos; if (size > buf->end - buf->last) { diff --git a/src/event/quic/ngx_event_quic_frames.c b/src/event/quic/ngx_event_quic_frames.c index 3b23100a0..3afef6ccc 100644 --- a/src/event/quic/ngx_event_quic_frames.c +++ b/src/event/quic/ngx_event_quic_frames.c @@ -603,6 +603,25 @@ ngx_quic_write_buffer(ngx_connection_t *c, ngx_quic_buffer_t *qb, continue; } + if (in->buf->last < in->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "negative size buf in quic write buffer " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + if (p == b->last || limit == 0) { break; } diff --git a/src/os/unix/ngx_solaris_sendfilev_chain.c b/src/os/unix/ngx_solaris_sendfilev_chain.c index 39bcafaaf..4b1de17ab 100644 --- a/src/os/unix/ngx_solaris_sendfilev_chain.c +++ b/src/os/unix/ngx_solaris_sendfilev_chain.c @@ -97,6 +97,25 @@ ngx_solaris_sendfilev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) if (ngx_buf_in_memory_only(cl->buf)) { fd = SFV_FD_SELF; + if (cl->buf->last < cl->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "negative size buf in output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + cl->buf->temporary, + cl->buf->recycled, + cl->buf->in_file, + cl->buf->start, + cl->buf->pos, + cl->buf->last, + cl->buf->file, + cl->buf->file_pos, + cl->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + size = cl->buf->last - cl->buf->pos; if (send + size > limit) { diff --git a/src/os/unix/ngx_udp_sendmsg_chain.c b/src/os/unix/ngx_udp_sendmsg_chain.c index fa917b569..a63cc95a3 100644 --- a/src/os/unix/ngx_udp_sendmsg_chain.c +++ b/src/os/unix/ngx_udp_sendmsg_chain.c @@ -159,6 +159,25 @@ ngx_udp_output_chain_to_iovec(ngx_iovec_t *vec, ngx_chain_t *in, ngx_log_t *log) return NGX_CHAIN_ERROR; } + if (in->buf->last < in->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, log, 0, + "negative size buf in output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + size = in->buf->last - in->buf->pos; if (prev == in->buf->pos) { diff --git a/src/os/unix/ngx_writev_chain.c b/src/os/unix/ngx_writev_chain.c index e38a3aae0..9e1663b2c 100644 --- a/src/os/unix/ngx_writev_chain.c +++ b/src/os/unix/ngx_writev_chain.c @@ -147,6 +147,25 @@ ngx_output_chain_to_iovec(ngx_iovec_t *vec, ngx_chain_t *in, size_t limit, return NGX_CHAIN_ERROR; } + if (in->buf->last < in->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, log, 0, + "negative size buf in output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + in->buf->temporary, + in->buf->recycled, + in->buf->in_file, + in->buf->start, + in->buf->pos, + in->buf->last, + in->buf->file, + in->buf->file_pos, + in->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + size = in->buf->last - in->buf->pos; if (size > limit - total) { diff --git a/src/os/win32/ngx_wsasend_chain.c b/src/os/win32/ngx_wsasend_chain.c index 66e06ccf0..512cd92c0 100644 --- a/src/os/win32/ngx_wsasend_chain.c +++ b/src/os/win32/ngx_wsasend_chain.c @@ -65,6 +65,44 @@ ngx_wsasend_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) continue; } + if (!ngx_buf_in_memory(cl->buf)) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "bad buf in output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + cl->buf->temporary, + cl->buf->recycled, + cl->buf->in_file, + cl->buf->start, + cl->buf->pos, + cl->buf->last, + cl->buf->file, + cl->buf->file_pos, + cl->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + + if (cl->buf->last < cl->buf->pos) { + ngx_log_error(NGX_LOG_ALERT, c->log, 0, + "negative size buf in output chain " + "t:%d r:%d f:%d %p %p-%p %p %O-%O", + cl->buf->temporary, + cl->buf->recycled, + cl->buf->in_file, + cl->buf->start, + cl->buf->pos, + cl->buf->last, + cl->buf->file, + cl->buf->file_pos, + cl->buf->file_last); + + ngx_debug_point(); + + return NGX_CHAIN_ERROR; + } + size = cl->buf->last - cl->buf->pos; if (send + size > limit) {