Commit 6d46abb92d for openssl.org
commit 6d46abb92d1c03e85e57f18e64a209dad02aa579
Author: Viktor Dukhovni <viktor@openssl.org>
Date: Thu Nov 5 23:57:20 2026 +1100
Keep exported provider keys until the EVP_PKEY is freed
EVP_PKEY_set_params() increments the dirty count of a provider key,
and the next export to another provider then freed every cached export
of that key. Operation contexts in those providers still held the
freed key data. The keys of the post-quantum algorithms are not
reference counted, so such a context used freed memory the next time
it ran.
Note however that at least ML-KEM and ML-DSA keys are immutable
once initialised, so applications should not expect to be able
to modify "live" keys. The same policy will soon also apply
to SLH-DSA, LMS and ML-KEM hybrid keys.
Stop freeing cached exports when the dirty count changes. Each cache
entry now records the dirty count it was exported at and is used only
while that count is current. An outdated entry stays in the cache,
unused, until the EVP_PKEY is freed, which cannot happen while an
operation context holding the key is live. Look up the newest entry
first, so a current entry is preferred, and make the check made after
a concurrent export use the same rule, so it can no longer return an
entry that predates a change.
Return early from EVP_PKEY_set_params() when the key manager has no
set_params method. The key cannot have changed, so there is no reason
to make the cached exports out of date.
Keys with a legacy origin still flush the cache, because their
providers hold their own references. A provider key that is changed
and then exported again keeps one extra copy per change until it is
freed.
Add a test that exports an ML-KEM key to a second provider, changes
its dirty count, exports it to a third provider and then uses the
second one.
Assisted-by: Claude Code:claude-sonnet-5-5
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:00 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 a099e817a0..7321d74728 100644
--- a/crypto/evp/keymgmt_lib.c
+++ b/crypto/evp/keymgmt_lib.c
@@ -122,19 +122,18 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
if (!CRYPTO_THREAD_read_lock(pk->lock))
return NULL;
/*
- * If the provider native "origin" hasn't changed since last time, we
- * try to find our keymgmt in the operation cache. If it has changed
- * and our keymgmt isn't found, we will clear the cache further down.
+ * If this key is already exported to |keymgmt|, and the "origin" hasn't
+ * changed since that export, no more to do. Entries exported before the
+ * last change are never freed here, because operation contexts may still
+ * be using them. They stay in the cache, unused, until the key is
+ * released (see the comment on OP_CACHE_ELEM).
*/
- if (pk->dirty_cnt == pk->dirty_cnt_copy) {
- /* If this key is already exported to |keymgmt|, no more to do */
- op = evp_keymgmt_util_find_operation_cache(pk, keymgmt, selection);
- if (op != NULL && op->keymgmt != NULL) {
- void *ret = op->keydata;
-
- CRYPTO_THREAD_unlock(pk->lock);
- return ret;
- }
+ op = evp_keymgmt_util_find_operation_cache(pk, keymgmt, selection);
+ if (op != NULL && op->gen == pk->dirty_cnt) {
+ void *ret = op->keydata;
+
+ CRYPTO_THREAD_unlock(pk->lock);
+ return ret;
}
CRYPTO_THREAD_unlock(pk->lock);
@@ -175,7 +174,7 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
}
/* Check to make sure some other thread didn't get there first */
op = evp_keymgmt_util_find_operation_cache(pk, keymgmt, selection);
- if (op != NULL && op->keydata != NULL) {
+ if (op != NULL && op->gen == pk->dirty_cnt) {
void *ret = op->keydata;
CRYPTO_THREAD_unlock(pk->lock);
@@ -189,13 +188,6 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
return ret;
}
- /*
- * If the dirty counter changed since last time, then clear the
- * operation cache. In that case, we know that |i| is zero.
- */
- if (pk->dirty_cnt != pk->dirty_cnt_copy)
- evp_keymgmt_util_clear_operation_cache(pk);
-
/* Add the new export to the operation cache */
if (!evp_keymgmt_util_cache_keydata(pk, keymgmt, import_data.keydata,
selection)) {
@@ -204,9 +196,6 @@ void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
return NULL;
}
- /* Synchronize the dirty count */
- pk->dirty_cnt_copy = pk->dirty_cnt;
-
CRYPTO_THREAD_unlock(pk->lock);
return import_data.keydata;
@@ -232,16 +221,18 @@ OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
EVP_KEYMGMT *keymgmt,
int selection)
{
- int i, end = sk_OP_CACHE_ELEM_num(pk->operation_cache);
+ int i;
OP_CACHE_ELEM *p;
/*
* A comparison and sk_P_CACHE_ELEM_find() are avoided to not cause
* problems when we've only a read lock.
* A keymgmt is a match if the |keymgmt| pointers are identical or if the
- * provider and the name ID match
+ * provider and the name ID match.
+ * The newest entry is found first, so that an entry exported from the
+ * current "origin" is preferred over older ones for the same keymgmt.
*/
- for (i = 0; i < end; i++) {
+ for (i = sk_OP_CACHE_ELEM_num(pk->operation_cache) - 1; i >= 0; i--) {
p = sk_OP_CACHE_ELEM_value(pk->operation_cache, i);
if ((p->selection & selection) == selection
&& (keymgmt == p->keymgmt
@@ -270,6 +261,7 @@ int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
p->keydata = keydata;
p->keymgmt = keymgmt;
p->selection = selection;
+ p->gen = pk->dirty_cnt;
if (!EVP_KEYMGMT_up_ref(keymgmt)) {
OPENSSL_free(p);
diff --git a/crypto/evp/p_lib.c b/crypto/evp/p_lib.c
index c9da0329b1..9eb717bf45 100644
--- a/crypto/evp/p_lib.c
+++ b/crypto/evp/p_lib.c
@@ -2317,6 +2317,9 @@ int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
{
if (pkey != NULL) {
if (evp_pkey_is_provided(pkey)) {
+ /* Without a set_params method the key cannot change */
+ if (pkey->keymgmt->set_params == NULL)
+ return 1;
pkey->dirty_cnt++;
return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
}
diff --git a/include/crypto/evp.h b/include/crypto/evp.h
index 6d9f899d17..d3a9b9c41b 100644
--- a/include/crypto/evp.h
+++ b/include/crypto/evp.h
@@ -337,10 +337,19 @@ int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
* This requires that the "origin" backend (whether it's a legacy or a
* provider "origin") implements exports, and that the target provider
* has an EVP_KEYMGMT that implements import.
+ * Operation contexts hold the cached keydata without owning it, so for a
+ * provider "origin" entries are only freed when its key is released: when
+ * the EVP_PKEY is freed, or when its key is replaced (for example by
+ * generating or importing into an existing EVP_PKEY). Replacing the key
+ * also frees the "origin" keydata, so it is not safe while operation
+ * contexts use that EVP_PKEY.
+ * |gen| is the "origin"'s dirty count when the entry was exported, and the
+ * entry is current only while that still matches.
*/
typedef struct {
EVP_KEYMGMT *keymgmt;
void *keydata;
+ size_t gen;
int selection;
} OP_CACHE_ELEM;
@@ -457,8 +466,9 @@ struct evp_pkey_st {
STACK_OF(OP_CACHE_ELEM) *operation_cache;
/*
- * We keep a copy of that "origin"'s dirty count, so we know if the
- * operation cache needs flushing.
+ * For a legacy "origin", we keep a copy of its dirty count, so we know
+ * if the operation cache needs flushing. Not used with a provider
+ * "origin", whose cache entries carry their own dirty count.
*/
size_t dirty_cnt_copy;
diff --git a/test/evp_extra_test2.c b/test/evp_extra_test2.c
index 54d82caf68..9685d9cd2d 100644
--- a/test/evp_extra_test2.c
+++ b/test/evp_extra_test2.c
@@ -1608,6 +1608,69 @@ end:
return ret;
}
+#ifndef OPENSSL_NO_ML_KEM
+/*
+ * Provider keys for the PQ algorithms are not reference counted, so the
+ * key exported to a second provider must stay alive while an encapsulation
+ * context made from it is in use, even after the origin key's dirty count
+ * changes and the key is exported to a third provider. ML-KEM has a
+ * set_params method, so EVP_PKEY_set_params() does change the dirty count.
+ * Run under ASan to see the failure.
+ */
+static int test_pq_export_survives_set_params(void)
+{
+ OSSL_PARAM noparams[] = { OSSL_PARAM_END };
+ OSSL_LIB_CTX *libctx[3] = { NULL, NULL, NULL };
+ EVP_PKEY_CTX *ectx = NULL, *other = NULL, *dctx = NULL;
+ EVP_PKEY *pkey = NULL;
+ unsigned char *wrapped = NULL;
+ unsigned char secret[64], secret2[64];
+ size_t wlen = 0, slen = 0, slen2 = sizeof(secret2);
+ int i, ret = 0;
+
+ for (i = 0; i < 3; i++)
+ if (!TEST_ptr(libctx[i] = OSSL_LIB_CTX_new()))
+ goto end;
+
+ if (!TEST_ptr(pkey = EVP_PKEY_Q_keygen(libctx[0], NULL, "ML-KEM-768"))
+ /* Exported to the second provider */
+ || !TEST_ptr(ectx = EVP_PKEY_CTX_new_from_pkey(libctx[1], pkey, NULL))
+ || !TEST_int_gt(EVP_PKEY_encapsulate_init(ectx, NULL), 0)
+ /* The key does not change, but its dirty count does */
+ || !TEST_true(EVP_PKEY_set_params(pkey, noparams))
+ /* Exported to the third provider */
+ || !TEST_ptr(other = EVP_PKEY_CTX_new_from_pkey(libctx[2], pkey, NULL))
+ || !TEST_int_gt(EVP_PKEY_encapsulate_init(other, NULL), 0))
+ goto end;
+
+ /* The second provider's context must still work */
+ if (!TEST_int_gt(EVP_PKEY_encapsulate(ectx, NULL, &wlen, NULL, &slen), 0)
+ || !TEST_size_t_le(slen, sizeof(secret))
+ || !TEST_ptr(wrapped = OPENSSL_malloc(wlen))
+ || !TEST_int_gt(EVP_PKEY_encapsulate(ectx, wrapped, &wlen, secret,
+ &slen),
+ 0)
+ || !TEST_ptr(dctx = EVP_PKEY_CTX_new_from_pkey(libctx[0], pkey, NULL))
+ || !TEST_int_gt(EVP_PKEY_decapsulate_init(dctx, NULL), 0)
+ || !TEST_int_gt(EVP_PKEY_decapsulate(dctx, secret2, &slen2, wrapped,
+ wlen),
+ 0)
+ || !TEST_mem_eq(secret, slen, secret2, slen2))
+ goto end;
+
+ ret = 1;
+end:
+ OPENSSL_free(wrapped);
+ EVP_PKEY_CTX_free(dctx);
+ EVP_PKEY_CTX_free(other);
+ EVP_PKEY_CTX_free(ectx);
+ EVP_PKEY_free(pkey);
+ for (i = 0; i < 3; i++)
+ OSSL_LIB_CTX_free(libctx[i]);
+ return ret;
+}
+#endif
+
static int test_evp_md_ctx_serialize(int tstid)
{
static const char *algs[] = {
@@ -1872,6 +1935,9 @@ int setup_tests(void)
ADD_TEST(test_evp_md_ctx_dup);
ADD_TEST(test_evp_md_ctx_copy);
ADD_TEST(test_evp_md_ctx_copy2);
+#ifndef OPENSSL_NO_ML_KEM
+ ADD_TEST(test_pq_export_survives_set_params);
+#endif
ADD_ALL_TESTS(test_evp_md_ctx_serialize, 13);
ADD_ALL_TESTS(test_provider_unload_effective, 2);
#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5