LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run
@ 2021-07-26 15:27 Dongliang Mu
2021-08-02 18:25 ` Corentin Labbe
0 siblings, 1 reply; 4+ messages in thread
From: Dongliang Mu @ 2021-07-26 15:27 UTC (permalink / raw)
To: Corentin Labbe, Herbert Xu, David S. Miller, Maxime Ripard,
Chen-Yu Tsai, Jernej Skrabec, Ard Biesheuvel, Jonathan Corbet,
Eric Biggers, Xiang Chen, Mauro Carvalho Chehab
Cc: Dongliang Mu, Corentin Labbe, Jason A. Donenfeld, linux-crypto,
linux-arm-kernel, linux-sunxi, linux-kernel
In sun8i_ce_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: 56f6d5aee88d ("crypto: sun8i-ce - support hash algorithms")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
---
.../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 28 +++++++++----------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
index 88194718a806..d454ad99deee 100644
--- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
@@ -286,16 +286,14 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
/* the padding could be up to two block. */
buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
- if (!buf) {
- err = -ENOMEM;
- goto theend;
- }
+ if (!buf)
+ return -ENOMEM;
bf = (__le32 *)buf;
result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
if (!result) {
- err = -ENOMEM;
- goto theend;
+ kfree(buf);
+ return -ENOMEM;
}
flow = rctx->flow;
@@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
err = -EINVAL;
- goto theend;
+ goto err_result;
}
len = areq->nbytes;
@@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
if (len > 0) {
dev_err(ce->dev, "remaining len %d\n", len);
err = -EINVAL;
- goto theend;
+ goto err_unmap_sg;
}
addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE);
cet->t_dst[0].addr = cpu_to_le32(addr_res);
@@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
if (dma_mapping_error(ce->dev, addr_res)) {
dev_err(ce->dev, "DMA map dest\n");
err = -EINVAL;
- goto theend;
+ goto err_unmap_sg;
}
byte_count = areq->nbytes;
@@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
if (dma_mapping_error(ce->dev, addr_pad)) {
dev_err(ce->dev, "DMA error on padding SG\n");
err = -EINVAL;
- goto theend;
+ goto err_addr_res;
}
if (ce->variant->hash_t_dlen_in_bits)
@@ -405,15 +403,15 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE);
+err_addr_res:
+ dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
+err_unmap_sg:
dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
DMA_TO_DEVICE);
- dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
-
-
memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
-theend:
- kfree(buf);
+err_result:
kfree(result);
+ kfree(buf);
crypto_finalize_hash_request(engine, breq, err);
return 0;
}
--
2.25.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run
2021-07-26 15:27 [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run Dongliang Mu
@ 2021-08-02 18:25 ` Corentin Labbe
2021-08-03 5:57 ` Dongliang Mu
0 siblings, 1 reply; 4+ messages in thread
From: Corentin Labbe @ 2021-08-02 18:25 UTC (permalink / raw)
To: Dongliang Mu
Cc: Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai,
Jernej Skrabec, Ard Biesheuvel, Jonathan Corbet, Eric Biggers,
Xiang Chen, Mauro Carvalho Chehab, Corentin Labbe,
Jason A. Donenfeld, linux-crypto, linux-arm-kernel, linux-sunxi,
linux-kernel
Le Mon, Jul 26, 2021 at 11:27:12PM +0800, Dongliang Mu a écrit :
> In sun8i_ce_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.
>
I think it could be better worded, error handling is already there (but bad).
> Fixes: 56f6d5aee88d ("crypto: sun8i-ce - support hash algorithms")
> Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> ---
> .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 28 +++++++++----------
> 1 file changed, 13 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> index 88194718a806..d454ad99deee 100644
> --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> @@ -286,16 +286,14 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
>
> /* the padding could be up to two block. */
> buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
> - if (!buf) {
> - err = -ENOMEM;
> - goto theend;
Please keep all goto error for being consistent.
> - }
> + if (!buf)
> + return -ENOMEM;
> bf = (__le32 *)buf;
>
> result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
> if (!result) {
> - err = -ENOMEM;
> - goto theend;
> + kfree(buf);
> + return -ENOMEM;
> }
>
> flow = rctx->flow;
> @@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
> dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
> err = -EINVAL;
> - goto theend;
> + goto err_result;
> }
>
> len = areq->nbytes;
> @@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> if (len > 0) {
> dev_err(ce->dev, "remaining len %d\n", len);
> err = -EINVAL;
> - goto theend;
> + goto err_unmap_sg;
> }
> addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE);
> cet->t_dst[0].addr = cpu_to_le32(addr_res);
> @@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> if (dma_mapping_error(ce->dev, addr_res)) {
> dev_err(ce->dev, "DMA map dest\n");
> err = -EINVAL;
> - goto theend;
> + goto err_unmap_sg;
> }
>
> byte_count = areq->nbytes;
> @@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> if (dma_mapping_error(ce->dev, addr_pad)) {
> dev_err(ce->dev, "DMA error on padding SG\n");
> err = -EINVAL;
> - goto theend;
> + goto err_addr_res;
> }
>
> if (ce->variant->hash_t_dlen_in_bits)
> @@ -405,15 +403,15 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
>
> dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE);
> +err_addr_res:
> + dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> +err_unmap_sg:
> dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
> DMA_TO_DEVICE);
> - dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> -
> -
> memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
The result should be copied only when everything is ok. Please add a "if (!err)"
Thanks for your work
Regards
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run
2021-08-02 18:25 ` Corentin Labbe
@ 2021-08-03 5:57 ` Dongliang Mu
2021-08-03 6:06 ` Dongliang Mu
0 siblings, 1 reply; 4+ messages in thread
From: Dongliang Mu @ 2021-08-03 5:57 UTC (permalink / raw)
To: Corentin Labbe
Cc: Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai,
Jernej Skrabec, Ard Biesheuvel, Jonathan Corbet, Eric Biggers,
Xiang Chen, Mauro Carvalho Chehab, Corentin Labbe,
Jason A. Donenfeld, linux-crypto, linux-arm-kernel, linux-sunxi,
linux-kernel
On Tue, Aug 3, 2021 at 2:25 AM Corentin Labbe <clabbe.montjoie@gmail.com> wrote:
>
> Le Mon, Jul 26, 2021 at 11:27:12PM +0800, Dongliang Mu a écrit :
> > In sun8i_ce_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.
> >
>
> I think it could be better worded, error handling is already there (but bad).
Sure. How about "Fix this by freeing the objects allocated by
dma_mmap_sg/single when errors occur."?
>
> > Fixes: 56f6d5aee88d ("crypto: sun8i-ce - support hash algorithms")
> > Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> > ---
> > .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 28 +++++++++----------
> > 1 file changed, 13 insertions(+), 15 deletions(-)
> >
> > diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > index 88194718a806..d454ad99deee 100644
> > --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > @@ -286,16 +286,14 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> >
> > /* the padding could be up to two block. */
> > buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
> > - if (!buf) {
> > - err = -ENOMEM;
> > - goto theend;
>
> Please keep all goto error for being consistent.
OK, no problem.
BTW, usually for the 1st malloc failure, I prefer returning with errno directly.
>
> > - }
> > + if (!buf)
> > + return -ENOMEM;
> > bf = (__le32 *)buf;
> >
> > result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
> > if (!result) {
> > - err = -ENOMEM;
> > - goto theend;
> > + kfree(buf);
> > + return -ENOMEM;
> > }
> >
> > flow = rctx->flow;
> > @@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
> > dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
> > err = -EINVAL;
> > - goto theend;
> > + goto err_result;
> > }
> >
> > len = areq->nbytes;
> > @@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > if (len > 0) {
> > dev_err(ce->dev, "remaining len %d\n", len);
> > err = -EINVAL;
> > - goto theend;
> > + goto err_unmap_sg;
> > }
> > addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE);
> > cet->t_dst[0].addr = cpu_to_le32(addr_res);
> > @@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > if (dma_mapping_error(ce->dev, addr_res)) {
> > dev_err(ce->dev, "DMA map dest\n");
> > err = -EINVAL;
> > - goto theend;
> > + goto err_unmap_sg;
> > }
> >
> > byte_count = areq->nbytes;
> > @@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > if (dma_mapping_error(ce->dev, addr_pad)) {
> > dev_err(ce->dev, "DMA error on padding SG\n");
> > err = -EINVAL;
> > - goto theend;
> > + goto err_addr_res;
> > }
> >
> > if (ce->variant->hash_t_dlen_in_bits)
> > @@ -405,15 +403,15 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
> >
> > dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE);
> > +err_addr_res:
> > + dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> > +err_unmap_sg:
> > dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
> > DMA_TO_DEVICE);
> > - dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> > -
> > -
> > memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
>
> The result should be copied only when everything is ok. Please add a "if (!err)"
Sure.
>
> Thanks for your work
> Regards
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run
2021-08-03 5:57 ` Dongliang Mu
@ 2021-08-03 6:06 ` Dongliang Mu
0 siblings, 0 replies; 4+ messages in thread
From: Dongliang Mu @ 2021-08-03 6:06 UTC (permalink / raw)
To: Corentin Labbe
Cc: Herbert Xu, David S. Miller, Maxime Ripard, Chen-Yu Tsai,
Jernej Skrabec, Ard Biesheuvel, Jonathan Corbet, Eric Biggers,
Xiang Chen, Mauro Carvalho Chehab, Corentin Labbe,
Jason A. Donenfeld, linux-crypto, linux-arm-kernel, linux-sunxi,
linux-kernel
On Tue, Aug 3, 2021 at 1:57 PM Dongliang Mu <mudongliangabcd@gmail.com> wrote:
>
> On Tue, Aug 3, 2021 at 2:25 AM Corentin Labbe <clabbe.montjoie@gmail.com> wrote:
> >
> > Le Mon, Jul 26, 2021 at 11:27:12PM +0800, Dongliang Mu a écrit :
> > > In sun8i_ce_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.
> > >
> >
> > I think it could be better worded, error handling is already there (but bad).
>
> Sure. How about "Fix this by freeing the objects allocated by
> dma_mmap_sg/single when errors occur."?
>
> >
> > > Fixes: 56f6d5aee88d ("crypto: sun8i-ce - support hash algorithms")
> > > Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> > > ---
> > > .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 28 +++++++++----------
> > > 1 file changed, 13 insertions(+), 15 deletions(-)
> > >
> > > diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > > index 88194718a806..d454ad99deee 100644
> > > --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > > +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c
> > > @@ -286,16 +286,14 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > >
> > > /* the padding could be up to two block. */
> > > buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
> > > - if (!buf) {
> > > - err = -ENOMEM;
> > > - goto theend;
> >
> > Please keep all goto error for being consistent.
>
> OK, no problem.
>
> BTW, usually for the 1st malloc failure, I prefer returning with errno directly.
For this case, as crypto_finalize_hash_request must be executed, so
directly return is incorrect. I will add a goto label before
crypto_finalize_hash_request.
>
> >
> > > - }
> > > + if (!buf)
> > > + return -ENOMEM;
> > > bf = (__le32 *)buf;
> > >
> > > result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
> > > if (!result) {
> > > - err = -ENOMEM;
> > > - goto theend;
> > > + kfree(buf);
> > > + return -ENOMEM;
> > > }
> > >
> > > flow = rctx->flow;
> > > @@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > > if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
> > > dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
> > > err = -EINVAL;
> > > - goto theend;
> > > + goto err_result;
> > > }
> > >
> > > len = areq->nbytes;
> > > @@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > > if (len > 0) {
> > > dev_err(ce->dev, "remaining len %d\n", len);
> > > err = -EINVAL;
> > > - goto theend;
> > > + goto err_unmap_sg;
> > > }
> > > addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE);
> > > cet->t_dst[0].addr = cpu_to_le32(addr_res);
> > > @@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > > if (dma_mapping_error(ce->dev, addr_res)) {
> > > dev_err(ce->dev, "DMA map dest\n");
> > > err = -EINVAL;
> > > - goto theend;
> > > + goto err_unmap_sg;
> > > }
> > >
> > > byte_count = areq->nbytes;
> > > @@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > > if (dma_mapping_error(ce->dev, addr_pad)) {
> > > dev_err(ce->dev, "DMA error on padding SG\n");
> > > err = -EINVAL;
> > > - goto theend;
> > > + goto err_addr_res;
> > > }
> > >
> > > if (ce->variant->hash_t_dlen_in_bits)
> > > @@ -405,15 +403,15 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
> > > err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
> > >
> > > dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE);
> > > +err_addr_res:
> > > + dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> > > +err_unmap_sg:
> > > dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
> > > DMA_TO_DEVICE);
> > > - dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
> > > -
> > > -
> > > memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
> >
> > The result should be copied only when everything is ok. Please add a "if (!err)"
>
> Sure.
>
> >
> > Thanks for your work
> > Regards
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-08-03 6:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 15:27 [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run Dongliang Mu
2021-08-02 18:25 ` Corentin Labbe
2021-08-03 5:57 ` Dongliang Mu
2021-08-03 6:06 ` Dongliang Mu
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).