Skip to content

Commit d08f805

Browse files
committed
upstream: Move clearing TLS session from prepare phase to destroy phase
This is because lifecycle of TLS is not synchronized with the current implementation. Somewhere, we observed: Our observation “TLS is freed too early in the upstream prepare-destroy phase → UAF risk” case is existing in the current code base. So, even with Keepalive enabled, our Fluent Bit code base shows multiple conditions where the TLS session is freed during the “prepare destroy” step, which can race with async I/O and cause a use-after-free in ssl_write_internal. Moving TLS freeing to the final destroy_conn() phase mitigates this. Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent ba96208 commit d08f805

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/flb_upstream.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,6 @@ static int prepare_destroy_conn(struct flb_connection *u_conn)
515515
}
516516

517517
if (u_conn->fd > 0) {
518-
#ifdef FLB_HAVE_TLS
519-
if (u_conn->tls_session != NULL) {
520-
flb_tls_session_destroy(u_conn->tls_session);
521-
522-
u_conn->tls_session = NULL;
523-
}
524-
#endif
525518
shutdown_connection(u_conn);
526519

527520
flb_socket_close(u_conn->fd);
@@ -572,6 +565,15 @@ static int destroy_conn(struct flb_connection *u_conn)
572565
return 0;
573566
}
574567

568+
/* Delay to destroy TLS session for safety */
569+
#ifdef FLB_HAVE_TLS
570+
if (u_conn->tls_session != NULL) {
571+
flb_tls_session_destroy(u_conn->tls_session);
572+
573+
u_conn->tls_session = NULL;
574+
}
575+
#endif
576+
575577
mk_list_del(&u_conn->_head);
576578

577579
flb_connection_destroy(u_conn);

0 commit comments

Comments
 (0)