[PATCH] crypto: safexcel - Unwind SHA3 initialization if HMAC setup fails

From: Wentao Liang

Date: Tue Nov 03 2026 - 01:40:33 EST


safexcel_hmac_sha3_cra_init() first initializes the SHA3 context, which
allocates an ahash fallback. If allocating the shash transform or its
descriptor fails afterwards, that fallback is not released. The crypto
core does not invoke cra_exit after cra_init returns an error.

Call safexcel_sha3_cra_exit() on these failure paths to release the
resources acquired by safexcel_sha3_cra_init().

Fixes: 6c1c09b3dd0c ("crypto: inside-secure - Add HMAC-SHA3 family of authentication algorithms")
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: DeepSeek-V4.1-Flash and OpenAI Codex
Signed-off-by: Wentao Liang <vulab@xxxxxxxxxxx>
---
drivers/crypto/inside-secure/safexcel_hash.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/inside-secure/safexcel_hash.c b/drivers/crypto/inside-secure/safexcel_hash.c
index 8f107da66685..308a1992a738 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -2649,17 +2649,24 @@ static int safexcel_hmac_sha3_cra_init(struct crypto_tfm *tfm, const char *alg)

/* Allocate precalc basic digest implementation */
ctx->shpre = crypto_alloc_shash(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
- if (IS_ERR(ctx->shpre))
- return PTR_ERR(ctx->shpre);
+ if (IS_ERR(ctx->shpre)) {
+ ret = PTR_ERR(ctx->shpre);
+ goto err_sha3;
+ }

ctx->shdesc = kmalloc(sizeof(*ctx->shdesc) +
crypto_shash_descsize(ctx->shpre), GFP_KERNEL);
if (!ctx->shdesc) {
crypto_free_shash(ctx->shpre);
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto err_sha3;
}
ctx->shdesc->tfm = ctx->shpre;
return 0;
+
+err_sha3:
+ safexcel_sha3_cra_exit(tfm);
+ return ret;
}

static void safexcel_hmac_sha3_cra_exit(struct crypto_tfm *tfm)
--
2.34.1