netfilter: nf_conntrack_reasm: fix IPv6 transport header offset
Subsystems (3)
PATCH v3 1/1 netfilter: nf_conntrack_reasm: fix IPv6 transport header offset
nf_ct_frag6_reasm() resets the transport header to the bytes following the Fragment header. If Destination Options precedes Fragment, the Next Header field at IP6CB(skb)->nhoff still selects the options handler, which may misread the following UDP header as options and drop a valid packet.
On native IPv6 input, preserve the transport offset and use fhoff to locate the Fragment header.
The UDP test datagrams dropped on the unpatched kernel are received after the fix, and the ipv6_fragmentation and ip_defrag selftests pass.
---
v3:
- Preserve the receive position so v3 processes options that v2 skipped before Fragment.
- Retarget Fixes to the commit that first passed the reassembled skb up
the stack.
v2: https://lore.kernel.org/netfilter-devel/[email protected]/
Codex assisted with this patch.
include/net/netfilter/ipv6/nf_defrag_ipv6.h | 2 ++ net/ipv6/netfilter/nf_conntrack_reasm.c | 30 +++++++++++++++------ net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 6 +++-- 3 files changed, 28 insertions(+), 10 deletions(-)
include/net/netfilter/ipv6/nf_defrag_ipv6.h +2 -0
@@ -11,6 +11,8 @@ void nf_defrag_ipv6_disable(struct net *net);int nf_ct_frag6_init (void );void nf_ct_frag6_cleanup (void );int nf_ct_frag6_gather (struct net *net,struct sk_buff *skb,u32 user); +int nf_ct_frag6_gather_hook (struct net *net,struct sk_buff *skb,u32 user, +bool preserve_transport);struct inet_frags_ctl ;
net/ipv6/netfilter/nf_conntrack_reasm.c +22 -8
@@ -320,8 +320,9 @@ static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb, struct sk_buff *prev_tail, struct net_device *dev, int *refs) { + struct frag_hdr *fhdr;void *reasm_data; -int payload_len; +int payload_len, unfrag_len;u8 ecn;inet_frag_kill (&fq->q, refs);
@@ -345,14 +346,16 @@ static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,/* We have to remove fragment header from datagram and to relocate * header in order to calculate ICV correctly. */ -skb_network_header (skb)[fq->nhoffset] =skb_transport_header (skb)[0 ]; + fhdr = (struct frag_hdr *)skb->data -1 ; +skb_network_header (skb)[fq->nhoffset] = fhdr->nexthdr;memmove (skb->head +sizeof (struct frag_hdr ), skb->head, (skb->data - skb->head) -sizeof (struct frag_hdr ));if (skb_mac_header_was_set (skb)) skb->mac_header +=sizeof (struct frag_hdr ); skb->network_header +=sizeof (struct frag_hdr ); -skb_reset_transport_header (skb); + skb->transport_header +=sizeof (struct frag_hdr ); + unfrag_len = -skb_network_offset (skb);inet_frag_reasm_finish (&fq->q, skb, reasm_data, false);
@@ -365,8 +368,7 @@ static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,/* Yes, and fold redundant checksum back. 8) */ if (skb->ip_summed ==CHECKSUM_COMPLETE ) - skb->csum =csum_partial (skb_network_header (skb), -skb_network_header_len (skb), + skb->csum =csum_partial (skb_network_header (skb), unfrag_len, skb->csum); fq->q.rb_fragments =RB_ROOT ;
@@ -443,7 +445,8 @@ find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)return 0 ; } -int nf_ct_frag6_gather (struct net *net,struct sk_buff *skb,u32 user) +int nf_ct_frag6_gather_hook (struct net *net,struct sk_buff *skb,u32 user, +bool preserve_transport) {u16 savethdr = skb->transport_header;u8 nexthdr =NEXTHDR_FRAGMENT ;
@@ -474,9 +477,15 @@ int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)if (!pskb_may_pull (skb, fhoff +sizeof (*fhdr)))return -ENOMEM ; -skb_set_transport_header (skb, fhoff); +/* On IPv6 input this cursor and IP6CB(skb)->nhoff describe the +* next header for local delivery. It can precede the fragment. +*/ +if (!preserve_transport || !skb_transport_header_was_set (skb) || +skb_transport_offset (skb) <skb_network_offset (skb) + (int )sizeof (*hdr) || +skb_transport_offset (skb) > fhoff) +skb_set_transport_header (skb, fhoff); hdr =ipv6_hdr (skb); - fhdr = (struct frag_hdr *)skb_transport_header (skb); + fhdr = (struct frag_hdr *)(skb->data + fhoff);rcu_read_lock (); fq = fq_find(net, fhdr->identification, user, hdr,
@@ -500,6 +509,11 @@ int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)inet_frag_putn (&fq->q, refs);return ret; } + +int nf_ct_frag6_gather (struct net *net,struct sk_buff *skb,u32 user) +{ +return nf_ct_frag6_gather_hook (net, skb, user, false); +}EXPORT_SYMBOL_GPL (nf_ct_frag6_gather);static int nf_ct_net_init (struct net *net)
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +4 -2
@@ -64,8 +64,10 @@ static unsigned int ipv6_defrag(void *priv,return NF_ACCEPT ;#endif - err =nf_ct_frag6_gather (state->net, skb, -nf_ct6_defrag_user (state->hook, skb)); + err =nf_ct_frag6_gather_hook (state->net, skb, +nf_ct6_defrag_user (state->hook, skb), + state->hook ==NF_INET_PRE_ROUTING && + !nf_bridge_in_prerouting (skb));/* queued */ if (err == -EINPROGRESS )return NF_STOLEN ;
base-commit: 734bef548a96e8774d6eef5dfa2026c6edc4e80b