From 1dfb35de0800967c972eee35e19f3dfc8d50e512 Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Fri, 18 Sep 2026 18:27:20 +1000 Subject: [PATCH] crypto: api - Fix DUP_FIRST crash on unregister The flag CRYPTO_ALG_DUP_FIRST is buggy because upon unregistration it tries to unregister the original algorithm rather than the duplicate which was allocated during registration. Fix this by locating the duplicated algorithm and unregistering that instead of the original. Also remove the duplicate crypto_destroy_alg call. Reported-by: Chenghai Huang Fixes: f1440a90465b ("crypto: api - Add support for duplicating algorithms before registration") Signed-off-by: Herbert Xu --- crypto/algapi.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index 37de377719ae..a6839e2a20f3 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -424,7 +424,6 @@ static void crypto_free_alg(struct crypto_alg *alg) unsigned int algsize = alg->cra_type->algsize; u8 *p = (u8 *)alg - algsize; - crypto_destroy_alg(alg); kfree(p); } @@ -494,9 +493,31 @@ void crypto_unregister_alg(struct crypto_alg *alg) LIST_HEAD(list); down_write(&crypto_alg_sem); + if (alg->cra_flags & CRYPTO_ALG_DUP_FIRST) { + struct crypto_alg *q, *r = NULL; + + list_for_each_entry(q, &crypto_alg_list, cra_list) { + if (crypto_is_larval(q)) + continue; + if (strcmp(q->cra_driver_name, alg->cra_driver_name)) + continue; + r = q; + break; + } + + if (!r) { + up_write(&crypto_alg_sem); + ret = -ENOENT; + goto not_found; + } + + alg = r; + } + ret = crypto_remove_alg(alg, &list); up_write(&crypto_alg_sem); +not_found: if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name)) return;