Skip to contentSkip to conversation

netfilter: nft_exthdr: skip SCTP chunk evaluation for non-first fragments

PATCH In review Fernando Fernandez Mancera 2 participants 7 messages Apr 17 – Apr 18

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

PATCH 1/4 netfilter: nft_exthdr: skip SCTP chunk evaluation for non-first fragments

The SCTP chunk matching logic in nft_exthdr relies on SCTP common header being present at the transport header offset. For fragmented packets at IP level, only the first fragment would match this condition.

The nft_exthdr could be used in a PREROUTING chain with a priority lower than -400. This would bypass defragmentation. In addition, it can be use in stateless environments so it should work on a environment where defragmentation is not being performed at all.

Add a check for pkt->fragoff to ensure exthdr SCTP only evaluates unfragmented packets or the first fragment in the stream.

Fixes 133dc203d77d ("netfilter: nft_exthdr: Support SCTP chunks")
---
 net/netfilter/nft_exthdr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +1 -1
index 7eedf4e3ae9c..8eb708bb8cff 100644
@@ -376,7 +376,7 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 	const struct sctp_chunkhdr *sch;
 	struct sctp_chunkhdr _sch;
 
-	if (pkt->tprot != IPPROTO_SCTP)
+	if (pkt->tprot != IPPROTO_SCTP || pkt->fragoff)
 		goto err;
 
 	do {
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/4 netfilter: nft_tproxy: skip evaluation for non-first fragments

The tproxy expression relies on L4 ports to perform socke lookups. For fragmented packets, every fragment carries the transport protocol used but only the first fragment contains the L4 header.

As nftables is not evaluating chain priority, a tproxy expression could be attached to a PREROUTING chain with a priority lower than -400. This would bypass defragmentation.

Add a check for pkt->fragoff to ensure tproxy only evaluates unfragmented packets or the first fragment in the stream.

Fixes 4ed8eb6570a4 ("netfilter: nf_tables: Add native tproxy support")
---
 net/netfilter/nft_tproxy.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nft_tproxy.c +4 -4
index 50481280abd2..8080cbd878cd 100644
@@ -30,8 +30,8 @@ static void nft_tproxy_eval_v4(const struct nft_expr *expr,
 	__be16 tport = 0;
 	struct sock *sk;
 
-	if (pkt->tprot != IPPROTO_TCP &&
-	    pkt->tprot != IPPROTO_UDP) {
+	if ((pkt->tprot != IPPROTO_TCP &&
+	     pkt->tprot != IPPROTO_UDP) || pkt->fragoff) {
 		regs->verdict.code = NFT_BREAK;
 		return;
 	}
@@ -97,8 +97,8 @@ static void nft_tproxy_eval_v6(const struct nft_expr *expr,
 
 	memset(&taddr, 0, sizeof(taddr));
 
-	if (pkt->tprot != IPPROTO_TCP &&
-	    pkt->tprot != IPPROTO_UDP) {
+	if ((pkt->tprot != IPPROTO_TCP &&
+	     pkt->tprot != IPPROTO_UDP) || pkt->fragoff) {
 		regs->verdict.code = NFT_BREAK;
 		return;
 	}
Signature · 1 line
2.53.0
#2 lore raw

PATCH 3/4 netfilter: nft_osf: skip evaluation for non-first fragments

In reply to Fernando Fernandez Mancera

The osf expression extracts TCP options to match them against fingerprints. For fragmented packets, every fragment carries the transport protocol used but only the first fragment contains the TCP header.

As nftables is not evaluating chain priority, a osf expression could be attached to a PREROUTING chain with a priority lower than -400. This would bypass defragmentation. In addition, nft_osf should be able to work in stateless environments, therefore it can be use in situation when defragmentation is not being performed.

Add a check for pkt->fragoff to ensure osf only evaluates unfragmented packets or the first fragment in the stream.

Fixes b96af92d6eaf ("netfilter: nf_tables: implement Passive OS fingerprint module in nft_osf")
---
 net/netfilter/nft_osf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_osf.c +1 -1
index 1c0b493ef0a9..ceca87e405eb 100644
@@ -28,7 +28,7 @@ static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	struct nf_osf_data data;
 	struct tcphdr _tcph;
 
-	if (pkt->tprot != IPPROTO_TCP) {
+	if (pkt->tprot != IPPROTO_TCP || pkt->fragoff) {
 		regs->verdict.code = NFT_BREAK;
 		return;
 	}
Signature · 1 line
2.53.0
#3 lore raw

PATCH 4/4 netfilter: xtables: fix L4 header parsing for non-first fragments

In reply to Fernando Fernandez Mancera

The TPROXY target and osf match relies on L4 header to operate. For fragmented packets, every fragment carries the transport protocol identifier, but only the first fragment contains the L4 header.

As the 'raw' table can be configured to run at priority -450 (before defragmentation at -400), the target/match can be reached before reassembly. In this case, non-first fragments have their payload incorrectly parsed as a TCP/UDP header.

Add a fragment check to ensure TPROXY/osf only evaluates unfragmented packets or the first fragment in the stream.

Fixes 902d6a4c2a4f ("netfilter: nf_defrag: Skip defrag if NOTRACK is set")
---
 net/netfilter/xt_TPROXY.c | 8 ++++++--
 net/netfilter/xt_osf.c    | 3 +++
 2 files changed, 9 insertions(+), 2 deletions(-)
net/netfilter/xt_TPROXY.c +6 -2
index e4bea1d346cf..ac4b011ce48c 100644
@@ -40,6 +40,9 @@ tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
 	struct udphdr _hdr, *hp;
 	struct sock *sk;
 
+	if (ip_is_fragment(iph))
+		return NF_DROP;
+
 	hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
 	if (hp == NULL)
 		return NF_DROP;
@@ -106,6 +109,7 @@ tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
 {
 	const struct ipv6hdr *iph = ipv6_hdr(skb);
 	const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
+	unsigned short fragoff = 0;
 	struct udphdr _hdr, *hp;
 	struct sock *sk;
 	const struct in6_addr *laddr;
@@ -113,8 +117,8 @@ tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
 	int thoff = 0;
 	int tproto;
 
-	tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
-	if (tproto < 0)
+	tproto = ipv6_find_hdr(skb, &thoff, -1, &fragoff, NULL);
+	if (tproto < 0 || fragoff)
 		return NF_DROP;
 
 	hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
net/netfilter/xt_osf.c +3 -0
index dc9485854002..889dff4daff0 100644
@@ -27,6 +27,9 @@
 static bool
 xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
 {
+	if (ip_is_fragment(ip_hdr(skb)))
+		return false;
+
 	return nf_osf_match(skb, xt_family(p), xt_hooknum(p), xt_in(p),
 			    xt_out(p), p->matchinfo, xt_net(p), nf_osf_fingers);
 }
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 4/4 nf] netfilter: xtables: fix L4 header parsing for non-first fragments

Quoted 8 lines · Fernando Fernandez Mancera

The TPROXY target and osf match relies on L4 header to operate. For fragmented packets, every fragment carries the transport protocol identifier, but only the first fragment contains the L4 header.

As the 'raw' table can be configured to run at priority -450 (before defragmentation at -400), the target/match can be reached before reassembly. In this case, non-first fragments have their payload incorrectly parsed as a TCP/UDP header.

I see, this refers to a misconfiguration scenario.

Quoted 2 lines

Add a fragment check to ensure TPROXY/osf only evaluates unfragmented packets or the first fragment in the stream.

LGTM this combo patch for osf and TPROXY in xtables.

Thanks.

Quoted 57 lines
Fixes 902d6a4c2a4f ("netfilter: nf_defrag: Skip defrag if NOTRACK is set")
---
 net/netfilter/xt_TPROXY.c | 8 ++++++--
 net/netfilter/xt_osf.c    | 3 +++
 2 files changed, 9 insertions(+), 2 deletions(-)
net/netfilter/xt_TPROXY.c +6 -2
index e4bea1d346cf..ac4b011ce48c 100644
@@ -40,6 +40,9 @@ tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
 	struct udphdr _hdr, *hp;
 	struct sock *sk;
 
+	if (ip_is_fragment(iph))
+		return NF_DROP;
+
 	hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
 	if (hp == NULL)
 		return NF_DROP;
@@ -106,6 +109,7 @@ tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
 {
 	const struct ipv6hdr *iph = ipv6_hdr(skb);
 	const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
+	unsigned short fragoff = 0;
 	struct udphdr _hdr, *hp;
 	struct sock *sk;
 	const struct in6_addr *laddr;
@@ -113,8 +117,8 @@ tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
 	int thoff = 0;
 	int tproto;
 
-	tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
-	if (tproto < 0)
+	tproto = ipv6_find_hdr(skb, &thoff, -1, &fragoff, NULL);
+	if (tproto < 0 || fragoff)
 		return NF_DROP;
 
 	hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
net/netfilter/xt_osf.c +3 -0
index dc9485854002..889dff4daff0 100644
@@ -27,6 +27,9 @@
 static bool
 xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
 {
+	if (ip_is_fragment(ip_hdr(skb)))
+		return false;
+
 	return nf_osf_match(skb, xt_family(p), xt_hooknum(p), xt_in(p),
 			    xt_out(p), p->matchinfo, xt_net(p), nf_osf_fingers);
 }
Signature · 1 line
2.53.0
#5 lore raw
In reply to Fernando Fernandez Mancera

Hi Fernando,

Quoted 8 lines · Fernando Fernandez Mancera

The SCTP chunk matching logic in nft_exthdr relies on SCTP common header being present at the transport header offset. For fragmented packets at IP level, only the first fragment would match this condition.

The nft_exthdr could be used in a PREROUTING chain with a priority lower than -400. This would bypass defragmentation. In addition, it can be use in stateless environments so it should work on a environment where defragmentation is not being performed at all.

Yes, and stateless filtering is still a valid configuration, ie. nf_conntrack is not loaded.

Quoted 2 lines

Add a check for pkt->fragoff to ensure exthdr SCTP only evaluates unfragmented packets or the first fragment in the stream.

I would suggest to squash the three small patches to check for pkt->fragoff in one patch. The three expressions have been already around for a while (backporting the combo patch that makes the same logical change should be easy) and it is basically the same logical change.

Thanks!

Quoted 22 lines
Fixes 133dc203d77d ("netfilter: nft_exthdr: Support SCTP chunks")
---
 net/netfilter/nft_exthdr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +1 -1
index 7eedf4e3ae9c..8eb708bb8cff 100644
@@ -376,7 +376,7 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
 	const struct sctp_chunkhdr *sch;
 	struct sctp_chunkhdr _sch;
 
-	if (pkt->tprot != IPPROTO_SCTP)
+	if (pkt->tprot != IPPROTO_SCTP || pkt->fragoff)
 		goto err;
 
 	do {
Signature · 1 line
2.53.0
#6 lore raw
Quoted 15 lines · Pablo Neira Ayuso

Hi Fernando,

Quoted 8 lines · Fernando Fernandez Mancera

The SCTP chunk matching logic in nft_exthdr relies on SCTP common header being present at the transport header offset. For fragmented packets at IP level, only the first fragment would match this condition.

The nft_exthdr could be used in a PREROUTING chain with a priority lower than -400. This would bypass defragmentation. In addition, it can be use in stateless environments so it should work on a environment where defragmentation is not being performed at all.

Yes, and stateless filtering is still a valid configuration, ie. nf_conntrack is not loaded.

Quoted 9 lines
Quoted 2 lines

Add a check for pkt->fragoff to ensure exthdr SCTP only evaluates unfragmented packets or the first fragment in the stream.

I would suggest to squash the three small patches to check for pkt->fragoff in one patch. The three expressions have been already around for a while (backporting the combo patch that makes the same logical change should be easy) and it is basically the same logical change.

Hi Pablo,

Thanks for the review! I am not sure about squashing them as they all have different blamed commits. I find accurate fixes tag quite useful when handling backports and I guess others do too (also for stable kernels). Is that convincing?

Anyway, not a big deal if there is a strong preference I will squash them.

Thanks,
Fernando.

Quoted 24 lines

Thanks!

Quoted 22 lines
Fixes 133dc203d77d ("netfilter: nft_exthdr: Support SCTP chunks")
---
  net/netfilter/nft_exthdr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +1 -1
index 7eedf4e3ae9c..8eb708bb8cff 100644
@@ -376,7 +376,7 @@ static void nft_exthdr_sctp_eval(const struct nft_expr *expr,
  	const struct sctp_chunkhdr *sch;
  	struct sctp_chunkhdr _sch;
 
-	if (pkt->tprot != IPPROTO_SCTP)
+	if (pkt->tprot != IPPROTO_SCTP || pkt->fragoff)
  		goto err;
 
  	do {
Signature · 1 line
2.53.0