linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run
@ 2021-07-26 15:25 Dongliang Mu
  2021-07-26 15:31 ` Dongliang Mu
  2021-08-02 18:27 ` Corentin Labbe
  0 siblings, 2 replies; 3+ messages in thread
From: Dongliang Mu @ 2021-07-26 15:25 UTC (permalink / raw)
  To: Corentin Labbe, Herbert Xu, David S. Miller, Maxime Ripard,
	Chen-Yu Tsai, Jernej Skrabec, Ard Biesheuvel, Xiang Chen,
	Eric Biggers, Dongliang Mu, Colin Ian King
  Cc: linux-crypto, linux-arm-kernel, linux-sunxi, linux-kernel

In sun8i_ss_hash_run, all the dma_mmap_sg/single will cause memory leak
due to no corresponding unmap operation if errors happen.

Fix this by adding error handling part for all the dma_mmap_sg/single.

Fixes: d9b45418a917 ("crypto: sun8i-ss - support hash algorithms")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
---
v1->v2: move crypto_finalize_hash_request to the end of function; move
the memcpy after the dma_mmap_sg/single functions.
v2->v3: remove some unrelated code changes; delete the fix of return value
since there is no corresponding handling code
v3->v4: change sun8i_ce to sun8i_ss
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index 3c073eb3db03..5448705e8ae1 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -368,14 +368,14 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
 		dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
 		err = -EINVAL;
-		goto theend;
+		goto err_result;
 	}
 
 	addr_res = dma_map_single(ss->dev, result, digestsize, DMA_FROM_DEVICE);
 	if (dma_mapping_error(ss->dev, addr_res)) {
 		dev_err(ss->dev, "DMA map dest\n");
 		err = -EINVAL;
-		goto theend;
+		goto err_unmap_sg;
 	}
 
 	len = areq->nbytes;
@@ -390,7 +390,7 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	if (len > 0) {
 		dev_err(ss->dev, "remaining len %d\n", len);
 		err = -EINVAL;
-		goto theend;
+		goto err_addr_res;
 	}
 
 	byte_count = areq->nbytes;
@@ -428,18 +428,19 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	if (dma_mapping_error(ss->dev, addr_pad)) {
 		dev_err(ss->dev, "DMA error on padding SG\n");
 		err = -EINVAL;
-		goto theend;
+		goto err_addr_res;
 	}
 
 	err = sun8i_ss_run_hash_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
 
 	dma_unmap_single(ss->dev, addr_pad, j * 4, DMA_TO_DEVICE);
+err_addr_res:
+	dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE);
+err_unmap_sg:
 	dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src),
 		     DMA_TO_DEVICE);
-	dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE);
-
 	memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
-theend:
+err_result:
 	kfree(pad);
 	kfree(result);
 	crypto_finalize_hash_request(engine, breq, err);
-- 
2.25.1


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

* Re: [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run
  2021-07-26 15:25 [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run Dongliang Mu
@ 2021-07-26 15:31 ` Dongliang Mu
  2021-08-02 18:27 ` Corentin Labbe
  1 sibling, 0 replies; 3+ messages in thread
From: Dongliang Mu @ 2021-07-26 15:31 UTC (permalink / raw)
  To: Corentin Labbe, Herbert Xu, David S. Miller, Maxime Ripard,
	Chen-Yu Tsai, Jernej Skrabec, Ard Biesheuvel, Xiang Chen,
	Eric Biggers, Dongliang Mu, Colin Ian King
  Cc: linux-crypto, linux-arm-kernel, linux-sunxi, linux-kernel

On Mon, Jul 26, 2021 at 11:25 PM Dongliang Mu <mudongliangabcd@gmail.com> wrote:
>
> In sun8i_ss_hash_run, all the dma_mmap_sg/single will cause memory leak
> due to no corresponding unmap operation if errors happen.
>
> Fix this by adding error handling part for all the dma_mmap_sg/single.
>
> Fixes: d9b45418a917 ("crypto: sun8i-ss - support hash algorithms")
> Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> ---
> v1->v2: move crypto_finalize_hash_request to the end of function; move
> the memcpy after the dma_mmap_sg/single functions.
> v2->v3: remove some unrelated code changes; delete the fix of return value
> since there is no corresponding handling code
> v3->v4: change sun8i_ce to sun8i_ss

From v3 to v4, I changed the title and commit message from sun8i_ce to
sun8i_ss because I suddenly realized that the code change below is for
sun8i_ss_hash_run, other than sun8i_ce_hash_run.

Furthermore, sun8i_ce is also prone to this issue. I've sent another
patch to fix it.

>  drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
> index 3c073eb3db03..5448705e8ae1 100644
> --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
> +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
> @@ -368,14 +368,14 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
>         if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
>                 dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
>                 err = -EINVAL;
> -               goto theend;
> +               goto err_result;
>         }
>
>         addr_res = dma_map_single(ss->dev, result, digestsize, DMA_FROM_DEVICE);
>         if (dma_mapping_error(ss->dev, addr_res)) {
>                 dev_err(ss->dev, "DMA map dest\n");
>                 err = -EINVAL;
> -               goto theend;
> +               goto err_unmap_sg;
>         }
>
>         len = areq->nbytes;
> @@ -390,7 +390,7 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
>         if (len > 0) {
>                 dev_err(ss->dev, "remaining len %d\n", len);
>                 err = -EINVAL;
> -               goto theend;
> +               goto err_addr_res;
>         }
>
>         byte_count = areq->nbytes;
> @@ -428,18 +428,19 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
>         if (dma_mapping_error(ss->dev, addr_pad)) {
>                 dev_err(ss->dev, "DMA error on padding SG\n");
>                 err = -EINVAL;
> -               goto theend;
> +               goto err_addr_res;
>         }
>
>         err = sun8i_ss_run_hash_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
>
>         dma_unmap_single(ss->dev, addr_pad, j * 4, DMA_TO_DEVICE);
> +err_addr_res:
> +       dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> +err_unmap_sg:
>         dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src),
>                      DMA_TO_DEVICE);
> -       dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> -
>         memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
> -theend:
> +err_result:
>         kfree(pad);
>         kfree(result);
>         crypto_finalize_hash_request(engine, breq, err);
> --
> 2.25.1
>

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

* Re: [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run
  2021-07-26 15:25 [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run Dongliang Mu
  2021-07-26 15:31 ` Dongliang Mu
@ 2021-08-02 18:27 ` Corentin Labbe
  1 sibling, 0 replies; 3+ messages in thread
From: Corentin Labbe @ 2021-08-02 18:27 UTC (permalink / raw)
  To: Dongliang Mu
  Cc: Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai,
	Jernej Skrabec, Ard Biesheuvel, Xiang Chen, Eric Biggers,
	Colin Ian King, linux-crypto, linux-arm-kernel, linux-sunxi,
	linux-kernel

Le Mon, Jul 26, 2021 at 11:25:21PM +0800, Dongliang Mu a écrit :
> In sun8i_ss_hash_run, all the dma_mmap_sg/single will cause memory leak
> due to no corresponding unmap operation if errors happen.
> 
> Fix this by adding error handling part for all the dma_mmap_sg/single.
> 

Hello

I have nearly the same comment than on your sun8i-ce patch, (copy result only if err===0 and wording).

Regards

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

end of thread, other threads:[~2021-08-02 18:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 15:25 [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run Dongliang Mu
2021-07-26 15:31 ` Dongliang Mu
2021-08-02 18:27 ` 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).