All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ryan Prinz" <rc4d5a@protonmail.com>,
	"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
Subject: Re: [PATCH] crypto: hisilicon/hpre - fix missing DH/ECDH public key validation
Date: Mon, 23 Nov 2026 22:09:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-230116@kernel.org> (raw)
In-Reply-To: <20261123215144.1677044-1-rc4d5a@protonmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] crypto: hisilicon/hpre: wrong P-192 key size in
  hpre_ecdh_peer_pubkey_valid()
- [Medium] crypto: hisilicon/hpre: GFP_KERNEL alloc in
  hpre_ecdh_peer_pubkey_valid()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] crypto: hisilicon/hpre: heap buffer overflow in
  hpre_ecdh_src_data_init() [1]
--

commit 0db0261ad258477cf8a2d64e1bd4053cb55e5606
Author: Ryan Prinz <rc4d5a@protonmail.com>

crypto: hisilicon/hpre - fix missing DH/ECDH public key validation

This commit adds peer public key validation to hpre_dh_compute_value()
and hpre_ecdh_compute_value() before requests are submitted to the
hardware queue.

> diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
> index 66a276786a7d0..a5702d2d73b3d 100644
> --- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c
> +++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c

[ ... ]

> @@ -1526,6 +1599,54 @@ static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
>  	return 0;
>  }
>  
> +static int hpre_ecdh_peer_pubkey_valid(struct hpre_ctx *ctx,
> +				       struct scatterlist *src,
> +				       unsigned int src_len)
> +{
> +	const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
> +	unsigned int nbytes = ctx->key_sz;
> +	unsigned int half = src_len >> 1;
> +	u8 coord_buf[2 * ECC_MAX_BYTES] = { };
> +	struct ecc_point *pk;
> +	size_t copied;
> +	int ret;
> +
> +	if (!curve)
> +		return -EINVAL;
> +
> +	/*
> +	 * Src_data is gx and gy concatenated, each zero-padded up to
> +	 * nbytes, matching the convention hpre_ecdh_src_data_init() uses
> +	 * for the hardware submission path.
> +	 */
> +	if (!half || half > nbytes || 2 * nbytes > sizeof(coord_buf))
> +		return -EINVAL;
> +
> +	copied = sg_pcopy_to_buffer(src, sg_nents_for_len(src, src_len),
> +				    coord_buf + (nbytes - half), half, 0);
> +	if (copied != half)
> +		return -EINVAL;
> +
> +	copied = sg_pcopy_to_buffer(src, sg_nents_for_len(src, src_len),
> +				    coord_buf + nbytes + (nbytes - half),
> +				    half, half);
> +	if (copied != half)
> +		return -EINVAL;
> +
> +	pk = ecc_alloc_point(curve->g.ndigits);
> +	if (!pk)
> +		return -ENOMEM;

[Severity: Medium]
Can ecc_alloc_point() sleep when CRYPTO_TFM_REQ_MAY_SLEEP is not set in
the request flags?

ecc_alloc_point() unconditionally allocates with GFP_KERNEL, whereas
hpre_dh_compute_value() and hpre_ecdh_src_data_init() select GFP_ATOMIC
when CRYPTO_TFM_REQ_MAY_SLEEP is clear.  Using stack buffers with
ECC_POINT_INIT() would avoid the allocation altogether.

> +
> +	ecc_swap_digits(coord_buf, pk->x, curve->g.ndigits);
> +	ecc_swap_digits(coord_buf + nbytes, pk->y, curve->g.ndigits);
> +
> +	ret = ecc_is_pubkey_valid_partial(curve, pk);

[Severity: High]
Does setting nbytes to ctx->key_sz break validation for ecdh-nist-p192?

For ECC_CURVE_NIST_P192, hpre_ecdh_supported_curve() sets ctx->key_sz to
HPRE_ECC_HW256_KSZ_B (32 bytes, the 256-bit hardware slot width), while
the curve coordinate size is 24 bytes (curve->g.ndigits = 3):

drivers/crypto/hisilicon/hpre/hpre_crypto.c:hpre_ecdh_supported_curve() {
	switch (id) {
	case ECC_CURVE_NIST_P192:
	case ECC_CURVE_NIST_P256:
		return HPRE_ECC_HW256_KSZ_B;
}

When a valid 48-byte P-192 public key (half = 24) is checked, the
coordinates are right-aligned to coord_buf + 8 and coord_buf + 40, but
ecc_swap_digits() reads only 3 digits (24 bytes) starting at coord_buf
and coord_buf + 32.  That shifts each coordinate right by 64 bits and
causes ecc_is_pubkey_valid_partial() to reject every valid 48-byte P-192
public key with -EINVAL (including the testmgr self-test), while a
64-byte input on P-192 passes half <= nbytes and has only the upper 24
bytes of each 32-byte coordinate validated before
hpre_ecdh_src_data_init() copies all 32 bytes to the hardware buffer.

nbytes should be derived from curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT
instead of ctx->key_sz.

> +
> +	ecc_free_point(pk);
> +
> +	return ret;
> +}
> +
>  static int hpre_ecdh_compute_value(struct kpp_request *req)
>  {
>  	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
> @@ -1543,6 +1664,12 @@ static int hpre_ecdh_compute_value(struct kpp_request *req)
>  	}
>  
>  	if (req->src) {
> +		ret = hpre_ecdh_peer_pubkey_valid(ctx, req->src, req->src_len);
> +		if (unlikely(ret)) {
> +			dev_err(dev, "invalid peer public key, ret = %d!\n", ret);
> +			return ret;
> +		}
> +
>  		ret = hpre_ecdh_src_data_init(hpre_req, req->src, req->src_len);

[Severity: Critical]
This problem wasn't introduced by this patch, but can an odd req->src_len
pass validation and cause a 1-byte DMA buffer overflow in
hpre_ecdh_src_data_init() [1]?

Neither hpre_ecdh_peer_pubkey_valid() nor hpre_ecdh_src_data_init()
rejects an odd length when computing req->src_len >> 1:

drivers/crypto/hisilicon/hpre/hpre_crypto.c:hpre_ecdh_src_data_init() {
	...
	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))
		return -ENOMEM;

	tmpshift = ctx->key_sz << 1;
	scatterwalk_map_and_copy(ptr + tmpshift, data, 0, len, 0);
}

For a 32-byte curve such as NIST P-256, a 65-byte input whose first 64
bytes form a valid point passes both half <= nbytes (65 >> 1 == 32) and
shift >= 0, after which scatterwalk_map_and_copy() writes 65 bytes at
offset 64 of the 128-byte (ctx->key_sz << 2) buffer.

[1] https://sashiko.dev/#/bug/linux-42c6cdd1-7f7e-4071-bb2f-a4db5935267e

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123215144.1677044-1-rc4d5a@protonmail.com?part=1

      reply	other threads:[~2026-11-23 22:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 21:52 [PATCH] crypto: hisilicon/hpre - fix missing DH/ECDH public key validation Ryan Prinz
2026-11-23 22:09 ` sashiko-bot [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=sashiko-outbox-230116@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=liulongfang@huawei.com \
    --cc=rc4d5a@protonmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.