mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stephane Debusne <stephane.debusne@cynove.com>
To: herbert@gondor.apana.org.au, lukas@wunner.de
Cc: ignat@cloudflare.com, davem@davemloft.net, horia.geanta@nxp.com,
	pankaj.gupta@nxp.com, gaurav.jain@nxp.com,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Stephane Debusne <stephane.debusne@cynove.com>,
	stable@vger.kernel.org
Subject: [PATCH 1/2] crypto: akcipher - use a separate buffer in the sync helpers
Date: Fri, 16 Oct 2026 11:03:32 +0200	[thread overview]
Message-ID: <20261016090333.2247764-2-stephane.debusne@cynove.com> (raw)
In-Reply-To: <20261016090333.2247764-1-stephane.debusne@cynove.com>

crypto_akcipher_sync_prep() places the data buffer right after the
request context, in the same allocation, and passes it to the driver
in place (src == dst).

On non-coherent DMA, the start of this buffer may share a cache line
with the end of the request context. A DMA-based driver such as the
CAAM (caampkc) writes req_ctx->edesc and req_ctx->akcipher_op_done
after dma_map_sg() of the destination, which dirties that line. On
arm64, dma_unmap_sg(DMA_FROM_DEVICE) cleans and invalidates partial
lines (dc civac), so the stale CPU copy is written back over the result
written by the device.

crypto_akcipher_sync_encrypt() and crypto_akcipher_sync_decrypt() are
used by rsassa_pkcs1_sign() and by software_key_eds_op() (keyctl
pkey_encrypt and pkey_decrypt), so RSA signing and encryption through
rsa-caam are affected.

On i.MX8M Mini (CAAM era 9, rsa-caam, 6.18), the buffer starts at
offset 40 of a 64-byte line shared with the end of the request context,
and crypto_akcipher_sync_encrypt() returns a result whose first 24
bytes still hold the input in 199 of 200 calls.

Allocate the data buffer separately, so that it is DMA-safe like any
kmalloc() buffer.

Fixes: addde1f2c966 ("crypto: akcipher - Add sync interface without SG lists")
Cc: stable@vger.kernel.org
Signed-off-by: Stephane Debusne <stephane.debusne@cynove.com>
---
 crypto/akcipher.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/crypto/akcipher.c b/crypto/akcipher.c
index 630bb1973..2381ed43b 100644
--- a/crypto/akcipher.c
+++ b/crypto/akcipher.c
@@ -170,23 +170,29 @@ static int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data)
 	struct akcipher_request *req;
 	struct scatterlist *sg;
 	unsigned int mlen;
-	unsigned int len;
 	u8 *buf;
 
 	mlen = max(data->slen, data->dlen);
 
-	len = sizeof(*req) + reqsize + mlen;
-	if (len < mlen)
-		return -EOVERFLOW;
-
-	req = kzalloc(len, GFP_KERNEL);
+	req = kzalloc(sizeof(*req) + reqsize, GFP_KERNEL);
 	if (!req)
 		return -ENOMEM;
 
+	/*
+	 * The data buffer is mapped for DMA by hardware drivers. Keep it in
+	 * its own allocation: placed right after the request context, it
+	 * may share a cache line with fields the driver writes while the
+	 * buffer is mapped, which corrupts the result on non-coherent DMA.
+	 */
+	buf = kzalloc(mlen, GFP_KERNEL);
+	if (!buf) {
+		kfree(req);
+		return -ENOMEM;
+	}
+
 	data->req = req;
 	akcipher_request_set_tfm(req, data->tfm);
 
-	buf = (u8 *)(req + 1) + reqsize;
 	data->buf = buf;
 	memcpy(buf, data->src, data->slen);
 
@@ -207,6 +213,7 @@ static int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data,
 	err = crypto_wait_req(err, &data->cwait);
 	memcpy(data->dst, data->buf, data->dlen);
 	data->dlen = data->req->dst_len;
+	kfree_sensitive(data->buf);
 	kfree_sensitive(data->req);
 	return err;
 }
-- 
2.43.0


  reply	other threads:[~2026-10-16  9:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16  9:03 [PATCH 0/2] crypto: fix RSA results corrupted by non-coherent DMA Stephane Debusne
2026-10-16  9:03 ` Stephane Debusne [this message]
2026-10-16  9:03 ` [PATCH 2/2] crypto: rsassa-pkcs1 - use a separate buffer for the RSA output Stephane Debusne
2026-10-16 22:14 ` [PATCH 0/2] crypto: fix RSA results corrupted by non-coherent DMA Jeff Barnes
2026-10-17  8:23   ` Stephane Debusne
2026-10-17 10:39 ` Stephane Debusne

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261016090333.2247764-2-stephane.debusne@cynove.com \
    --to=stephane.debusne@cynove.com \
    --cc=davem@davemloft.net \
    --cc=gaurav.jain@nxp.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=horia.geanta@nxp.com \
    --cc=ignat@cloudflare.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=pankaj.gupta@nxp.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®