mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ryan Prinz <rc4d5a@protonmail.com>
To: Zhiqi Song <songzhiqi1@huawei.com>,
	Longfang Liu <liulongfang@huawei.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ryan Prinz <rc4d5a@protonmail.com>
Subject: [PATCH 2/2] crypto: hisilicon/hpre - fix heap overflow on oversized ECDH public key
Date: Tue, 24 Nov 2026 09:26:30 +0000	[thread overview]
Message-ID: <20261124092546.1712228-2-rc4d5a@protonmail.com> (raw)
In-Reply-To: <20261124092546.1712228-1-rc4d5a@protonmail.com>

hpre_ecdh_src_data_init() bounds the caller-supplied src length only
indirectly, via shift = ctx->key_sz - (len >> 1). That checks len >> 1
against ctx->key_sz, so an odd len one byte above the real limit
(2 * ctx->key_sz + 1) still passes: len >> 1 truncates down to the
same value as len == 2 * ctx->key_sz, so shift comes out to 0.

scatterwalk_map_and_copy(ptr + tmpshift, data, 0, len, 0) then copies
all len bytes starting at tmpshift == ctx->key_sz << 1, into a buffer
sized ctx->key_sz << 2. For P-256 (ctx->key_sz == 32), len == 65
writes bytes at offsets 64 through 128 into a 128-byte buffer,
overflowing the DMA-coherent allocation by one byte. The overflowing
byte's value comes from the attacker's input, not from kernel memory.

Neither of the two in-tree callers of "ecdh-nist-pNNN" can trigger
this today: net/bluetooth/ecdh_helper.c's compute_ecdh_secret() and
drivers/char/tpm/tpm2-sessions.c both hardcode the request length to
2 * key_sz at their own call sites. This is a missing bounds check in
hpre_ecdh_src_data_init().

Found during review of the DH/ECDH public key validation patch sent
as 1/2 of this series. It predates that patch and applies regardless
of whether 1/2 is installed, since nothing before this change
checked len against its upper bound directly.

Fixed by checking len against its upper bound, 2 * ctx->key_sz.

Fixes: 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
No HiSilicon HPRE hardware was available to reproduce this. Verified
by building the modified driver against an unmodified kernel tree,
confirming the module compiles and links cleanly with no new
warnings. Separately verified the fix in a standalone test simulating
the buffer-offset arithmetic for several lengths around the boundary,
for a P-256-sized key (key_sz=32): len=65 previously wrote one byte
past the end of the 128-byte buffer and is now rejected; len=64 and
len=63 (both previously valid) are unaffected; len=66 (already
rejected before this change) remains rejected.

AI Disclosure: An AI assistant was used to assist with the
analysis and testing. I have verified the vulnerability, the fix, and
the test results myself and take responsibility for this contribution,
per Documentation/process/coding-assistants.rst.

 drivers/crypto/hisilicon/hpre/hpre_crypto.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
index 66a276786a7d..bf55dbc0c894 100644
--- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c
+++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
@@ -1484,6 +1484,9 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
 	int shift;

 	/* Src_data include gx and gy. */
+	if (unlikely(len > ctx->key_sz << 1))
+		return -EINVAL;
+
 	shift = ctx->key_sz - (len >> 1);
 	if (unlikely(shift < 0))
 		return -EINVAL;
--
2.51.2


      reply	other threads:[~2026-11-24  9:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  9:26 [PATCH 1/2] crypto: hisilicon/hpre - fix missing DH/ECDH public key validation Ryan Prinz
2026-11-24  9:26 ` Ryan Prinz [this message]

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=20261124092546.1712228-2-rc4d5a@protonmail.com \
    --to=rc4d5a@protonmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liulongfang@huawei.com \
    --cc=songzhiqi1@huawei.com \
    /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®