comparison src/event/quic/ngx_event_quic_streams.c @ 9112:d59277dd3d8c

QUIC: fixed post-close use-after-free. 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).
author Roman Arutyunyan <arut@nginx.com>
date Mon, 22 May 2023 15:59:42 +0400
parents 3bb003fcd682
children ad3d34ddfdcc
comparison
equal deleted inserted replaced
9111:68fa4b86ed46 9112:d59277dd3d8c
1082 static void 1082 static void
1083 ngx_quic_stream_cleanup_handler(void *data) 1083 ngx_quic_stream_cleanup_handler(void *data)
1084 { 1084 {
1085 ngx_connection_t *c = data; 1085 ngx_connection_t *c = data;
1086 1086
1087 ngx_quic_stream_t *qs; 1087 ngx_quic_stream_t *qs;
1088 ngx_quic_connection_t *qc;
1088 1089
1089 qs = c->quic; 1090 qs = c->quic;
1090 1091
1091 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, qs->parent->log, 0, 1092 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, qs->parent->log, 0,
1092 "quic stream id:0x%xL cleanup", qs->id); 1093 "quic stream id:0x%xL cleanup", qs->id);
1093 1094
1094 if (ngx_quic_shutdown_stream(c, NGX_RDWR_SHUTDOWN) != NGX_OK) { 1095 if (ngx_quic_shutdown_stream(c, NGX_RDWR_SHUTDOWN) != NGX_OK) {
1095 ngx_quic_close_connection(c, NGX_ERROR); 1096 goto failed;
1096 return;
1097 } 1097 }
1098 1098
1099 qs->connection = NULL; 1099 qs->connection = NULL;
1100 1100
1101 if (ngx_quic_close_stream(qs) != NGX_OK) { 1101 if (ngx_quic_close_stream(qs) != NGX_OK) {
1102 ngx_quic_close_connection(c, NGX_ERROR); 1102 goto failed;
1103 return; 1103 }
1104 } 1104
1105 return;
1106
1107 failed:
1108
1109 qc = ngx_quic_get_connection(qs->parent);
1110 qc->error = NGX_QUIC_ERR_INTERNAL_ERROR;
1111
1112 ngx_post_event(&qc->close, &ngx_posted_events);
1105 } 1113 }
1106 1114
1107 1115
1108 static ngx_int_t 1116 static ngx_int_t
1109 ngx_quic_close_stream(ngx_quic_stream_t *qs) 1117 ngx_quic_close_stream(ngx_quic_stream_t *qs)