[nginx] Mail: blocked client write events during various operati...

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


details:   http://freenginx.org/hg/nginx/rev/ef3ed1c4bcd2
branches:  stable-1.30
changeset: 9629:ef3ed1c4bcd2
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Tue Nov 24 06:19:51 2026 +0300
description:
Mail: blocked client write events during various operations.

Previously, only read events were blocked during various operations,
notably during auth_http requests, connecting to upstream servers, and
DNS resolution in SMTP.  With ngx_mail_send() as a write event handler,
this might result in use-after-free if ngx_mail_send() is called due to
a write event and closes the session due to an error, while an ongoing
operation, such as an auth_http request, still references it
(CVE-2026-113425).

The fix is to block write events similarly to how read events are
currently blocked during corresponding operations.

See also:
https://github.com/nginx/nginx/commit/df9d8d940ecb83a50b1e55ccc296d0a29cfe774c
https://github.com/nginx/nginx/commit/e2d23d45fa93ed236a9cdedcb54271e0a064c4f3
https://github.com/nginx/nginx/commit/5deda780a9d5d1bcd5c6731d3118fa090ae900cc
https://github.com/nginx/nginx/commit/4c5ace9f1d5419334a2e6772a0885ee7ed3d6e91

diffstat:

 src/mail/ngx_mail_auth_http_module.c |  27 +++++++++++++++++++++++++++
 src/mail/ngx_mail_proxy_module.c     |  19 +++++++++++++++++++
 src/mail/ngx_mail_smtp_handler.c     |  36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 0 deletions(-)

diffs (175 lines):

diff --git a/src/mail/ngx_mail_auth_http_module.c b/src/mail/ngx_mail_auth_http_module.c
--- a/src/mail/ngx_mail_auth_http_module.c
+++ b/src/mail/ngx_mail_auth_http_module.c
@@ -74,6 +74,7 @@ static void ngx_mail_auth_send_error(ngx
 static ngx_int_t ngx_mail_auth_http_parse_header_line(ngx_mail_session_t *s,
     ngx_mail_auth_http_ctx_t *ctx);
 static void ngx_mail_auth_http_block_read(ngx_event_t *rev);
+static void ngx_mail_auth_http_block_write(ngx_event_t *wev);
 static void ngx_mail_auth_http_dummy_handler(ngx_event_t *ev);
 static ngx_buf_t *ngx_mail_auth_http_create_request(ngx_mail_session_t *s,
     ngx_pool_t *pool, ngx_mail_auth_http_conf_t *ahcf);
@@ -231,6 +232,7 @@ ngx_mail_auth_http_init(ngx_mail_session
     ctx->peer.connection->pool = s->connection->pool;
 
     s->connection->read->handler = ngx_mail_auth_http_block_read;
+    s->connection->write->handler = ngx_mail_auth_http_block_write;
     ctx->peer.connection->read->handler = ngx_mail_auth_http_read_handler;
     ctx->peer.connection->write->handler = ngx_mail_auth_http_write_handler;
 
@@ -783,6 +785,8 @@ ngx_mail_auth_http_process_headers(ngx_m
 
             ngx_close_connection(ctx->peer.connection);
 
+            s->connection->write->handler = ngx_mail_send;
+
             if (ctx->err.len) {
 
                 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
@@ -1254,6 +1258,29 @@ ngx_mail_auth_http_block_read(ngx_event_
 
 
 static void
+ngx_mail_auth_http_block_write(ngx_event_t *wev)
+{
+    ngx_connection_t          *c;
+    ngx_mail_session_t        *s;
+    ngx_mail_auth_http_ctx_t  *ctx;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0,
+                   "mail auth http block write");
+
+    if (ngx_handle_write_event(wev, 0) != NGX_OK) {
+        c = wev->data;
+        s = c->data;
+
+        ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module);
+
+        ngx_close_connection(ctx->peer.connection);
+        ngx_destroy_pool(ctx->pool);
+        ngx_mail_session_internal_server_error(s);
+    }
+}
+
+
+static void
 ngx_mail_auth_http_dummy_handler(ngx_event_t *ev)
 {
     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, ev->log, 0,
diff --git a/src/mail/ngx_mail_proxy_module.c b/src/mail/ngx_mail_proxy_module.c
--- a/src/mail/ngx_mail_proxy_module.c
+++ b/src/mail/ngx_mail_proxy_module.c
@@ -24,6 +24,7 @@ typedef struct {
 
 
 static void ngx_mail_proxy_block_read(ngx_event_t *rev);
+static void ngx_mail_proxy_block_write(ngx_event_t *wev);
 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
@@ -165,6 +166,7 @@ ngx_mail_proxy_init(ngx_mail_session_t *
     p->upstream.connection->pool = s->connection->pool;
 
     s->connection->read->handler = ngx_mail_proxy_block_read;
+    s->connection->write->handler = ngx_mail_proxy_block_write;
     p->upstream.connection->write->handler = ngx_mail_proxy_write_handler;
 
     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
@@ -224,6 +226,23 @@ ngx_mail_proxy_block_read(ngx_event_t *r
 
 
 static void
+ngx_mail_proxy_block_write(ngx_event_t *wev)
+{
+    ngx_connection_t    *c;
+    ngx_mail_session_t  *s;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy block write");
+
+    if (ngx_handle_read_event(wev, 0) != NGX_OK) {
+        c = wev->data;
+        s = c->data;
+
+        ngx_mail_proxy_close_session(s);
+    }
+}
+
+
+static void
 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
 {
     u_char                 *p;
diff --git a/src/mail/ngx_mail_smtp_handler.c b/src/mail/ngx_mail_smtp_handler.c
--- a/src/mail/ngx_mail_smtp_handler.c
+++ b/src/mail/ngx_mail_smtp_handler.c
@@ -16,6 +16,7 @@ static void ngx_mail_smtp_resolve_addr_h
 static void ngx_mail_smtp_resolve_name(ngx_event_t *rev);
 static void ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx);
 static void ngx_mail_smtp_block_reading(ngx_event_t *rev);
+static void ngx_mail_smtp_block_writing(ngx_event_t *wev);
 static void ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c);
 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev);
 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s,
@@ -91,6 +92,7 @@ ngx_mail_smtp_init_session(ngx_mail_sess
 
     s->resolver_ctx = ctx;
     c->read->handler = ngx_mail_smtp_block_reading;
+    c->write->handler = ngx_mail_smtp_block_writing;
 
     if (ngx_resolve_addr(ctx) != NGX_OK) {
         ngx_mail_close_connection(c);
@@ -278,6 +280,38 @@ ngx_mail_smtp_block_reading(ngx_event_t 
 
 
 static void
+ngx_mail_smtp_block_writing(ngx_event_t *wev)
+{
+    ngx_connection_t    *c;
+    ngx_mail_session_t  *s;
+    ngx_resolver_ctx_t  *ctx;
+
+    c = wev->data;
+    s = c->data;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "smtp writing blocked");
+
+    if (ngx_handle_write_event(wev, 0) != NGX_OK) {
+
+        if (s->resolver_ctx) {
+            ctx = s->resolver_ctx;
+
+            if (ctx->handler == ngx_mail_smtp_resolve_addr_handler) {
+                ngx_resolve_addr_done(ctx);
+
+            } else if (ctx->handler == ngx_mail_smtp_resolve_name_handler) {
+                ngx_resolve_name_done(ctx);
+            }
+
+            s->resolver_ctx = NULL;
+        }
+
+        ngx_mail_close_connection(c);
+    }
+}
+
+
+static void
 ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c)
 {
     ngx_msec_t                 timeout;
@@ -307,6 +341,7 @@ ngx_mail_smtp_greeting(ngx_mail_session_
     }
 
     c->read->handler = ngx_mail_smtp_init_protocol;
+    c->write->handler = ngx_mail_send;
 
     s->out = sscf->greeting;
 
@@ -336,6 +371,7 @@ ngx_mail_smtp_invalid_pipelining(ngx_eve
         cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
 
         c->read->handler = ngx_mail_smtp_init_protocol;
+        c->write->handler = ngx_mail_send;
 
         ngx_add_timer(c->read, cscf->timeout);
 


More information about the nginx-devel mailing list