linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] crypto: sun8i-ss - Fix memory leak in sun8i_ss_prng_generate()
@ 2020-09-28 17:59 Gustavo A. R. Silva
  2020-10-02  7:11 ` Corentin Labbe
  0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2020-09-28 17:59 UTC (permalink / raw)
  To: Corentin Labbe, Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai
  Cc: linux-crypto, linux-arm-kernel, linux-kernel, Gustavo A. R. Silva

Set _err_ to the return error code -EFAULT before jumping to the new
label err_d, so resources for _d_ can be released before returning
from function sun8i_ss_prng_generate().

Addresses-Coverity-ID: 1497459 ("Resource leak")
Fixes: ac2614d721de ("crypto: sun8i-ss - Add support for the PRNG")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
index 08a1473b2145..0573f6289e8b 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
@@ -103,7 +103,8 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
 	dma_iv = dma_map_single(ss->dev, ctx->seed, ctx->slen, DMA_TO_DEVICE);
 	if (dma_mapping_error(ss->dev, dma_iv)) {
 		dev_err(ss->dev, "Cannot DMA MAP IV\n");
-		return -EFAULT;
+		err = -EFAULT;
+		goto err_d;
 	}
 
 	dma_dst = dma_map_single(ss->dev, d, todo, DMA_FROM_DEVICE);
@@ -160,7 +161,7 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
 	dma_unmap_single(ss->dev, dma_dst, todo, DMA_FROM_DEVICE);
 err_iv:
 	dma_unmap_single(ss->dev, dma_iv, ctx->slen, DMA_TO_DEVICE);
-
+err_d:
 	if (!err) {
 		memcpy(dst, d, dlen);
 		/* Update seed */
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH][next] crypto: sun8i-ss - Fix memory leak in sun8i_ss_prng_generate()
  2020-09-28 17:59 [PATCH][next] crypto: sun8i-ss - Fix memory leak in sun8i_ss_prng_generate() Gustavo A. R. Silva
@ 2020-10-02  7:11 ` Corentin Labbe
  0 siblings, 0 replies; 2+ messages in thread
From: Corentin Labbe @ 2020-10-02  7:11 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai,
	linux-crypto, linux-arm-kernel, linux-kernel

On Mon, Sep 28, 2020 at 12:59:45PM -0500, Gustavo A. R. Silva wrote:
> Set _err_ to the return error code -EFAULT before jumping to the new
> label err_d, so resources for _d_ can be released before returning
> from function sun8i_ss_prng_generate().
> 
> Addresses-Coverity-ID: 1497459 ("Resource leak")
> Fixes: ac2614d721de ("crypto: sun8i-ss - Add support for the PRNG")
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
> index 08a1473b2145..0573f6289e8b 100644
> --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
> +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c
> @@ -103,7 +103,8 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
>  	dma_iv = dma_map_single(ss->dev, ctx->seed, ctx->slen, DMA_TO_DEVICE);
>  	if (dma_mapping_error(ss->dev, dma_iv)) {
>  		dev_err(ss->dev, "Cannot DMA MAP IV\n");
> -		return -EFAULT;
> +		err = -EFAULT;
> +		goto err_d;
>  	}
>  
>  	dma_dst = dma_map_single(ss->dev, d, todo, DMA_FROM_DEVICE);
> @@ -160,7 +161,7 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
>  	dma_unmap_single(ss->dev, dma_dst, todo, DMA_FROM_DEVICE);
>  err_iv:
>  	dma_unmap_single(ss->dev, dma_iv, ctx->slen, DMA_TO_DEVICE);
> -
> +err_d:
>  	if (!err) {
>  		memcpy(dst, d, dlen);
>  		/* Update seed */
> -- 
> 2.27.0
> 

Hello

The label could be better placed just before the kfree.
In error case, there are no need to memzero the not used "d".

But a patch with it, was already sent to the ML.
Anyway if you fix it, you could add "Acked-by: Corentin Labbe <clabbe.montjoie@gmail.com>"

Regards

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-02  7:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-28 17:59 [PATCH][next] crypto: sun8i-ss - Fix memory leak in sun8i_ss_prng_generate() Gustavo A. R. Silva
2020-10-02  7:11 ` Corentin Labbe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).