Skip to contentSkip to conversation

crypto: Assorted bug fixes across crypto drivers

PATCH In review Lee Jones 3 participants 14 messages Nov 23 – Nov 24

Subsystems (13)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover crypto: Assorted bug fixes across crypto drivers

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

Cc "Christophe Leroy (CS GROUP)" <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc "Horia Geantă" <[email protected]>
Cc "Ritesh Harjani (IBM)" <[email protected]>
Cc Aleksander Jan Bajkowski <[email protected]>
Cc Alexandre Belloni <[email protected]>
Cc Antoine Tenart <[email protected]>
Cc Bartosz Golaszewski <[email protected]>
Cc Bharat Bhushan <[email protected]>
Cc bui duc phuc <[email protected]>
Cc Bulent Abali <[email protected]>
Cc Chen-Yu Tsai <[email protected]>
Cc Claudiu Beznea <[email protected]>
Cc Corentin Labbe <[email protected]>
Cc Dan Streetman <[email protected]>
Cc Evgeniy Polyakov <[email protected]>
Cc Haren Myneni <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Jernej Skrabec <[email protected]>
Cc Kim Phillips <[email protected]>
Cc Longfang Liu <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Meenakshi Aggarwal <[email protected]>
Cc Meng Yu <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Nicholas Piggin <[email protected]>
Cc Nicolas Ferre <[email protected]>
Cc Nicolas Royer <[email protected]>
Cc Pankaj Gupta <[email protected]>
Cc Pascal van Leeuwen <[email protected]>
Cc Paul Louvel <[email protected]>
Cc Robert Jennings <[email protected]>
Cc Sahil Malhotra <[email protected]>
Cc Samuel Holland <[email protected]>
Cc Seth Jennings <[email protected]>
Cc Shrikanth Hegde <[email protected]>
Cc Srujana Challa <[email protected]>
Cc Stanimir Varbanov <[email protected]>
Cc Steffen Trumtrar <[email protected]>
Cc Thorsten Blum <[email protected]>
Cc Tudor Ambarus <[email protected]>
Cc Weili Qian <[email protected]>
Cc Zaibo Xu <[email protected]>
Cc Zhiqi Song <[email protected]>

Lee Jones (10):
crypto: caam - Fix descriptor buffer overflow in caam_process_blob()
crypto: marvell - Fix AEAD key buffer overflow for SHA-384/512 HMAC
crypto: nx - Fix bounce buffer copy overflow in decompress()
crypto: sun8i-ss - Fix scatterlist array overflow for HMAC requests
crypto: hisilicon/hpre - Fix buffer overflow on odd ECDH input size
crypto: talitos - Remove duplicate IV unmap in AEAD encrypt path
crypto: safexcel - Check scatterlist errors before max() for in-place
crypto: qce - Pass mapped scatterlist count to qce_dma_prep_sgs()
crypto: qce - Terminate RX DMA channel if TX preparation fails
crypto: atmel-tdes - Update output IV before clearing busy flag

 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c  |  7 +++++--
 drivers/crypto/atmel-tdes.c                        |  7 +++++--
 drivers/crypto/caam/blob_gen.c                     | 12 ++++++++----
 drivers/crypto/hisilicon/hpre/hpre_crypto.c        |  5 +++--
 drivers/crypto/inside-secure/safexcel_cipher.c     |  7 +++----
 drivers/crypto/marvell/octeontx/otx_cptvf_algs.h   |  2 +-
 drivers/crypto/marvell/octeontx2/otx2_cptvf_algs.h |  2 +-
 drivers/crypto/nx/nx-842.c                         |  5 ++++-
 drivers/crypto/qce/dma.c                           |  8 ++++++--
 drivers/crypto/qce/sha.c                           |  9 +++++----
 drivers/crypto/talitos.c                           |  4 ----
 11 files changed, 41 insertions(+), 27 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH 1/10 crypto: caam - Fix descriptor buffer overflow in caam_process_blob()

Commit a703a4c2a328 ("KEYS: trusted: caam based protected key") switched caam_process_blob() from append_seq_in_ptr_intlen() and append_seq_out_ptr_intlen() to append_seq_in_ptr() and append_seq_out_ptr(), each of which emits an extra 32-bit extended length word, and added append_key(), append_fifo_store(), and append_jump() when encapsulating a protected key.

However, CAAM_BLOB_DESC_BYTES_MAX was not updated and still reserves only 44 or 52 bytes (for 32-bit or 64-bit DMA addresses), whereas caam_process_blob() now writes up to 60 bytes for standard blobs on 64-bit DMA platforms and up to 72 or 88 bytes when encapsulating a protected key, overflowing the kzalloc()-allocated job descriptor buffer into adjacent slab memory.

Update CAAM_BLOB_DESC_BYTES_MAX to account for the protected-key commands and the extended sequence pointer length words.

Fixes a703a4c2a328 ("KEYS: trusted: caam based protected key")
---
 drivers/crypto/caam/blob_gen.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/crypto/caam/blob_gen.c +8 -4
index c18dbac56493..b4fdc15dd0b6 100644
@@ -23,12 +23,16 @@
 #define CAAM_BLOB_DESC_BYTES_MAX					\
 	/* Command to initialize & stating length of descriptor */	\
 	(CAAM_CMD_SZ +							\
+	/* Commands to load plain key, store protected key and jump */	\
+	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
+	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
+	 CAAM_CMD_SZ +							\
 	/* Command to append the key-modifier + key-modifier data */	\
 	 CAAM_CMD_SZ + CAAM_BLOB_KEYMOD_LENGTH +			\
-	/* Command to include input key + pointer to the input key */	\
-	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
-	/* Command to include output key + pointer to the output key */	\
-	 CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
+	/* Command to include input key + pointer and length */		\
+	 2 * CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
+	/* Command to include output key + pointer and length */	\
+	 2 * CAAM_CMD_SZ + CAAM_PTR_SZ_MAX +				\
 	/* Command describing the operation to perform */		\
 	 CAAM_CMD_SZ)
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

PATCH 2/10 crypto: marvell - Fix AEAD key buffer overflow for SHA-384/512 HMAC

In reply to Lee Jones

Commit 311eea7e37c4 ("crypto: octeontx - Fix authenc setkey") and commit 7ccb750dcac8 ("crypto: octeontx2 - Fix authenc setkey") updated aead_hmac_init() in the OcteonTX and OcteonTX2 CPT VF drivers to hash authentication keys only when authkeylen exceeds the hash block size (128 bytes for SHA-384 and SHA-512), copying shorter keys directly into ctx->key followed by the encryption key.

However, OTX_CPT_MAX_HASH_KEY_SIZE and OTX2_CPT_MAX_HASH_KEY_SIZE are defined as 64 rather than 128, sizing ctx->key at only 96 bytes. An HMAC-SHA384 or HMAC-SHA512 AEAD key with an authentication key between 65 and 128 bytes writes up to 160 bytes into ctx->key, clobbering the adjacent hashalg, sdesc, ipad, and opad pointers in struct otx_cpt_aead_ctx and struct otx2_cpt_aead_ctx before writing through ctx->ipad.

Increase OTX_CPT_MAX_HASH_KEY_SIZE and OTX2_CPT_MAX_HASH_KEY_SIZE to 128 to accommodate a full SHA-384/512 block-sized HMAC key.

Fixes 311eea7e37c4 ("crypto: octeontx - Fix authenc setkey")
Fixes 7ccb750dcac8 ("crypto: octeontx2 - Fix authenc setkey")
---
 drivers/crypto/marvell/octeontx/otx_cptvf_algs.h   | 2 +-
 drivers/crypto/marvell/octeontx2/otx2_cptvf_algs.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/marvell/octeontx/otx_cptvf_algs.h +1 -1
index a50b5e2f8d00..29976126d5cf 100644
@@ -15,7 +15,7 @@
 #include "otx_cpt_common.h"
 
 #define OTX_CPT_MAX_ENC_KEY_SIZE    32
-#define OTX_CPT_MAX_HASH_KEY_SIZE   64
+#define OTX_CPT_MAX_HASH_KEY_SIZE   128
 #define OTX_CPT_MAX_KEY_SIZE (OTX_CPT_MAX_ENC_KEY_SIZE + \
 			      OTX_CPT_MAX_HASH_KEY_SIZE)
 enum otx_cpt_request_type {
drivers/crypto/marvell/octeontx2/otx2_cptvf_algs.h +1 -1
index d29f84f01cee..cbc590e9e020 100644
@@ -12,7 +12,7 @@
 #include "cn10k_cpt.h"
 
 #define OTX2_CPT_MAX_ENC_KEY_SIZE    32
-#define OTX2_CPT_MAX_HASH_KEY_SIZE   64
+#define OTX2_CPT_MAX_HASH_KEY_SIZE   128
 #define OTX2_CPT_MAX_KEY_SIZE (OTX2_CPT_MAX_ENC_KEY_SIZE + \
 			       OTX2_CPT_MAX_HASH_KEY_SIZE)
 enum otx2_cpt_request_type {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

PATCH 3/10 crypto: nx - Fix bounce buffer copy overflow in decompress()

In reply to Lee Jones

When decompress() falls back to software 842 decompression and the remaining destination capacity p->oremain is smaller than required_len, it redirects decompression into the bounce buffer ctx->dbounce with dlen set to BOUNCE_BUFFER_SIZE.

After sw842_decompress() completes and ignore bytes are subtracted, decompress() copies dlen bytes from ctx->dbounce into p->out via memcpy() before update_param() validates that dlen does not exceed p->oremain. A crafted compressed stream whose decompressed output exceeds p->oremain therefore overflows the caller's destination buffer by up to BOUNCE_BUFFER_SIZE bytes.

Validate that dlen does not exceed p->oremain and return -ENOSPC before copying out of ctx->dbounce.

Fixes ed70b479c2c0 ("crypto: nx - add hardware 842 crypto comp alg")
---
 drivers/crypto/nx/nx-842.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/nx/nx-842.c +4 -1
index b4404a138962..4f96c2b2397b 100644
@@ -427,8 +427,11 @@ static int decompress(struct nx842_crypto_ctx *ctx,
 	if (ignore)
 		pr_debug("ignoring last %x bytes\n", ignore);
 
-	if (dst == ctx->dbounce)
+	if (dst == ctx->dbounce) {
+		if (dlen > p->oremain)
+			return -ENOSPC;
 		memcpy(p->out, dst, dlen);
+	}
 
 	pr_debug("decompress slen %x padding %x dlen %x ignore %x\n",
 		 slen, padding, dlen, ignore);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#4 lore raw

Re: [PATCH 03/10] crypto: nx - Fix bounce buffer copy overflow in decompress()

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] crypto: nx: out-of-bounds write in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] crypto: nx: out-of-bounds write in compress() [1] --

commit 6d00d8097910c1ed1be4ad69177b9d0594c5365b
Author: Lee Jones <[email protected]>

crypto: nx - Fix bounce buffer copy overflow in decompress()

This commit validates that the decompressed length does not exceed the remaining destination buffer capacity before copying from ctx->dbounce into p->out in decompress().

Quoted 5 lines
drivers/crypto/nx/nx-842.c +0 -0
index b4404a1389621..4f96c2b2397be 100644

drivers/crypto/nx/nx-842.c static int decompress(struct nx842_crypto_ctx *ctx, · line 434

Quoted 9 lines
drivers/crypto/nx/nx-842.c +4 -1
@@ -427,8 +427,11 @@ static int decompress(struct nx842_crypto_ctx *ctx,
 	if (ignore)
 		pr_debug("ignoring last %x bytes\n", ignore);
 
-	if (dst == ctx->dbounce)
+	if (dst == ctx->dbounce) {
+		if (dlen > p->oremain)
+			return -ENOSPC;
 		memcpy(p->out, dst, dlen);
+	}

[Severity: Critical]
This problem wasn't introduced by this patch, but can compress() also overflow p->out when copying from ctx->dbounce [1]?

When compress() jumps to the nospc fallback to use ctx->dbounce, it sets dlen to min(p->oremain, BOUNCE_BUFFER_SIZE) and dskip to 0 without subtracting hdrsize:

drivers/crypto/nx/nx-842.c:compress() {
	...
	if (dlen < c->minimum) {
nospc:
		dst = ctx->dbounce;
		dlen = min(p->oremain, BOUNCE_BUFFER_SIZE);
		dlen = round_down(dlen, c->multiple);
		dskip = 0;
		pr_debug("using comp dbounce buffer, len %x\n", dlen);
	}
	...
	dskip += hdrsize;

	if (dst == ctx->dbounce)
		memcpy(p->out + dskip, dst, dlen);
	...
}

After compression completes, dskip is incremented by hdrsize and dlen bytes are copied to p->out + dskip. If the compressed output reaches the dlen limit, hdrsize + dlen exceeds p->oremain and memcpy() writes up to hdrsize bytes past the end of p->out before update_param() checks the remaining space.

[1] https://sashiko.dev/#/bug/linux-f0b43673-8815-4f13-a7d9-658940af36b2

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#5 lore raw

PATCH 4/10 crypto: sun8i-ss - Fix scatterlist array overflow for HMAC requests

In reply to Lee Jones

sun8i_ss_hash_need_fallback() reserves one hardware scatterlist slot out of MAX_SG (8) for the trailing hash padding buffer, allowing up to 7 input scatterlist entries.

However, for HMAC requests (tfmctx->keylen > 0), sun8i_ss_hash_run() also shifts the mapped source array up by one slot to insert the inner pad at index 0 and increments i before writing the trailing padding entry at rctx->t_src[i] and rctx->t_dst[i]. When an HMAC request contains 7 block-aligned scatterlist entries, i reaches 8 (MAX_SG), writing past the end of rctx->t_src and rctx->t_dst, corrupting rctx->method and rctx->flow, and indexing out of bounds into ss->flows.

Reserve an extra scatterlist slot in sun8i_ss_hash_need_fallback() when tfmctx->keylen is non-zero so 7-entry HMAC requests use the software fallback.

Fixes 801b7d572c0a ("crypto: sun8i-ss - add hmac(sha1)")
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +5 -2
index 56ba4ff6d5ff..c2e0c6520f84 100644
@@ -344,9 +344,11 @@ static int sun8i_ss_run_hash_task(struct sun8i_ss_dev *ss,
 static bool sun8i_ss_hash_need_fallback(struct ahash_request *areq)
 {
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
+	struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
 	struct ahash_alg *alg = crypto_ahash_alg(tfm);
 	struct sun8i_ss_alg_template *algt;
 	struct scatterlist *sg;
+	int max_sg;
 
 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash.base);
 
@@ -360,8 +362,9 @@ static bool sun8i_ss_hash_need_fallback(struct ahash_request *areq)
 		return true;
 	}
 
-	/* we need to reserve one SG for the padding one */
-	if (sg_nents(areq->src) > MAX_SG - 1) {
+	/* Reserve one SG for padding and, for HMAC, one for ipad */
+	max_sg = tfmctx->keylen ? MAX_SG - 2 : MAX_SG - 1;
+	if (sg_nents(areq->src) > max_sg) {
 		algt->stat_fb_sgnum++;
 		return true;
 	}
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#6 lore raw

PATCH 5/10 crypto: hisilicon/hpre - Fix buffer overflow on odd ECDH input size

In reply to Lee Jones

hpre_ecdh_src_data_init() validates the input public key length using ctx->key_sz - (len >> 1) < 0 before copying len bytes into the upper half (offset ctx->key_sz << 1) of a (ctx->key_sz << 2)-byte coherent DMA buffer.

Because len >> 1 truncates the least significant bit, an odd input length of (ctx->key_sz << 1) + 1 (such as 65 bytes for NIST P-256) evaluates shift to 0, bypasses the bounds check, and writes one byte past the end of the coherent DMA allocation in scatterwalk_map_and_copy(). A zero length when ctx->key_sz is unset similarly bypasses the check.

Reject zero, odd, or oversized lengths explicitly before computing the coordinate offset.

Fixes 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")
---
 drivers/crypto/hisilicon/hpre/hpre_crypto.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_crypto.c +3 -2
index 66a276786a7d..78ae987d338a 100644
@@ -1484,9 +1484,10 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
 	int shift;
 
 	/* Src_data include gx and gy. */
+	if (unlikely(!len || (len & 1) || len > (ctx->key_sz << 1)))
+		return -EINVAL;
+
 	shift = ctx->key_sz - (len >> 1);
-	if (unlikely(shift < 0))
-		return -EINVAL;
 
 	ptr = dma_alloc_coherent(dev, ctx->key_sz << 2, &dma, gfp);
 	if (unlikely(!ptr))
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer Reviewed-by on v1 5/10
#7 lore raw

Re: [PATCH 05/10] crypto: hisilicon/hpre - Fix buffer overflow on odd ECDH input size

Quoted 28 lines · Lee Jones

hpre_ecdh_src_data_init() validates the input public key length using ctx->key_sz - (len >> 1) < 0 before copying len bytes into the upper half (offset ctx->key_sz << 1) of a (ctx->key_sz << 2)-byte coherent DMA buffer.

Because len >> 1 truncates the least significant bit, an odd input length of (ctx->key_sz << 1) + 1 (such as 65 bytes for NIST P-256) evaluates shift to 0, bypasses the bounds check, and writes one byte past the end of the coherent DMA allocation in scatterwalk_map_and_copy(). A zero length when ctx->key_sz is unset similarly bypasses the check.

Reject zero, odd, or oversized lengths explicitly before computing the coordinate offset.

Fixes 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")
---
 drivers/crypto/hisilicon/hpre/hpre_crypto.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_crypto.c +0 -0
index 66a276786a7d..78ae987d338a 100644

drivers/crypto/hisilicon/hpre/hpre_crypto.c static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req, · line 1491

Quoted 10 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +3 -2
@@ -1484,9 +1484,10 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
 	int shift;
 
 	/* Src_data include gx and gy. */
+	if (unlikely(!len || (len & 1) || len > (ctx->key_sz << 1)))
+		return -EINVAL;
+
 	shift = ctx->key_sz - (len >> 1);
-	if (unlikely(shift < 0))
-		return -EINVAL;
 

Thanks!
Longfang.

Quoted 3 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +0 -0
 	ptr = dma_alloc_coherent(dev, ctx->key_sz << 2, &dma, gfp);
 	if (unlikely(!ptr))
author
#8 lore raw

PATCH 6/10 crypto: talitos - Remove duplicate IV unmap in AEAD encrypt path

In reply to Lee Jones

Commit 2e13ce0812c6 ("crypto: talitos - DMA map key in setkey()") moved key DMA mapping into setkey(), but inadvertently added an extra dma_unmap_single() call on edesc->iv_dma in ipsec_esp_encrypt_done().

Because ipsec_esp() populates civ_ptr with edesc->iv_dma and ipsec_esp_unmap() already unmaps civ_ptr via unmap_single_talitos_ptr(), calling dma_unmap_single() on edesc->iv_dma after ipsec_esp_unmap() unmaps the same IV DMA address twice on every completed AEAD encryption request.

Remove the duplicate dma_unmap_single() call and the now-unused local variables from ipsec_esp_encrypt_done().

Fixes 2e13ce0812c6 ("crypto: talitos - DMA map key in setkey()")
---
 drivers/crypto/talitos.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/crypto/talitos.c +0 -4
index 8a9f0d1e7df9..e9af242c7134 100644
@@ -1097,16 +1097,12 @@ static void ipsec_esp_encrypt_done(struct device *dev,
 				   int err)
 {
 	struct aead_request *areq = context;
-	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
-	unsigned int ivsize = crypto_aead_ivsize(authenc);
 	struct talitos_edesc *edesc;
 
 	edesc = container_of(desc, struct talitos_edesc, desc);
 
 	ipsec_esp_unmap(dev, edesc, areq, true);
 
-	dma_unmap_single(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
-
 	kfree(edesc);
 
 	aead_request_complete(areq, err);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#9 lore raw

Re: [PATCH 06/10] crypto: talitos - Remove duplicate IV unmap in AEAD encrypt path

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] crypto: talitos: DMA address…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] crypto: talitos: DMA address truncation in unmap_single_talitos_ptr() [1]
--

commit 7421990fbcd39641bb3ac45651a2ed7bcb79c461
Author: Lee Jones <[email protected]>

crypto: talitos - Remove duplicate IV unmap in AEAD encrypt path

This commit removes a duplicate dma_unmap_single() call on edesc->iv_dma in ipsec_esp_encrypt_done(), relying on ipsec_esp_unmap() to unmap civ_ptr via unmap_single_talitos_ptr().

Quoted 7 lines
drivers/crypto/talitos.c +0 -0
index c7d9ce2905be7..be7a281319450 100644
@@ -1097,16 +1097,12 @@ static void ipsec_esp_encrypt_done(struct device *dev,
 				   int err)
 {

drivers/crypto/talitos.c static void ipsec_esp_encrypt_done(struct device *dev, · line 1108

Quoted 10 lines
drivers/crypto/talitos.c +0 -3
 	struct aead_request *areq = context;
-	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
-	unsigned int ivsize = crypto_aead_ivsize(authenc);
 	struct talitos_edesc *edesc;
 
 	edesc = container_of(desc, struct talitos_edesc, desc);
 
 	ipsec_esp_unmap(dev, edesc, areq, true);
 
-	dma_unmap_single(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);

[Severity: Critical]
This problem wasn't introduced by this patch, but does unmap_single_talitos_ptr() truncate >32-bit DMA addresses [1] when ipsec_esp_unmap() unmaps civ_ptr?

While to_talitos_ptr() stores the upper bits of a >32-bit DMA address in ptr->eptr on non-SEC1 hardware, unmap_single_talitos_ptr() ignores ptr->eptr and only passes the lower 32 bits to dma_unmap_single():

drivers/crypto/talitos.c:unmap_single_talitos_ptr() {
	struct talitos_private *priv = dev_get_drvdata(dev);\
	bool is_sec1 = has_ftr_sec1(priv);

	dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
			 from_talitos_ptr_len(ptr, is_sec1), dir);
}

On systems with >4GB physical memory, this leaks the intended IOMMU mapping and unmaps an unrelated truncated DMA address, which can cause IOMMU faults and memory corruption.

[1] https://sashiko.dev/#/bug/linux-fc697335-3f0d-4823-8b99-a6069def8475

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#10 lore raw

PATCH 7/10 crypto: safexcel - Check scatterlist errors before max() for in-place

In reply to Lee Jones

In safexcel_send_req(), sg_nents_for_len() returns -EINVAL when a scatterlist is shorter than the required payload length. For in-place requests (src == dst), the driver combines sreq->nr_src and sreq->nr_dst using max() before checking for errors.

When an in-place AEAD encryption request provides a scatterlist large enough for totlen_src (sreq->nr_src > 0) but too short for the appended authentication tag in totlen_dst (sreq->nr_dst == -EINVAL), max() selects the positive sreq->nr_src and discards -EINVAL. The driver then maps only the undersized buffer while programming the engine token to emit totlen_dst bytes.

Check both sreq->nr_src and sreq->nr_dst for negative error values before combining them with max().

Fixes 19b347b32bb1 ("crypto: inside-secure - fix scatter/gather list to descriptor conversion")
---
 drivers/crypto/inside-secure/safexcel_cipher.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/crypto/inside-secure/safexcel_cipher.c +3 -4
index b031cb9652ec..e7816f6ddeaa 100644
@@ -736,14 +736,13 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 	queued = totlen_src;
 
 	if (src == dst) {
-		sreq->nr_src = max(sreq->nr_src, sreq->nr_dst);
-		sreq->nr_dst = sreq->nr_src;
-		if (unlikely((totlen_src || totlen_dst) &&
-		    (sreq->nr_src <= 0))) {
+		if (unlikely(sreq->nr_src < 0 || sreq->nr_dst < 0)) {
 			dev_err(priv->dev, "In-place buffer not large enough (need %d bytes)!",
 				max(totlen_src, totlen_dst));
 			return -EINVAL;
 		}
+		sreq->nr_src = max(sreq->nr_src, sreq->nr_dst);
+		sreq->nr_dst = sreq->nr_src;
 		if (sreq->nr_src > 0 &&
 		    !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL))
 			return -EIO;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#11 lore raw

PATCH 8/10 crypto: qce - Pass mapped scatterlist count to qce_dma_prep_sgs()

In reply to Lee Jones

In qce_ahash_async_req_handle(), dma_map_sg() maps req->src and returns the number of coalesced DMA segments in ret, which is immediately overwritten when mapping rctx->result_sg. The function then passes the unmapped segment count rctx->src_nents to qce_dma_prep_sgs().

When an IOMMU coalesces scatterlist entries so the mapped count is smaller than rctx->src_nents, dmaengine_prep_slave_sg() walks past the mapped entries and reads uninitialised DMA addresses and lengths from the tail of req->src, while dma_unmap_sg() still requires the original rctx->src_nents count.

Save the mapped segment count from dma_map_sg() in a dedicated src_nents variable and pass it to qce_dma_prep_sgs(), matching the skcipher path.

Fixes ec8f5d8f6f76 ("crypto: qce - Qualcomm crypto engine driver")
---
 drivers/crypto/qce/sha.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/crypto/qce/sha.c +5 -4
index b67d749db05f..56106f415db6 100644
@@ -77,7 +77,7 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
 	struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
 	struct qce_device *qce = tmpl->qce;
 	unsigned long flags = rctx->flags;
-	int ret;
+	int ret, src_nents;
 
 	if (IS_SHA_HMAC(flags)) {
 		rctx->authkey = ctx->authkey;
@@ -93,8 +93,9 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
 		return rctx->src_nents;
 	}
 
-	ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
-	if (!ret)
+	src_nents = dma_map_sg(qce->dev, req->src, rctx->src_nents,
+			       DMA_TO_DEVICE);
+	if (!src_nents)
 		return -EIO;
 
 	sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
@@ -105,7 +106,7 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
 		goto error_unmap_src;
 	}
 
-	ret = qce_dma_prep_sgs(&qce->dma, req->src, rctx->src_nents,
+	ret = qce_dma_prep_sgs(&qce->dma, req->src, src_nents,
 			       &rctx->result_sg, 1, qce_ahash_done, async_req);
 	if (ret)
 		goto error_unmap_dst;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#12 lore raw

PATCH 9/10 crypto: qce - Terminate RX DMA channel if TX preparation fails

In reply to Lee Jones

qce_dma_prep_sgs() prepares and submits the rxchan DMA descriptor via qce_dma_prep_sg() before preparing the txchan descriptor. If the subsequent txchan preparation fails, qce_dma_prep_sgs() returns an error while leaving the submitted rxchan descriptor queued in the DMA engine, and callers jump straight to unmapping their scatterlists without calling qce_dma_terminate_all().

On the next cryptographic request, qce_dma_issue_pending() flushes the stale rxchan descriptor against already-unmapped memory.

Terminate rxchan via dmaengine_terminate_async() when preparing txchan fails in qce_dma_prep_sgs().

Fixes ec8f5d8f6f76 ("crypto: qce - Qualcomm crypto engine driver")
---
 drivers/crypto/qce/dma.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/crypto/qce/dma.c +6 -2
index 68cafd4741ad..726e7fd3421e 100644
@@ -116,8 +116,12 @@ int qce_dma_prep_sgs(struct qce_dma_data *dma, struct scatterlist *rx_sg,
 	if (ret)
 		return ret;
 
-	return qce_dma_prep_sg(txchan, tx_sg, tx_nents, flags, DMA_DEV_TO_MEM,
-			       cb, cb_param);
+	ret = qce_dma_prep_sg(txchan, tx_sg, tx_nents, flags, DMA_DEV_TO_MEM,
+			      cb, cb_param);
+	if (ret)
+		dmaengine_terminate_async(rxchan);
+
+	return ret;
 }
 
 void qce_dma_issue_pending(struct qce_dma_data *dma)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#13 lore raw

PATCH 10/10 crypto: atmel-tdes - Update output IV before clearing busy flag

In reply to Lee Jones

In atmel_tdes_finish_req(), dd->flags clears TDES_FLAGS_BUSY without holding dd->lock and before calling atmel_tdes_set_iv_as_last_ciphertext_block().

As soon as TDES_FLAGS_BUSY is cleared, a concurrent atmel_tdes_handle_queue() caller on another CPU can dequeue the next request and overwrite dd->req (as well as modify dd->flags). When atmel_tdes_set_iv_as_last_ciphertext_block() then reads dd->req, it overwrites the newly dequeued request's IV with the previous request's ciphertext and leaves the completed request's output IV un-updated.

Update the output IV before clearing TDES_FLAGS_BUSY, and clear TDES_FLAGS_BUSY under dd->lock to serialise flag updates with atmel_tdes_handle_queue().

Fixes 61b0dd664fa1 ("crypto: atmel-tdes - Set the IV after {en,de}crypt")
---
 drivers/crypto/atmel-tdes.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/crypto/atmel-tdes.c +5 -2
index c86d06169ab9..392a2a3a8b85 100644
@@ -504,14 +504,17 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
 {
 	struct skcipher_request *req = dd->req;
 	struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
+	unsigned long flags;
 
 	clk_disable_unprepare(dd->iclk);
 
-	dd->flags &= ~TDES_FLAGS_BUSY;
-
 	if (!err && (rctx->mode & TDES_FLAGS_OPMODE_MASK) != TDES_FLAGS_ECB)
 		atmel_tdes_set_iv_as_last_ciphertext_block(dd);
 
+	spin_lock_irqsave(&dd->lock, flags);
+	dd->flags &= ~TDES_FLAGS_BUSY;
+	spin_unlock_irqrestore(&dd->lock, flags);
+
 	skcipher_request_complete(req, err);
 }
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog