netfilter: nft_exthdr: skip SCTP chunk evaluation for non-first fragments
Subsystems (3)
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.
--- net/netfilter/nft_exthdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +1 -1
@@ -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
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.
--- net/netfilter/nft_tproxy.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nft_tproxy.c +4 -4
@@ -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
PATCH 3/4 netfilter: nft_osf: skip evaluation for non-first fragments
In reply to Fernando Fernandez ManceraThe 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.
--- net/netfilter/nft_osf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nft_osf.c +1 -1
@@ -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
PATCH 4/4 netfilter: xtables: fix L4 header parsing for non-first fragments
In reply to Fernando Fernandez ManceraThe 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.
--- 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
@@ -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
@@ -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
Pablo Neira Ayuso
maintainerRe: [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")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- 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@@ -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@@ -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
Pablo Neira Ayuso
maintainerHi Fernando,
Quoted 8 lines · Fernando Fernandez Mancera
The SCTP chunk matching logic in
nft_exthdrrelies 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_exthdrcould 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->fragoffto 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")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nft_exthdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/netfilter/nft_exthdr.c +1
-1@@ -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
Quoted 15 lines · Pablo Neira Ayuso
Hi Fernando,
Quoted 8 lines · Fernando Fernandez Mancera
The SCTP chunk matching logic in
nft_exthdrrelies 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_exthdrcould 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_conntrackis not loaded.
Quoted 9 lines
Quoted 2 lines
Add a check for
pkt->fragoffto 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->fragoffin 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")Signed-off-by Fernando Fernandez Mancera <[email protected]>--- net/netfilter/nft_exthdr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/netfilter/nft_exthdr.c +1
-1@@ -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