git: e0c6b88706a9 - main - irdma: improve TCP header validation

From: Richard Scheffenegger <rscheff_at_FreeBSD.org>
Date: Fri, 06 Nov 2026 20:56:19 UTC
The branch main has been updated by rscheff:

URL: https://cgit.FreeBSD.org/src/commit/?id=e0c6b88706a9fff9e4090803ba2338dd0ce97eba

commit e0c6b88706a9fff9e4090803ba2338dd0ce97eba
Author:     Richard Scheffenegger <rscheff@FreeBSD.org>
AuthorDate: 2026-11-06 20:42:20 +0000
Commit:     Richard Scheffenegger <rscheff@FreeBSD.org>
CommitDate: 2026-11-06 20:42:45 +0000

    irdma: improve TCP header validation
    
    iWARP handling code deals with TCP header processing outside of the normal TCP stack.
    Add validation checks to verify the internal integrity of the TCP headers.
    
    Reviewed By: tuexen, O11 Intel Networking group, #transport, bartosz.sobczak_intel.com
    Sponsored by: NetApp, Inc.
    MFC after: 3 days
    Differential Revision: https://reviews.freebsd.org/D58323
---
 sys/dev/irdma/irdma_cm.c    | 22 ++++++++++++++++++++--
 sys/dev/irdma/irdma_utils.c | 13 +++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/sys/dev/irdma/irdma_cm.c b/sys/dev/irdma/irdma_cm.c
index 669d2cf5cb10..7323c07650ee 100644
--- a/sys/dev/irdma/irdma_cm.c
+++ b/sys/dev/irdma/irdma_cm.c
@@ -822,13 +822,20 @@ irdma_event_connect_error(struct irdma_cm_event *event)
  */
 static int
 irdma_process_options(struct irdma_cm_node *cm_node, u8 *optionsloc,
-		      u32 optionsize, u32 syn_pkt)
+		      int optionsize, u32 syn_pkt)
 {
 	u32 tmp;
-	u32 offset = 0;
+	int offset = 0;
 	union all_known_options *all_options;
 	char got_mss_option = 0;
 
+	if ((optionsize < 0) || (optionsize > MAX_TCPOPTLEN)) {
+		irdma_debug(&cm_node->iwdev->rf->sc_dev, IRDMA_DEBUG_CM,
+			    "Option Size: %d out of bound\n",
+			    optionsize);
+		/* return silently */
+		return 0;
+	}
 	while (offset < optionsize) {
 		all_options = (union all_known_options *)(optionsloc + offset);
 		switch (all_options->base.optionnum) {
@@ -2668,7 +2675,11 @@ irdma_handle_syn_pkt(struct irdma_cm_node *cm_node,
 	u32 inc_sequence;
 	int optionsize;
 
+	if (tcph->th_off < 5) {
+		return;
+	}
 	optionsize = (tcph->th_off << 2) - sizeof(*tcph);
+
 	inc_sequence = ntohl(tcph->th_seq);
 
 	switch (cm_node->state) {
@@ -2736,7 +2747,11 @@ irdma_handle_synack_pkt(struct irdma_cm_node *cm_node,
 	u32 inc_sequence;
 	int optionsize;
 
+	if (tcph->th_off < 5) {
+		return;
+	}
 	optionsize = (tcph->th_off << 2) - sizeof(*tcph);
+
 	inc_sequence = ntohl(tcph->th_seq);
 	switch (cm_node->state) {
 	case IRDMA_CM_STATE_SYN_SENT:
@@ -2811,6 +2826,9 @@ irdma_handle_ack_pkt(struct irdma_cm_node *cm_node,
 	int optionsize;
 	u32 datasize = rbuf->datalen;
 
+	if (tcph->th_off < 5) {
+		return -EINVAL;
+	}
 	optionsize = (tcph->th_off << 2) - sizeof(*tcph);
 
 	if (irdma_check_seq(cm_node, tcph))
diff --git a/sys/dev/irdma/irdma_utils.c b/sys/dev/irdma/irdma_utils.c
index ef8cb38d8139..a89d91a06822 100644
--- a/sys/dev/irdma/irdma_utils.c
+++ b/sys/dev/irdma/irdma_utils.c
@@ -1541,6 +1541,13 @@ irdma_gen1_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
 	}
 
 	buf->tcphlen = tcph->th_off << 2;
+	if (buf->tcphlen < 20) {
+		irdma_debug(buf->vsi->dev, IRDMA_DEBUG_ERR,
+			    "tcp hlen too small 0x%x\n",
+			    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;
@@ -1581,6 +1588,12 @@ irdma_puda_get_tcpip_info(struct irdma_puda_cmpl_info *info,
 	pkt_len = info->payload_len;
 	buf->totallen = pkt_len;
 	buf->tcphlen = tcph->th_off << 2;
+	if (buf->tcphlen < 20) {
+		irdma_debug(buf->vsi->dev, IRDMA_DEBUG_ERR,
+			    "tcp hlen too small 0x%x\n",
+			    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;