Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: Jacob Moroni <jmoroni@google.com>
To: tatyana.e.nikolova@intel.com, jgg@ziepe.ca, leon@kernel.org
Cc: linux-rdma@vger.kernel.org, jdz@google.com,
	 Jacob Moroni <jmoroni@google.com>
Subject: [PATCH rdma-next 5/8] RDMA/irdma: Check user queue buffers against the queue size
Date: Mon, 30 Nov 2026 01:33:04 +0000	[thread overview]
Message-ID: <20261130013307.2848330-6-jmoroni@google.com> (raw)
In-Reply-To: <20261130013307.2848330-1-jmoroni@google.com>

In the legacy two-step approach, user space first registers the
memory for a QP/CQ/SRQ (giving the number of pages for each ring),
then creates the object (giving the ring size). The issue is that
the number of pages provided in step 1 and the ring size provided
in step 2 are never compared.

A user can therefore register a single page and create or resize a
queue of any size on top of it. The hardware then accesses memory past
the end of the registration, or PBLEs that belong to other
registrations.

Fix this by storing the page counts in the registration and rejecting
a queue that does not fit in them. rdma-core derives the page counts
from the same sizes, so it is not affected.

Fixes: b48c24c2d710 ("RDMA/irdma: Implement device supported verb APIs")
Signed-off-by: Jacob Moroni <jmoroni@google.com>
---
 drivers/infiniband/hw/irdma/verbs.c | 40 +++++++++++++++++++++++++++--
 drivers/infiniband/hw/irdma/verbs.h |  4 +++
 2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/drivers/infiniband/hw/irdma/verbs.c b/drivers/infiniband/hw/irdma/verbs.c
index de727270ab87..3eb0d4c77c87 100644
--- a/drivers/infiniband/hw/irdma/verbs.c
+++ b/drivers/infiniband/hw/irdma/verbs.c
@@ -658,6 +658,16 @@ static void irdma_setup_virt_qp(struct irdma_device *iwdev,
 	}
 }
 
+/**
+ * irdma_wq_pages - Number of HW pages covered by a given ring size (in quanta)
+ * @quanta: ring size in quanta
+ */
+static u32 irdma_wq_pages(u32 quanta)
+{
+	return max(DIV_ROUND_UP(quanta * IRDMA_QP_WQE_MIN_SIZE,
+				IRDMA_HW_PAGE_SIZE), 1U);
+}
+
 /**
  * irdma_setup_umode_qp - setup sq and rq size in user mode qp
  * @ucontext: user context
@@ -677,6 +687,7 @@ static int irdma_setup_umode_qp(struct irdma_ucontext *ucontext,
 				struct uverbs_attr_bundle *attrs)
 {
 	struct irdma_qp_uk_init_info *ukinfo = &info->qp_uk_init_info;
+	struct irdma_qp_mr *qpmr;
 	u32 sq_quanta, rq_quanta;
 	unsigned long flags;
 	int ret;
@@ -720,6 +731,8 @@ static int irdma_setup_umode_qp(struct irdma_ucontext *ucontext,
 			(ukinfo->rq_depth - IRDMA_RQ_RSVD) >> ukinfo->rq_shift;
 		ukinfo->sq_size = ukinfo->sq_depth >> ukinfo->sq_shift;
 		ukinfo->rq_size = ukinfo->rq_depth >> ukinfo->rq_shift;
+		sq_quanta = ukinfo->sq_depth;
+		rq_quanta = ukinfo->rq_depth;
 	}
 
 	if (attrs && uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_QP_BUF_UMEM)) {
@@ -768,6 +781,11 @@ static int irdma_setup_umode_qp(struct irdma_ucontext *ucontext,
 		}
 	}
 
+	qpmr = &iwqp->iwpbl->qp_mr;
+	if (qpmr->sq_pages < irdma_wq_pages(sq_quanta) ||
+	    qpmr->rq_pages < irdma_wq_pages(rq_quanta))
+		return -EINVAL;
+
 	irdma_setup_virt_qp(iwdev, iwqp, info);
 
 	return 0;
@@ -2256,6 +2274,13 @@ static int irdma_resize_cq(struct ib_cq *ibcq, unsigned int entries,
 			return -ENOMEM;
 
 		cqmr_buf = &iwpbl_buf->cq_mr;
+		cqe_size = iwcq->sc_cq.cq_uk.avoid_mem_cflct ? 64 : 32;
+		if (cqmr_buf->cq_pages < DIV_ROUND_UP(info.cq_size * cqe_size,
+						      IRDMA_HW_PAGE_SIZE)) {
+			ret = -EINVAL;
+			goto error;
+		}
+
 		if (iwpbl_buf->pbl_allocated) {
 			info.virtual_map = true;
 			info.pbl_chunk_size = 1;
@@ -2454,6 +2479,7 @@ static int irdma_setup_umode_srq(struct irdma_device *iwdev,
 	struct irdma_srq_mr *srqmr;
 	struct irdma_pbl *iwpbl;
 	unsigned long flags;
+	u8 shift;
 	int ret;
 
 	iwsrq->user_mode = true;
@@ -2464,13 +2490,14 @@ static int irdma_setup_umode_srq(struct irdma_device *iwdev,
 	if (ret)
 		return ret;
 
+	irdma_get_wqe_shift(ukinfo->uk_attrs, ukinfo->max_srq_frag_cnt, 0,
+			    &shift);
+
 	if (attrs && uverbs_attr_is_valid(attrs, UVERBS_ATTR_CREATE_SRQ_BUF_UMEM)) {
 		struct irdma_mem_reg_req srq_req = {};
 		struct ib_umem *umem;
 		u32 total;
-		u8 shift;
 
-		irdma_get_wqe_shift(ukinfo->uk_attrs, ukinfo->max_srq_frag_cnt, 0, &shift);
 		srq_req.rq_pages =
 			DIV_ROUND_UP((ukinfo->srq_size << shift) * IRDMA_QP_WQE_MIN_SIZE,
 				     IRDMA_HW_PAGE_SIZE);
@@ -2508,6 +2535,8 @@ static int irdma_setup_umode_srq(struct irdma_device *iwdev,
 		iwsrq->iwpbl = iwpbl;
 	}
 	srqmr = &iwpbl->srq_mr;
+	if (srqmr->rq_pages < irdma_wq_pages(ukinfo->srq_size << shift))
+		return -EINVAL;
 
 	if (iwpbl->pbl_allocated) {
 		info->virtual_map = true;
@@ -2904,6 +2933,9 @@ static int irdma_setup_umode_cq(struct irdma_device *iwdev,
 	}
 
 	cqmr = &iwcq->iwpbl->cq_mr;
+	if (cqmr->cq_pages < DIV_ROUND_UP(ukinfo->cq_size * cqe_size,
+					  IRDMA_HW_PAGE_SIZE))
+		return -EINVAL;
 
 	if (rf->sc_dev.hw_attrs.uk_attrs.feature_flags &
 	    IRDMA_FEATURE_CQ_RESIZE) {
@@ -3307,6 +3339,8 @@ static int irdma_handle_q_mem(struct irdma_device *iwdev,
 	case IRDMA_MEMREG_TYPE_QP:
 		total = req->sq_pages + req->rq_pages;
 		hmc_p = &qpmr->sq_pbl;
+		qpmr->sq_pages = req->sq_pages;
+		qpmr->rq_pages = req->rq_pages;
 		qpmr->shadow = (dma_addr_t)arr[total];
 		/* Need to use physical address for RQ of QP
 		 * in case it is associated with SRQ.
@@ -3333,6 +3367,7 @@ static int irdma_handle_q_mem(struct irdma_device *iwdev,
 		break;
 	case IRDMA_MEMREG_TYPE_SRQ:
 		hmc_p = &srqmr->srq_pbl;
+		srqmr->rq_pages = req->rq_pages;
 		srqmr->shadow = (dma_addr_t)arr[req->rq_pages];
 		if (lvl)
 			ret = irdma_check_mem_contiguous(arr, req->rq_pages,
@@ -3345,6 +3380,7 @@ static int irdma_handle_q_mem(struct irdma_device *iwdev,
 	break;
 	case IRDMA_MEMREG_TYPE_CQ:
 		hmc_p = &cqmr->cq_pbl;
+		cqmr->cq_pages = req->cq_pages;
 
 		if (!(iwdev->rf->sc_dev.hw_attrs.uk_attrs.feature_flags &
 		      IRDMA_FEATURE_CQ_RESIZE))
diff --git a/drivers/infiniband/hw/irdma/verbs.h b/drivers/infiniband/hw/irdma/verbs.h
index 6f696a9cf107..a24cc75f1e04 100644
--- a/drivers/infiniband/hw/irdma/verbs.h
+++ b/drivers/infiniband/hw/irdma/verbs.h
@@ -65,11 +65,13 @@ struct irdma_hmc_pble {
 struct irdma_cq_mr {
 	struct irdma_hmc_pble cq_pbl;
 	dma_addr_t shadow;
+	u16 cq_pages;
 };
 
 struct irdma_srq_mr {
 	struct irdma_hmc_pble srq_pbl;
 	dma_addr_t shadow;
+	u16 rq_pages;
 };
 
 struct irdma_qp_mr {
@@ -78,6 +80,8 @@ struct irdma_qp_mr {
 	dma_addr_t shadow;
 	dma_addr_t rq_pa;
 	struct page *sq_page;
+	u16 sq_pages;
+	u16 rq_pages;
 };
 
 struct irdma_cq_buf {
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30  1:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  1:32 [PATCH rdma-next 0/8] RDMA/irdma: Fix validation of user and iWARP input Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 1/8] RDMA/irdma: Fix QP ID leak when QP creation fails early Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 2/8] RDMA/irdma: Fix SRQ ID leak when the SRQ depth is rejected Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 3/8] RDMA/irdma: Reject a PBL backed CQ shadow area Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 4/8] RDMA/irdma: Validate legacy user QP sizes Jacob Moroni
2026-11-30  1:33 ` Jacob Moroni [this message]
2026-11-30  1:33 ` [PATCH rdma-next 6/8] RDMA/irdma: Validate iWARP TCP header lengths Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 7/8] RDMA/irdma: Validate iWARP TCP option lengths Jacob Moroni
2026-11-30  1:33 ` [PATCH rdma-next 8/8] RDMA/irdma: Fix MPA v2 private data length handling Jacob Moroni
2026-11-30  1:44 ` [PATCH rdma-next 0/8] RDMA/irdma: Fix validation of user and iWARP input sashiko-bot

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=20261130013307.2848330-6-jmoroni@google.com \
    --to=jmoroni@google.com \
    --cc=jdz@google.com \
    --cc=jgg@ziepe.ca \
    --cc=leon@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=tatyana.e.nikolova@intel.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox