Skip to content

Commit ccb679f

Browse files
GoodLuck612herbertx
authored andcommitted
crypto: starfive - Fix memory leak in starfive_aes_aead_do_one_req()
The starfive_aes_aead_do_one_req() function allocates rctx->adata with kzalloc() but fails to free it if sg_copy_to_buffer() or starfive_aes_hw_init() fails, which lead to memory leaks. Since rctx->adata is unconditionally freed after the write_adata operations, ensure consistent cleanup by freeing the allocation in these earlier error paths as well. Compile tested only. Issue found using a prototype static analysis tool and code review. Fixes: 7467147 ("crypto: starfive - Use dma for aes requests") Signed-off-by: Zilin Guan <zilin@seu.edu.cn> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent cd576c8 commit ccb679f

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

drivers/crypto/starfive/jh7110-aes.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,10 @@ static int starfive_aes_aead_do_one_req(struct crypto_engine *engine, void *areq
669669
return -ENOMEM;
670670

671671
if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, cryp->assoclen),
672-
rctx->adata, cryp->assoclen) != cryp->assoclen)
672+
rctx->adata, cryp->assoclen) != cryp->assoclen) {
673+
kfree(rctx->adata);
673674
return -EINVAL;
675+
}
674676
}
675677

676678
if (cryp->total_in)
@@ -681,8 +683,11 @@ static int starfive_aes_aead_do_one_req(struct crypto_engine *engine, void *areq
681683
ctx->rctx = rctx;
682684

683685
ret = starfive_aes_hw_init(ctx);
684-
if (ret)
686+
if (ret) {
687+
if (cryp->assoclen)
688+
kfree(rctx->adata);
685689
return ret;
690+
}
686691

687692
if (!cryp->assoclen)
688693
goto write_text;

0 commit comments

Comments
 (0)