]> git.kaiwu.me - nginx.git/commitdiff
Validated buffer sizes in send chain callbacks
authorVadim Zhestikov <v.zhestikov@f5.com>
Tue, 10 Nov 2026 18:52:32 +0000 (10:52 -0800)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Wed, 25 Nov 2026 13:31:32 +0000 (17:31 +0400)
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.

src/event/ngx_event_openssl.c
src/event/quic/ngx_event_quic_frames.c
src/os/unix/ngx_solaris_sendfilev_chain.c
src/os/unix/ngx_udp_sendmsg_chain.c
src/os/unix/ngx_writev_chain.c
src/os/win32/ngx_wsasend_chain.c

index 8ba0122d4f0f3bc91190fe5289c0b4734ffceea5..27635855cbcbb8a4baf4f82dedb6626c838b4385 100644 (file)
@@ -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) {
index 3b23100a04ece6abb41b5bcb8eca4f491030c9d5..3afef6ccc7fb5f6815a6100624c96072f698f168 100644 (file)
@@ -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;
             }
index 39bcafaaf232e63b27f15f12dab3d029a269c81c..4b1de17abc3e34e874c0f875d84736e829796a14 100644 (file)
@@ -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) {
index fa917b5690b622e7809c8393bf1ebea2c5e7cfef..a63cc95a38b0254f4a36d69d3eab7d1546a57a2d 100644 (file)
@@ -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) {
index e38a3aae00ddaaa88473c3382f034dd279c7f117..9e1663b2cfe05c4e0336fd8d09f01561ead0b313 100644 (file)
@@ -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) {
index 66e06ccf02ae6193baa34d441cd27dbbd43cb96b..512cd92c0f2cd11cdc9c25f5570ea9bf65945e84 100644 (file)
@@ -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) {