netfilter: ip6tables: set hotdrop for malformed extension header matches
Subsystems (3)
PATCH 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, or when SID selector reads fail.
For ipv6header, set hotdrop when the next extension header is not fully present or 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 is a follow-up to the previous IPv6 extension header hotdrop fix:
https://lore.kernel.org/netfilter-devel/[email protected]/
--- net/ipv6/netfilter/ip6t_hbh.c | 21 +++++++++----- net/ipv6/netfilter/ip6t_ipv6header.c | 10 +++++-- net/ipv6/netfilter/ip6t_srh.c | 42 ++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 19 deletions(-)
net/ipv6/netfilter/ip6t_hbh.c +14 -7
@@ -104,8 +104,10 @@ 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) { + par->hotdrop = true; +return false; + }/* Type check */ if (*tp != (optinfo->opts[temp] &0xFF00 ) >>8 ) {
@@ -120,13 +122,17 @@ hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par)u16 spec_len;/* length field exists ? */ -if (hdrlen <2 ) -break ; +if (hdrlen <2 ) { + par->hotdrop = true; +return false; + } lp =skb_header_pointer (skb, ptr +1 ,sizeof (_optlen), &_optlen); -if (lp ==NULL ) -break ; +if (!lp) { + par->hotdrop = true; +return false; + } spec_len = optinfo->opts[temp] &0x00FF ;if (spec_len !=0x00FF && spec_len != *lp) {
@@ -147,7 +153,8 @@ 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 ; + par->hotdrop = true; +return false; } ptr += optlen; hdrlen -= optlen;
net/ipv6/netfilter/ip6t_ipv6header.c +7 -3
@@ -53,8 +53,10 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)break ; }/* Is there enough space for the next ext header? */ -if (len < (int )sizeof (struct ipv6_opt_hdr )) +if (len < (int )sizeof (struct ipv6_opt_hdr )) { + par->hotdrop = true;return false; + }/* ESP -> evaluate */ if (nexthdr ==NEXTHDR_ESP ) { temp |=MASK_ESP ;
@@ -99,8 +101,10 @@ 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 -9
89 lines not on this page · raw message
Signature · 1 line
2.34.1
Phil Sutter
reviewerHi,
Quoted 7 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.
There is another candidate for hotdrop in there, e.g. the "Packet smaller than it's length field" check in line 76. Or is this a legitimate non-match?
Given the many common blocks, maybe introduce a 'hotdrop' goto label to jump to instead of break/return?
Quoted 5 lines
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, or when SID selector reads fail.
I think the 'srh->segments_left > srh->first_segment' case is also a candidate:
According to RFC8200, segments_left contains the "Number of route segments remaining, i.e., number of explicitly listed intermediate nodes still to be visited before reaching the final destination."
RFC8754 reads: "Last Entry: contains the index (zero based), in the Segment List, of the last element of the Segment List." ('first_segment' is called Last Entry in there.)
AIUI, segments_left should never exceed first_segment in a packet. Though RFC8754 mentions a case where "Segments Left is greater than Last Entry", but it's about HMAC verification and it doesn't explain why it should happen.
[...]
net/ipv6/netfilter/ip6t_ipv6header.c ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par) · line 59
Quoted 13 lines
net/ipv6/netfilter/ip6t_ipv6header.c +3
-1@@ -53,8 +53,10 @@ ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)break ; }/* Is there enough space for the next ext header? */ -if (len < (int )sizeof (struct ipv6_opt_hdr )) +if (len < (int )sizeof (struct ipv6_opt_hdr )) { + par->hotdrop = true;return false; + }
This check is actually redundant, no? The following call to skb_header_pointer() should discover the skb->len underrun?
Cheers, Phil
Hi Phil,
Thanks a lot for the review.
Quoted 3 lines
There is another candidate for hotdrop in there, e.g. the "Packet smaller than it's length field" check in line 76. Or is this a legitimate non-match?
This should already be covered in the current nf/net tree by the previous IPv6 extension header hotdrop fix, but I will double-check the base before sending v2.
Quoted 2 lines
Given the many common blocks, maybe introduce a 'hotdrop' goto label to jump to instead of break/return?
That makes sense to me for the repeated hbh error paths. I will look at the flow again and use a hotdrop label where it keeps the code clearer.
Quoted 2 lines
I think the '
srh->segments_left>srh->first_segment' case is also a candidate:
I will revisit this one and check it against RFC8200/RFC8754. My current understanding is that this looks more like malformed input than a normal rule mismatch, so I will account for it in v2 if the check fits cleanly.
Thanks,
Zhixing