DTLS listener leaves a stale connection entry when allocation fails in SSL_clear()
I found this while reviewing #32964. This concerns the DTLS listener support added for OpenSSL 4.1.
The tests deliberately fail allocations during SSL_clear. They confirm the cleanup defect, but they don't establish how likely it is in production. I don't have evidence of practical remote exploitation.
When SSL_clear needs to replace the DTLS state of an accepted connection, ossl_ssl_connection_reset temporarily detaches its listener association from d1. If ssl_init fails, the cleanup releases the saved receive queue and listener reference, but it doesn't remove the connection from established_conns. The relevant code is in ssl_lib.c.
For the allocation failures tested, d1 is left NULL. This means dtls1_free can't unregister the connection when it's freed, since that cleanup depends on d1->listener. The listener is left holding a pointer to a freed SSL object, even if the application checks the failed reset and frees the connection. The local ASan results show an access to freed memory in dtls_listener_packet_handler on both revisions.
The allocation failure checks also cover failures during record layer initialization, before DTLS1_STATE is allocated. Those leave the same stale entry.
The failed reset should unregister the connection while the saved listener reference is still valid. The existing ossl_dtls_listener_unregister_established_conn helper can do this using saved_listener and saved_peer_addr. It takes the listener mutex internally, so the call belongs before the existing mutex block in the PR version.
For me, this as a robustness fix with limited practical urgency based on the evidence so far. Given the small change, it seems worth fixing before 4.1 is released, with an allocation failure regression test to make sure a failed reset leaves no stale registration.
Source: openssl/openssl