History

netfilter: xt_recent: avoid high order page allocations

xt_recent can try high order page allocations and this can fail. iptables: page allocation failure: order:9, mode:0xc0d0 It also wastes about half the allocated space because of kmalloc() power-of-two roundups and struct recent_table layout. Use vmalloc() instead to save space and be less prone to allocation errors when memory is fragmented.

Eric Dumazet committed 13 years ago

Commit 2727de71 parent 757ae311 file · +18 −5Browse files

Unified · Split

net/netfilter/xt_recent.c+18−5
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929 #include <linux/skbuff.h>
3030 #include <linux/inet.h>
3131 #include <linux/slab.h>
32+#include <linux/vmalloc.h>
3233 #include <net/net_namespace.h>
3334 #include <net/netns/generic.h>
3435
@@ -310,6 +311,14 @@ out: · 275 unmodified lines
310311 return ret;
311312 }
312313
314+static void recent_table_free(void *addr)
315+{
316+ if (is_vmalloc_addr(addr))
317+ vfree(addr);
318+ else
319+ kfree(addr);
320+}
321+
313322 static int recent_mt_check(const struct xt_mtchk_param *par,
314323 const struct xt_recent_mtinfo_v1 *info)
315324 {
@@ -322,6 +331,7 @@ static int recent_mt_check(const struct xt_mtchk_param *par, · 6 unmodified lines
322331 #endif
323332 unsigned int i;
324333 int ret = -EINVAL;
334+ size_t sz;
325335
326336 if (unlikely(!hash_rnd_inited)) {
327337 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
@@ -360,8 +370,11 @@ static int recent_mt_check(const struct xt_mtchk_param *par, · 32 unmodified lines
360370 goto out;
361371 }
362372
363− t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
364− GFP_KERNEL);
373+ sz = sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size;
374+ if (sz <= PAGE_SIZE)
375+ t = kzalloc(sz, GFP_KERNEL);
376+ else
377+ t = vzalloc(sz);
365378 if (t == NULL) {
366379 ret = -ENOMEM;
367380 goto out;
@@ -377,14 +390,14 @@ static int recent_mt_check(const struct xt_mtchk_param *par, · 9 unmodified lines
377390 uid = make_kuid(&init_user_ns, ip_list_uid);
378391 gid = make_kgid(&init_user_ns, ip_list_gid);
379392 if (!uid_valid(uid) || !gid_valid(gid)) {
380− kfree(t);
393+ recent_table_free(t);
381394 ret = -EINVAL;
382395 goto out;
383396 }
384397 pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
385398 &recent_mt_fops, t);
386399 if (pde == NULL) {
387− kfree(t);
400+ recent_table_free(t);
388401 ret = -ENOMEM;
389402 goto out;
390403 }
@@ -435,7 +448,7 @@ static void recent_mt_destroy(const struct xt_mtdtor_param *par) · 44 unmodified lines
435448 remove_proc_entry(t->name, recent_net->xt_recent);
436449 #endif
437450 recent_table_flush(t);
438− kfree(t);
451+ recent_table_free(t);
439452 }
440453 mutex_unlock(&recent_mutex);
441454 }