From: jwalch Date: Thu, 29 Oct 2020 17:32:49 +0000 (-0400) Subject: Prevent potential UAF in init_thread_deregister() X-Git-Tag: openssl-3.0.0-alpha8~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3d7e7e7c48210b515ef5e05f4acf6dc58377331c;p=thirdparty%2Fopenssl.git Prevent potential UAF in init_thread_deregister() I discovered the potential for use-after-free on glob_tevent_reg & its members in this function as a consequence of some static (de-)initialization fiasco in C++ client code. Long story short, an EVP_PKEY_free() was happening after OPENSSL_cleanup(). Aside from being freed the EVP_PKEY object wasn't actually being used after cleanup, it was basically just an ordering issue. Obviously the application behavior here is somewhat suspect, but IMO is basically benign. Crashing (most typical outcome of a UAF) doesn't seem the optimal response. At any rate, the issue can be avoided (at least with regard to this function) by simply updating the pointer to NULL rather than leaving it pointing to the freed memory, as is the typical practice. Reviewed-by: Matt Caswell Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/13267) --- diff --git a/crypto/initthread.c b/crypto/initthread.c index f460252ff9f..93160f577cc 100644 --- a/crypto/initthread.c +++ b/crypto/initthread.c @@ -389,6 +389,8 @@ static int init_thread_deregister(void *index, int all) return 0; if (!all) CRYPTO_THREAD_write_lock(gtr->lock); + else + glob_tevent_reg = NULL; for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) { THREAD_EVENT_HANDLER **hands = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);