Use a write lock during ossl_provider_find()
authorMatt Caswell <matt@openssl.org>
Tue, 9 Nov 2021 14:32:14 +0000 (14:32 +0000)
committerMatt Caswell <matt@openssl.org>
Mon, 15 Nov 2021 14:22:41 +0000 (14:22 +0000)
A "find" operation on a stack can end up sorting the underlying stack. In
this case it is necessary to use a "write" lock to synchronise access to
the stack across multiple threads.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17018)

crypto/provider_core.c
doc/man3/DEFINE_STACK_OF.pod

index 127c7c9aba47fa6b6a38ebd6f5f6118ea9bed199..a11676d03ce460c98a2928dc3d9f6133aea0af8b 100644 (file)
@@ -424,7 +424,11 @@ OSSL_PROVIDER *ossl_provider_find(OSSL_LIB_CTX *libctx, const char *name,
 #endif
 
         tmpl.name = (char *)name;
-        if (!CRYPTO_THREAD_read_lock(store->lock))
+        /*
+         * A "find" operation can sort the stack, and therefore a write lock is
+         * required.
+         */
+        if (!CRYPTO_THREAD_write_lock(store->lock))
             return NULL;
         if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) != -1)
             prov = sk_OSSL_PROVIDER_value(store->providers, i);
index d7152466f4d3b4e14ee6f23784e55299229f9816..ec9eda81c6f83c824603dda1314b08e85bf99de0 100644 (file)
@@ -178,7 +178,10 @@ where a comparison function has been specified, I<sk> is sorted and
 B<sk_I<TYPE>_find>() returns the index of a matching element or B<-1> if there
 is no match. Note that, in this case the comparison function will usually
 compare the values pointed to rather than the pointers themselves and
-the order of elements in I<sk> can change.
+the order of elements in I<sk> can change. Note that because the stack may be
+sorted as the result of a B<sk_I<TYPE>_find>() call, if a lock is being used to
+synchronise access to the stack across multiple threads, then that lock must be
+a "write" lock.
 
 B<sk_I<TYPE>_find_ex>() operates like B<sk_I<TYPE>_find>() except when a
 comparison function has been specified and no matching element is found.