Skip to contentSkip to conversation

crypto: qat - fix missing DH public key validation

PATCH Superseded Ryan Prinz 2 participants 3 messages Nov 25

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ryan Prinz

author
#0 lore raw

PATCH v3 1/1 crypto: qat - fix missing DH public key validation

qat_dh_compute_value() passes the peer's DH public value into the hardware queue without checking it. The only checks on req->src are on length (req->src_len > ctx->p_size); never on the value itself.

crypto/dh.c's dh_is_pubkey_valid() is the generic software check for
this: it rejects a value outside [2, p-2], but only when FIPS mode is enabled, and only for callers using the software implementation. QAT's hardware path has no equivalent check in either mode.

QAT registers "dh" at cra_priority 1000, versus the generic software implementation's 100, so any caller requesting "dh" gets this unvalidated path whenever a QAT device is present and the requested modulus size (1536/2048/3072/4096 bits) is one QAT supports; qat_dh_set_secret() only falls back to software for sizes outside that set.

drivers/nvme/common/auth.c requests "ffdheNNNN(dh)" for NVMe-oF's in-band DH-HMAC-CHAP authentication. Three of its five supported group sizes, 2048, 3072 and 4096 bits, match QAT's hardware sizes. Forcing the derived session key Ks to a known value lets an attacker who holds a handshake transcript perform an offline brute-force attack against the pre-shared key, which is what the DH exchange is documented to protect against on top of plain CHAP.

Fixed by validating the peer-supplied value before it reaches the hardware queue, rejecting any value outside [2, p-2].

Fixes c9839143ebbf ("crypto: qat - Add DH support")

---
v3:
- Giovanni pointed out that this range check was about to exist twice, byte-for-byte identical, once here and once in the HiSilicon HPRE fix for the same bug. Moved the comparison itself into crypto_dh_pubkey_in_range() in crypto/dh_helper.c, exported via dh.h, so this and the HPRE fix (and the generic software path, if that's wired up too) share one implementation instead of each carrying a private copy. This driver's own zero-padding and (p - 1) computation are unchanged; only the final comparison call moved. Depends on the separate "crypto: dh - add shared DH public key range check" patch.
v2:
- Corrected the Fixes: tag, found by Sashiko's automated review:
2acbb8771f6a only added the req->src_len > ctx->p_size length check, to fix an unrelated integer underflow; it's unrelated to the missing value check this patch adds. That's been missing since DH support was first added, in c9839143ebbf, which is now the correct tag.

No Intel QuickAssist hardware was available to reproduce this. Verified by building the modified driver (along with crypto/dh.c, crypto/dh_helper.c and crypto/kpp.c) against an unmodified kernel tree on x86_64, confirming the module compiles and links cleanly with no new warnings; and verifying the range-check arithmetic in a standalone test exercising the boundary values 0, 1, 2, p-2, p-1, p and a value larger than p, which all produced the expected accept/reject result. The comparison is now crypto_dh_pubkey_in_range() unmodified rather than a private copy; this patch's own buffer handling (zero-padding, p - 1 computation) is unchanged from v2.

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/intel/qat/qat_common/qat_asym_algs.c | 43 ++++++++++++++++++
 1 file changed, 43 insertions(+)
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c +43 -0
@@ -216,6 +216,43 @@ static unsigned long qat_dh_fn_id(unsigned int len, bool g2)
 	}
 }
 
+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 (!crypto_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 +278,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;
+	}
+
 	memset(msg, '\0', sizeof(*msg));
 	ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
 					  ICP_QAT_FW_COMN_REQ_FLAG_SET);
Signature · 1 line
2.51.2
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] crypto: qat - build failure from undeclared crypto_dh_pubkey_in_range() - [Low] crypto: qat…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] crypto: qat - build failure from undeclared crypto_dh_pubkey_in_range()
- [Low] crypto: qat - duplicate buffer allocation and copy in qat_dh_compute_value()
--

commit 0140ee9c1bcbef53ad898f6e8736ea350a10f33a
Author: Ryan Prinz <[email protected]>

crypto: qat - fix missing DH public key validation

This commit adds range validation for peer Diffie-Hellman public keys in qat_dh_compute_value() before submitting requests to the QAT hardware queue.

Quoted 28 lines
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c +20 -0
index 77d59e977472d..e7532c66404d8 100644
@@ -216,6 +216,43 @@ static unsigned long qat_dh_fn_id(unsigned int len, bool g2)
 	}
 }
 
+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;

drivers/crypto/intel/qat/qat_common/qat_asym_algs.c static unsigned long qat_dh_fn_id(unsigned int len, bool g2) · line 248

Quoted 10 lines
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c +10 -0
+		goto free_y;
+	}
+
+	for (i = p_size - 1; i >= 0; i--) {
+		if (p_minus_1[i]-- != 0)
+			break;
+	}
+
+	if (!crypto_dh_pubkey_in_range(y, p_minus_1, p_size))
+		ret = -EINVAL;

[Severity: High]
Does this break the build when CONFIG_CRYPTO_DEV_QAT is enabled? crypto_dh_pubkey_in_range() is not declared in include/crypto/dh.h or defined anywhere in this tree, because the prerequisite commit "crypto: dh - add shared DH public key range check" noted in the patch commentary is missing.

Quoted 11 lines
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c +7 -0
+
+	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);

drivers/crypto/intel/qat/qat_common/qat_asym_algs.c static int qat_dh_compute_value(struct kpp_request *req) · line 285

Quoted 8 lines
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c +5 -0
@@ -241,6 +278,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() re-checks src_len > p_size, allocates y, and copies req->src via scatterwalk_map_and_copy(), even though qat_dh_compute_value() already produces a contiguous zero-padded ctx->p_size buffer in vaddr right before dma_map_single():

drivers/crypto/intel/qat/qat_common/qat_asym_algs.c:qat_dh_compute_value() {
	...
		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;
		}
	...
}

Validating vaddr there would avoid allocating a second buffer and copying req->src twice on every request.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
In reply to Ryan Prinz

Confirmed -- this is expected, and the fix isn't a code change.

This patch depends on a separately-sent patch, "crypto: dh - add shared DH public key range check" (Message-ID <[email protected]>), which adds crypto_dh_pubkey_in_range() to crypto/dh_helper.c and declares it in dh.h. That patch needs to be applied first; this one was never meant to build on its own without it, which is exactly the build failure reported above.

Apologies for not making that dependency something the build tooling could see -- the commit message notes it in prose, but that doesn't help an automated check applying this patch in isolation. I'll send any future multi-patch dependency as a linked series instead.