]> git.kaiwu.me - nginx.git/commitdiff
QUIC: fixed post-close use-after-free.
authorRoman Arutyunyan <arut@nginx.com>
Mon, 22 May 2023 11:59:42 +0000 (15:59 +0400)
committerRoman Arutyunyan <arut@nginx.com>
Mon, 22 May 2023 11:59:42 +0000 (15:59 +0400)
Previously, ngx_quic_close_connection() could be called in a way that QUIC
connection was accessed after the call.  In most cases the connection is not
closed right away, but close timeout is scheduled.  However, it's not always
the case.  Also, if the close process started earlier for a different reason,
calling ngx_quic_close_connection() may actually close the connection.  The
connection object should not be accessed after that.

Now, when possible, return statement is added to eliminate post-close connection
object access.  In other places ngx_quic_close_connection() is substituted with
posting close event.

Also, the new way of closing connection in ngx_quic_stream_cleanup_handler()
fixes another problem in this function.  Previously it passed stream connection
instead of QUIC connection to ngx_quic_close_connection().  This could result
in incomplete connection shutdown.  One consequence of that could be that QUIC
streams were freed without shutting down their application contexts.  This could
result in another use-after-free.

Found by Coverity (CID 1530402).

src/event/quic/ngx_event_quic.c
src/event/quic/ngx_event_quic_ack.c
src/event/quic/ngx_event_quic_streams.c

index b4033bc3f30d6cd297a98e0730db4e034d3cbc12..b559c485d3116caf8ec4bd829655dfd27e10d586 100644 (file)
@@ -844,7 +844,7 @@ ngx_quic_handle_packet(ngx_connection_t *c, ngx_quic_conf_t *conf,
                               "quic stateless reset packet detected");
 
                 qc->draining = 1;
-                ngx_quic_close_connection(c, NGX_OK);
+                ngx_post_event(&qc->close, &ngx_posted_events);
 
                 return NGX_OK;
             }
@@ -1390,7 +1390,7 @@ ngx_quic_handle_frames(ngx_connection_t *c, ngx_quic_header_t *pkt)
 
     if (do_close) {
         qc->draining = 1;
-        ngx_quic_close_connection(c, NGX_OK);
+        ngx_post_event(&qc->close, &ngx_posted_events);
     }
 
     if (pkt->path != qc->path && nonprobing) {
index 062b065f929ec08aa6e773c6b5c368026296f63a..21ee888c9cbcf9d22e403946db027248b41ea951 100644 (file)
@@ -806,6 +806,7 @@ void ngx_quic_lost_handler(ngx_event_t *ev)
 
     if (ngx_quic_detect_lost(c, NULL) != NGX_OK) {
         ngx_quic_close_connection(c, NGX_ERROR);
+        return;
     }
 
     ngx_quic_connstate_dbg(c);
index 6664dfd7b8957fb2d4470ee58e643783f95c06b3..b4b0eda052784f910e5dbb9b15cd62047b62391b 100644 (file)
@@ -1084,7 +1084,8 @@ ngx_quic_stream_cleanup_handler(void *data)
 {
     ngx_connection_t *c = data;
 
-    ngx_quic_stream_t  *qs;
+    ngx_quic_stream_t      *qs;
+    ngx_quic_connection_t  *qc;
 
     qs = c->quic;
 
@@ -1092,16 +1093,23 @@ ngx_quic_stream_cleanup_handler(void *data)
                    "quic stream id:0x%xL cleanup", qs->id);
 
     if (ngx_quic_shutdown_stream(c, NGX_RDWR_SHUTDOWN) != NGX_OK) {
-        ngx_quic_close_connection(c, NGX_ERROR);
-        return;
+        goto failed;
     }
 
     qs->connection = NULL;
 
     if (ngx_quic_close_stream(qs) != NGX_OK) {
-        ngx_quic_close_connection(c, NGX_ERROR);
-        return;
+        goto failed;
     }
+
+    return;
+
+failed:
+
+    qc = ngx_quic_get_connection(qs->parent);
+    qc->error = NGX_QUIC_ERR_INTERNAL_ERROR;
+
+    ngx_post_event(&qc->close, &ngx_posted_events);
 }