Skip to contentSkip to conversation

RDMA/irdma: Fix validation of user and iWARP input

PATCH New Jacob Moroni 2 participants 10 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover RDMA/irdma: Fix validation of user and iWARP input

This series fixes several issues in irdma where input from user space or from the network is not validated properly.

Patches 1-5 are reachable from user space through uverbs:
- Patches 1 and 2 fix QP and SRQ ID leaks in the create error paths.
- Patches 3-5 add missing validation of the queue memory and queue sizes provided by user space. rdma-core does not send any of the requests that are now rejected.

Patches 6-8 are in the iWARP connection setup path, where the driver parses raw TCP segments and MPA frames from the peer:
- Patches 6 and 7 validate the TCP header length and the TCP options.
- Patch 8 fixes the handling of the MPA v2 private data length.

Testing:
- Patches 1-5 were tested on GEN3 HW. perftest, pyverbs and a subset of rdma-unit-test give the same results with and without them. A test program that issues the malformed requests through the uverbs write interface confirms that they are now rejected and that the IDs are no longer leaked.
- Patches 6-8 were tested on an E830 in iWARP mode as described in each patch, with the whole series applied.

Jacob Moroni (8):
RDMA/irdma: Fix QP ID leak when QP creation fails early RDMA/irdma: Fix SRQ ID leak when the SRQ depth is rejected RDMA/irdma: Reject a PBL backed CQ shadow area RDMA/irdma: Validate legacy user QP sizes RDMA/irdma: Check user queue buffers against the queue size RDMA/irdma: Validate iWARP TCP header lengths RDMA/irdma: Validate iWARP TCP option lengths RDMA/irdma: Fix MPA v2 private data length handling

 drivers/infiniband/hw/irdma/cm.c    | 64 +++++++++++++++++++++--------
 drivers/infiniband/hw/irdma/puda.c  |  4 ++
 drivers/infiniband/hw/irdma/utils.c |  7 ++++
 drivers/infiniband/hw/irdma/verbs.c | 53 ++++++++++++++++++++++--
 drivers/infiniband/hw/irdma/verbs.h |  4 ++
 5 files changed, 110 insertions(+), 22 deletions(-)

base-commit: a8671ee9bde18105775c2307bc71b0933bd24ed4

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw

PATCH 1/8 RDMA/irdma: Fix QP ID leak when QP creation fails early

irdma_free_qp_rsrc() frees the QP ID stored in sc_qp.qp_uk.qp_id, but that field is not set until irdma_uk_qp_init(). If QP creation fails before then, the field is still zero and the allocated ID is leaked. For a GEN3 GSI QP, the vport and hwqp1_rsvd are not released either.

Fix this by storing the QP ID as soon as it is allocated.

While in there, remove the redundant assignment of qp.

Fixes d6ed4b69b8ea ("RDMA/irdma: Add GEN3 virtual QP1 support")
---
 drivers/infiniband/hw/irdma/verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/irdma/verbs.c +1 -1
index 04ebba436eea..5c4da4bb0779 100644
@@ -1117,8 +1117,8 @@ static int irdma_create_qp(struct ib_qp *ibqp,
 		iwqp->ibqp.qp_num = qp_num;
 	}
 
+	qp->qp_uk.qp_id = qp_num;
 	iwqp->iwpd = iwpd;
-	qp = &iwqp->sc_qp;
 	iwqp->iwscq = to_iwcq(init_attr->send_cq);
 	iwqp->iwrcq = to_iwcq(init_attr->recv_cq);
 	iwqp->host_ctx.va = init_info.host_ctx;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#2 lore raw

PATCH 2/8 RDMA/irdma: Fix SRQ ID leak when the SRQ depth is rejected

In reply to Jacob Moroni

irdma_create_srq() allocates the SRQ ID before it validates the requested depth, and returns directly if the depth is rejected, so the ID is leaked.

max_wr comes from user space, so a user can repeat this until no SRQ IDs are left.

Fix this by jumping to the existing unwind label to free the ID.

Fixes 563e1feb5f6e ("RDMA/irdma: Add SRQ support")
---
 drivers/infiniband/hw/irdma/verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/irdma/verbs.c +1 -1
index 5c4da4bb0779..9a673669b72a 100644
@@ -2603,7 +2603,7 @@ static int irdma_create_srq(struct ib_srq *ibsrq,
 	err_code = irdma_get_srqdepth(ukinfo->uk_attrs, attr->max_wr,
 				      shift, &depth);
 	if (err_code)
-		return err_code;
+		goto free_rsrc;
 
 	/* Actual SRQ size in WRs for ring and HW */
 	ukinfo->srq_size = depth >> shift;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#3 lore raw

PATCH 3/8 RDMA/irdma: Reject a PBL backed CQ shadow area

In reply to Jacob Moroni

On devices with IRDMA_FEATURE_CQ_RESIZE, the CQ shadow area is a separate user registration. irdma_setup_umode_cq() takes its DMA address from cq_pbl.addr, which is a union with the PBLE index and only holds an address if the registration is not PBL backed.

rdma-core registers the shadow area as a single page, which never uses a PBL. However, a user can register it with more than one page of non-contiguous memory, which then causes the PBLE index to be given to the HW instead of the DMA address that the HW expects.

The hardware cannot take the shadow area through a PBL, so fix this by rejecting such a registration.

Fixes b48c24c2d710 ("RDMA/irdma: Implement device supported verb APIs")
---
 drivers/infiniband/hw/irdma/verbs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/infiniband/hw/irdma/verbs.c +2 -0
index 9a673669b72a..93328b0abcbd 100644
@@ -2938,6 +2938,8 @@ static int irdma_setup_umode_cq(struct irdma_device *iwdev,
 				return -EPROTO;
 		}
 		cqmr_shadow = &iwcq->iwpbl_shadow->cq_mr;
+		if (iwcq->iwpbl_shadow->pbl_allocated)
+			return -EINVAL;
 		info->shadow_area_pa = cqmr_shadow->cq_pbl.addr;
 	} else {
 		info->shadow_area_pa = cqmr->shadow;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#4 lore raw

PATCH 4/8 RDMA/irdma: Validate legacy user QP sizes

In reply to Jacob Moroni

A user context without IRDMA_ALLOC_UCTX_USE_RAW_ATTR passes SQ and RQ sizes in units of WQEs, which the kernel then multiplies by the WQE size to get the actual ring sizes. Only the values from user space are checked against the HW limits and there is no lower bound enforced.

An SQ size of zero gives an empty ring, which causes a divide by zero in the various ring macros. The resulting ring sizes (post multiplication) can also exceed the HW limits.

Fix this by rejecting an empty SQ and ring sizes above max_hw_wq_quanta and max_hw_rq_quanta, similar to how it's done for the current ABI.

Fixes b48c24c2d710 ("RDMA/irdma: Implement device supported verb APIs")
---
 drivers/infiniband/hw/irdma/verbs.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/infiniband/hw/irdma/verbs.c +7 -0
index 93328b0abcbd..de727270ab87 100644
@@ -677,6 +677,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;
+	u32 sq_quanta, rq_quanta;
 	unsigned long flags;
 	int ret;
 
@@ -696,6 +697,12 @@ static int irdma_setup_umode_qp(struct irdma_ucontext *ucontext,
 		ukinfo->rq_size = init_attr->cap.max_recv_wr;
 		irdma_uk_calc_shift_wq(ukinfo, &ukinfo->sq_shift,
 				       &ukinfo->rq_shift);
+		sq_quanta = ukinfo->sq_size << ukinfo->sq_shift;
+		rq_quanta = ukinfo->rq_size << ukinfo->rq_shift;
+		if (!sq_quanta ||
+		    sq_quanta > ukinfo->uk_attrs->max_hw_wq_quanta ||
+		    rq_quanta > ukinfo->uk_attrs->max_hw_rq_quanta)
+			return -EINVAL;
 	} else {
 		ret = irdma_uk_calc_depth_shift_sq(ukinfo, &ukinfo->sq_depth,
 						   &ukinfo->sq_shift);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#5 lore raw

PATCH 5/8 RDMA/irdma: Check user queue buffers against the queue size

In reply to Jacob Moroni

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")
---
 drivers/infiniband/hw/irdma/verbs.c | 40 +++++++++++++++++++++++++++--
 drivers/infiniband/hw/irdma/verbs.h |  4 +++
 2 files changed, 42 insertions(+), 2 deletions(-)
drivers/infiniband/hw/irdma/verbs.c +38 -2
index de727270ab87..3eb0d4c77c87 100644
@@ -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))
drivers/infiniband/hw/irdma/verbs.h +4 -0
index 6f696a9cf107..a24cc75f1e04 100644
@@ -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 {
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#6 lore raw

PATCH 6/8 RDMA/irdma: Validate iWARP TCP header lengths

In reply to Jacob Moroni

For iWARP, raw TCP segments are delivered to the driver via the ILQ and IEQ. These segments never go through the kernel TCP stack so the driver must perform its own validation. The issue is that the TCP header length (data offset) is used without any validation.

If the data offset is below 5, the computed options size ends up negative and irdma_process_options() walks past the end of the segment and if the header length is larger than what was actually received, buf->datalen wraps and the MPA parser is handed a bogus length. This can be hit without an established connection.

Fix this by dropping segments where the TCP header is too short or does not fit the packet.

Previously, header parsing could only fail on GEN1, so the error path in irdma_puda_poll_cmpl() needed fixing as well now that it can fail on GEN2.

Tested on an Intel E830 in iWARP mode by sending crafted SYN segments with a data offset below 5 and with a data offset past the end of the packet. Both are dropped, and a well-formed SYN is still accepted.

Fixes 915cc7ac0f8e ("RDMA/irdma: Add miscellaneous utility definitions")
Fixes a3a06db504d3 ("RDMA/irdma: Add privileged UDA queue implementation")
---
 drivers/infiniband/hw/irdma/puda.c  | 4 ++++
 drivers/infiniband/hw/irdma/utils.c | 7 +++++++
 2 files changed, 11 insertions(+)
drivers/infiniband/hw/irdma/puda.c +4 -0
index 4f1a8c97faf1..46eb10d6c9dc 100644
@@ -373,9 +373,13 @@ int irdma_puda_poll_cmpl(struct irdma_sc_dev *dev, struct irdma_sc_cq *cq,
 				irdma_ilq_putback_rcvbuf(&rsrc->qp, buf,
 							 info.wqe_idx);
 			} else {
+				if (!rsrc->rxq_invalid_cnt)
+					rsrc->rx_wqe_idx = info.wqe_idx;
+				rsrc->rxq_invalid_cnt++;
 				irdma_puda_ret_bufpool(rsrc, buf);
 				irdma_puda_replenish_rq(rsrc, false);
 			}
+			ret = 0;
 			goto done;
 		}
 
drivers/infiniband/hw/irdma/utils.c +7 -0
index f4bc08ae0553..c2b89e166ffe 100644
@@ -1598,6 +1598,9 @@ static int irdma_gen1_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
 	}
 
 	buf->tcphlen = tcph->doff << 2;
+	if (buf->tcphlen < sizeof(*tcph) || pkt_len < iphlen + buf->tcphlen)
+		return -EINVAL;
+
 	buf->datalen = pkt_len - iphlen - buf->tcphlen;
 	buf->data = buf->datalen ? buf->tcph + buf->tcphlen : NULL;
 	buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
@@ -1637,6 +1640,10 @@ int irdma_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
 	pkt_len = info->payload_len;
 	buf->totallen = pkt_len;
 	buf->tcphlen = tcph->doff << 2;
+	if (buf->tcphlen < sizeof(*tcph) ||
+	    pkt_len < IRDMA_TCP_OFFSET + buf->tcphlen)
+		return -EINVAL;
+
 	buf->datalen = pkt_len - IRDMA_TCP_OFFSET - buf->tcphlen;
 	buf->data = buf->datalen ? buf->tcph + buf->tcphlen : NULL;
 	buf->hdrlen = IRDMA_TCP_OFFSET + buf->tcphlen;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#7 lore raw

PATCH 7/8 RDMA/irdma: Validate iWARP TCP option lengths

In reply to Jacob Moroni

irdma_process_options() walks the TCP options using the length byte of each option, but only the MSS option has its length checked. The issue is that an option with a length of zero never advances the offset, so the loop never terminates and the CPU is stuck in the CEQ tasklet. A length that runs past the end of the options results in an out of bounds read. This can be hit with a single SYN to a listening port.

Fix this by checking that each option length is at least 2 and fits in what is left of the options. Also reject a negative options size, require the window scale option to be 3 bytes, and clamp the shift count to 14 as per RFC 7323.

When a SYN has bad options, irdma_handle_tcp_options() no longer calls irdma_passive_open_err() since irdma_handle_syn_pkt() already does. Calling it twice underflows the cm_node refcount on GEN1.

Tested on an Intel E830 in iWARP mode by sending crafted SYNs with a zero-length option, an option length past the end, and an oversized window scale. The first two are dropped, the scale is clamped, and a well-formed SYN is still accepted.

Fixes 146b9756f14c ("RDMA/irdma: Add connection manager")
---
 drivers/infiniband/hw/irdma/cm.c | 58 ++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 18 deletions(-)
drivers/infiniband/hw/irdma/cm.c +40 -18
index bc30e465c670..b3394f084098 100644
@@ -693,22 +693,35 @@ static int irdma_process_options(struct irdma_cm_node *cm_node, u8 *optionsloc,
 	u32 offset = 0;
 	union all_known_options *all_options;
 	char got_mss_option = 0;
+	u8 optlen;
 
 	while (offset < optionsize) {
 		all_options = (union all_known_options *)(optionsloc + offset);
-		switch (all_options->base.optionnum) {
-		case OPTION_NUM_EOL:
-			offset = optionsize;
+		if (all_options->base.optionnum == OPTION_NUM_EOL)
 			break;
-		case OPTION_NUM_NONE:
+
+		if (all_options->base.optionnum == OPTION_NUM_NONE) {
 			offset += 1;
 			continue;
+		}
+
+		/* Every other option has a length, which counts the kind and
+		 * length bytes as well and has to fit in what is left.
+		 */
+		if (optionsize - offset < 2)
+			return -EINVAL;
+
+		optlen = all_options->base.len;
+		if (optlen < 2 || optlen > optionsize - offset)
+			return -EINVAL;
+
+		switch (all_options->base.optionnum) {
 		case OPTION_NUM_MSS:
 			ibdev_dbg(&cm_node->iwdev->ibdev,
 				  "CM: MSS Length: %d Offset: %d Size: %d\n",
-				  all_options->mss.len, offset, optionsize);
+				  optlen, offset, optionsize);
 			got_mss_option = 1;
-			if (all_options->mss.len != 4)
+			if (optlen != 4)
 				return -EINVAL;
 			tmp = ntohs(all_options->mss.mss);
 			if ((cm_node->ipv4 &&
@@ -720,8 +733,14 @@ static int irdma_process_options(struct irdma_cm_node *cm_node, u8 *optionsloc,
 				cm_node->tcp_cntxt.mss = tmp;
 			break;
 		case OPTION_NUM_WINDOW_SCALE:
+			if (optlen != 3)
+				return -EINVAL;
+			/* A larger shift count is treated as the largest one,
+			 * as per RFC 7323.
+			 */
 			cm_node->tcp_cntxt.snd_wscale =
-				all_options->windowscale.shiftcount;
+				min_t(u8, all_options->windowscale.shiftcount,
+				      TCP_MAX_WSCALE);
 			break;
 		default:
 			ibdev_dbg(&cm_node->iwdev->ibdev,
@@ -729,7 +748,7 @@ static int irdma_process_options(struct irdma_cm_node *cm_node, u8 *optionsloc,
 				  all_options->base.optionnum);
 			break;
 		}
-		offset += all_options->base.len;
+		offset += optlen;
 	}
 	if (!got_mss_option && syn_pkt)
 		cm_node->tcp_cntxt.mss = IRDMA_CM_DEFAULT_MSS;
@@ -749,20 +768,23 @@ static int irdma_handle_tcp_options(struct irdma_cm_node *cm_node,
 				    int passive)
 {
 	u8 *optionsloc = (u8 *)&tcph[1];
-	int ret;
+	int ret = 0;
 
-	if (optionsize) {
+	if (optionsize < 0)
+		ret = -EINVAL;
+	else if (optionsize)
 		ret = irdma_process_options(cm_node, optionsloc, optionsize,
 					    (u32)tcph->syn);
-		if (ret) {
-			ibdev_dbg(&cm_node->iwdev->ibdev,
-				  "CM: Node %p, Sending Reset\n", cm_node);
-			if (passive)
-				irdma_passive_open_err(cm_node, true);
-			else
-				irdma_active_open_err(cm_node, true);
+	if (ret) {
+		/* The caller drops a bad SYN on a listening node itself */
+		if (cm_node->state == IRDMA_CM_STATE_LISTENING)
 			return ret;
-		}
+
+		if (passive)
+			irdma_passive_open_err(cm_node, true);
+		else
+			irdma_active_open_err(cm_node, true);
+		return ret;
 	}
 
 	cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#8 lore raw

PATCH 8/8 RDMA/irdma: Fix MPA v2 private data length handling

In reply to Jacob Moroni

For MPA v2, the private data length in the frame includes the 4 byte RTR message that precedes the private data. irdma_build_mpa_v2() sets it up this way, but irdma_parse_mpa() treats the entire length as private data.

The issue is that a v2 frame with a length below 4 passes the length check without containing a full RTR message, so the RTR message is read from past the end of the received data. For a valid v2 frame, the private data copy reads 4 bytes past the end of the received data and hands them to the application.

Fix this by rejecting v2 frames with a private data length below 4 and by excluding the RTR message from the private data. Note that the private data length reported to the application is now the length that the peer actually sent.

Tested on an E830 in iWARP mode with a small program that connects over loopback with known private data in both directions (verified with 0 bytes and 16 bytes). The private data now matches what was sent and MPA v2 is still negotiated.

Fixes 146b9756f14c ("RDMA/irdma: Add connection manager")
---
 drivers/infiniband/hw/irdma/cm.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/infiniband/hw/irdma/cm.c +6 -0
index b3394f084098..d39e0e174fbf 100644
@@ -1112,6 +1112,12 @@ static int irdma_parse_mpa(struct irdma_cm_node *cm_node, u8 *buf, u32 *type,
 
 	switch (mpa_frame->rev) {
 	case IETF_MPA_V2:
+		/* The private data length of a v2 frame includes the RTR
+		 * message, which is not private data.
+		 */
+		if (priv_data_len < IETF_RTR_MSG_SIZE)
+			return -EINVAL;
+		priv_data_len -= IETF_RTR_MSG_SIZE;
 		mpa_hdr_len += IETF_RTR_MSG_SIZE;
 		ret = irdma_negotiate_mpa_v2_ird_ord(cm_node, buf);
 		if (ret)
Signature · 1 line
2.56.1.839.g22a8290db9-goog