Fix an issue in provider_activate_fallbacks()
The above function was running while holding the store lock with a read
lock. Unfortunately it actually modifies the store, so a write lock is
required instead.
@@ -729,11 +729,26 @@ static int provider_forall_loaded(struct provider_store_st *store,
*/
static void provider_activate_fallbacks(struct provider_store_st *store)
{
- if (store->use_fallbacks) {
- int num_provs = sk_OSSL_PROVIDER_num(store->providers);
+ int use_fallbacks;
+ int num_provs;
int activated_fallback_count = 0;
int i;
+ CRYPTO_THREAD_read_lock(store->lock);
+ use_fallbacks = store->use_fallbacks;
+ CRYPTO_THREAD_unlock(store->lock);
+ if (!use_fallbacks)
+ return;
+
+ CRYPTO_THREAD_write_lock(store->lock);
+ /* Check again, just in case another thread changed it */
+ use_fallbacks = store->use_fallbacks;
+ if (!use_fallbacks) {
+ CRYPTO_THREAD_unlock(store->lock);
+ return;
+ }
+
+ num_provs = sk_OSSL_PROVIDER_num(store->providers);
for (i = 0; i < num_provs; i++) {
OSSL_PROVIDER *prov = sk_OSSL_PROVIDER_value(store->providers, i);
@@ -756,7 +771,8 @@ static void provider_activate_fallbacks(struct provider_store_st *store)
*/
if (activated_fallback_count > 0)
store->use_fallbacks = 0;
- }
+
+ CRYPTO_THREAD_unlock(store->lock);
}
int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
@@ -776,10 +792,9 @@ int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
#endif
if (store != NULL) {
- CRYPTO_THREAD_read_lock(store->lock);
-
provider_activate_fallbacks(store);
+ CRYPTO_THREAD_read_lock(store->lock);
/*
* Now, we sweep through all providers
*/
@@ -794,9 +809,7 @@ int ossl_provider_forall_loaded(OSSL_LIB_CTX *ctx,
int ossl_provider_available(OSSL_PROVIDER *prov)
{
if (prov != NULL) {
- CRYPTO_THREAD_read_lock(prov->store->lock);
provider_activate_fallbacks(prov->store);
- CRYPTO_THREAD_unlock(prov->store->lock);
return prov->flag_activated;
}
