Commit 2d1c10a592 for openssl.org

commit 2d1c10a592677ff34cdf60bd59c431d835a1fc26
Author: Viktor Dukhovni <viktor@openssl.org>
Date:   Fri Nov 6 18:26:17 2026 +1100

    Perform PKEY dirty_cnt updates under a write lock.

    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Reviewed-by: Bob Beck <beck@openssl.org>
    Reviewed-by: Andrew Dinh <andrewd@openssl.org>
    MergeDate: Wed Nov 25 08:52:07 2026
    (Merged from https://github.com/openssl/openssl/pull/33557)

diff --git a/crypto/evp/keymgmt_lib.c b/crypto/evp/keymgmt_lib.c
index 7321d74728..7e68d47f51 100644
--- a/crypto/evp/keymgmt_lib.c
+++ b/crypto/evp/keymgmt_lib.c
@@ -98,6 +98,7 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
 {
     struct evp_keymgmt_util_try_import_data_st import_data;
     OP_CACHE_ELEM *op;
+    size_t gen;

     /* Export to where? */
     if (keymgmt == NULL)
@@ -119,6 +120,7 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
             && pk->keymgmt->prov == keymgmt->prov))
         return pk->keydata;

+retry:
     if (!CRYPTO_THREAD_read_lock(pk->lock))
         return NULL;
     /*
@@ -135,6 +137,11 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
         CRYPTO_THREAD_unlock(pk->lock);
         return ret;
     }
+    /*
+     * Note the dirty count before reading the "origin".  The new entry is
+     * only cached if the count is the same when the export is done.
+     */
+    gen = pk->dirty_cnt;
     CRYPTO_THREAD_unlock(pk->lock);

     /* If the "origin" |keymgmt| doesn't support exporting, give up */
@@ -188,6 +195,16 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
         return ret;
     }

+    /*
+     * The "origin" changed while we were exporting, so the export may
+     * not match the current key.  Discard it and export again.
+     */
+    if (gen != pk->dirty_cnt) {
+        CRYPTO_THREAD_unlock(pk->lock);
+        evp_keymgmt_freedata(keymgmt, import_data.keydata);
+        goto retry;
+    }
+
     /* Add the new export to the operation cache */
     if (!evp_keymgmt_util_cache_keydata(pk, keymgmt, import_data.keydata,
             selection)) {
diff --git a/crypto/evp/p_lib.c b/crypto/evp/p_lib.c
index 9eb717bf45..5d71103e0c 100644
--- a/crypto/evp/p_lib.c
+++ b/crypto/evp/p_lib.c
@@ -2317,11 +2317,25 @@ int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
 {
     if (pkey != NULL) {
         if (evp_pkey_is_provided(pkey)) {
+            int ret;
+
             /* Without a set_params method the key cannot change */
             if (pkey->keymgmt->set_params == NULL)
                 return 1;
+
+            ret = evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
+
+            /*
+             * Increment the dirty count after the key has changed, under
+             * the lock the export code uses.  An export that read the key
+             * before or during the change carries an older count, so it
+             * is never used as current.
+             */
+            if (!CRYPTO_THREAD_write_lock(pkey->lock))
+                return 0;
             pkey->dirty_cnt++;
-            return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
+            CRYPTO_THREAD_unlock(pkey->lock);
+            return ret;
         }
 #ifndef FIPS_MODULE
         /*
diff --git a/doc/man3/EVP_PKEY_settable_params.pod b/doc/man3/EVP_PKEY_settable_params.pod
index 6760818cda..a998f0b9eb 100644
--- a/doc/man3/EVP_PKEY_settable_params.pod
+++ b/doc/man3/EVP_PKEY_settable_params.pod
@@ -53,6 +53,9 @@ size I<bsize> into a key I<pkey> for the associated field I<key_name>.

 These functions only work for B<EVP_PKEY>s that contain a provider side key.

+Setting parameters changes the key itself.  It must not be done while another
+thread is using the same B<EVP_PKEY>, or an operation context created from it.
+
 =head1 RETURN VALUES

 EVP_PKEY_settable_params() returns NULL on error or if it is not supported,