crypto: sun8i-ce: fix memory leak and return value of sun8i_ce_hash_run
PATCH v2 1/1 crypto: sun8i-ce: fix memory leak and return value of sun8i_ce_hash_run
This patch fixes some memory leak caused by dma_mmap_sg/single in the error handling code. In addition, it fixes the return value when errors related with dma_mmap_sg/single occur.
---v1->v2: move crypto_finalize_hash_request to the end of function; move the memcpy after the dma_mmap_sg/single functions.
.../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +14 -14
@@ -324,11 +324,10 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)struct sun8i_ss_alg_template *algt;struct sun8i_ss_dev *ss;struct scatterlist *sg; -int nr_sgs, err, digestsize; +int j, i, todo, nr_sgs, digestsize, err;unsigned int len;u64 fill, min_fill, byte_count;void *pad, *result; -int j, i, todo;__be64 *bebits;__le64 *lebits;dma_addr_t addr_res, addr_pad;
@@ -368,14 +367,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 +389,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;
@@ -421,27 +420,28 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) } addr_pad =dma_map_single (ss->dev, pad, j *4 ,DMA_TO_DEVICE ); - rctx->t_src[i].addr = addr_pad; - rctx->t_src[i].len = j; - rctx->t_dst[i].addr = addr_res; - rctx->t_dst[i].len = digestsize /4 ;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; } + rctx->t_src[i].addr = addr_pad; + rctx->t_src[i].len = j; + rctx->t_dst[i].addr = addr_res; + rctx->t_dst[i].len = digestsize /4 ; 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); -return 0 ; +return err; }
Signature · 1 line
2.25.1
Corentin Labbe
maintainerQuoted 4 lines · Dongliang Mu
This patch fixes some memory leak caused by dma_mmap_sg/single in the error handling code. In addition, it fixes the return value when errors related with dma_mmap_sg/single occur.
Hello
"some" is too imprecise, exactly which allocation is leaked ?
In fact it will be easier to say that all dma_map_xx are leaked, I just saw that my code is really bad.
Furthermore, you should have two fixes, one for the memory leak, one for the error code.
But for the error code, I am not sure it is needed, error code is already given to user via crypto_finalize_hash_request(). The "return 0" is for crypto/crypto_engine API, returning an error will not change anything since we dont have retry_support.
So I propose you to focus on dma_map_xxx() fix patch.
Quoted 1 line
Reported-by Dongliang Mu <[email protected]>
"Reported-by:" is for credit when someone else give you informations.
Quoted 1 line
Fixes 732b764099f65 ("crypto: sun8i-ce - fix two error path's memory leak")
This is not the patch which introduces the problem, the problem is from the beginning ("crypto: sun8i-ss - support hash algorithms")
Quoted 12 lines
Signed-off-by Dongliang Mu <[email protected]>---
v1->v2: movecrypto_finalize_hash_requestto the end of function; move the memcpy after the dma_mmap_sg/single functions..../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-)drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +0
-0
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) · line 331
Quoted 9 lines
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +1
-2@@ -324,11 +324,10 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)struct sun8i_ss_alg_template *algt;struct sun8i_ss_dev *ss;struct scatterlist *sg; -int nr_sgs, err, digestsize; +int j, i, todo, nr_sgs, digestsize, err;unsigned int len;u64 fill, min_fill, byte_count;void *pad, *result; -int j, i, todo;
This is a change unrelated with the subject.
Quoted 37 lines
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +3
-7__be64 *bebits;__le64 *lebits;dma_addr_t addr_res, addr_pad;@@ -368,14 +367,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 +389,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;@@ -421,27 +420,28 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) } addr_pad =dma_map_single (ss->dev, pad, j *4 ,DMA_TO_DEVICE ); - rctx->t_src[i].addr = addr_pad; - rctx->t_src[i].len = j; - rctx->t_dst[i].addr = addr_res; - rctx->t_dst[i].len = digestsize /4 ;
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) · line 431
Quoted 10 lines
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +5
-1if (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; } + rctx->t_src[i].addr = addr_pad; + rctx->t_src[i].len = j; + rctx->t_dst[i].addr = addr_res; + rctx->t_dst[i].len = digestsize /4 ;
This is right, it is useless to set addr_pad if it is "bad". But this is a change unrelated with the subject.
Regards