openssl icon indicating copy to clipboard operation
openssl copied to clipboard

TSAN: data race in ossl_init_thread_start / init_thread_deregister

Open briangreenery opened this issue 1 year ago • 3 comments

OpenSSL 3.5.4

The init_thread_deregister function iterates over all handlers registered globally in get_global_tevent_register, and it holds the gtr->lock while iterating.

The ossl_init_thread_start can add new handlers to the thread local handler list without holding a lock. While this is a thread local list of handlers, it is linked in the global handler list, and so the init_thread_deregister will iterate over it creating a data race.

WARNING: ThreadSanitizer: data race (pid=44618)
  Read of size 8 at 0x7b04000050e0 by thread T3 (mutexes: write M0):
    #0 init_thread_deregister openssl-3.5.4/crypto/initthread.c:448:16
    #1 ossl_init_thread_deregister openssl-3.5.4/crypto/initthread.c:478:12
    #2 ossl_provider_free openssl-3.5.4/crypto/provider_core.c:765:13
    #3 provider_deactivate_free openssl-3.5.4/crypto/provider_core.c:240:5
    #4 OPENSSL_sk_pop_free openssl-3.5.4/crypto/stack/stack.c:439:13
    #5 sk_OSSL_PROVIDER_pop_free openssl-3.5.4/crypto/provider_core.c:200:1
    #6 ossl_provider_store_free openssl-3.5.4/crypto/provider_core.c:296:5
    #7 context_deinit_objs openssl-3.5.4/crypto/context.c:290:9
    #8 context_deinit openssl-3.5.4/crypto/context.c:387:5
    #9 OSSL_LIB_CTX_free openssl-3.5.4/crypto/context.c:517:5
    ...

  Previous write of size 8 at 0x7b04000050e0 by thread T2:
    #0 ossl_init_thread_start openssl-3.5.4/crypto/initthread.c:418:12
    #1 ossl_err_get_state_int openssl-3.5.4/crypto/err/err.c:697:14
    #2 ERR_clear_error openssl-3.5.4/crypto/err/err.c:339:10
    #3 state_machine openssl-3.5.4/ssl/statem/statem.c:371:5
    #4 ossl_statem_accept openssl-3.5.4/ssl/statem/statem.c:311:12
    #5 SSL_do_handshake openssl-3.5.4/ssl/ssl_lib.c:5007:19
    ...

  Location is heap block of size 8 at 0x7b04000050e0 allocated by thread T2:
    #0 malloc
    #1 CRYPTO_malloc openssl-3.5.4/crypto/mem.c:211:11
    #2 CRYPTO_zalloc openssl-3.5.4/crypto/mem.c:231:11
    #3 init_get_thread_local openssl-3.5.4/crypto/initthread.c:101:26
    #4 ossl_init_thread_start openssl-3.5.4/crypto/initthread.c:392:13
    #5 ossl_err_get_state_int openssl-3.5.4/crypto/err/err.c:697:14
    #6 ERR_clear_error openssl-3.5.4/crypto/err/err.c:339:10
    #7 state_machine openssl-3.5.4/ssl/statem/statem.c:371:5
    #8 ossl_statem_accept openssl-3.5.4/ssl/statem/statem.c:311:12
    #9 SSL_do_handshake openssl-3.5.4/ssl/ssl_lib.c:5007:19
    ...

  Mutex M0 (0x7b100000a300) created at:
    #0 pthread_rwlock_init
    #1 CRYPTO_THREAD_lock_new openssl-3.5.4/crypto/threads_pthread.c:594:9
    #2 create_global_tevent_register openssl-3.5.4/crypto/initthread.c:65:29
    #3 create_global_tevent_register_ossl_ openssl-3.5.4/crypto/initthread.c:58:1
    #4 pthread_once
    #5 CRYPTO_THREAD_run_once openssl-3.5.4/crypto/threads_pthread.c:692:9
    #6 get_global_tevent_register openssl-3.5.4/crypto/initthread.c:79:10
    ...

briangreenery avatar Oct 22 '25 23:10 briangreenery

do you happen to have a reproducer that triggers this tsan error, so we can test fixes against it?

nhorman avatar Oct 23 '25 12:10 nhorman

We've been hitting intermittent SIGSEGV/SIGABRT crashes at process exit in a C++ application that uses gRPC (which links OpenSSL). The crashes occur because OPENSSL_cleanup (atexit) destroys global state while worker threads are still using it.

Here is a minimal reproducer (tested with OpenSSL 3.4.0, statically linked):

/*
 * Reproducer for https://github.com/openssl/openssl/issues/28981
 *
 * Race between OPENSSL_cleanup (atexit) and worker threads still using OpenSSL.
 *
 * Build (TSAN — detects the race reliably):
 *   gcc -O1 -g -fsanitize=thread -o repro repro.c -lssl -lcrypto -lpthread
 *
 * Build (plain — crashes on first run):
 *   gcc -O0 -g -o repro repro.c -lssl -lcrypto -lpthread
 *
 * Run:
 *   # TSAN build: single run reports 16 data races
 *   ./repro
 *
 *   # Plain build: crashes immediately
 *   ./repro
 */

#include <openssl/evp.h>
#include <openssl/err.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

static void *worker(void *arg)
{
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    const EVP_MD *md = EVP_sha256();
    unsigned char hash[EVP_MAX_MD_SIZE];
    unsigned int len;

    EVP_DigestInit_ex(ctx, md, NULL);
    EVP_DigestUpdate(ctx, "data", 4);
    EVP_DigestFinal_ex(ctx, hash, &len);
    EVP_MD_CTX_free(ctx);

    return NULL;
}

int main(void)
{
    pthread_attr_t attr;
    pthread_t tid;
    const int nthreads = 64;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    for (int i = 0; i < nthreads; i++)
        pthread_create(&tid, &attr, worker, NULL);

    pthread_attr_destroy(&attr);
    exit(0);
}

TSAN build — single run detects 16 data races in ossl_ht_get, evp_md_free_int, impl_free, OPENSSL_sk_pop_free, alg_cleanup, OPENSSL_LH_free, ossl_sa_free, CRYPTO_THREAD_lock_free.

Plain build — SIGSEGV on first run. GDB backtrace from core dump:

Thread 2 (main thread — atexit path):
#0  ossl_lh_strcasehash ()
#1  obj_name_hash ()
#2  getrn ()
#3  OPENSSL_LH_delete ()
#4  OBJ_NAME_remove ()
#5  OPENSSL_LH_doall ()
#6  OBJ_NAME_cleanup ()
#7  evp_cleanup_int ()
#8  OPENSSL_cleanup ()
#9  __run_exit_handlers () at exit.c:118
#10 __GI_exit ()
#11 main () at repro.c:85

Thread 1 (crashed worker — SIGSEGV in ossl_rcu_uptr_deref):
#0  ossl_rcu_uptr_deref ()
#1  ossl_ht_insert ()
#2  namemap_add_name ()
#3  ossl_namemap_add_name ()
#4  get_legacy_cipher_names ()
#5  OPENSSL_LH_doall_arg_thunk ()
#6  OBJ_NAME_do_all ()
#7  ossl_namemap_stored ()
#8  inner_evp_generic_fetch ()
#9  evp_generic_fetch ()
#10 EVP_MD_fetch ()
#11 evp_md_init_internal ()
#12 worker () at repro.c:52

Threads 3-21 (19 workers — blocked on destroyed lock):
#0  __pthread_rwlock_wrlock ()
#1  CRYPTO_THREAD_write_lock ()
#2  ossl_namemap_add_name ()
    ... same EVP_MD_fetch path as Thread 1 ...

The main thread is in OPENSSL_cleanup destroying the hash table (OBJ_NAME_cleanup → OPENSSL_LH_delete), while worker threads are concurrently inserting into the same table (ossl_namemap_add_name → ossl_ht_insert). The crash occurs when ossl_rcu_uptr_deref dereferences memory that the main thread has already freed. 19 other worker threads are deadlocked on a rwlock that the main thread destroyed.

This is a real-world problem for applications using libraries like gRPC that create internal background threads which cannot be joined before process exit.

kyusic avatar Mar 14 '26 01:03 kyusic

Ok, The general form of the problem is that, in your example, you've created a thread that is detached. That is the thread you create survives after the main process exits, and because you use openssl in that created thread (exactly as you've described), openssl cleans up automatically on the main threads exit.

In the openssl 3.x series the solution to this problem is to initialize the Openssl library as follows in the main routine:

OPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL);

This prevents the openssl library from cleaning up on exit. The detached thread is then responsible for cleanup (by adding a call to OPENSSL_cleanup(), if you care about freeing global resources, which isn't really critical).

In 4.x we've removed that requirement entirely, and added notes about the reachable memory issues that tools like valgrind might report as a result. See commit 7e56cab285e208c500110767c7edfaec9b96155c

nhorman avatar Mar 14 '26 13:03 nhorman