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
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to nhorman/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to openssl/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to openssl/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to openssl/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to openssl/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to ponapalt/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to siemens/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to MichaelA-Fireblocks/openssl by nhorman a year ago
- Allow for reuse of thread_local keys in threads_none If openssl is configured with no-threads, the implementation has a hard limit of 256 LIB_CTX values, as each LIB_CTX allocates a thread local key,... — committed to MichaelA-Fireblocks/openssl by nhorman a year ago
something like this:
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_localandCRYPTO_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.