From patchwork Fri Jan 4 08:18:39 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 209378 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 7C7C82C0080 for ; Fri, 4 Jan 2013 19:18:48 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752904Ab3ADISp (ORCPT ); Fri, 4 Jan 2013 03:18:45 -0500 Received: from mail-pa0-f43.google.com ([209.85.220.43]:63960 "EHLO mail-pa0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751448Ab3ADISn (ORCPT ); Fri, 4 Jan 2013 03:18:43 -0500 Received: by mail-pa0-f43.google.com with SMTP id fb10so9196918pad.30 for ; Fri, 04 Jan 2013 00:18:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:subject:from:to:cc:content-type:date:message-id :mime-version:x-mailer:content-transfer-encoding; bh=O61b3hzVsgMkiLPKAuqpj18wf09CulzVdU7zYSJN6Uk=; b=09AHF0IxFiXak6Fk4Cwqf62Md8eSyK8rKGIEzb3qOoT6q+D18XEQ+SVSMfQJngCbXI w1ekoboAKmUMz2bv7k12f9qqJVixXuZUPsdjltKt6+bC00k11aoSzkfZtRWSyhXgWizR IuOjYgehRKeK4vwH69H5Zi79BEDMLg8bPopfMlEtlISlEDKSbbs+RkcpTFShFrSLsLYa AC1+zI8RTdmDAjT208HP9FfTvSMXrkfKvw4amAg4/MxunMv4VW71OtESFTV8LN1raKBi pOBHwqVbndKtYairQVlxHGaWZAqPm2NiGsijBE6e9Hc0m4iM84HecHL/iZmbg4Cc/Gxh 5qnw== X-Received: by 10.66.84.10 with SMTP id u10mr153391621pay.24.1357287522724; Fri, 04 Jan 2013 00:18:42 -0800 (PST) Received: from [172.19.251.33] ([172.19.251.33]) by mx.google.com with ESMTPS id qb3sm31846705pbb.35.2013.01.04.00.18.40 (version=SSLv3 cipher=OTHER); Fri, 04 Jan 2013 00:18:41 -0800 (PST) Subject: [PATCH] netfilter: xt_recent: avoid high order page allocations From: Eric Dumazet To: Pablo Neira Ayuso Cc: netdev , netfilter-devel@vger.kernel.org, Miroslav Kratochvil , h.reindl@thelounge.net, Dave Jones Date: Fri, 04 Jan 2013 00:18:39 -0800 Message-ID: <1357287519.1678.70.camel@edumazet-glaptop> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Eric Dumazet 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. Reported-by: Miroslav Kratochvil Reported-by: Dave Jones Reported-by: Harald Reindl Signed-off-by: Eric Dumazet --- net/netfilter/xt_recent.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/net/netfilter/xt_recent.c b/net/netfilter/xt_recent.c index dab053e..978efc9 100644 --- a/net/netfilter/xt_recent.c +++ b/net/netfilter/xt_recent.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -310,6 +311,14 @@ out: return ret; } +static void recent_table_free(void *addr) +{ + if (is_vmalloc_addr(addr)) + vfree(addr); + else + kfree(addr); +} + static int recent_mt_check(const struct xt_mtchk_param *par, const struct xt_recent_mtinfo_v1 *info) { @@ -322,6 +331,7 @@ static int recent_mt_check(const struct xt_mtchk_param *par, #endif unsigned int i; int ret = -EINVAL; + size_t sz; if (unlikely(!hash_rnd_inited)) { get_random_bytes(&hash_rnd, sizeof(hash_rnd)); @@ -360,8 +370,11 @@ static int recent_mt_check(const struct xt_mtchk_param *par, goto out; } - t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size, - GFP_KERNEL); + sz = sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size; + if (sz <= PAGE_SIZE) + t = kzalloc(sz, GFP_KERNEL); + else + t = vzalloc(sz); if (t == NULL) { ret = -ENOMEM; goto out; @@ -377,14 +390,14 @@ static int recent_mt_check(const struct xt_mtchk_param *par, uid = make_kuid(&init_user_ns, ip_list_uid); gid = make_kgid(&init_user_ns, ip_list_gid); if (!uid_valid(uid) || !gid_valid(gid)) { - kfree(t); + recent_table_free(t); ret = -EINVAL; goto out; } pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent, &recent_mt_fops, t); if (pde == NULL) { - kfree(t); + recent_table_free(t); ret = -ENOMEM; goto out; } @@ -435,7 +448,7 @@ static void recent_mt_destroy(const struct xt_mtdtor_param *par) remove_proc_entry(t->name, recent_net->xt_recent); #endif recent_table_flush(t); - kfree(t); + recent_table_free(t); } mutex_unlock(&recent_mutex); }