Skip to content

Commit 5a3da1f

Browse files
strssndktndavem330
authored andcommitted
inet: limit length of fragment queue hash table bucket lists
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>
1 parent 271648b commit 5a3da1f

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
@@ -43,6 +43,13 @@ struct inet_frag_queue {
4343

4444
#define INETFRAGS_HASHSZ 64
4545

46+
/* averaged:
47+
* max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
48+
* rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
49+
* struct frag_queue))
50+
*/
51+
#define INETFRAGS_MAXDEPTH 128
52+
4653
struct inet_frags {
4754
struct hlist_head hash[INETFRAGS_HASHSZ];
4855
/* This rwlock is a global lock (seperate per IPv4, IPv6 and
@@ -76,6 +83,8 @@ int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force);
7683
struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
7784
struct inet_frags *f, void *key, unsigned int hash)
7885
__releases(&f->lock);
86+
void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
87+
const char *prefix);
7988

8089
static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
8190
{

‎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)
@@ -277,16 +278,33 @@ struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
277278
__releases(&f->lock)
278279
{
279280
struct inet_frag_queue *q;
281+
int depth = 0;
280282

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

290-
return inet_frag_create(nf, f, key);
293+
if (depth <= INETFRAGS_MAXDEPTH)
294+
return inet_frag_create(nf, f, key);
295+
else
296+
return ERR_PTR(-ENOBUFS);
291297
}
292298
EXPORT_SYMBOL(inet_frag_find);
299+
300+
void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
301+
const char *prefix)
302+
{
303+
static const char msg[] = "inet_frag_find: Fragment hash bucket"
304+
" list length grew over limit " __stringify(INETFRAGS_MAXDEPTH)
305+
". Dropping fragment.\n";
306+
307+
if (PTR_ERR(q) == -ENOBUFS)
308+
LIMIT_NETDEBUG(KERN_WARNING "%s%s", prefix, msg);
309+
}
310+
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
@@ -292,14 +292,11 @@ static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
292292
hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
293293

294294
q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
295-
if (q == NULL)
296-
goto out_nomem;
297-
295+
if (IS_ERR_OR_NULL(q)) {
296+
inet_frag_maybe_warn_overflow(q, pr_fmt());
297+
return NULL;
298+
}
298299
return container_of(q, struct ipq, q);
299-
300-
out_nomem:
301-
LIMIT_NETDEBUG(KERN_ERR pr_fmt("ip_frag_create: no memory left !\n"));
302-
return NULL;
303300
}
304301

305302
/* 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>
@@ -185,9 +188,10 @@ fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6
185188
hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
186189

187190
q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
188-
if (q == NULL)
191+
if (IS_ERR_OR_NULL(q)) {
192+
inet_frag_maybe_warn_overflow(q, pr_fmt());
189193
return NULL;
190-
194+
}
191195
return container_of(q, struct frag_queue, q);
192196
}
193197

0 commit comments

Comments
 (0)