openssl: OSSL_LIB_CTX_new fails after 125 times new and free with no threads build

While doing a project with tpm2-tss and openssl, Hit an issue when calling OSSL_LIB_CTX_new, and checking more, this failure happened with trace below:

OSSL_LIB_CTX_new -> context_init --> ossl_rand_ctx_new --> CRYPTO_THREAD_init_local(&dgbl->private, NULL)

And this failure not happened at the begin, but after calling OSSL_LIB_CTX_new multi-times. Then did a simple test with below code:

int didTestOpenssl_new(void) {
    int ret = 0;
    int i = 0;
    OSSL_LIB_CTX *libctx = NULL;

    for (i = 0; i < 1000; i++) {
        libctx = OSSL_LIB_CTX_new();
        
        if (libctx) {
            OSSL_LIB_CTX_free(libctx);
        } else {
            printf("\nError to new lib ctx after %d TIMES\n", i);
            break;
        }
    } 
    printf("\nnew lib ctx after %d TIMES\n", i);

    return ret;
}

and get below output:

Error to new lib ctx after 125 TIMES

new lib ctx after 125 TIMES

As can’t reduce the times to call OSSL_LIB_CTX_new as all the calling happened at tpm2-tss side. And seems this issue is related some resource, but as OSSL_LIB_CTX_free is called actually, seems no leak should happen. But did see that after 125 TIMES, failure happened.

Not sure this is a issue or related to other thing. Need some help to resolve this problem. Thanks.

openssl version is 3.1.6.

Thanks hhuang

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 26 (20 by maintainers)

Commits related to this issue

Most upvoted comments

something like this:

diff --git a/crypto/threads_none.c b/crypto/threads_none.c
index 240b7d9fdc..d47bcec87c 100644
--- a/crypto/threads_none.c
+++ b/crypto/threads_none.c
@@ -153,42 +153,48 @@ int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
 
 # define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
 
-static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
+struct thread_local_storage_entry {
+    void *data;
+    uint8_t used;
+};
+
+static struct thread_local_storage_entry thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
 
 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
 {
-    static unsigned int thread_local_key = 0;
+    int entry_idx = 0;
 
-    if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
-        return 0;
+    for (entry_idx = 0; entry_idx < OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX; entry_idx++) {
+        if (!thread_local_storage[entry_idx].used)
+            break;
+    }
 
-    *key = thread_local_key++;
+    if (entry_idx == OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
+        return 0;
 
-    thread_local_storage[*key] = NULL;
+    *key = entry_idx;
+    thread_local_storage[*key].used = 1;
+    thread_local_storage[*key].data = NULL;
 
     return 1;
 }
 
 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
 {
-    if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
-        return NULL;
-
-    return thread_local_storage[*key];
+    return thread_local_storage[*key].data;
 }
 
 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
 {
-    if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
-        return 0;
-
-    thread_local_storage[*key] = val;
+    thread_local_storage[*key].data = val;
 
     return 1;
 }
 
 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
 {
+    thread_local_storage[*key].used = 0;
+    thread_local_storage[*key].data = NULL;
     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
     return 1;
 }

it is a thread local variable, you are guaranteed no concurrency issues here, no locking is needed.

@simo5 I’ve created https://github.com/openssl/project/issues/1215 for the more broad issue of how we are using thread locals.

@nhorman, I wouldn’t remove the conditions in CRYPTO_THREAD_set_local and CRYPTO_THREAD_get_local.

Except in this particular test we are deallocating the context immediately which should mean CRYPTO_THREAD_cleanup_local() being also called for all thread locals in that context so this does not really explain things unless pthread_key_delete() doesn’t really remove the key or we have a bug somewhere missing CRYPTO_THREAD_cleanup_local() call on some thread local.