summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohamad Raizudeen <raizudeen.kerneldev@gmail.com>2026-08-22 14:15:08 +0530
committerHerbert Xu <herbert@gondor.apana.org.au>2026-09-11 15:03:03 +1000
commit6c2373dd818b777d0a81eb02acebc03940552dcd (patch)
tree4e960c2298061f908f820dde09e6ec3bc64384d0
parentf07d34f2083a7a292abbc918e90209d5c9f7e2f3 (diff)
downloadlinux-next-6c2373dd818b777d0a81eb02acebc03940552dcd.tar.gz
linux-next-6c2373dd818b777d0a81eb02acebc03940552dcd.zip
crypto: amlogic - Fix DMA memory leak in cipher error path
A DMA memory leak occurs in meson_cipher() on the mapping error paths. The driver jumps to the end of the function without unmapping the previously mapped source scatterlist and key/IV buffer when the destination apping fails. Additionally, a memory leak occurs when a scatterlist mapping succeeds but the returned count exceeds the driver's MAXDESC limit. In this case, the driver rejects the mapping without unmapping it. The BIDIRECTIONAL mapping branch also lacks the required 'MAXDESC -3' upper bound check. Fix this by introducing proper error labels, error_src and error_keyiv and unmap resources immediately inside the calidation checks to ensure all successfully mapped resources are cleaned up before returning the error. Fixes: 48fe583fe541 ("crypto: amlogic - Add crypto accelerator for amlogic GXL") Signed-off-by: Mohamad Raizudeen <raizudeen.kerneldev@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--drivers/crypto/amlogic/amlogic-gxl-cipher.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
index 29048da6f50a..d78815b0547d 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
@@ -177,10 +177,13 @@ static int meson_cipher(struct skcipher_request *areq)
if (areq->src == areq->dst) {
nr_sgs = dma_map_sg(mc->dev, areq->src, sg_nents(areq->src),
DMA_BIDIRECTIONAL);
- if (!nr_sgs) {
- dev_err(mc->dev, "Invalid SG count %d\n", nr_sgs);
+ if (!nr_sgs || nr_sgs > MAXDESC - 3) {
+ dev_err(mc->dev, "Invalid BIDIR SG count %d\n", nr_sgs);
err = -EINVAL;
- goto theend;
+
+ if (nr_sgs)
+ dma_unmap_sg(mc->dev, areq->src, sg_nents(areq->src), DMA_BIDIRECTIONAL);
+ goto error_keyiv;
}
nr_sgd = nr_sgs;
} else {
@@ -189,14 +192,20 @@ static int meson_cipher(struct skcipher_request *areq)
if (!nr_sgs || nr_sgs > MAXDESC - 3) {
dev_err(mc->dev, "Invalid SG count %d\n", nr_sgs);
err = -EINVAL;
- goto theend;
+
+ if (nr_sgs)
+ dma_unmap_sg(mc->dev, areq->src, sg_nents(areq->src), DMA_TO_DEVICE);
+ goto error_keyiv;
}
nr_sgd = dma_map_sg(mc->dev, areq->dst, sg_nents(areq->dst),
DMA_FROM_DEVICE);
if (!nr_sgd || nr_sgd > MAXDESC - 3) {
dev_err(mc->dev, "Invalid SG count %d\n", nr_sgd);
err = -EINVAL;
- goto theend;
+
+ if (nr_sgd)
+ dma_unmap_sg(mc->dev, areq->dst, sg_nents(areq->dst), DMA_FROM_DEVICE);
+ goto error_src;
}
}
@@ -251,6 +260,12 @@ static int meson_cipher(struct skcipher_request *areq)
ivsize, 0);
}
}
+ goto theend;
+
+error_src:
+ dma_unmap_sg(mc->dev, areq->src, sg_nents(areq->src), DMA_TO_DEVICE);
+error_keyiv:
+ dma_unmap_single(mc->dev, phykeyiv, keyivlen, DMA_TO_DEVICE);
theend:
kfree_sensitive(bkeyiv);
kfree_sensitive(backup_iv);