Skip to contentSkip to conversation

RDMA/rxe: Implement packet length validation on responder

PATCH Applied Daisuke Matsuda 4 participants 4 messages Nov 7, 2022 – Nov 9, 2022

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

PATCH v2 1/1 RDMA/rxe: Implement packet length validation on responder

The function check_length() is supposed to check the length of inbound packets on responder, but it actually has been a stub since the driver was born. Let it check the payload length and the DMA length.

---
FOR REVIEWERS
I referred to IB Specification Vol 1-Revision-1.5 to create this patch. Please see 9.7.4.1.6 (page.330).

v2: Fixed the conditional for 'last' packets. Thanks, Zhijian.

 drivers/infiniband/sw/rxe/rxe_resp.c | 34 ++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 7 deletions(-)
drivers/infiniband/sw/rxe/rxe_resp.c +27 -7
index c32bc12cc82f..382d2053db43 100644
@@ -393,16 +393,36 @@ static enum resp_states check_resource(struct rxe_qp *qp,
 static enum resp_states check_length(struct rxe_qp *qp,
 				     struct rxe_pkt_info *pkt)
 {
-	switch (qp_type(qp)) {
-	case IB_QPT_RC:
-		return RESPST_CHK_RKEY;
+	int mtu = qp->mtu;
+	u32 payload = payload_size(pkt);
+	u32 dmalen = reth_len(pkt);
 
-	case IB_QPT_UC:
-		return RESPST_CHK_RKEY;
+	/* RoCEv2 packets do not have LRH.
+	 * Let's skip checking it.
+	 */
 
-	default:
-		return RESPST_CHK_RKEY;
+	if ((pkt->opcode & RXE_START_MASK) &&
+	    (pkt->opcode & RXE_END_MASK)) {
+		/* "only" packets */
+		if (payload > mtu)
+			return RESPST_ERR_LENGTH;
+	} else if ((pkt->opcode & RXE_START_MASK) ||
+		   (pkt->opcode & RXE_MIDDLE_MASK)) {
+		/* "first" or "middle" packets */
+		if (payload != mtu)
+			return RESPST_ERR_LENGTH;
+	} else if (pkt->opcode & RXE_END_MASK) {
+		/* "last" packets */
+		if ((payload == 0) || (payload > mtu))
+			return RESPST_ERR_LENGTH;
+	}
+
+	if (pkt->opcode & (RXE_WRITE_MASK | RXE_READ_MASK)) {
+		if (dmalen > (1 << 31))
+			return RESPST_ERR_LENGTH;
 	}
+
+	return RESPST_CHK_RKEY;
 }
 
 static enum resp_states check_rkey(struct rxe_qp *qp,
Signature · 1 line
2.31.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Daisuke Matsuda

The function check_length() is supposed to check the length of inbound packets on responder, but it actually has been a stub since the driver was born. Let it check the payload length and the DMA length.

Quoted 7 lines

---
FOR REVIEWERS
I referred to IB Specification Vol 1-Revision-1.5 to create this patch. Please see 9.7.4.1.6 (page.330).

v2: Fixed the conditional for 'last' packets. Thanks, Zhijian.

It had better send the v2 patch in a new mail thread.

Zhu Yanjun

Quoted 55 lines
 drivers/infiniband/sw/rxe/rxe_resp.c | 34 ++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 7 deletions(-)
drivers/infiniband/sw/rxe/rxe_resp.c +27 -8
index c32bc12cc82f..382d2053db43 100644
@@ -393,16 +393,36 @@ static enum resp_states check_resource(struct rxe_qp *qp,
 static enum resp_states check_length(struct rxe_qp *qp,
                                     struct rxe_pkt_info *pkt)
 {
-       switch (qp_type(qp)) {
-       case IB_QPT_RC:
-               return RESPST_CHK_RKEY;
+       int mtu = qp->mtu;
+       u32 payload = payload_size(pkt);
+       u32 dmalen = reth_len(pkt);
 
-       case IB_QPT_UC:
-               return RESPST_CHK_RKEY;
+       /* RoCEv2 packets do not have LRH.
+        * Let's skip checking it.
+        */
 
-       default:
-               return RESPST_CHK_RKEY;
+       if ((pkt->opcode & RXE_START_MASK) &&
+           (pkt->opcode & RXE_END_MASK)) {
+               /* "only" packets */
+               if (payload > mtu)
+                       return RESPST_ERR_LENGTH;
+       } else if ((pkt->opcode & RXE_START_MASK) ||
+                  (pkt->opcode & RXE_MIDDLE_MASK)) {
+               /* "first" or "middle" packets */
+               if (payload != mtu)
+                       return RESPST_ERR_LENGTH;
+       } else if (pkt->opcode & RXE_END_MASK) {
+               /* "last" packets */
+               if ((payload == 0) || (payload > mtu))
+                       return RESPST_ERR_LENGTH;
+       }
+
+       if (pkt->opcode & (RXE_WRITE_MASK | RXE_READ_MASK)) {
+               if (dmalen > (1 << 31))
+                       return RESPST_ERR_LENGTH;
        }
+
+       return RESPST_CHK_RKEY;
 }
 
 static enum resp_states check_rkey(struct rxe_qp *qp,
--

2.31.1

Reviewed-by on v2 1/1
#2 lore raw
In reply to Daisuke Matsuda
Quoted 5 lines · Daisuke Matsuda

The function check_length() is supposed to check the length of inbound packets on responder, but it actually has been a stub since the driver was born. Let it check the payload length and the DMA length.

Looks good to me

Quoted 58 lines

---
FOR REVIEWERS
I referred to IB Specification Vol 1-Revision-1.5 to create this patch. Please see 9.7.4.1.6 (page.330).

v2: Fixed the conditional for 'last' packets. Thanks, Zhijian.

  drivers/infiniband/sw/rxe/rxe_resp.c | 34 ++++++++++++++++++++++------
  1 file changed, 27 insertions(+), 7 deletions(-)
drivers/infiniband/sw/rxe/rxe_resp.c +27 -7
index c32bc12cc82f..382d2053db43 100644
@@ -393,16 +393,36 @@ static enum resp_states check_resource(struct rxe_qp *qp,
  static enum resp_states check_length(struct rxe_qp *qp,
  				     struct rxe_pkt_info *pkt)
  {
-	switch (qp_type(qp)) {
-	case IB_QPT_RC:
-		return RESPST_CHK_RKEY;
+	int mtu = qp->mtu;
+	u32 payload = payload_size(pkt);
+	u32 dmalen = reth_len(pkt);
 
-	case IB_QPT_UC:
-		return RESPST_CHK_RKEY;
+	/* RoCEv2 packets do not have LRH.
+	 * Let's skip checking it.
+	 */
 
-	default:
-		return RESPST_CHK_RKEY;
+	if ((pkt->opcode & RXE_START_MASK) &&
+	    (pkt->opcode & RXE_END_MASK)) {
+		/* "only" packets */
+		if (payload > mtu)
+			return RESPST_ERR_LENGTH;
+	} else if ((pkt->opcode & RXE_START_MASK) ||
+		   (pkt->opcode & RXE_MIDDLE_MASK)) {
+		/* "first" or "middle" packets */
+		if (payload != mtu)
+			return RESPST_ERR_LENGTH;
+	} else if (pkt->opcode & RXE_END_MASK) {
+		/* "last" packets */
+		if ((payload == 0) || (payload > mtu))
+			return RESPST_ERR_LENGTH;
+	}
+
+	if (pkt->opcode & (RXE_WRITE_MASK | RXE_READ_MASK)) {
+		if (dmalen > (1 << 31))
+			return RESPST_ERR_LENGTH;
  	}
+
+	return RESPST_CHK_RKEY;
  }
 
  static enum resp_states check_rkey(struct rxe_qp *qp,
#3 lore raw
In reply to Daisuke Matsuda
Quoted 5 lines · Daisuke Matsuda

The function check_length() is supposed to check the length of inbound packets on responder, but it actually has been a stub since the driver was born. Let it check the payload length and the DMA length.

Applied, thanks!

[1/1] RDMA/rxe: Implement packet length validation on responder

      https://git.kernel.org/rdma/rdma/c/837a55847ead27

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>