Skip to contentSkip to conversation

crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement

PATCH Superseded Ryan Prinz 2 participants 2 messages Nov 24

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ryan Prinz

author
#0 lore raw

PATCH 1/1 crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement

hpre_ecdh_msg_request_set() requires req->dst_len to be at least 2 * ctx->key_sz, regardless of which operation is being performed. The generic KPP convention only needs that much for generate_public_key(); a shared secret is conventionally just the X coordinate, ctx->key_sz bytes.

Both in-tree callers of the generic ecdh-nist-pNNN names provide exactly that smaller buffer for compute_shared_secret():
net/bluetooth/ecdh_helper.c's compute_ecdh_secret() allocates secret[32] for P-256, and drivers/char/tpm/tpm2-sessions.c's session salt derivation does kpp_request_set_output(req, d, EC_PT_SZ). Both calls fail with -EOVERFLOW on HPRE hardware before the peer's public key is looked at.

Found by Sashiko's automated review of a related patch, as a pre-existing issue that patch doesn't introduce.

Fixed by requiring only the curve's own coordinate size for compute_shared_secret(), and using a bounce buffer for that case:
hpre_ecdh_dst_data_init() DMA-maps an internal 2 * key_sz buffer instead of the caller's, and hpre_ecdh_hw_data_clr_all() copies the X coordinate to the caller's destination. generate_public_key() is unaffected; it still requires, and writes directly into, a 2 * key_sz-sized caller buffer. areq->dst_len is also now set to whatever was written (curve_sz or 2 * key_sz) instead of reporting 2 * key_sz.

Fixes 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")

---
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. Verified the sizing and offset arithmetic in a standalone test across all three supported curves (P-192, P-256, P-384): the new compute_shared_secret() minimum (curve_sz) and unchanged generate_public_key() minimum (2 * key_sz) come out distinct and correct for each, and every read/write offset used by both the bounce-buffer copy-out and the existing direct-buffer compaction stays inside its respective buffer.

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 | 70 +++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 14 deletions(-)
drivers/crypto/hisilicon/hpre/hpre_crypto.c +56 -14
index 66a276786a7d..a3fec7c7549d 100644
@@ -1390,9 +1390,11 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
 				      struct scatterlist *dst,
 				      struct scatterlist *src)
 {
+	unsigned int curve_sz = hpre_ecdh_get_curvesz(ctx->curve_id);
 	struct device *dev = ctx->dev;
 	struct hpre_sqe *sqe = &req->req;
 	dma_addr_t dma;
+	char *p;
 
 	dma = le64_to_cpu(sqe->in);
 	if (unlikely(dma_mapping_error(dev, dma)))
@@ -1405,8 +1407,24 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
 	if (unlikely(dma_mapping_error(dev, dma)))
 		return;
 
-	if (dst)
+	if (req->dst) {
+		/*
+		 * Bounce buffer for compute_shared_secret(): holds the
+		 * hardware's full, hw-slot-padded X||Y output. Copy out
+		 * just the tightly-packed X coordinate to the caller's
+		 * own (possibly smaller) buffer.
+		 */
+		p = req->dst;
+		if (dst)
+			scatterwalk_map_and_copy(p + ctx->key_sz - curve_sz,
+						 dst, 0, curve_sz, 1);
+		dma_free_coherent(dev, ctx->key_sz << 1, req->dst, dma);
+	} else if (dst) {
 		dma_unmap_single(dev, dma, ctx->key_sz << 1, DMA_FROM_DEVICE);
+		p = sg_virt(dst);
+		memmove(p, p + ctx->key_sz - curve_sz, curve_sz);
+		memmove(p + curve_sz, p + (ctx->key_sz << 1) - curve_sz, curve_sz);
+	}
 }
 
 static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
@@ -1416,12 +1434,11 @@ static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
 	struct hpre_asym_request *req = NULL;
 	struct kpp_request *areq;
 	u64 overtime_thrhld;
-	char *p;
 	int ret;
 
 	ret = hpre_alg_res_post_hf(ctx, resp, (void **)&req);
 	areq = req->areq.ecdh;
-	areq->dst_len = ctx->key_sz << 1;
+	areq->dst_len = req->dst ? curve_sz : ctx->key_sz << 1;
 
 	overtime_thrhld = atomic64_read(&dfx[HPRE_OVERTIME_THRHLD].value);
 	if (overtime_thrhld && hpre_is_bd_timeout(req, overtime_thrhld))
@@ -1430,10 +1447,6 @@ static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
 	/* Do unmap before data processing */
 	hpre_ecdh_hw_data_clr_all(ctx, req, areq->dst, areq->src);
 
-	p = sg_virt(areq->dst);
-	memmove(p, p + ctx->key_sz - curve_sz, curve_sz);
-	memmove(p + curve_sz, p + areq->dst_len - curve_sz, curve_sz);
-
 	kpp_request_complete(areq, ret);
 
 	atomic64_inc(&dfx[HPRE_RECV_CNT].value);
@@ -1446,7 +1459,17 @@ static int hpre_ecdh_msg_request_set(struct hpre_ctx *ctx,
 	struct hpre_sqe *msg;
 	void *tmp;
 
-	if (req->dst_len < ctx->key_sz << 1) {
+	if (req->src) {
+		unsigned int curve_sz = hpre_ecdh_get_curvesz(ctx->curve_id);
+
+		if (!curve_sz)
+			return -EINVAL;
+
+		if (req->dst_len < curve_sz) {
+			req->dst_len = curve_sz;
+			return -EOVERFLOW;
+		}
+	} else if (req->dst_len < ctx->key_sz << 1) {
 		req->dst_len = ctx->key_sz << 1;
 		return -EOVERFLOW;
 	}
@@ -1503,12 +1526,15 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
 }
 
 static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
-				   struct scatterlist *data, unsigned int len)
+				   struct scatterlist *data, unsigned int len,
+				   bool is_shared_secret)
 {
+	gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
 	struct hpre_sqe *msg = &hpre_req->req;
 	struct hpre_ctx *ctx = hpre_req->ctx;
 	struct device *dev = ctx->dev;
 	dma_addr_t dma;
+	void *ptr;
 
 	if (unlikely(!data || !sg_is_last(data))) {
 		dev_err(dev, "data is illegal!\n");
@@ -1516,10 +1542,26 @@ static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
 	}
 
 	hpre_req->dst = NULL;
-	dma = dma_map_single(dev, sg_virt(data), ctx->key_sz << 1, DMA_FROM_DEVICE);
-	if (unlikely(dma_mapping_error(dev, dma))) {
-		dev_err(dev, "dma map data err!\n");
-		return -ENOMEM;
+
+	/*
+	 * compute_shared_secret() may be given a buffer sized for just
+	 * the X coordinate, per the generic KPP convention, but the
+	 * hardware always produces both X and Y. Use a bounce buffer so
+	 * the output has somewhere safe to land; hpre_ecdh_hw_data_clr_all()
+	 * copies out just X afterward.
+	 */
+	if (is_shared_secret) {
+		ptr = dma_alloc_coherent(dev, ctx->key_sz << 1, &dma, gfp);
+		if (unlikely(!ptr))
+			return -ENOMEM;
+		hpre_req->dst = ptr;
+	} else {
+		dma = dma_map_single(dev, sg_virt(data), ctx->key_sz << 1,
+				     DMA_FROM_DEVICE);
+		if (unlikely(dma_mapping_error(dev, dma))) {
+			dev_err(dev, "dma map data err!\n");
+			return -ENOMEM;
+		}
 	}
 
 	msg->out = cpu_to_le64(dma);
@@ -1552,7 +1594,7 @@ static int hpre_ecdh_compute_value(struct kpp_request *req)
 		msg->in = cpu_to_le64(ctx->ecdh.dma_g);
 	}
 
-	ret = hpre_ecdh_dst_data_init(hpre_req, req->dst, req->dst_len);
+	ret = hpre_ecdh_dst_data_init(hpre_req, req->dst, req->dst_len, !!req->src);
 	if (unlikely(ret)) {
 		dev_err(dev, "failed to init dst data, ret = %d!\n", ret);
 		goto clear_all;

--
2.51.2

#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] crypto: hpre: hpre_ecdh_hw_data_clr_all() corrupts dst buffer on error paths - [Medium] crypto…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] crypto: hpre: hpre_ecdh_hw_data_clr_all() corrupts dst buffer on error paths
- [Medium] crypto: hpre: hpre_ecdh_cb() reports wrong dst_len for P-192 public key
- [Medium] crypto: hisilicon/hpre - hpre_ecdh_dst_data_init() rejects multi-entry sg
--

commit 7c1c914742d923b9ff38c5599d17f759c401716d
Author: Ryan Prinz <[email protected]>

crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement

This commit lowers the minimum dst_len required for ECDH compute_shared_secret() to curve_sz by allocating a 2 * ctx->key_sz DMA bounce buffer and copying out the X coordinate on completion.

Quoted 30 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +11 -1
index 66a276786a7d0..a3fec7c7549dd 100644
@@ -1390,9 +1390,11 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
 				      struct scatterlist *dst,
 				      struct scatterlist *src)
 {
+	unsigned int curve_sz = hpre_ecdh_get_curvesz(ctx->curve_id);
 	struct device *dev = ctx->dev;
 	struct hpre_sqe *sqe = &req->req;
 	dma_addr_t dma;
+	char *p;
 
 	dma = le64_to_cpu(sqe->in);
 	if (unlikely(dma_mapping_error(dev, dma)))
@@ -1405,8 +1407,24 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
 	if (unlikely(dma_mapping_error(dev, dma)))
 		return;
 
-	if (dst)
+	if (req->dst) {
+		/*
+		 * Bounce buffer for compute_shared_secret(): holds the
+		 * hardware's full, hw-slot-padded X||Y output. Copy out
+		 * just the tightly-packed X coordinate to the caller's
+		 * own (possibly smaller) buffer.
+		 */
+		p = req->dst;
+		if (dst)

drivers/crypto/hisilicon/hpre/hpre_crypto.c static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx, · line 1428

Quoted 10 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +8 -0
+			scatterwalk_map_and_copy(p + ctx->key_sz - curve_sz,
+						 dst, 0, curve_sz, 1);
+		dma_free_coherent(dev, ctx->key_sz << 1, req->dst, dma);
+	} else if (dst) {
 		dma_unmap_single(dev, dma, ctx->key_sz << 1, DMA_FROM_DEVICE);
+		p = sg_virt(dst);
+		memmove(p, p + ctx->key_sz - curve_sz, curve_sz);
+		memmove(p + curve_sz, p + (ctx->key_sz << 1) - curve_sz, curve_sz);
+	}
 }

[Severity: Medium]
Does moving the destination buffer copy and memmove() compaction into hpre_ecdh_hw_data_clr_all() corrupt the caller's req->dst buffer on error paths?

When hpre_send() fails in hpre_ecdh_compute_value() after hpre_ecdh_dst_data_init() succeeds, the clear_all label calls hpre_ecdh_hw_data_clr_all(ctx, hpre_req, req->dst, req->src), which now copies zeros from the unused bounce buffer into req->dst or shifts sg_virt(req->dst) in place via memmove(). The same happens in hpre_ecdh_cb() when hpre_alg_res_post_hf() reports a hardware error. Also, because dma_unmap_single() is inside the else if (dst) branch, callers cannot simply pass dst = NULL on error without leaking the DMA mapping for generate_public_key().

drivers/crypto/hisilicon/hpre/hpre_crypto.c static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp) · line 1441

Quoted 13 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +1 -2
 static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
@@ -1416,12 +1434,11 @@ static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
 	struct hpre_asym_request *req = NULL;
 	struct kpp_request *areq;
 	u64 overtime_thrhld;
-	char *p;
 	int ret;
 
 	ret = hpre_alg_res_post_hf(ctx, resp, (void **)&req);
 	areq = req->areq.ecdh;
-	areq->dst_len = ctx->key_sz << 1;
+	areq->dst_len = req->dst ? curve_sz : ctx->key_sz << 1;

[Severity: Medium]
Should the false branch here be curve_sz << 1 instead of ctx->key_sz << 1?

For ECC_CURVE_NIST_P192, ctx->key_sz is 32 (HPRE_ECC_HW256_KSZ_B) while curve_sz is 24. On generate_public_key(), the two memmove() calls compact X and Y into 2 * curve_sz = 48 bytes at the start of dst, so setting areq->dst_len to ctx->key_sz << 1 reports 64 bytes including 16 trailing uncompacted bytes.

[ ... ]

Quoted 7 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +2 -1
@@ -1503,12 +1526,15 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
 }
 
 static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
-				   struct scatterlist *data, unsigned int len)
+				   struct scatterlist *data, unsigned int len,
+				   bool is_shared_secret)

drivers/crypto/hisilicon/hpre/hpre_crypto.c static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req, · line 1540

Quoted 10 lines
drivers/crypto/hisilicon/hpre/hpre_crypto.c +2 -0
 {
+	gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
 	struct hpre_sqe *msg = &hpre_req->req;
 	struct hpre_ctx *ctx = hpre_req->ctx;
 	struct device *dev = ctx->dev;
 	dma_addr_t dma;
+	void *ptr;
 
 	if (unlikely(!data || !sg_is_last(data))) {
 		dev_err(dev, "data is illegal!\n");

[Severity: Medium]
Does checking !sg_is_last(data) here still cause TPM2 session salt derivation in tpm_buf_append_salt() to fail with -EINVAL, as well as rejecting multi-entry destination scatterlists on the bounce-buffered compute_shared_secret() path?

Before calling crypto_kpp_compute_shared_secret(), tpm_buf_append_salt() calls crypto_kpp_generate_public_key() with a 2-entry scatterlist for X and Y:

drivers/char/tpm/tpm2-sessions.c:tpm_buf_append_salt() {
    ...
	sg_init_table(s, 2);
	sg_set_buf(&s[0], x, EC_PT_SZ);
	sg_set_buf(&s[1], y, EC_PT_SZ);
    ...
	kpp_request_set_input(req, NULL, 0);
	kpp_request_set_output(req, s, EC_PT_SZ*2);
	rc = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
    ...
}

Because s has two entries, !sg_is_last(data) rejects the generate_public_key() call before tpm_buf_append_salt() reaches compute_shared_secret(). In addition, when is_shared_secret is true, the output is copied via scatterwalk_map_and_copy() from a bounce buffer, so requiring sg_is_last(data) rejects multi-segment destination scatterlists there too (and leaves len unused).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1