[nginx] SSL: added buffer checks to ngx_ssl_send_chain().

Maxim Dounin mdounin at mdounin.ru
Tue Nov 24 15:44:04 UTC 2026


details:   http://freenginx.org/hg/nginx/rev/35b4f12054c8
branches:  
changeset: 9611:35b4f12054c8
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Tue Nov 24 06:19:24 2026 +0300
description:
SSL: added buffer checks to ngx_ssl_send_chain().

Similarly to buffer checks present in ngx_http_write_filter() and
ngx_output_chain(), these checks are expected to catch invalid buffers
if passed to the SSL layer.  Notably, this is expected to improve the
safety of HTTP/2, which performs complex buffer manipulations after the
write filter checks.

diffstat:

 src/event/ngx_event_openssl.c |  72 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 72 insertions(+), 0 deletions(-)

diffs (89 lines):

diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -3191,6 +3191,42 @@ ngx_ssl_send_chain(ngx_connection_t *c, 
                 continue;
             }
 
+            if (ngx_buf_size(in->buf) == 0) {
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "zero size buf in ssl send 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 (ngx_buf_size(in->buf) < 0) {
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "negative size buf in ssl send 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) {
@@ -3255,6 +3291,42 @@ ngx_ssl_send_chain(ngx_connection_t *c, 
                 continue;
             }
 
+            if (ngx_buf_size(in->buf) == 0) {
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "zero size buf in ssl send 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 (ngx_buf_size(in->buf) < 0) {
+                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
+                              "negative size buf in ssl send 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->in_file && c->ssl->sendfile) {
                 flush = 1;
                 break;


More information about the nginx-devel mailing list