Skip to contentSkip to conversation

common/cnxk: fix macsec teardown use-after-free

PATCH In review Aarnav JP 2 participants 6 messages Nov 2 – Nov 5

lore
Share & tools
lore

Aarnav JP

author
#0 lore raw

PATCH 1/5 common/cnxk: fix macsec teardown use-after-free

The idev mcs_list is walked from the mailbox interrupt thread via roc_idev_mcs_get() and dereferenced in mcs_event_cb_process(), while the control thread can concurrently free the mcs in roc_mcs_dev_fini(). With no lock on the list, an MCS interrupt during teardown could read freed memory or corrupt the TAILQ mid-splice.

Add a dedicated idev mcs_lock, held across the lookup+callback in the interrupt handler, the list insert in roc_mcs_dev_init(), and the TAILQ_REMOVE in roc_mcs_dev_fini() (which drains any in-flight interrupt before mcs and sa_port_map are freed). List helpers stay lock-free to avoid recursive locking.

Fixes f752780f30dc ("common/cnxk: add ROC MACsec initialization")
---
 drivers/common/cnxk/roc_dev.c       | 11 +++++++++--
 drivers/common/cnxk/roc_idev.c      |  1 +
 drivers/common/cnxk/roc_idev_priv.h |  1 +
 drivers/common/cnxk/roc_mcs.c       | 30 +++++++++++++++++++++--------
 4 files changed, 33 insertions(+), 10 deletions(-)
drivers/common/cnxk/roc_dev.c +9 -2
index a432fb8710..6d918bf17b 100644
@@ -669,15 +669,20 @@ static int
 mbox_up_handler_mcs_intr_notify(struct dev *dev, struct mcs_intr_info *info, struct msg_rsp *rsp)
 {
 	struct roc_mcs_event_desc desc = {0};
+	struct idev_cfg *idev = idev_get_cfg();
 	struct roc_mcs *mcs;
 
 	plt_base_dbg("pf:%d/vf:%d msg id 0x%x (%s) from: pf:%d/vf:%d", dev_get_pf(dev->pf_func),
 		     dev_get_vf(dev->pf_func), info->hdr.id, mbox_id2name(info->hdr.id),
 		     dev_get_pf(info->hdr.pcifunc), dev_get_vf(info->hdr.pcifunc));
 
+	if (idev == NULL)
+		goto exit;
+
+	plt_spinlock_lock(&idev->mcs_lock);
 	mcs = roc_idev_mcs_get(info->mcs_id);
 	if (!mcs)
-		goto exit;
+		goto unlock;
 
 	if (info->intr_mask) {
 		switch (info->intr_mask) {
@@ -739,12 +744,14 @@ mbox_up_handler_mcs_intr_notify(struct dev *dev, struct mcs_intr_info *info, str
 			desc.metadata.lmac_id = info->lmac_id;
 			break;
 		default:
-			goto exit;
+			goto unlock;
 		}
 
 		mcs_event_cb_process(mcs, &desc);
 	}
 
+unlock:
+	plt_spinlock_unlock(&idev->mcs_lock);
 exit:
 	rsp->hdr.rc = 0;
 	return 0;
drivers/common/cnxk/roc_idev.c +1 -0
index 6aa5b10ee9..17b99682fd 100644
@@ -45,6 +45,7 @@ idev_set_defaults(struct idev_cfg *idev)
 	TAILQ_INIT(&idev->roc_nix_list);
 	plt_spinlock_init(&idev->nix_inl_dev_lock);
 	plt_spinlock_init(&idev->npa_dev_lock);
+	plt_spinlock_init(&idev->mcs_lock);
 	__atomic_store_n(&idev->npa_refcnt, 0, __ATOMIC_RELEASE);
 }
 
drivers/common/cnxk/roc_idev_priv.h +1 -0
index dde555535b..ca47f6948e 100644
@@ -45,6 +45,7 @@ struct idev_cfg {
 	struct idev_nix_inl_rx_inj_cfg inl_rx_inj_cfg;
 	plt_spinlock_t nix_inl_dev_lock;
 	plt_spinlock_t npa_dev_lock;
+	plt_spinlock_t mcs_lock;
 	uint8_t dma_cs_offset;
 };
 
drivers/common/cnxk/roc_mcs.c +22 -8
index 0ae5618c09..1b45f5dad9 100644
@@ -839,17 +839,24 @@ struct roc_mcs *
 roc_mcs_dev_init(uint8_t mcs_idx)
 {
 	struct mcs_event_cb_list *cb_list;
+	struct idev_cfg *idev;
 	struct roc_mcs *mcs;
 	struct npa_lf *npa;
 
 	if (!(roc_feature_bphy_has_macsec() || roc_feature_nix_has_macsec()))
 		return NULL;
 
-	mcs = roc_idev_mcs_get(mcs_idx);
-	if (mcs) {
-		plt_info("Skipping device, mcs device already probed");
-		mcs->refcount++;
-		return mcs;
+	idev = idev_get_cfg();
+	if (idev) {
+		plt_spinlock_lock(&idev->mcs_lock);
+		mcs = roc_idev_mcs_get(mcs_idx);
+		if (mcs) {
+			plt_info("Skipping device, mcs device already probed");
+			mcs->refcount++;
+			plt_spinlock_unlock(&idev->mcs_lock);
+			return mcs;
+		}
+		plt_spinlock_unlock(&idev->mcs_lock);
 	}
 
 	mcs = plt_zmalloc(sizeof(struct roc_mcs), PLT_CACHE_LINE_SIZE);
@@ -870,8 +877,11 @@ roc_mcs_dev_init(uint8_t mcs_idx)
 	cb_list = (struct mcs_event_cb_list *)roc_mcs_to_mcs_cb_list(mcs);
 	TAILQ_INIT(cb_list);
 
+	idev = idev_get_cfg();
+	plt_spinlock_lock(&idev->mcs_lock);
 	roc_idev_mcs_set(mcs);
 	mcs->refcount++;
+	plt_spinlock_unlock(&idev->mcs_lock);
 
 	return mcs;
 exit:
@@ -882,12 +892,18 @@ roc_mcs_dev_init(uint8_t mcs_idx)
 void
 roc_mcs_dev_fini(struct roc_mcs *mcs)
 {
+	struct idev_cfg *idev = idev_get_cfg();
 	struct mcs_priv *priv;
 	int i;
 
+	plt_spinlock_lock(&idev->mcs_lock);
 	mcs->refcount--;
-	if (mcs->refcount > 0)
+	if (mcs->refcount > 0) {
+		plt_spinlock_unlock(&idev->mcs_lock);
 		return;
+	}
+	roc_idev_mcs_free(mcs);
+	plt_spinlock_unlock(&idev->mcs_lock);
 
 	priv = roc_mcs_to_mcs_priv(mcs);
 
@@ -902,7 +918,5 @@ roc_mcs_dev_fini(struct roc_mcs *mcs)
 
 	plt_free(mcs->sa_port_map);
 
-	roc_idev_mcs_free(mcs);
-
 	plt_free(mcs);
 }
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/5 common/cnxk: fix TM schq alloc OOB write

nix_tm_copy_rsp_to_nix() consumes the txsch alloc response verbatim. The count rsp->schq[lvl] is not bounded to the 128-entry list, so a large count over-reads schq_list[lvl][]; and the schq id is passed to plt_bitmap_set() as the bit position without checking it against NIX_TM_MAX_HW_TXSCHQ (2048). rte_bitmap_set() does not bound-check pos, so an id >= 2048 writes outside the bitmap and corrupts memory.

Clamp the count to MAX_TXSCHQ_PER_FUNC and reject any id >= NIX_TM_MAX_HW_TXSCHQ with -EINVAL, propagating the error out of nix_tm_alloc_txschq() so setup fails cleanly. Applied to both the discontiguous and contiguous lists.

Fixes df405df95eeb ("common/cnxk: add NIX TM helper to alloc/free resource")
---
 drivers/common/cnxk/roc_nix_priv.h   |  2 +-
 drivers/common/cnxk/roc_nix_tm.c     | 37 ++++++++++++++++++++++------
 drivers/common/cnxk/roc_nix_tm_ops.c |  8 ++++--
 3 files changed, 36 insertions(+), 11 deletions(-)
drivers/common/cnxk/roc_nix_priv.h +1 -1
index d4b3c7c1af..9779b336b0 100644
@@ -421,7 +421,7 @@ int nix_tm_alloc_txschq(struct nix *nix, enum roc_nix_tm_tree tree);
 int nix_tm_assign_resources(struct nix *nix, enum roc_nix_tm_tree tree);
 int nix_tm_release_resources(struct nix *nix, uint8_t hw_lvl, bool contig,
 			     bool above_thresh);
-void nix_tm_copy_rsp_to_nix(struct nix *nix, struct nix_txsch_alloc_rsp *rsp);
+int nix_tm_copy_rsp_to_nix(struct nix *nix, struct nix_txsch_alloc_rsp *rsp);
 
 int nix_tm_txsch_reg_config(struct nix *nix, enum roc_nix_tm_tree tree);
 int nix_tm_update_parent_info(struct nix *nix, enum roc_nix_tm_tree tree);
drivers/common/cnxk/roc_nix_tm.c +29 -8
index 76c0f01884..2f38b042f9 100644
@@ -1483,21 +1483,40 @@ nix_tm_assign_resources(struct nix *nix, enum roc_nix_tm_tree tree)
 	return 0;
 }
 
-void
+int
 nix_tm_copy_rsp_to_nix(struct nix *nix, struct nix_txsch_alloc_rsp *rsp)
 {
 	uint8_t lvl;
 	uint16_t i;
 
 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
-		for (i = 0; i < rsp->schq[lvl]; i++)
-			plt_bitmap_set(nix->schq_bmp[lvl],
-				       rsp->schq_list[lvl][i]);
 
-		for (i = 0; i < rsp->schq_contig[lvl]; i++)
-			plt_bitmap_set(nix->schq_contig_bmp[lvl],
-				       rsp->schq_contig_list[lvl][i]);
+		uint16_t cnt = PLT_MIN(rsp->schq[lvl], (uint16_t)MAX_TXSCHQ_PER_FUNC);
+
+		for (i = 0; i < cnt; i++) {
+			uint16_t id = rsp->schq_list[lvl][i];
+
+			if (id >= NIX_TM_MAX_HW_TXSCHQ) {
+				plt_err("Invalid schq %u for lvl %u in alloc rsp", id, lvl);
+				return -EINVAL;
+			}
+			plt_bitmap_set(nix->schq_bmp[lvl], id);
+		}
+
+		cnt = PLT_MIN(rsp->schq_contig[lvl], (uint16_t)MAX_TXSCHQ_PER_FUNC);
+
+		for (i = 0; i < cnt; i++) {
+			uint16_t id = rsp->schq_contig_list[lvl][i];
+
+			if (id >= NIX_TM_MAX_HW_TXSCHQ) {
+				plt_err("Invalid contig schq %u for lvl %u in alloc rsp", id, lvl);
+				return -EINVAL;
+			}
+			plt_bitmap_set(nix->schq_contig_bmp[lvl], id);
+		}
 	}
+
+	return 0;
 }
 
 int
@@ -1563,8 +1582,10 @@ nix_tm_alloc_txschq(struct nix *nix, enum roc_nix_tm_tree tree)
 			goto alloc_err;
 		}
 
-		nix_tm_copy_rsp_to_nix(nix, rsp);
+		rc = nix_tm_copy_rsp_to_nix(nix, rsp);
 		mbox_put(mbox);
+		if (rc)
+			goto alloc_err;
 	} while (pend);
 
 	nix->tm_link_cfg_lvl = rsp->link_cfg_lvl;
drivers/common/cnxk/roc_nix_tm_ops.c +6 -2
index 230e9b72f6..1086f471e6 100644
@@ -778,8 +778,10 @@ roc_nix_tm_prealloc_res(struct roc_nix *roc_nix, uint8_t lvl,
 			return rc;
 		}
 
-		nix_tm_copy_rsp_to_nix(nix, rsp);
+		rc = nix_tm_copy_rsp_to_nix(nix, rsp);
 		mbox_put(mbox);
+		if (rc)
+			return rc;
 	}
 
 	/* Preallocate contiguous */
@@ -797,8 +799,10 @@ roc_nix_tm_prealloc_res(struct roc_nix *roc_nix, uint8_t lvl,
 			return rc;
 		}
 
-		nix_tm_copy_rsp_to_nix(nix, rsp);
+		rc = nix_tm_copy_rsp_to_nix(nix, rsp);
 		mbox_put(mbox);
+		if (rc)
+			return rc;
 	}
 
 	/* Save thresholds */
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 3/5 common/cnxk: fix null callback deref in CPT IRQ

In reply to Aarnav JP

nix_inl_cpt_cq_cb() dispatched through inl_dev->work_cb() without a NULL check, unlike the SSO sibling path. A CPT completion processed before the callback is registered or after it is cleared caused a NULL function-pointer call on the interrupt thread, crashing the PMD.

Snapshot the callback once and skip dispatch when it is NULL; the entries are still drained and the interrupt acknowledged, so the queue does not stall.

Fixes a7d64a7740e0 ("net/cnxk: handle soft expiry")
---
 drivers/common/cnxk/roc_nix_inl_dev_irq.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/common/cnxk/roc_nix_inl_dev_irq.c +6 -1
index 977b5e05e2..82f8c5edfb 100644
@@ -50,6 +50,7 @@ nix_inl_cpt_cq_cb(struct roc_cpt_lf *lf)
 {
 	struct idev_cfg *idev = idev_get_cfg();
 	struct nix_inl_dev *inl_dev = NULL;
+	roc_nix_inl_sso_work_cb_t work_cb;
 	enum nix_inl_event_type cq_type;
 	uint32_t port_id = UINT32_MAX;
 	union cpt_lf_cq_base cq_base;
@@ -102,6 +103,9 @@ nix_inl_cpt_cq_cb(struct roc_cpt_lf *lf)
 		port_id = roc_nix->port_id;
 	}
 
+	/* Snapshot once so the test and the call use the same pointer */
+	work_cb = inl_dev->work_cb;
+
 	for (i = 0; i < count; i++) {
 		cq_s = (struct cpt_cq_s *)(uintptr_t)((((uint64_t)cq_base.s.addr << 7)) +
 						      (head << 5));
@@ -129,7 +133,8 @@ nix_inl_cpt_cq_cb(struct roc_cpt_lf *lf)
 				goto done;
 			}
 			gw[1] = (uint64_t)(uintptr_t)wqe;
-			inl_dev->work_cb(gw, sa, cq_type, (void *)cq_s, port_id);
+			if (work_cb)
+				work_cb(gw, sa, cq_type, (void *)cq_s, port_id);
 		}
 done:
 		head = (head + 1) % lf->cq_size;
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 4/5 common/cnxk: fix OOB access in RQ FC config

In reply to Aarnav JP

nix_fc_rq_config_set() indexes nix->rqs[] with the caller-supplied fc_cfg->rq_cfg.rq without any bound or NULL check, unlike roc_nix_rq_init(). An out-of-range rq reads a pointer past the nb_rx_queues-sized array and then dereferences and writes through it (rq->tc), and an in-range but uninitialised rq is a NULL deref.

Reject rq >= nb_rx_queues with NIX_ERR_QUEUE_INVALID_RANGE and a NULL rq with NIX_ERR_PARAM before use, matching the sibling ROC helpers.

Fixes 78fb5efed869 ("common/cnxk: remove flow control config at queue setup")
---
 drivers/common/cnxk/roc_nix_fc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/common/cnxk/roc_nix_fc.c +5 -0
index abf923b722..8164a2ba0e 100644
@@ -358,7 +358,12 @@ nix_fc_rq_config_set(struct roc_nix *roc_nix, struct roc_nix_fc_cfg *fc_cfg)
 	struct roc_nix_rq *rq;
 	int rc;
 
+	if (fc_cfg->rq_cfg.rq >= nix->nb_rx_queues)
+		return NIX_ERR_QUEUE_INVALID_RANGE;
+
 	rq = nix->rqs[fc_cfg->rq_cfg.rq];
+	if (rq == NULL)
+		return NIX_ERR_PARAM;
 
 	if (rq->sso_ena) {
 		pool_drop_pct = fc_cfg->rq_cfg.pool_drop_pct;
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 5/5 common/cnxk: fix OOB write in rep count copy

In reply to Aarnav JP

roc_nix_max_rep_count() copied rsp->rep_cnt entries from the get_rep_cnt mailbox response into the fixed 64-entry roc_nix->rep_pfvf_map[] without validating the AF-supplied count. rep_cnt is a uint16_t, so a malformed or malicious AF response could overrun the array (and read past the response's 64-entry map), corrupting adjacent driver state.

Reject responses with rep_cnt greater than MAX_PFVF_REP before copying.

Fixes 162c77c18838 ("common/cnxk: add support for representors")
---
 drivers/common/cnxk/roc_nix.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/common/cnxk/roc_nix.c +6 -0
index 8e3903051d..ae6a918682 100644
@@ -589,6 +589,12 @@ roc_nix_max_rep_count(struct roc_nix *roc_nix)
 	if (rc)
 		goto exit;
 
+	if (rsp->rep_cnt > MAX_PFVF_REP) {
+		plt_err("Invalid rep_cnt %u in get_rep_cnt rsp", rsp->rep_cnt);
+		rc = -EIO;
+		goto exit;
+	}
+
 	roc_nix->rep_cnt = rsp->rep_cnt;
 	for (i = 0; i < rsp->rep_cnt; i++)
 		roc_nix->rep_pfvf_map[i] = rsp->rep_pfvf_map[i];
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 5/5] common/cnxk: fix OOB write in rep count copy

Quoted 7 lines · Aarnav JP

roc_nix_max_rep_count() copied rsp->rep_cnt entries from the get_rep_cnt mailbox response into the fixed 64-entry roc_nix->rep_pfvf_map[] without validating the AF-supplied count. rep_cnt is a uint16_t, so a malformed or malicious AF response could overrun the array (and read past the response's 64-entry map), corrupting adjacent driver state.

Quoted 6 lines

Reject responses with rep_cnt greater than MAX_PFVF_REP before copying.

Fixes 162c77c18838 ("common/cnxk: add support for representors")

Series applied to dpdk-next-net-mrvl/for-main. Thanks

Quoted 24 lines
---
 drivers/common/cnxk/roc_nix.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/common/cnxk/roc_nix.c +6 -1
index 8e3903051d..ae6a918682 100644
@@ -589,6 +589,12 @@ roc_nix_max_rep_count(struct roc_nix *roc_nix)
        if (rc)
                goto exit;
 
+       if (rsp->rep_cnt > MAX_PFVF_REP) {
+               plt_err("Invalid rep_cnt %u in get_rep_cnt rsp", rsp->rep_cnt);
+               rc = -EIO;
+               goto exit;
+       }
+
        roc_nix->rep_cnt = rsp->rep_cnt;
        for (i = 0; i < rsp->rep_cnt; i++)
                roc_nix->rep_pfvf_map[i] = rsp->rep_pfvf_map[i];
--

2.43.0