Skip to contentSkip to conversation

crypto: caam - fix in-place RSA DMA mapping

PATCH New Jeff Barnes 1 participant 3 messages Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover crypto: caam - fix in-place RSA DMA mapping

This series fixes DMA ownership for in-place RSA requests in rsa-caam. Mapping the same scatterlist independently for source and destination can overwrite DMA metadata and cause ARM-LPAE/IOMMU unmap warnings.

The series is based on Changwei Zou's v6 error-path and destination bounce-buffer fixes:

https://lore.kernel.org/linux-crypto/[email protected]/

Patch 1 validates source and destination scatterlist extents before walking or mapping them. It is separate so it can be backported independently.

Patch 2 maps an aligned, exact source/destination alias once with DMA_BIDIRECTIONAL when the mapping produces a single DMA segment. Multi-segment aliases use Changwei's destination bounce-buffer infrastructure instead of being rejected.

The original alias relationship is recorded before stripping leading zeros. Requests whose effective source advances also use destination bouncing, avoiding overlapping streaming mappings through different scatterlist metadata.

Changes since the RFC:
- Split scatterlist extent validation into a separate patch.
- Build on Changwei's fixup_dst and bounce-buffer infrastructure.
- Replace multi-segment -EOPNOTSUPP rejection with destination bouncing.
- Handle aliases whose effective source advances during zero stripping.
- Add Fixes and stable CC tags.

Previous RFC:

https://lore.kernel.org/linux-crypto/[email protected]/

Testing:

A backport of the prerequisites and this series was tested on an arm64 system with CAAM behind an SMMUv2, a translated IOMMU domain, and DMA-API debugging enabled, running kernel 6.6.160.4.

All 12 RSA-2048 public-operation alias tests passed. These covered single-entry shared mapping, two- and three-entry bounce fallback, leading-zero stripping within and across entries, and unaligned buffers, each with direct and backlog-enabled submission.

No DMA-API, ARM-LPAE/IOMMU, or scatterlist-iterator warnings were observed. The test run also included an independent leading-zero iterator boundary fix, which is not included in this series.

Backlog-enabled submission does not establish that requests actually entered the backlog. Private RSA operations and noncoherent/dma-direct hardware were not covered by this test run.

GitHub Copilot assisted with implementing the agreed DMA/bounce-buffer design, developing regression tests, and drafting commit messages.

Review follow-up:
The multi-segment rejection and zero-length-input concerns raised by Sashiko are addressed by this series. The short-input SEC4/PDB padding accounting issue exists in the unpatched baseline and remains unfixed by this series. The detailed response to Herbert is here:

https://lore.kernel.org/linux-crypto/[email protected]/

Jeff Barnes (2):
crypto: caam - validate RSA scatterlist extents before mapping
crypto: caam - fix in-place RSA payload DMA mapping

 drivers/crypto/caam/caampkc.c | 89 ++++++++++++++++++++++++++---------
 drivers/crypto/caam/caampkc.h |  3 ++
 2 files changed, 70 insertions(+), 22 deletions(-)

base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
prerequisite-patch-id: de99c6374460d838f0474066325d03bea7a4f979
prerequisite-patch-id: 33fe41f9cfd4a8ee1afa1fc6e915a9747acd2f37

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 crypto: caam - validate RSA scatterlist extents before mapping

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")
---
 drivers/crypto/caam/caampkc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/crypto/caam/caampkc.c +5 -0
index 4f381b5010ab0..d81ef97861c0c 100644
@@ -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;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 crypto: caam - fix in-place RSA payload DMA mapping

In reply to Jeff Barnes

CAAM maps the effective RSA source DMA_TO_DEVICE and the destination DMA_FROM_DEVICE independently. When both use the same scatterlist, the second mapping can overwrite the DMA metadata needed to release the first. On an arm64 system with CAAM behind an SMMU this caused ARM-LPAE/IOMMU unmap warnings and RSA signature self-test failures.

For an aligned exact effective-head alias, map the entries covering the larger source/destination extent once with DMA_BIDIRECTIONAL. If this returns a single DMA segment, retain that mapping and release it once with the original entry count on completion or allocation failure.

If the shared mapping returns multiple DMA segments, release it before mapping either hardware view independently. Use the existing destination bounce buffer, mapping the source DMA_TO_DEVICE and the distinct bounce buffer DMA_FROM_DEVICE. This avoids separate bounded SEC4 views without rejecting requests that previously worked on dma-direct systems.

Detect the original req->src == req->dst relationship before stripping leading zeros. If stripping advances the effective source, force the destination bounce path even though the effective heads differ. This avoids overlapping streaming mappings through different SG metadata. Arbitrary overlapping buffers described by otherwise distinct original and effective SG heads are outside the scope of this fix.

Reuse the fixup_dst and bounce-buffer lifetime handling from Changwei's unaligned-destination fix. In particular, unmap the payload before copy-back, copy only the RSA output on successful completion, and retain the bounce buffer across job-ring retries and accepted backlog requests. Non-aliased requests retain their existing mapping directions.

Unlike the RFC, multi-segment aliases no longer return -EOPNOTSUPP.

---
 drivers/crypto/caam/caampkc.c | 84 ++++++++++++++++++++++++++---------
 drivers/crypto/caam/caampkc.h |  3 ++
 2 files changed, 65 insertions(+), 22 deletions(-)
drivers/crypto/caam/caampkc.c +62 -22
index d81ef97861c0c..bffd499164436 100644
@@ -51,8 +51,15 @@ static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
 {
 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
 
-	dma_unmap_sg(dev, req_ctx->fixup_dst, edesc->dst_nents, DMA_FROM_DEVICE);
-	dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents, DMA_TO_DEVICE);
+	if (edesc->shared_nents) {
+		dma_unmap_sg(dev, req_ctx->fixup_src, edesc->shared_nents,
+			     DMA_BIDIRECTIONAL);
+	} else {
+		dma_unmap_sg(dev, req_ctx->fixup_dst, edesc->dst_nents,
+			     DMA_FROM_DEVICE);
+		dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents,
+			     DMA_TO_DEVICE);
+	}
 
 	if (edesc->sec4_sg_bytes)
 		dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes,
@@ -330,14 +337,18 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
 	struct caam_rsa_key *key = &ctx->key;
 	struct rsa_edesc *edesc;
+	struct scatterlist *sg;
 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
 		       GFP_KERNEL : GFP_ATOMIC;
 	int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
 	int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
 	int src_nents, dst_nents;
+	int shared_nents = 0;
 	int mapped_src_nents, mapped_dst_nents;
 	unsigned int diff_size = 0;
-	int lzeros;
+	bool in_place = req->src == req->dst;
+	bool bounce;
+	int lzeros, i;
 
 	if (req->src_len > key->n_sz) {
 		/*
@@ -373,26 +384,48 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 
 	req_ctx->bounce_buf = NULL;
 	req_ctx->fixup_dst = req->dst;
-	if (req->dst_len > 0) {
-		struct scatterlist *sg;
-		int i;
-
-		for_each_sg(req->dst, sg, dst_nents, i) {
-			if (!sg_is_dma_aligned(sg)) {
-				unsigned int bounce_len =
-					ALIGN(req->dst_len,
-					      dma_get_cache_alignment());
-
-				req_ctx->bounce_buf = kzalloc(bounce_len, flags);
-				if (!req_ctx->bounce_buf)
-					return ERR_PTR(-ENOMEM);
-				sg_init_one(&req_ctx->dst, req_ctx->bounce_buf,
-					    bounce_len);
-				req_ctx->fixup_dst = &req_ctx->dst;
-				dst_nents = 1;
-				break;
-			}
+	bounce = in_place && req_ctx->fixup_src != req->src;
+	for_each_sg(req->dst, sg, dst_nents, i) {
+		if (!sg_is_dma_aligned(sg)) {
+			bounce = true;
+			break;
+		}
+	}
+
+	if (!bounce && req_ctx->fixup_src == req_ctx->fixup_dst) {
+		shared_nents = max(src_nents, dst_nents);
+		mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src,
+					      shared_nents, DMA_BIDIRECTIONAL);
+		if (unlikely(!mapped_src_nents)) {
+			dev_err(dev, "unable to map shared S/G\n");
+			goto bounce_fail;
+		}
+
+		if (mapped_src_nents == 1) {
+			mapped_dst_nents = mapped_src_nents;
+			goto alloc_edesc;
 		}
+
+		/*
+		 * Distinct DMA segments need independently bounded hardware
+		 * views. Bounce the destination instead of sharing SEC4 tables.
+		 */
+		dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
+			     DMA_BIDIRECTIONAL);
+		shared_nents = 0;
+		bounce = true;
+	}
+
+	if (bounce) {
+		unsigned int bounce_len = ALIGN(req->dst_len,
+						dma_get_cache_alignment());
+
+		req_ctx->bounce_buf = kzalloc(bounce_len, flags);
+		if (!req_ctx->bounce_buf)
+			goto bounce_fail;
+		sg_init_one(&req_ctx->dst, req_ctx->bounce_buf, bounce_len);
+		req_ctx->fixup_dst = &req_ctx->dst;
+		dst_nents = 1;
 	}
 
 	mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
@@ -408,6 +441,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 		goto src_fail;
 	}
 
+alloc_edesc:
 	if (!diff_size && mapped_src_nents == 1)
 		sec4_sg_len = 0; /* no need for an input hw s/g table */
 	else
@@ -442,6 +476,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 	/* Save nents for later use in Job Descriptor */
 	edesc->src_nents = src_nents;
 	edesc->dst_nents = dst_nents;
+	edesc->shared_nents = shared_nents;
 
 	req_ctx->edesc = edesc;
 
@@ -469,6 +504,11 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 sec4_sg_fail:
 	kfree(edesc);
 dst_fail:
+	if (shared_nents) {
+		dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
+			     DMA_BIDIRECTIONAL);
+		goto bounce_fail;
+	}
 	dma_unmap_sg(dev, req_ctx->fixup_dst, dst_nents, DMA_FROM_DEVICE);
 src_fail:
 	dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
drivers/crypto/caam/caampkc.h +3 -0
index 8810bcc435ccd..5ec93d8973347 100644
@@ -126,6 +126,8 @@ struct caam_rsa_req_ctx {
  * rsa_edesc - s/w-extended rsa descriptor
  * @src_nents     : number of segments in input s/w scatterlist
  * @dst_nents     : number of segments in output s/w scatterlist
+ * @shared_nents  : original shared DMA_BIDIRECTIONAL map/unmap count,
+ *                 or zero for independent source and destination mappings
  * @mapped_src_nents: number of segments in input h/w link table
  * @mapped_dst_nents: number of segments in output h/w link table
  * @sec4_sg_bytes : length of h/w link table
@@ -138,6 +140,7 @@ struct caam_rsa_req_ctx {
 struct rsa_edesc {
 	int src_nents;
 	int dst_nents;
+	int shared_nents;
 	int mapped_src_nents;
 	int mapped_dst_nents;
 	int sec4_sg_bytes;
Signature · 1 line
2.43.0