Skip to contentSkip to conversation

net/zxdh: fix issues reported by GCC analyzer

PATCH Applied Junlong Wang 2 participants 5 messages Nov 28 – Dec 1 Network drivers

lore
Share & tools
lore
#0 lore raw

cover net/zxdh: fix issues reported by GCC analyzer

Two memory safety issues in the zxdh driver, both found with gcc -fanalyzer:

- zxdh_get_res_info() copies a reply length taken from the device into a caller buffer whose size it is not told. Both callers pass a one-byte object, so an oversized length overwrites their stack, and a length above the receive buffer size also overruns that buffer (patch 1).
- fd_flow_parse_pattern() and fd_flow_parse_action() allocate a flow entry when rte_flow_validate() passes dh_flow as NULL, and free it on only one exit, so every validate leaks memory (patch 2).

Junlong Wang (2):
net/zxdh: bound resource table reply length before copying net/zxdh: avoid leaking flow entry on validation

 drivers/net/zxdh/zxdh_common.c | 24 ++++++++++----
 drivers/net/zxdh/zxdh_flow.c   | 58 ++++++++++------------------------
 2 files changed, 33 insertions(+), 49 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/2 net/zxdh: bound resource table reply length before copying

zxdh_get_res_info() copies a device-supplied reply length into the caller's buffer without knowing that buffer's size. Both callers pass the address of a uint8_t local, so a reply longer than one byte overwrites the caller's stack. Nothing bounds the length either: it sits in a nested TBL reply header, which zxdh_bar_chan_sync_msg_send() does not look at, and a value above ZXDH_RSC_TBL_CONTENT_LEN_MAX also overruns recv_buf.

Pass the destination capacity to the function and reject a reply that exceeds it or the recv_buf size, rather than truncating a malformed length into a wrong panel or hash id.

While here, report a reply whose status byte is not the success code as ZXDH_BAR_MSG_ERR_REPLY and log that byte: such a reply was returned to the caller as success. Copy with memcpy() rather than rte_memcpy():
the length is already checked, and the inlined body makes gcc -fanalyzer report a false out-of-bounds write on the caller's one-byte object.

Fixes 6310e3976b ("net/zxdh: get device backend infos")
---
 drivers/net/zxdh/zxdh_common.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/net/zxdh/zxdh_common.c +17 -7
index 2b821b1dc8..22cdfbacc4 100644
@@ -183,13 +183,14 @@ zxdh_fill_res_para(struct rte_eth_dev *dev, struct zxdh_res_para *param)
 }
 
 static int
-zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t field, uint8_t *res, uint16_t *len)
+zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t field,
+		  uint8_t *res, uint16_t max_len, uint16_t *len)
 {
 	struct zxdh_pci_bar_msg in = {0};
 	uint8_t recv_buf[ZXDH_RSC_TBL_CONTENT_LEN_MAX + 8] = {0};
 	int ret = 0;
 
-	if (!res || !dev)
+	if (!res || !dev || !len)
 		return ZXDH_BAR_MSG_ERR_NULL;
 
 	struct zxdh_tbl_msg_header tbl_msg = {
@@ -224,11 +225,18 @@ zxdh_get_res_info(struct zxdh_res_para *dev, uint8_t field, uint8_t *res, uint16
 
 	if (tbl_reps->check != ZXDH_TBL_MSG_PRO_SUCCESS) {
 		PMD_DRV_LOG(ERR,
-			"get resource_field failed. pcieid: 0x%x, ret: %d", dev->pcie_id, ret);
-		return ret;
+			"get resource_field failed. pcieid: 0x%x, check: %u",
+			dev->pcie_id, tbl_reps->check);
+		return ZXDH_BAR_MSG_ERR_REPLY;
+	}
+	if (tbl_reps->len > max_len || tbl_reps->len > ZXDH_RSC_TBL_CONTENT_LEN_MAX) {
+		PMD_DRV_LOG(ERR,
+			"invalid resource_field len: %u (max: %u). pcieid: 0x%x",
+			tbl_reps->len, max_len, dev->pcie_id);
+		return -EINVAL;
 	}
 	*len = tbl_reps->len;
-	rte_memcpy(res, (recv_buf + ZXDH_REPS_HEADER_OFFSET +
+	memcpy(res, (recv_buf + ZXDH_REPS_HEADER_OFFSET +
 		sizeof(struct zxdh_tbl_msg_reps_header)), *len);
 	return ret;
 }
@@ -239,7 +247,8 @@ zxdh_get_res_panel_id(struct zxdh_res_para *in, uint8_t *panel_id)
 	uint8_t reps = 0;
 	uint16_t reps_len = 0;
 
-	if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_PNLID, &reps, &reps_len) != ZXDH_BAR_MSG_OK)
+	if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_PNLID, &reps, sizeof(reps),
+			      &reps_len) != ZXDH_BAR_MSG_OK)
 		return -1;
 
 	*panel_id = reps;
@@ -262,7 +271,8 @@ zxdh_get_res_hash_id(struct zxdh_res_para *in, uint8_t *hash_id)
 	uint8_t reps = 0;
 	uint16_t reps_len = 0;
 
-	if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_HASHID, &reps, &reps_len) != ZXDH_BAR_MSG_OK)
+	if (zxdh_get_res_info(in, ZXDH_TBL_FIELD_HASHID, &reps, sizeof(reps),
+			      &reps_len) != ZXDH_BAR_MSG_OK)
 		return -1;
 
 	*hash_id = reps;
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH v1 1/2] net/zxdh: bound resource table reply length before copying

Quoted 12 lines · On Sat, 28 Nov 2026 12:04:57 +0800 Junlong Wang <[email protected]>…

zxdh_get_res_info() copies a device-supplied reply length into the caller's buffer without knowing that buffer's size. Both callers pass the address of a uint8_t local, so a reply longer than one byte overwrites the caller's stack. Nothing bounds the length either: it sits in a nested TBL reply header, which zxdh_bar_chan_sync_msg_send() does not look at, and a value above ZXDH_RSC_TBL_CONTENT_LEN_MAX also overruns recv_buf.

Pass the destination capacity to the function and reject a reply that exceeds it or the recv_buf size, rather than truncating a malformed length into a wrong panel or hash id.

Quoted 8 lines

While here, report a reply whose status byte is not the success code as ZXDH_BAR_MSG_ERR_REPLY and log that byte: such a reply was returned to the caller as success. Copy with memcpy() rather than rte_memcpy():
the length is already checked, and the inlined body makes gcc -fanalyzer report a false out-of-bounds write on the caller's one-byte object.

Fixes 6310e3976b ("net/zxdh: get device backend infos")

Applied both to next-net and fixed commit hash to full 12 characters which is what checkpatches wants.

#3 lore raw

PATCH 2/2 net/zxdh: avoid leaking flow entry on validation

In reply to Junlong Wang

zxdh_flow_validate() reuses the pattern and action parsers of zxdh_flow_create(), but passes dh_flow as NULL; both parsers then rte_zmalloc() a struct zxdh_flow_info to parse into.

Neither parser releases that allocation on the validation path. fd_flow_parse_pattern() has no cleanup at all, so the memory leaks on its success return and on all ten of its early returns. fd_flow_parse_action() only frees through its free_flow label, so its success return and the four checks made after the action loop leak as well. Each validate therefore leaks 368 bytes of the rte_malloc heap, and rte_flow_validate() is a side-effect free API that an application may call as often as it wants.

Keep the parse code shared, but make it fall back to a zeroed local struct zxdh_flow_info when dh_flow is NULL instead of allocating one. The memory then goes away with the stack frame, no return path needs cleanup, and the free_flow label of fd_flow_parse_action() has nothing left to do.

The local struct is zeroed exactly like rte_zmalloc() was, it never escapes the parser, and the create path keeps parsing into dh_flow unchanged. Stack use grows by 184 bytes per parser.

Fixes d0af48c1b952 ("net/zxdh: support flow API")
---
 drivers/net/zxdh/zxdh_flow.c | 58 ++++++++++--------------------------
 1 file changed, 16 insertions(+), 42 deletions(-)
drivers/net/zxdh/zxdh_flow.c +16 -42
index a51ce9ef77..776c02117f 100644
@@ -1238,7 +1238,8 @@ static int fd_flow_parse_pattern(struct rte_eth_dev *dev, const struct rte_flow_
 			 struct rte_flow_error *error, struct zxdh_flow *dh_flow)
 {
 	struct zxdh_hw *priv = dev->data->dev_private;
-	struct zxdh_flow_info *flow = NULL;
+	struct zxdh_flow_info local_flow = {0};
+	struct zxdh_flow_info *flow = dh_flow ? &dh_flow->flowentry : &local_flow;
 	const struct rte_flow_item *item;
 	const struct rte_flow_item_eth *eth_spec, *eth_mask;
 	const struct rte_flow_item_vlan *vlan_spec, *vlan_mask;
@@ -1250,18 +1251,6 @@ static int fd_flow_parse_pattern(struct rte_eth_dev *dev, const struct rte_flow_
 	const struct rte_flow_item_vxlan *vxlan_spec, *vxlan_mask;
 	struct fd_flow_key *key, *key_mask;
 
-	if (dh_flow) {
-		flow = &dh_flow->flowentry;
-	} else {
-		flow = rte_zmalloc("dh_flow", sizeof(*flow), 0);
-		if (flow == NULL) {
-			rte_flow_error_set(error, EINVAL,
-						 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
-						 "Failed to allocate memory ");
-			return -rte_errno;
-		}
-	}
-
 	key = &flow->fd_flow.key;
 	key_mask = &flow->fd_flow.key_mask;
 	key->vfid = rte_cpu_to_be_16(priv->vfid);
@@ -1648,7 +1637,8 @@ static int
 fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *actions,
 			 struct rte_flow_error *error, struct zxdh_flow *dh_flow)
 {
-	struct zxdh_flow_info *flow = NULL;
+	struct zxdh_flow_info local_flow = {0};
+	struct zxdh_flow_info *flow = dh_flow ? &dh_flow->flowentry : &local_flow;
 	struct fd_flow_result *result = NULL;
 	const struct rte_flow_item *enc_item = NULL;
 	uint8_t action_bitmap = 0;
@@ -1658,17 +1648,6 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 	int ret;
 
 	rte_errno = 0;
-	if (dh_flow) {
-		flow = &dh_flow->flowentry;
-	} else {
-		flow = rte_zmalloc("dh_flow", sizeof(*flow), 0);
-		if (flow == NULL) {
-			rte_flow_error_set(error, EINVAL,
-					 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
-					 "Failed to allocate memory ");
-			return -rte_errno;
-		}
-	}
 	result = &flow->fd_flow.result;
 	action_bitmap = result->action_idx;
 
@@ -1681,11 +1660,11 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"rss action does no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			ret = validate_action_rss(dev, actions, error);
 			if (ret)
-				goto free_flow;
+				return -rte_errno;
 			action_bitmap |= (1 << FD_ACTION_RSS_BIT);
 			break;
 		}
@@ -1696,7 +1675,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi mark action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_mark *act_mark = actions->conf;
 			result->mark_fd_id = rte_cpu_to_le_32(act_mark->id);
@@ -1710,14 +1689,14 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi count action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_count *act_count = actions->conf;
 			if (act_count->id > MAX_FLOW_COUNT_NUM) {
 				rte_flow_error_set(error, EINVAL,
 							RTE_FLOW_ERROR_TYPE_ACTION, actions,
 							"count action id no support.");
-				goto free_flow;
+				return -rte_errno;
 			};
 			result->countid = act_count->id;
 			action_bitmap |= (1 << FD_ACTION_COUNT_BIT);
@@ -1730,7 +1709,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi queue action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			const struct rte_flow_action_queue *act_q;
 			act_q = actions->conf;
@@ -1738,14 +1717,14 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"Invalid queue ID");
-				goto free_flow;
+				return -rte_errno;
 			}
 			ret = zxdh_hw_qid_to_logic_qid(dev, act_q->index << 1);
 			if (ret < 0) {
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"Invalid phy queue ID .");
-				goto free_flow;
+				return -rte_errno;
 			}
 			result->qid = rte_cpu_to_le_16(ret);
 			action_bitmap |= (1 << FD_ACTION_QUEUE_BIT);
@@ -1760,7 +1739,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_DROP_BIT);
 			break;
@@ -1772,7 +1751,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_VXLAN_DECAP);
 			break;
@@ -1787,7 +1766,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 				rte_flow_error_set(error, EINVAL,
 						RTE_FLOW_ERROR_TYPE_ACTION, actions,
 						"multi drop action no support.");
-				goto free_flow;
+				return -rte_errno;
 			}
 			action_bitmap |= (1 << FD_ACTION_VXLAN_ENCAP);
 			break;
@@ -1795,7 +1774,7 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 			rte_flow_error_set(error, EINVAL,
 				RTE_FLOW_ERROR_TYPE_ACTION, actions,
 				"Invalid action.");
-			goto free_flow;
+			return -rte_errno;
 		}
 	}
 
@@ -1829,11 +1808,6 @@ fd_flow_parse_action(struct rte_eth_dev *dev, const struct rte_flow_action *acti
 
 	result->action_idx = action_bitmap;
 	return 0;
-
-free_flow:
-	if (!dh_flow)
-		rte_free(flow);
-	return -rte_errno;
 }
 
 static int
Signature · 1 line
2.27.0
#4 lore raw
In reply to Junlong Wang
Quoted 12 lines · On Sat, 28 Nov 2026 12:04:56 +0800 Junlong Wang <[email protected]>…

Two memory safety issues in the zxdh driver, both found with gcc -fanalyzer:

- zxdh_get_res_info() copies a reply length taken from the device into a caller buffer whose size it is not told. Both callers pass a one-byte object, so an oversized length overwrites their stack, and a length above the receive buffer size also overruns that buffer (patch 1).
- fd_flow_parse_pattern() and fd_flow_parse_action() allocate a flow entry when rte_flow_validate() passes dh_flow as NULL, and free it on only one exit, so every validate leaks memory (patch 2).

Quoted 8 lines

Junlong Wang (2):
net/zxdh: bound resource table reply length before copying net/zxdh: avoid leaking flow entry on validation

 drivers/net/zxdh/zxdh_common.c | 24 ++++++++++----
 drivers/net/zxdh/zxdh_flow.c   | 58 ++++++++++------------------------
 2 files changed, 33 insertions(+), 49 deletions(-)

Applied both to next-net and fixed the commit tag AI complained about. You might want to look at the other Info: finding.

Review: [PATCH v1 0/2] net/zxdh: GCC analyzer fixes

Series applies cleanly to main (27.03-rc0) and builds with -Dwerror=true at each commit.

[PATCH v1 1/2] net/zxdh: bound resource table reply length before copying

Warning:

Fixes hash is 10 characters; must be 12.
    Fixes: 6310e3976bba ("net/zxdh: get device backend infos")

Info:

A short reply still returns success. Both callers need exactly one byte, but a reply with len == 0 passes the new check, copies nothing, and the caller returns its initializer (reps = 0) as the panel or hash id. That is the "wrong panel or hash id" outcome the commit message says it avoids. Since both callers want an exact size, reject anything else:

	if (tbl_reps->len != max_len ||
	    tbl_reps->len > ZXDH_RSC_TBL_CONTENT_LEN_MAX) {

or have the callers check reps_len == sizeof(reps).

The new length check returns -EINVAL while every other exit from zxdh_get_res_info() returns a ZXDH_BAR_MSG_* code. Callers only test != ZXDH_BAR_MSG_OK so it works, but ZXDH_BAR_MSG_ERR_LEN already exists for this case and keeps the return convention consistent.