Skip to contentSkip to conversation

netfilter: ip6tables: set hotdrop for malformed extension header matches

PATCH In review Zhixing Chen 2 participants 3 messages Jul 24 – Aug 7

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

PATCH v3 1/1 netfilter: ip6tables: set hotdrop for malformed extension header matches

The hbh, srh and ipv6header matches have paths that return false for malformed IPv6 extension header packets without setting hotdrop.

For hbh, strict option parsing stops when the option type or length field cannot be read, or when advancing to the next requested option would exceed the available header data. Mark these packets for hotdrop instead of treating them as a rule mismatch.

For srh, keep a missing SRH as a normal mismatch, but set hotdrop when header lookup fails for other reasons, when the SRH fixed header is not present, when the advertised SRH length exceeds the available skb data, when segments_left exceeds first_segment, when the SID list implied by first_segment exceeds the advertised SRH length, or when SID selector reads fail.

For ipv6header, set hotdrop when the advertised extension header length exceeds the available skb data.

Returning false treats the packet as a rule mismatch. Set hotdrop for these malformed packets so they cannot bypass rules intended to drop packets with these IPv6 extension headers.

---

Changes in v3:
- Remove the unused len variable from ipv6header_mt6().
- Validate that the SID list implied by first_segment fits within the advertised SRH length in srh1_mt6().

Changes in v2:
- Use hotdrop labels for hbh and srh paths.
- Mark SRH packets with segments_left greater than first_segment for hotdrop.
- Drop the redundant ipv6header length check before skb_header_pointer().

v2: https://lore.kernel.org/netdev/[email protected]/T/
v1: https://lore.kernel.org/netdev/[email protected]/T/

---
 net/ipv6/netfilter/ip6t_hbh.c        | 27 +++++++++--------
 net/ipv6/netfilter/ip6t_ipv6header.c | 13 +++-----
 net/ipv6/netfilter/ip6t_srh.c        | 44 +++++++++++++++++++++-------
 3 files changed, 51 insertions(+), 33 deletions(-)
net/ipv6/netfilter/ip6t_hbh.c +14 -13
index 6d1a5d2026a6..1b5dcc92b7da 100644
@@ -62,21 +62,18 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 			    NEXTHDR_HOP : NEXTHDR_DEST, NULL, NULL);
 	if (err < 0) {
 		if (err != -ENOENT)
-			par->hotdrop = true;
+			goto hotdrop;
 		return false;
 	}
 
 	oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh);
-	if (oh == NULL) {
-		par->hotdrop = true;
-		return false;
-	}
+	if (!oh)
+		goto hotdrop;
 
 	hdrlen = ipv6_optlen(oh);
 	if (skb->len - ptr < hdrlen) {
 		/* Packet smaller than it's length field */
-		par->hotdrop = true;
-		return false;
+		goto hotdrop;
 	}
 
 	pr_debug("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen);
@@ -104,8 +101,8 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 				break;
 			tp = skb_header_pointer(skb, ptr, sizeof(_opttype),
 						&_opttype);
-			if (tp == NULL)
-				break;
+			if (!tp)
+				goto hotdrop;
 
 			/* Type check */
 			if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) {
@@ -121,12 +118,12 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 
 				/* length field exists ? */
 				if (hdrlen < 2)
-					break;
+					goto hotdrop;
 				lp = skb_header_pointer(skb, ptr + 1,
 							sizeof(_optlen),
 							&_optlen);
-				if (lp == NULL)
-					break;
+				if (!lp)
+					goto hotdrop;
 				spec_len = optinfo->opts[temp] & 0x00FF;
 
 				if (spec_len != 0x00FF && spec_len != *lp) {
@@ -147,7 +144,7 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 			if ((ptr > skb->len - optlen || hdrlen < optlen) &&
 			    temp < optinfo->optsnr - 1) {
 				pr_debug("new pointer is too large!\n");
-				break;
+				goto hotdrop;
 			}
 			ptr += optlen;
 			hdrlen -= optlen;
@@ -159,6 +156,10 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 	}
 
 	return false;
+
+hotdrop:
+	par->hotdrop = true;
+	return false;
 }
 
 static int hbh_mt6_check(const struct xt_mtchk_param *par)
net/ipv6/netfilter/ip6t_ipv6header.c +4 -9
index c52ff929c93b..91a35e83b27f 100644
@@ -28,7 +28,6 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 {
 	const struct ip6t_ipv6header_info *info = par->matchinfo;
 	unsigned int temp;
-	int len;
 	u8 nexthdr;
 	unsigned int ptr;
 
@@ -38,8 +37,6 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 	nexthdr = ipv6_hdr(skb)->nexthdr;
 	/* pointer to the 1st exthdr */
 	ptr = sizeof(struct ipv6hdr);
-	/* available length */
-	len = skb->len - ptr;
 	temp = 0;
 
 	while (nf_ip6_ext_hdr(nexthdr)) {
@@ -52,9 +49,6 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 			temp |= MASK_NONE;
 			break;
 		}
-		/* Is there enough space for the next ext header? */
-		if (len < (int)sizeof(struct ipv6_opt_hdr))
-			return false;
 		/* ESP -> evaluate */
 		if (nexthdr == NEXTHDR_ESP) {
 			temp |= MASK_ESP;
@@ -97,10 +91,11 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
 		}
 
 		nexthdr = hp->nexthdr;
-		len -= hdrlen;
 		ptr += hdrlen;
-		if (ptr > skb->len)
-			break;
+		if (ptr > skb->len) {
+			par->hotdrop = true;
+			return false;
+		}
 	}
 
 	if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP)
net/ipv6/netfilter/ip6t_srh.c +33 -11

110 lines not on this page · raw message

base-commit: 56d96fededd61192cd7cc8d2b0f36adfd59036c3

Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · Zhixing Chen

The hbh, srh and ipv6header matches have paths that return false for malformed IPv6 extension header packets without setting hotdrop.

For hbh, strict option parsing stops when the option type or length field cannot be read, or when advancing to the next requested option would exceed the available header data. Mark these packets for hotdrop instead of treating them as a rule mismatch.

For srh, keep a missing SRH as a normal mismatch, but set hotdrop when header lookup fails for other reasons, when the SRH fixed header is not present, when the advertised SRH length exceeds the available skb data, when segments_left exceeds first_segment, when the SID list implied by first_segment exceeds the advertised SRH length, or when SID selector reads fail.

Quoted 6 lines

For ipv6header, set hotdrop when the advertised extension header length exceeds the available skb data.

Returning false treats the packet as a rule mismatch. Set hotdrop for these malformed packets so they cannot bypass rules intended to drop packets with these IPv6 extension headers.

This patch should target nf-next, it does not fix any real crash.

There are also issues in this patch to be addressed, see:

https://sashiko.dev/#/patchset/20260724110111.18783-1-running910%40gmail.com

#2 lore raw
Quoted 5 lines

This patch should target nf-next, it does not fix any real crash.

There are also issues in this patch to be addressed, see:

https://sashiko.dev/#/patchset/20260724110111.18783-1-running910%40gmail.com

Hi Pablo,

Thanks for the review.

I have retargeted this to nf-next in v4, as suggested. I also carefully reviewed the relevant code paths together with the sashiko report and updated the patch accordingly.

I have just sent v4.

Thanks,
Zhixing