Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH v4] crypto: rsassa-pkcs1 - Avoid cacheline sharing with underlying driver
@ 2026-07-30 11:20 Changwei Zou
  2026-07-30 11:35 ` Lukas Wunner
  0 siblings, 1 reply; 3+ messages in thread
From: Changwei Zou @ 2026-07-30 11:20 UTC (permalink / raw)
  To: lukas
  Cc: Martin.Kepplinger-Novakovic, changwei.zou, davem, herbert, ignat,
	linux-crypto, linux-kernel, martink

out_buf is used as a DMA buffer for the RSA verification operation.
If out_buf is not aligned to CRYPTO_DMA_ALIGN, cacheline sharing
problems (data corruption) would occur on CPUs with DMA-incoherent caches,
leading to -EKEYREJECTED.

Allocate out_buf separately via kmalloc(), which guarantees cacheline
alignment on architectures without fully coherent DMA.
This acts as a defensive measure, and avoids the need for an extra copy
in the underlying driver, which should check alignment before supplying
buffers to the hardware.

The intermittent error 'Key was rejected by service' on i.MX8 with CAAM
can be triggered when loading signed kernel modules.

    for i in $(seq 1 100); do
        sudo modprobe xfs 2>&1 && echo "SUCCESS on attempt $i" \
        && sudo rmmod xfs || echo "FAILED on attempt $i"
    done

Signed-off-by: Changwei Zou <changwei.zou@canonical.com>
---
 crypto/rsassa-pkcs1.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/crypto/rsassa-pkcs1.c b/crypto/rsassa-pkcs1.c
index 94fa5e9600e7..cbd76394ebf0 100644
--- a/crypto/rsassa-pkcs1.c
+++ b/crypto/rsassa-pkcs1.c
@@ -227,6 +227,7 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
 	struct scatterlist sg;
 	unsigned int dst_len;
 	unsigned int pos;
+	u8 *key_buf __free(kfree_sensitive) = NULL;
 	u8 *out_buf;
 	int err;
 
@@ -237,12 +238,12 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
 		return -EINVAL;
 
 	/* RFC 8017 sec 8.2.2 step 2 - RSA verification */
-	child_req = kmalloc(sizeof(*child_req) + child_reqsize + ctx->key_size,
-			    GFP_KERNEL);
-	if (!child_req)
+	child_req = kmalloc(sizeof(*child_req) + child_reqsize, GFP_KERNEL);
+	key_buf = kmalloc(ctx->key_size, GFP_KERNEL);
+	if (!child_req || !key_buf)
 		return -ENOMEM;
 
-	out_buf = (u8 *)(child_req + 1) + child_reqsize;
+	out_buf = key_buf;
 	memcpy(out_buf, src, slen);
 
 	crypto_init_wait(&cwait);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-30 12:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-30 11:20 [PATCH v4] crypto: rsassa-pkcs1 - Avoid cacheline sharing with underlying driver Changwei Zou
2026-07-30 11:35 ` Lukas Wunner
2026-07-30 12:06   ` Changwei Zou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox