[PATCH nf-next v5] netfilter: ip6tables: hotdrop malformed hbh/dst and srh headers
From: Zhixing Chen <hidden>
Date: 2026-09-18 06:18:38
Also in:
netfilter-devel
Subsystem:
networking [general], the rest · Maintainers:
"David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
The hbh/dst and srh matches have paths that return false for malformed IPv6 extension headers without setting hotdrop. For hbh/dst, strict option parsing can stop when option data cannot be read or when the current option length exceeds the available header data. Treat these packets as malformed and set hotdrop. Apply the boundary check to the current option regardless of whether the rule asks for more options, so a malformed last requested option cannot be treated as a normal match result. 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 the SID list implied by first_segment exceeds the advertised SRH length, or when SID selector reads fail. Apply the SID-list length validation consistently to both revisions of the srh match. Returning false treats these malformed packets as rule mismatches. Set hotdrop so rule evaluation terminates and the packets are dropped instead. Signed-off-by: Zhixing Chen <redacted> --- Changes in v5: - Rebase onto the latest nf-next tree. - Drop the ipv6header change because ipv6header_mt6() does not stop at non-first fragments, so changing its short-header handling may affect valid fragmented traffic and requires separate consideration. - Keep segments_left greater than first_segment as a normal mismatch, since reduced SRHs may legitimately have segments_left equal to first_segment plus one. - Apply the first_segment/SID-list length validation to both srh match revisions to keep malformed-header handling consistent. Changes in v4: - Retarget to nf-next. - Do not turn ipv6header ptr overruns into hotdrop because ipv6header_mt6() does not stop at non-first fragments. - Fix hbh/dst strict option parsing so a malformed last requested option also sets hotdrop. 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(). v4: https://lore.kernel.org/netdev/20260807084559.63276-1-running910@gmail.com/T/ (local) v3: https://lore.kernel.org/netdev/20260724110111.18783-1-running910@gmail.com/T/ (local) v2: https://lore.kernel.org/netdev/20260714032124.7042-1-running910@gmail.com/T/ (local) v1: https://lore.kernel.org/netdev/20260709063012.33160-1-running910@gmail.com/T/ (local) --- net/ipv6/netfilter/ip6t_hbh.c | 33 +++++++++++++------------- net/ipv6/netfilter/ip6t_srh.c | 44 ++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 25 deletions(-)
diff --git a/net/ipv6/netfilter/ip6t_hbh.c b/net/ipv6/netfilter/ip6t_hbh.c
index 6008dcff8488..53fe8520a1f9 100644
--- a/net/ipv6/netfilter/ip6t_hbh.c
+++ b/net/ipv6/netfilter/ip6t_hbh.c@@ -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; + /* Packet smaller than its length field */ + goto hotdrop; } ret = (!(optinfo->flags & IP6T_OPTS_LEN) ||
@@ -94,8 +91,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)
@@ -107,12 +104,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)
@@ -123,9 +120,9 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par) optlen = 1; } - if ((ptr > skb->len - optlen || hdrlen < optlen) && - temp < optinfo->optsnr - 1) - break; + if (ptr > skb->len || optlen > skb->len - ptr || + hdrlen < optlen) + goto hotdrop; ptr += optlen; hdrlen -= optlen;
@@ -137,6 +134,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)
diff --git a/net/ipv6/netfilter/ip6t_srh.c b/net/ipv6/netfilter/ip6t_srh.c
index db0fd64d8986..c89f5eb68926 100644
--- a/net/ipv6/netfilter/ip6t_srh.c
+++ b/net/ipv6/netfilter/ip6t_srh.c@@ -27,20 +27,29 @@ static bool srh_mt6(const struct sk_buff *skb, struct xt_action_param *par) struct ipv6_sr_hdr *srh; struct ipv6_sr_hdr _srh; int hdrlen, srhoff = 0; + int err; - if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) + err = ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL); + if (err < 0) { + if (err != -ENOENT) + goto hotdrop; return false; + } srh = skb_header_pointer(skb, srhoff, sizeof(_srh), &_srh); if (!srh) - return false; + goto hotdrop; hdrlen = ipv6_optlen(srh); if (skb->len - srhoff < hdrlen) - return false; + goto hotdrop; if (srh->type != IPV6_SRCRT_TYPE_4) return false; + if (sizeof(*srh) + + ((srh->first_segment + 1) * sizeof(struct in6_addr)) > hdrlen) + goto hotdrop; + if (srh->segments_left > srh->first_segment) return false;
@@ -111,6 +120,10 @@ static bool srh_mt6(const struct sk_buff *skb, struct xt_action_param *par) !(srh->tag == srhinfo->tag))) return false; return true; + +hotdrop: + par->hotdrop = true; + return false; } static bool srh1_mt6(const struct sk_buff *skb, struct xt_action_param *par)
@@ -121,20 +134,29 @@ static bool srh1_mt6(const struct sk_buff *skb, struct xt_action_param *par) struct in6_addr _psid, _nsid, _lsid; struct ipv6_sr_hdr *srh; struct ipv6_sr_hdr _srh; + int err; - if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) + err = ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL); + if (err < 0) { + if (err != -ENOENT) + goto hotdrop; return false; + } srh = skb_header_pointer(skb, srhoff, sizeof(_srh), &_srh); if (!srh) - return false; + goto hotdrop; hdrlen = ipv6_optlen(srh); if (skb->len - srhoff < hdrlen) - return false; + goto hotdrop; if (srh->type != IPV6_SRCRT_TYPE_4) return false; + if (sizeof(*srh) + + ((srh->first_segment + 1) * sizeof(struct in6_addr)) > hdrlen) + goto hotdrop; + if (srh->segments_left > srh->first_segment) return false;
@@ -207,7 +229,7 @@ static bool srh1_mt6(const struct sk_buff *skb, struct xt_action_param *par) ((srh->segments_left + 1) * sizeof(struct in6_addr)); psid = skb_header_pointer(skb, psidoff, sizeof(_psid), &_psid); if (!psid) - return false; + goto hotdrop; if (NF_SRH_INVF(srhinfo, IP6T_SRH_INV_PSID, ipv6_masked_addr_cmp(psid, &srhinfo->psid_msk, &srhinfo->psid_addr)))
@@ -222,7 +244,7 @@ static bool srh1_mt6(const struct sk_buff *skb, struct xt_action_param *par) ((srh->segments_left - 1) * sizeof(struct in6_addr)); nsid = skb_header_pointer(skb, nsidoff, sizeof(_nsid), &_nsid); if (!nsid) - return false; + goto hotdrop; if (NF_SRH_INVF(srhinfo, IP6T_SRH_INV_NSID, ipv6_masked_addr_cmp(nsid, &srhinfo->nsid_msk, &srhinfo->nsid_addr)))
@@ -234,13 +256,17 @@ static bool srh1_mt6(const struct sk_buff *skb, struct xt_action_param *par) lsidoff = srhoff + sizeof(struct ipv6_sr_hdr); lsid = skb_header_pointer(skb, lsidoff, sizeof(_lsid), &_lsid); if (!lsid) - return false; + goto hotdrop; if (NF_SRH_INVF(srhinfo, IP6T_SRH_INV_LSID, ipv6_masked_addr_cmp(lsid, &srhinfo->lsid_msk, &srhinfo->lsid_addr))) return false; } return true; + +hotdrop: + par->hotdrop = true; + return false; } static int srh_mt6_check(const struct xt_mtchk_param *par)
base-commit: 87b80c2f6b05cad9f0ff9136709c62a0f59923e3 -- 2.34.1