[PATCH v2 1/2] crypto: caam - validate RSA scatterlist extents before mapping
From: Jeff Barnes
Date: Thu Nov 19 2026 - 15:00:11 EST
rsa_edesc_alloc() passes the result of sg_nents_for_len() directly to
dma_map_sg(). An undersized source or destination scatterlist therefore
supplies a negative entry count to the DMA API. The destination alignment
walk also consumes this unchecked count.
Check both extents before walking the destination, allocating a bounce
buffer, or acquiring any DMA mappings. Propagate negative errors and
reject an empty extent with -EINVAL instead of attempting a zero-entry
mapping.
Keep this validation separate from the in-place DMA ownership fix so
that it can be backported independently.
Fixes: 8c419778ab57 ("crypto: caam - add support for RSA algorithm")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Jeff Barnes <jeffbarnes@xxxxxxxxxxxxxxxxxxx>
---
drivers/crypto/caam/caampkc.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index 4f381b5010ab0..d81ef97861c0c 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -364,7 +364,12 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
src_nents = sg_nents_for_len(req_ctx->fixup_src,
req_ctx->fixup_src_len);
+ if (unlikely(src_nents <= 0))
+ return ERR_PTR(src_nents < 0 ? src_nents : -EINVAL);
+
dst_nents = sg_nents_for_len(req->dst, req->dst_len);
+ if (unlikely(dst_nents <= 0))
+ return ERR_PTR(dst_nents < 0 ? dst_nents : -EINVAL);
req_ctx->bounce_buf = NULL;
req_ctx->fixup_dst = req->dst;
--
2.43.0