From: sashiko-bot@kernel.org
To: "Ryan Prinz" <rc4d5a@protonmail.com>,
"Giovanni Cabiddu" <giovanni.cabiddu@intel.com>
Cc: "Herbert Xu" <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
qat-linux@intel.com, linux-crypto@vger.kernel.org
Subject: Re: [PATCH v2] crypto: qat - fix missing DH public key validation
Date: Tue, 24 Nov 2026 14:23:13 +0000 [thread overview]
Message-ID: <sashiko-outbox-231332@kernel.org> (raw)
In-Reply-To: <20261124141238.1743680-1-rc4d5a@protonmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] crypto: qat - duplicate buffer allocation and copy in
qat_dh_compute_value()
--
commit 64e695c4154aabd945fc196f5f802b7fe41f7873
Author: Ryan Prinz <rc4d5a@protonmail.com>
crypto: qat - fix missing DH public key validation
This commit adds range validation for peer-supplied Diffie-Hellman public
keys in qat_dh_compute_value() so values outside [2, p - 2] are rejected
before reaching the QAT hardware queue.
> diff --git a/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c b/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
> index 77d59e977472d..78f1b949614b9 100644
> --- a/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
> +++ b/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
> @@ -216,6 +216,63 @@ static unsigned long qat_dh_fn_id(unsigned int len, bool g2)
> }
> }
>
> +/* Returns true iff 2 <= y <= p - 2, given big-endian buffers of p_size bytes. */
> +static bool qat_dh_pubkey_in_range(const u8 *y, const u8 *p_minus_1,
> + unsigned int p_size)
> +{
> + bool is_zero_or_one = true;
> + unsigned int i;
> +
> + for (i = 0; i < p_size - 1; i++) {
> + if (y[i]) {
> + is_zero_or_one = false;
> + break;
> + }
> + }
> +
> + if (is_zero_or_one && y[p_size - 1] <= 1)
> + return false;
> +
> + return memcmp(y, p_minus_1, p_size) < 0;
> +}
> +
> +static int qat_dh_peer_pubkey_valid(struct qat_dh_ctx *ctx,
> + struct scatterlist *src,
> + unsigned int src_len, gfp_t flags)
> +{
> + unsigned int p_size = ctx->p_size;
> + u8 *p_minus_1, *y;
> + int i, ret = 0;
> +
> + if (!src_len || src_len > p_size)
> + return -EINVAL;
> +
> + y = kzalloc(p_size, flags);
> + if (!y)
> + return -ENOMEM;
> +
> + scatterwalk_map_and_copy(y + (p_size - src_len), src, 0, src_len, 0);
> +
> + p_minus_1 = kmemdup(ctx->p, p_size, flags);
> + if (!p_minus_1) {
> + ret = -ENOMEM;
> + goto free_y;
> + }
> +
> + for (i = p_size - 1; i >= 0; i--) {
> + if (p_minus_1[i]-- != 0)
> + break;
> + }
> +
> + if (!qat_dh_pubkey_in_range(y, p_minus_1, p_size))
> + ret = -EINVAL;
> +
> + kfree_sensitive(p_minus_1);
> +free_y:
> + kfree_sensitive(y);
> + return ret;
> +}
> +
> static int qat_dh_compute_value(struct kpp_request *req)
> {
> struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
> @@ -241,6 +298,12 @@ static int qat_dh_compute_value(struct kpp_request *req)
> if (req->src_len > ctx->p_size)
> return -EINVAL;
>
> + if (req->src) {
> + ret = qat_dh_peer_pubkey_valid(ctx, req->src, req->src_len, flags);
> + if (unlikely(ret))
> + return ret;
> + }
> +
[Severity: Low]
This isn't a bug, but qat_dh_peer_pubkey_valid() duplicates the buffer
preparation that qat_dh_compute_value() already performs right before
dma_map_single():
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c:qat_dh_compute_value() {
...
if (req->src) {
if (sg_is_last(req->src) && req->src_len == ctx->p_size) {
qat_req->src_align = NULL;
vaddr = sg_virt(req->src);
} else {
int shift = ctx->p_size - req->src_len;
qat_req->src_align = kzalloc(ctx->p_size, flags);
if (unlikely(!qat_req->src_align))
return ret;
scatterwalk_map_and_copy(qat_req->src_align + shift,
req->src, 0, req->src_len, 0);
vaddr = qat_req->src_align;
}
...
}
On every shared-secret request, qat_dh_peer_pubkey_valid() re-checks
src_len > p_size, allocates y and p_minus_1, copies req->src with
scatterwalk_map_and_copy(), and zeroes and frees both buffers via
kfree_sensitive(). Validating vaddr right before dma_map_single() (and
comparing against ctx->p without a per-request kmemdup()) would check the
exact buffer mapped for DMA while avoiding the duplicate scatterlist copy
and extra GFP_ATOMIC allocations when CRYPTO_TFM_REQ_MAY_SLEEP is clear.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124141238.1743680-1-rc4d5a@protonmail.com?part=1
next prev parent reply other threads:[~2026-11-24 14:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 14:12 [PATCH v2] crypto: qat - fix missing DH public key validation Ryan Prinz
2026-11-24 14:23 ` sashiko-bot [this message]
2026-11-25 18:04 ` Giovanni Cabiddu
2026-11-25 23:31 ` rc4d5a
2026-12-02 1:27 ` liulongfang
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-231332@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=davem@davemloft.net \
--cc=giovanni.cabiddu@intel.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=qat-linux@intel.com \
--cc=rc4d5a@protonmail.com \
--cc=sashiko-reviews@lists.linux.dev \
/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.