net: ip6 defrag: backport fixes
Subsystems (3)
v2 cover net: ip6 defrag: backport fixes
Lars Persson <[email protected]> reported that a label was unused in the 4.14 version of this patchset, and the issue was present in the 4.19 patchset as well, so I'm sending a v2 that fixes it.
The original 4.19 patchset queued for stable is OK, and can be used as is, but this v2 is a bit better: it fixes the unused label issue and handles overlapping fragments better.
Sorry for the mess/v2.
=======================
Currently, 4.19 and earlier stable kernels contain a security fix that is not fully IPv6 standard compliant.
This patchset backports IPv6 defrag fixes from 5.1rc that restore standard-compliance.
Original 5.1 patchet: https://patchwork.ozlabs.org/cover/1029418/
v2 changes: handle overlapping fragments the way it is done upstream
Peter Oskolkov (3):
net: IP defrag: encapsulate rbtree defrag code into callable functions
net: IP6 defrag: use rbtrees for IPv6 defrag
net: IP6 defrag: use rbtrees in nf_conntrack_reasm.c
include/net/inet_frag.h | 16 +- include/net/ipv6_frag.h | 11 +- net/ipv4/inet_fragment.c | 293 +++++++++++++++++++++++ net/ipv4/ip_fragment.c | 302 +++--------------------- net/ipv6/netfilter/nf_conntrack_reasm.c | 260 ++++++-------------- net/ipv6/reassembly.c | 240 ++++++------------- 6 files changed, 488 insertions(+), 634 deletions(-)
Signature · 1 line
2.21.0.593.g511ec345e18-goog
PATCH v2 1/3 net: IP defrag: encapsulate rbtree defrag code into callable functions
[ Upstream commit c23f35d19db3b36ffb9e04b08f1d91565d15f84f ]
This is a refactoring patch: without changing runtime behavior, it moves rbtree-related code from IPv4-specific files/functions into .h/.c defrag files shared with IPv6 defragmentation code.
v2: make handling of overlapping packets match upstream.
--- include/net/inet_frag.h | 16 ++- net/ipv4/inet_fragment.c | 293 +++++++++++++++++++++++++++++++++++++ net/ipv4/ip_fragment.c | 302 +++++---------------------------------- 3 files changed, 342 insertions(+), 269 deletions(-)
include/net/inet_frag.h +14 -2
@@ -77,8 +77,8 @@ struct inet_frag_queue {struct timer_list timer;spinlock_t lock;refcount_t refcnt; -struct sk_buff *fragments;/* Used in IPv6. */ -struct rb_root rb_fragments;/* Used in IPv4. */ +struct sk_buff *fragments;/* used in 6lopwpan IPv6. */ +struct rb_root rb_fragments;/* Used in IPv4/IPv6. */ struct sk_buff *fragments_tail;struct sk_buff *last_run_head;ktime_t stamp;
@@ -153,4 +153,16 @@ static inline void add_frag_mem_limit(struct netns_frags *nf, long val)extern const u8 ip_frag_ecn_table[16 ]; +/* Return values of inet_frag_queue_insert() */ +#define IPFRAG_OK 0 +#define IPFRAG_DUP 1 +#define IPFRAG_OVERLAP 2 +int inet_frag_queue_insert (struct inet_frag_queue *q,struct sk_buff *skb, +int offset,int end); +void *inet_frag_reasm_prepare (struct inet_frag_queue *q,struct sk_buff *skb, +struct sk_buff *parent); +void inet_frag_reasm_finish (struct inet_frag_queue *q,struct sk_buff *head, +void *reasm_data); +struct sk_buff *inet_frag_pull_head (struct inet_frag_queue *q); +#endif
net/ipv4/inet_fragment.c +293 -0
308 lines not on this page · raw message
net/ipv4/ip_fragment.c +35 -267
400 lines not on this page · raw message
Signature · 1 line
2.21.0.593.g511ec345e18-goog
PATCH v2 2/3 net: IP6 defrag: use rbtrees for IPv6 defrag
In reply to Peter Oskolkov[ Upstream commit d4289fcc9b16b89619ee1c54f829e05e56de8b9a ]
Currently, IPv6 defragmentation code drops non-last fragments that are smaller than 1280 bytes: see
commit 0ed4229b08c1 ("ipv6: defrag: drop non-last frags smaller than min mtu")
This behavior is not specified in IPv6 RFCs and appears to break compatibility with some IPv6 implemenations, as reported here:
https://www.spinics.net/lists/netdev/msg543846.html
This patch re-uses common IP defragmentation queueing and reassembly code in IPv6, removing the 1280 byte restriction.
v2: change handling of overlaps to match that of upstream.
--- include/net/ipv6_frag.h | 11 +- net/ipv6/reassembly.c | 240 +++++++++++----------------------------- 2 files changed, 75 insertions(+), 176 deletions(-)
include/net/ipv6_frag.h +9 -2
@@ -82,8 +82,15 @@ ip6frag_expire_frag_queue(struct net *net, struct frag_queue *fq)__IP6_INC_STATS (net,__in6_dev_get (dev),IPSTATS_MIB_REASMTIMEOUT );/* Don't send error if the first segment did not arrive. */ - head = fq->q.fragments; -if (!(fq->q.flags &INET_FRAG_FIRST_IN ) || !head) +if (!(fq->q.flags &INET_FRAG_FIRST_IN )) + goto out; + +/* sk_buff::dev and sk_buff::rbnode are unionized. So we +* pull the head out of the tree in order to be able to +* deal with head->dev. +*/ + head =inet_frag_pull_head (&fq->q); +if (!head) goto out; head->dev = dev;
net/ipv6/reassembly.c +66 -174
365 lines not on this page · raw message
Signature · 1 line
2.21.0.593.g511ec345e18-goog
PATCH v2 3/3 net: IP6 defrag: use rbtrees in nf_conntrack_reasm.c
In reply to Peter Oskolkov[ Upstream commit 997dd96471641e147cb2c33ad54284000d0f5e35 ]
Currently, IPv6 defragmentation code drops non-last fragments that are smaller than 1280 bytes: see
commit 0ed4229b08c1 ("ipv6: defrag: drop non-last frags smaller than min mtu")
This behavior is not specified in IPv6 RFCs and appears to break compatibility with some IPv6 implemenations, as reported here:
https://www.spinics.net/lists/netdev/msg543846.html
This patch re-uses common IP defragmentation queueing and reassembly code in IP6 defragmentation in nf_conntrack, removing the 1280 byte restriction.
--- net/ipv6/netfilter/nf_conntrack_reasm.c | 260 +++++++----------------- 1 file changed, 71 insertions(+), 189 deletions(-)
net/ipv6/netfilter/nf_conntrack_reasm.c +71 -189
@@ -136,6 +136,9 @@ static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net) }#endif +static int nf_ct_frag6_reasm (struct frag_queue *fq,struct sk_buff *skb, +struct sk_buff *prev_tail,struct net_device *dev); +static inline u8 ip6_frag_ecn (const struct ipv6hdr *ipv6h) {return 1 << (ipv6_get_dsfield (ipv6h) &INET_ECN_MASK );
@@ -177,9 +180,10 @@ static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,static int nf_ct_frag6_queue (struct frag_queue *fq,struct sk_buff *skb,const struct frag_hdr *fhdr,int nhoff) { -struct sk_buff *prev, *next;unsigned int payload_len; -int offset, end; +struct net_device *dev; +struct sk_buff *prev; +int offset, end, err;u8 ecn;if (fq->q.flags &INET_FRAG_COMPLETE ) {
@@ -254,55 +258,18 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, goto err; } -/* Find out which fragments are in front and at the back of us -* in the chain of fragments so far. We must know where to put -* this fragment, right? -*/ - prev = fq->q.fragments_tail; -if (!prev || prev->ip_defrag_offset < offset) { - next =NULL ; - goto found; - } - prev =NULL ; -for (next = fq->q.fragments; next !=NULL ; next = next->next) { -if (next->ip_defrag_offset >= offset) -break ;/* bingo! */ - prev = next; - } - -found: -/* RFC5722, Section 4: -* When reassembling an IPv6 datagram, if -* one or more its constituent fragments is determined to be an -* overlapping fragment, the entire datagram (and any constituent -* fragments, including those not yet received) MUST be silently -* discarded. -*/ - -/* Check for overlap with preceding fragment. */ -if (prev && - (prev->ip_defrag_offset + prev->len) > offset) - goto discard_fq; - -/* Look for overlap with succeeding segment. */ -if (next && next->ip_defrag_offset < end) - goto discard_fq; - -/* Note : skb->ip_defrag_offset and skb->dev share the same location */ -if (skb->dev) - fq->iif = skb->dev->ifindex; +/* Note : skb->rbnode and skb->dev share the same location. */ + dev = skb->dev;/* Makes sure compiler wont do silly aliasing games */ barrier (); - skb->ip_defrag_offset = offset; -/* Insert this fragment in the chain of fragments. */ - skb->next = next; -if (!next) - fq->q.fragments_tail = skb; -if (prev) - prev->next = skb; -else - fq->q.fragments = skb; + prev = fq->q.fragments_tail; + err =inet_frag_queue_insert (&fq->q, skb, offset, end); +if (err) + goto insert_error; + +if (dev) + fq->iif = dev->ifindex; fq->q.stamp = skb->tstamp; fq->q.meat += skb->len;
@@ -319,11 +286,25 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, fq->q.flags |=INET_FRAG_FIRST_IN ; } -return 0 ; +if (fq->q.flags == (INET_FRAG_FIRST_IN |INET_FRAG_LAST_IN ) && + fq->q.meat == fq->q.len) { +unsigned long orefdst = skb->_skb_refdst; + + skb->_skb_refdst =0UL ; + err =nf_ct_frag6_reasm (fq, skb, prev, dev); + skb->_skb_refdst = orefdst; +return err; + } + +skb_dst_drop (skb); +return -EINPROGRESS ; -discard_fq: +insert_error: +if (err ==IPFRAG_DUP ) + goto err;inet_frag_kill (&fq->q); err: +skb_dst_drop (skb);return -EINVAL ; }
@@ -333,147 +314,67 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,* It is called with locked fq, and caller must check that * queue is eligible for reassembly i.e. it is not COMPLETE, * the last and the first frames arrived and all the bits are here. -* -* returns true if *prev skb has been transformed into the reassembled -* skb, false otherwise. */ -static bool -nf_ct_frag6_reasm (struct frag_queue *fq,struct sk_buff *prev,struct net_device *dev) +static int nf_ct_frag6_reasm (struct frag_queue *fq,struct sk_buff *skb, +struct sk_buff *prev_tail,struct net_device *dev) { -struct sk_buff *fp, *head = fq->q.fragments; -int payload_len, delta; +void *reasm_data; +int payload_len;u8 ecn;inet_frag_kill (&fq->q); -WARN_ON (head ==NULL ); -WARN_ON (head->ip_defrag_offset !=0 ); - ecn = ip_frag_ecn_table[fq->ecn];if (unlikely (ecn ==0xff )) -return false; + goto err; + + reasm_data =inet_frag_reasm_prepare (&fq->q, skb, prev_tail); +if (!reasm_data) + goto err; -/* Unfragmented part is taken from the first segment. */ - payload_len = ((head->data -skb_network_header (head)) - + payload_len = ((skb->data -skb_network_header (skb)) -sizeof (struct ipv6hdr ) + fq->q.len -sizeof (struct frag_hdr ));if (payload_len >IPV6_MAXPLEN ) {net_dbg_ratelimited ("nf_ct_frag6_reasm: payload len = %d\n" , payload_len); -return false; - } - - delta = - head->truesize; - -/* Head of list must not be cloned. */ -if (skb_unclone (head,GFP_ATOMIC )) -return false; - - delta += head->truesize; -if (delta) -add_frag_mem_limit (fq->q.net, delta); - -/* If the first fragment is fragmented itself, we split -* it to two chunks: the first with data and paged part -* and the second, holding only fragments. */ -if (skb_has_frag_list (head)) { -struct sk_buff *clone; -int i, plen =0 ; - - clone =alloc_skb (0 ,GFP_ATOMIC ); -if (clone ==NULL ) -return false; - - clone->next = head->next; - head->next = clone; -skb_shinfo (clone)->frag_list =skb_shinfo (head)->frag_list; -skb_frag_list_init (head); -for (i =0 ; i <skb_shinfo (head)->nr_frags; i++) - plen +=skb_frag_size (&skb_shinfo (head)->frags[i]); - clone->len = clone->data_len = head->data_len - plen; - head->data_len -= clone->len; - head->len -= clone->len; - clone->csum =0 ; - clone->ip_summed = head->ip_summed; - -add_frag_mem_limit (fq->q.net, clone->truesize); - } - -/* morph head into last received skb: prev. -* -* This allows callers of ipv6 conntrack defrag to continue -* to use the last skb(frag) passed into the reasm engine. -* The last skb frag 'silently' turns into the full reassembled skb. -* -* Since prev is also part of q->fragments we have to clone it first. -*/ -if (head != prev) { -struct sk_buff *iter; - - fp =skb_clone (prev,GFP_ATOMIC ); -if (!fp) -return false; - - fp->next = prev->next; - - iter = head; -while (iter) { -if (iter->next == prev) { - iter->next = fp; -break ; - } - iter = iter->next; - } - -skb_morph (prev, head); - prev->next = head->next; -consume_skb (head); - head = prev; + goto err; }/* We have to remove fragment header from datagram and to relocate * header in order to calculate ICV correctly. */ -skb_network_header (head)[fq->nhoffset] =skb_transport_header (head)[0 ]; -memmove (head->head +sizeof (struct frag_hdr ), head->head, - (head->data - head->head) -sizeof (struct frag_hdr )); - head->mac_header +=sizeof (struct frag_hdr ); - head->network_header +=sizeof (struct frag_hdr ); - -skb_shinfo (head)->frag_list = head->next; -skb_reset_transport_header (head); -skb_push (head, head->data -skb_network_header (head)); - -for (fp = head->next; fp; fp = fp->next) { - head->data_len += fp->len; - head->len += fp->len; -if (head->ip_summed != fp->ip_summed) - head->ip_summed =CHECKSUM_NONE ; -else if (head->ip_summed ==CHECKSUM_COMPLETE ) - head->csum =csum_add (head->csum, fp->csum); - head->truesize += fp->truesize; - fp->sk =NULL ; - } -sub_frag_mem_limit (fq->q.net, head->truesize); +skb_network_header (skb)[fq->nhoffset] =skb_transport_header (skb)[0 ]; +memmove (skb->head +sizeof (struct frag_hdr ), skb->head, + (skb->data - skb->head) -sizeof (struct frag_hdr )); + skb->mac_header +=sizeof (struct frag_hdr ); + skb->network_header +=sizeof (struct frag_hdr ); + +skb_reset_transport_header (skb); + +inet_frag_reasm_finish (&fq->q, skb, reasm_data); - head->ignore_df =1 ; - head->next =NULL ; - head->dev = dev; - head->tstamp = fq->q.stamp; -ipv6_hdr (head)->payload_len =htons (payload_len); -ipv6_change_dsfield (ipv6_hdr (head),0xff , ecn); -IP6CB (head)->frag_max_size =sizeof (struct ipv6hdr ) + fq->q.max_size; + skb->ignore_df =1 ; + skb->dev = dev; +ipv6_hdr (skb)->payload_len =htons (payload_len); +ipv6_change_dsfield (ipv6_hdr (skb),0xff , ecn); +IP6CB (skb)->frag_max_size =sizeof (struct ipv6hdr ) + fq->q.max_size;/* Yes, and fold redundant checksum back. 8) */ -if (head->ip_summed ==CHECKSUM_COMPLETE ) - head->csum =csum_partial (skb_network_header (head), -skb_network_header_len (head), - head->csum); +if (skb->ip_summed ==CHECKSUM_COMPLETE ) + skb->csum =csum_partial (skb_network_header (skb), +skb_network_header_len (skb), + skb->csum); fq->q.fragments =NULL ; fq->q.rb_fragments =RB_ROOT ; fq->q.fragments_tail =NULL ; + fq->q.last_run_head =NULL ; -return true; +return 0 ; + +err: +inet_frag_kill (&fq->q); +return -EINVAL ; }/*
@@ -542,7 +443,6 @@ find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)int nf_ct_frag6_gather (struct net *net,struct sk_buff *skb,u32 user) {u16 savethdr = skb->transport_header; -struct net_device *dev = skb->dev;int fhoff, nhoff, ret;struct frag_hdr *fhdr;struct frag_queue *fq;
@@ -565,10 +465,6 @@ int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user) hdr =ipv6_hdr (skb); fhdr = (struct frag_hdr *)skb_transport_header (skb); -if (skb->len -skb_network_offset (skb) <IPV6_MIN_MTU && - fhdr->frag_off &htons (IP6_MF )) -return -EINVAL ; -skb_orphan (skb); fq =fq_find (net, fhdr->identification, user, hdr, skb->dev ? skb->dev->ifindex :0 );
@@ -580,31 +476,17 @@ int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)spin_lock_bh (&fq->q.lock); ret =nf_ct_frag6_queue (fq, skb, fhdr, nhoff); -if (ret <0 ) { -if (ret == -EPROTO ) { - skb->transport_header = savethdr; - ret =0 ; - } - goto out_unlock; +if (ret == -EPROTO ) { + skb->transport_header = savethdr; + ret =0 ; }/* after queue has assumed skb ownership, only 0 or -EINPROGRESS * must be returned. */ - ret = -EINPROGRESS ; -if (fq->q.flags == (INET_FRAG_FIRST_IN |INET_FRAG_LAST_IN ) && - fq->q.meat == fq->q.len) { -unsigned long orefdst = skb->_skb_refdst; - - skb->_skb_refdst =0UL ; -if (nf_ct_frag6_reasm (fq, skb, dev)) - ret =0 ; - skb->_skb_refdst = orefdst; - }else { -skb_dst_drop (skb); - } +if (ret) + ret = -EINPROGRESS ; -out_unlock:spin_unlock_bh (&fq->q.lock);inet_frag_put (&fq->q);return ret;
Signature · 1 line
2.21.0.593.g511ec345e18-goog
Sasha Levin
maintainerQuoted 9 lines · Peter Oskolkov
Lars Persson <[email protected]> reported that a label was unused in the 4.14 version of this patchset, and the issue was present in the 4.19 patchset as well, so I'm sending a v2 that fixes it.
The original 4.19 patchset queued for stable is OK, and can be used as is, but this v2 is a bit better: it fixes the unused label issue and handles overlapping fragments better.
Sorry for the mess/v2.
I've replace the patches in 4.19, thank you.
--
Thanks,
Sasha