[PATCH v2] crypto: amlogic: Fix DMA memory leak in cipher error path

From: Mohamad Raizudeen

Date: Sat Aug 22 2026 - 04:45:43 EST


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@xxxxxxxxx>
---
Changes in v2:
- Add missing '> MAXDESC - 3' upper bound check for the BIDIRECTIONAL
scatterlist mapping branch.
- Unmap scatterlists immediately inside the 'if' block when the count
exceeds MAXDESC - 3, because dma_map_sg() actually succeeds in this
case and the mapping must be undone.

drivers/crypto/amlogic/amlogic-gxl-cipher.c | 25 ++++++++++++++++-----
1 file 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..39b44ec89997 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);
--
2.53.0