[PATCH 05/10] crypto: hisilicon/hpre - Fix buffer overflow on odd ECDH input size
From: Lee Jones
Date: Mon Nov 23 2026 - 10:13:03 EST
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")
Closes: https://sashiko.dev/#/bug/linux-42c6cdd1-7f7e-4071-bb2f-a4db5935267e
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/crypto/hisilicon/hpre/hpre_crypto.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
index 66a276786a7d..78ae987d338a 100644
--- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c
+++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
@@ -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))
--
2.56.1.750.gf70dc682cf-goog