Skip to contentSkip to conversation

ip: Use rb trees for IP frag queue.

PATCH Superseded Peter Oskolkov 4 participants 8 messages Aug 3, 2018 – Aug 6, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ip: Use rb trees for IP frag queue.

This patchset
* changes IPv4 defrag behavior to match that of IPv6: overlapping fragments now cause the whole IP datagram to be discarded (suggested by David Miller): there are no legitimate use cases for overlapping fragments;
* changes IPv4 defrag queue from a list to a rb tree (suggested by Eric Dumazet): this change removes a potential attach vector.

Upcoming patches will contain similar changes for IPv6 frag queue, as well as a comprehensive IP defrag self-test (temporarily delayed).

Peter Oskolkov (3):
ip: discard IPv4 datagrams with overlapping segments.
net: modify skb_rbtree_purge to return the truesize of all purged
skbs.
ip: use rb trees for IP frag queue.

 include/linux/skbuff.h                  |  11 +-
 include/net/inet_frag.h                 |   3 +-
 include/uapi/linux/snmp.h               |   1 +
 net/core/skbuff.c                       |   6 +-
 net/ipv4/inet_fragment.c                |  16 +-
 net/ipv4/ip_fragment.c                  | 239 +++++++++++-------------
 net/ipv4/proc.c                         |   1 +
 net/ipv6/netfilter/nf_conntrack_reasm.c |   1 +
 net/ipv6/reassembly.c                   |   1 +
 9 files changed, 139 insertions(+), 140 deletions(-)
Signature · 1 line
2.18.0.597.ga71716f1ad-goog
#1 lore raw

PATCH 1/3 ip: discard IPv4 datagrams with overlapping segments.

This behavior is required in IPv6, and there is little need to tolerate overlapping fragments in IPv4. This change simplifies the code and eliminates potential DDoS attack vectors.

---
 include/uapi/linux/snmp.h |  1 +
 net/ipv4/ip_fragment.c    | 75 ++++++++++-----------------------------
 net/ipv4/proc.c           |  1 +
 3 files changed, 21 insertions(+), 56 deletions(-)
include/uapi/linux/snmp.h +1 -0
index e5ebc83827ab..da1a144f1a51 100644
@@ -40,6 +40,7 @@ enum
 	IPSTATS_MIB_REASMREQDS,			/* ReasmReqds */
 	IPSTATS_MIB_REASMOKS,			/* ReasmOKs */
 	IPSTATS_MIB_REASMFAILS,			/* ReasmFails */
+	IPSTATS_MIB_REASM_OVERLAPS,		/* ReasmOverlaps */
 	IPSTATS_MIB_FRAGOKS,			/* FragOKs */
 	IPSTATS_MIB_FRAGFAILS,			/* FragFails */
 	IPSTATS_MIB_FRAGCREATES,		/* FragCreates */
net/ipv4/ip_fragment.c +19 -56

96 lines not on this page · raw message

net/ipv4/proc.c +1 -0
index b46e4cf9a55a..70289682a670 100644
@@ -119,6 +119,7 @@ static const struct snmp_mib snmp4_ipextstats_list[] = {
 	SNMP_MIB_ITEM("InECT1Pkts", IPSTATS_MIB_ECT1PKTS),
 	SNMP_MIB_ITEM("InECT0Pkts", IPSTATS_MIB_ECT0PKTS),
 	SNMP_MIB_ITEM("InCEPkts", IPSTATS_MIB_CEPKTS),
+	SNMP_MIB_ITEM("ReasmOverlaps", IPSTATS_MIB_REASM_OVERLAPS),
 	SNMP_MIB_SENTINEL
 };
 
Signature · 1 line
2.18.0.597.ga71716f1ad-goog
#2 lore raw

Re: [PATCH net-next 1/3] ip: discard IPv4 datagrams with overlapping segments.

include/uapi/linux/snmp.h enum · line 46

Quoted 12 lines · On Thu, 2 Aug 2018 22:45:58 +0000 Peter Oskolkov <[email protected]> wrote:
include/uapi/linux/snmp.h +1 -0
index e5ebc83827ab..da1a144f1a51 100644
@@ -40,6 +40,7 @@ enum
 	IPSTATS_MIB_REASMREQDS,			/* ReasmReqds */
 	IPSTATS_MIB_REASMOKS,			/* ReasmOKs */
 	IPSTATS_MIB_REASMFAILS,			/* ReasmFails */
+	IPSTATS_MIB_REASM_OVERLAPS,		/* ReasmOverlaps */
 	IPSTATS_MIB_FRAGOKS,			/* FragOKs */
 	IPSTATS_MIB_FRAGFAILS,			/* FragFails */
 	IPSTATS_MIB_FRAGCREATES,		/* FragCreates */

Inserting new entries in the middle of an enum means the numeric values will change. Isn't this going to break userspace ABI?

#3 lore raw

Re: [PATCH net-next 1/3] ip: discard IPv4 datagrams with overlapping segments.

Quoted 19 lines · Stephen Hemminger

include/uapi/linux/snmp.h enum · line 46

Quoted 12 lines · On Thu, 2 Aug 2018 22:45:58 +0000 Peter Oskolkov <[email protected]> wrote:
include/uapi/linux/snmp.h +1 -0
index e5ebc83827ab..da1a144f1a51 100644
@@ -40,6 +40,7 @@ enum
 	IPSTATS_MIB_REASMREQDS,			/* ReasmReqds */
 	IPSTATS_MIB_REASMOKS,			/* ReasmOKs */
 	IPSTATS_MIB_REASMFAILS,			/* ReasmFails */
+	IPSTATS_MIB_REASM_OVERLAPS,		/* ReasmOverlaps */
 	IPSTATS_MIB_FRAGOKS,			/* FragOKs */
 	IPSTATS_MIB_FRAGFAILS,			/* FragFails */
 	IPSTATS_MIB_FRAGCREATES,		/* FragCreates */

Inserting new entries in the middle of an enum means the numeric values will change. Isn't this going to break userspace ABI?

I would argue the only exported thing from the kernel are the Key:Val in the /proc files.

Not sure why these enum are uapi.

Commit 46c2fa39877ed70415ee2b1acfb9129e956f6de4 added LINUX_MIB_TCPFASTOPENBLACKHOLE in the middle, and really nobody complained.

Commit 0604475119de5f80dc051a5db055c6a2a75bd542 added LINUX_MIBSTCPMEMORYPRESSURESCHRONO in the middle as well.

I am pretty sure we should maintain locality of these counters to lower number of dirtied cache lines, say under IP frag DDOS ;)

#4 lore raw

Re: [PATCH net-next 1/3] ip: discard IPv4 datagrams with overlapping segments.

Quoted 29 lines · On Thu, 2 Aug 2018 16:33:55 -0700 Eric Dumazet <[email protected]> wrote:
Quoted 19 lines · Stephen Hemminger

include/uapi/linux/snmp.h enum · line 46

Quoted 12 lines · On Thu, 2 Aug 2018 22:45:58 +0000 Peter Oskolkov <[email protected]> wrote:
include/uapi/linux/snmp.h +1 -0
index e5ebc83827ab..da1a144f1a51 100644
@@ -40,6 +40,7 @@ enum
 	IPSTATS_MIB_REASMREQDS,			/* ReasmReqds */
 	IPSTATS_MIB_REASMOKS,			/* ReasmOKs */
 	IPSTATS_MIB_REASMFAILS,			/* ReasmFails */
+	IPSTATS_MIB_REASM_OVERLAPS,		/* ReasmOverlaps */
 	IPSTATS_MIB_FRAGOKS,			/* FragOKs */
 	IPSTATS_MIB_FRAGFAILS,			/* FragFails */
 	IPSTATS_MIB_FRAGCREATES,		/* FragCreates */  

Inserting new entries in the middle of an enum means the numeric values will change. Isn't this going to break userspace ABI?

I would argue the only exported thing from the kernel are the Key:Val in the /proc files.

Not sure why these enum are uapi.

Commit 46c2fa39877ed70415ee2b1acfb9129e956f6de4 added LINUX_MIB_TCPFASTOPENBLACKHOLE in the middle, and really nobody complained.

Quoted 6 lines

Commit 0604475119de5f80dc051a5db055c6a2a75bd542 added LINUX_MIBSTCPMEMORYPRESSURESCHRONO in the middle as well.

I am pretty sure we should maintain locality of these counters to lower number of dirtied cache lines, say under IP frag DDOS ;)

Agree with eric, go ahead and keep things local

#5 lore raw

PATCH 2/3 net: modify skb_rbtree_purge to return the truesize of all purged skbs.

In reply to Peter Oskolkov
---
 include/linux/skbuff.h | 2 +-
 net/core/skbuff.c      | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/skbuff.h +1 -1
index fd3cb1b247df..47848367c816 100644
@@ -2585,7 +2585,7 @@ static inline void __skb_queue_purge(struct sk_buff_head *list)
 		kfree_skb(skb);
 }
 
-void skb_rbtree_purge(struct rb_root *root);
+unsigned int skb_rbtree_purge(struct rb_root *root);
 
 void *netdev_alloc_frag(unsigned int fragsz);
 
net/core/skbuff.c +5 -1
index 51b0a9126e12..8d574a88125d 100644
@@ -2858,23 +2858,27 @@ EXPORT_SYMBOL(skb_queue_purge);
 /**
  *	skb_rbtree_purge - empty a skb rbtree
  *	@root: root of the rbtree to empty
+ *	Return value: the sum of truesizes of all purged skbs.
  *
  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
  *	the list and one reference dropped. This function does not take
  *	any lock. Synchronization should be handled by the caller (e.g., TCP
  *	out-of-order queue is protected by the socket lock).
  */
-void skb_rbtree_purge(struct rb_root *root)
+unsigned int skb_rbtree_purge(struct rb_root *root)
 {
 	struct rb_node *p = rb_first(root);
+	unsigned int sum = 0;
 
 	while (p) {
 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
 
 		p = rb_next(p);
 		rb_erase(&skb->rbnode, root);
+		sum += skb->truesize;
 		kfree_skb(skb);
 	}
+	return sum;
 }
 
 /**
Signature · 1 line
2.18.0.597.ga71716f1ad-goog
#6 lore raw

PATCH 3/3 ip: use rb trees for IP frag queue.

In reply to Peter Oskolkov

Similar to TCP OOO RX queue, it makes sense to use rb trees to store IP fragments, so that OOO fragments are inserted faster.

Tested:

- a follow-up patch contains a rather comprehensive ip defrag self-test (functional)
- ran neper udp_stream -c -H <host> -F 100 -l 300 -T 20:

    netstat --statistics
    Ip:
        282078937 total packets received
        0 forwarded
        0 incoming packets discarded
        946760 incoming packets delivered
        18743456 requests sent out
        101 fragments dropped after timeout
        282077129 reassemblies required
        944952 packets reassembled ok
        262734239 packet reassembles failed

(The numbers/stats above are somewhat better re:
reassemblies vs a kernel without this patchset. More comprehensive performance testing TBD).

---
 include/linux/skbuff.h                  |   9 +-
 include/net/inet_frag.h                 |   3 +-
 net/ipv4/inet_fragment.c                |  16 ++-
 net/ipv4/ip_fragment.c                  | 182 +++++++++++++-----------
 net/ipv6/netfilter/nf_conntrack_reasm.c |   1 +
 net/ipv6/reassembly.c                   |   1 +
 6 files changed, 121 insertions(+), 91 deletions(-)
include/linux/skbuff.h +6 -3
index 47848367c816..7ebdf158a795 100644
@@ -676,13 +676,16 @@ struct sk_buff {
 				 * UDP receive path is one user.
 				 */
 				unsigned long		dev_scratch;
-				int			ip_defrag_offset;
 			};
 		};
-		struct rb_node		rbnode; /* used in netem & tcp stack */
+		struct rb_node		rbnode; /* used in netem, ip4 defrag, and tcp stack */
 		struct list_head	list;
 	};
-	struct sock		*sk;
+
+	union {
+		struct sock		*sk;
+		int			ip_defrag_offset;
+	};
 
 	union {
 		ktime_t		tstamp;
include/net/inet_frag.h +2 -1
index f4272a29dc44..b86d14528188 100644
@@ -75,7 +75,8 @@ struct inet_frag_queue {
 	struct timer_list	timer;
 	spinlock_t		lock;
 	refcount_t		refcnt;
-	struct sk_buff		*fragments;
+	struct sk_buff		*fragments;  /* Used in IPv6. */
+	struct rb_root		rb_fragments; /* Used in IPv4. */
 	struct sk_buff		*fragments_tail;
 	ktime_t			stamp;
 	int			len;
net/ipv4/inet_fragment.c +10 -6
index ccd140e4082d..6d258a5669e7 100644
@@ -137,12 +137,16 @@ void inet_frag_destroy(struct inet_frag_queue *q)
 	fp = q->fragments;
 	nf = q->net;
 	f = nf->f;
-	while (fp) {
-		struct sk_buff *xp = fp->next;
-
-		sum_truesize += fp->truesize;
-		kfree_skb(fp);
-		fp = xp;
+	if (fp) {
+		do {
+			struct sk_buff *xp = fp->next;
+
+			sum_truesize += fp->truesize;
+			kfree_skb(fp);
+			fp = xp;
+		} while (fp);
+	} else {
+		sum_truesize = skb_rbtree_purge(&q->rb_fragments);
 	}
 	sum = sum_truesize + f->qsize;
 
net/ipv4/ip_fragment.c +101 -81

321 lines not on this page · raw message

net/ipv6/netfilter/nf_conntrack_reasm.c +1 -0
index 0610bdab721c..38d69ef516d5 100644
@@ -463,6 +463,7 @@ nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev,  struct net_devic
 					  head->csum);
 
 	fq->q.fragments = NULL;
+	fq->q.rb_fragments = RB_ROOT;
 	fq->q.fragments_tail = NULL;
 
 	return true;
net/ipv6/reassembly.c +1 -0
index 6edd2ac8ae4b..b4e558ab39fa 100644
@@ -405,6 +405,7 @@ static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
 	rcu_read_unlock();
 	fq->q.fragments = NULL;
+	fq->q.rb_fragments = RB_ROOT;
 	fq->q.fragments_tail = NULL;
 	return 1;
 
Signature · 1 line
2.18.0.597.ga71716f1ad-goog
maintainer
#7 lore raw
In reply to Peter Oskolkov

From: Peter Oskolkov <[email protected]>
Date: Thu, 2 Aug 2018 22:45:57 +0000

Quoted 10 lines

This patchset
* changes IPv4 defrag behavior to match that of IPv6: overlapping fragments now cause the whole IP datagram to be discarded (suggested by David Miller): there are no legitimate use cases for overlapping fragments;
* changes IPv4 defrag queue from a list to a rb tree (suggested by Eric Dumazet): this change removes a potential attach vector.

Upcoming patches will contain similar changes for IPv6 frag queue, as well as a comprehensive IP defrag self-test (temporarily delayed).

Looks good, series applied, thanks!