Skip to content

Commit 95c4d95

Browse files
strssndktngregkh
authored andcommitted
inet: limit length of fragment queue hash table bucket lists
[ Upstream commit 5a3da1f ] This patch introduces a constant limit of the fragment queue hash table bucket list lengths. Currently the limit 128 is choosen somewhat arbitrary and just ensures that we can fill up the fragment cache with empty packets up to the default ip_frag_high_thresh limits. It should just protect from list iteration eating considerable amounts of cpu. If we reach the maximum length in one hash bucket a warning is printed. This is implemented on the caller side of inet_frag_find to distinguish between the different users of inet_fragment.c. I dropped the out of memory warning in the ipv4 fragment lookup path, because we already get a warning by the slab allocator. Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Jesper Dangaard Brouer <jbrouer@redhat.com> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e2bf3e0 commit 95c4d95

5 files changed

Lines changed: 44 additions & 16 deletions

File tree

‎include/net/inet_frag.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ struct inet_frag_queue {
3535

3636
#define INETFRAGS_HASHSZ 64
3737

38+
/* averaged:
39+
* max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
40+
* rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
41+
* struct frag_queue))
42+
*/
43+
#define INETFRAGS_MAXDEPTH 128
44+
3845
struct inet_frags {
3946
struct hlist_head hash[INETFRAGS_HASHSZ];
4047
rwlock_t lock;
@@ -65,6 +72,8 @@ int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force);
6572
struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
6673
struct inet_frags *f, void *key, unsigned int hash)
6774
__releases(&f->lock);
75+
void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
76+
const char *prefix);
6877

6978
static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
7079
{

‎net/ipv4/inet_fragment.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/rtnetlink.h>
2222
#include <linux/slab.h>
2323

24+
#include <net/sock.h>
2425
#include <net/inet_frag.h>
2526

2627
static void inet_frag_secret_rebuild(unsigned long dummy)
@@ -276,16 +277,33 @@ struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
276277
{
277278
struct inet_frag_queue *q;
278279
struct hlist_node *n;
280+
int depth = 0;
279281

280282
hlist_for_each_entry(q, n, &f->hash[hash], list) {
281283
if (q->net == nf && f->match(q, key)) {
282284
atomic_inc(&q->refcnt);
283285
read_unlock(&f->lock);
284286
return q;
285287
}
288+
depth++;
286289
}
287290
read_unlock(&f->lock);
288291

289-
return inet_frag_create(nf, f, key);
292+
if (depth <= INETFRAGS_MAXDEPTH)
293+
return inet_frag_create(nf, f, key);
294+
else
295+
return ERR_PTR(-ENOBUFS);
290296
}
291297
EXPORT_SYMBOL(inet_frag_find);
298+
299+
void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
300+
const char *prefix)
301+
{
302+
static const char msg[] = "inet_frag_find: Fragment hash bucket"
303+
" list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
304+
". Dropping fragment.\n";
305+
306+
if (PTR_ERR(q) == -ENOBUFS)
307+
LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg);
308+
}
309+
EXPORT_SYMBOL(inet_frag_maybe_warn_overflow);

‎net/ipv4/ip_fragment.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,11 @@ static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
299299
hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
300300

301301
q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
302-
if (q == NULL)
303-
goto out_nomem;
304-
302+
if (IS_ERR_OR_NULL(q)) {
303+
inet_frag_maybe_warn_overflow(q, pr_fmt());
304+
return NULL;
305+
}
305306
return container_of(q, struct ipq, q);
306-
307-
out_nomem:
308-
LIMIT_NETDEBUG(KERN_ERR pr_fmt("ip_frag_create: no memory left !\n"));
309-
return NULL;
310307
}
311308

312309
/* Is the fragment too far ahead to be part of ipq? */

‎net/ipv6/netfilter/nf_conntrack_reasm.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* 2 of the License, or (at your option) any later version.
1515
*/
1616

17+
#define pr_fmt(fmt) "IPv6-nf: " fmt
18+
1719
#include <linux/errno.h>
1820
#include <linux/types.h>
1921
#include <linux/string.h>
@@ -180,13 +182,11 @@ static inline struct frag_queue *fq_find(struct net *net, __be32 id,
180182

181183
q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
182184
local_bh_enable();
183-
if (q == NULL)
184-
goto oom;
185-
185+
if (IS_ERR_OR_NULL(q)) {
186+
inet_frag_maybe_warn_overflow(q, pr_fmt());
187+
return NULL;
188+
}
186189
return container_of(q, struct frag_queue, q);
187-
188-
oom:
189-
return NULL;
190190
}
191191

192192

‎net/ipv6/reassembly.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* YOSHIFUJI,H. @USAGI Always remove fragment header to
2727
* calculate ICV correctly.
2828
*/
29+
30+
#define pr_fmt(fmt) "IPv6: " fmt
31+
2932
#include <linux/errno.h>
3033
#include <linux/types.h>
3134
#include <linux/string.h>
@@ -197,9 +200,10 @@ fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6
197200
hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
198201

199202
q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
200-
if (q == NULL)
203+
if (IS_ERR_OR_NULL(q)) {
204+
inet_frag_maybe_warn_overflow(q, pr_fmt());
201205
return NULL;
202-
206+
}
203207
return container_of(q, struct frag_queue, q);
204208
}
205209

0 commit comments

Comments
 (0)