Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp732852imm; Thu, 13 Sep 2018 07:00:16 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYUFCtu2nDOv86gHfFqpDIbGTdm/nIXQUSiF9YHGTCAIRdRUX/AciTCjhDnU0DAsXXmvtqf X-Received: by 2002:a63:db15:: with SMTP id e21-v6mr7395218pgg.418.1536847216933; Thu, 13 Sep 2018 07:00:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536847216; cv=none; d=google.com; s=arc-20160816; b=BP42CTbAyrM7GIytsB5JUc63SZYFyzgow5HkIySN9LSptCVNgT7Ec/9giK5Sz1Jkis PMNg8X7ritoiAl0dC7+rZD1P9XJmICNtxeQoPTwoCmGC68LEruB2cncy65H4AqD6TURm 0NvhDQHHDN7za0dV/jcgpj4Z/hXm5cck290VNHZj7rSEIssJR0Z18ir/kdYJwpQe4bSb /31qQwgs05o3pW0pZkyNBKTRqVdUwF4BG3VKK9ZDJ1tPUdXSR+8yq923a1OrnDzQhHzW IjpGYbjjZX0YEcpPFxTTGteho3WAVZQBefIzuw7csXS7RtOfJ4Z7oR3RuzRMREMBOkiO Wi5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=CmUG95UWXdUNrOew0avYpGMvpFqXZ3CvWAJoqBO7qKQ=; b=wfKZO+maTzJBrBBeZ8d0TPfAjvVnvL0OZ9LL5+AMvXeJBw2p89rWzsNrrAU5Klxvnt rTofAtQrX95dhPY1Gf6yK8BKl/+73S4Df2F56er3kVX2QUIy4qf8opVeSxQ88Rc8bpgV Y2pEGUNz1prDFlZyd9amqErjTe/HMudHktKJr4CZIGoG5Ya6q58NvqPRlhasjvEqyt09 GFY1KWEMB5MTh6l6zQdJZcf7MUsZG7f9fAHIIsaIlgEoO/NAfIIpHJbkd3dadWKSXdhR EZiilBx62mImVHUwGpu9JGFKz6DjkZO9H6RahihJQA+8r9mD1By0un0A457up6Vi5j7G 1tyg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o20-v6si4159077pgd.58.2018.09.13.07.00.01; Thu, 13 Sep 2018 07:00:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731415AbeIMTIP (ORCPT + 99 others); Thu, 13 Sep 2018 15:08:15 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:34626 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728566AbeIMTIO (ORCPT ); Thu, 13 Sep 2018 15:08:14 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id C62D6D0E; Thu, 13 Sep 2018 13:58:36 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, shaochun chen , Florian Westphal , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 4.18 075/197] netfilter: fix memory leaks on netlink_dump_start error Date: Thu, 13 Sep 2018 15:30:24 +0200 Message-Id: <20180913131844.524514107@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180913131841.568116777@linuxfoundation.org> References: <20180913131841.568116777@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Florian Westphal [ Upstream commit 3e673b23b541b8e7f773b2d378d6eb99831741cd ] Shaochun Chen points out we leak dumper filter state allocations stored in dump_control->data in case there is an error before netlink sets cb_running (after which ->done will be called at some point). In order to fix this, add .start functions and move allocations there. Same pattern as used in commit 90fd131afc565159c9e0ea742f082b337e10f8c6 ("netfilter: nf_tables: move dumper state allocation into ->start"). Reported-by: shaochun chen Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_conntrack_netlink.c | 26 +++++++++++++++++--------- net/netfilter/nfnetlink_acct.c | 29 +++++++++++++---------------- 2 files changed, 30 insertions(+), 25 deletions(-) --- a/net/netfilter/nf_conntrack_netlink.c +++ b/net/netfilter/nf_conntrack_netlink.c @@ -821,6 +821,21 @@ ctnetlink_alloc_filter(const struct nlat #endif } +static int ctnetlink_start(struct netlink_callback *cb) +{ + const struct nlattr * const *cda = cb->data; + struct ctnetlink_filter *filter = NULL; + + if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { + filter = ctnetlink_alloc_filter(cda); + if (IS_ERR(filter)) + return PTR_ERR(filter); + } + + cb->data = filter; + return 0; +} + static int ctnetlink_filter_match(struct nf_conn *ct, void *data) { struct ctnetlink_filter *filter = data; @@ -1240,19 +1255,12 @@ static int ctnetlink_get_conntrack(struc if (nlh->nlmsg_flags & NLM_F_DUMP) { struct netlink_dump_control c = { + .start = ctnetlink_start, .dump = ctnetlink_dump_table, .done = ctnetlink_done, + .data = (void *)cda, }; - if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { - struct ctnetlink_filter *filter; - - filter = ctnetlink_alloc_filter(cda); - if (IS_ERR(filter)) - return PTR_ERR(filter); - - c.data = filter; - } return netlink_dump_start(ctnl, skb, nlh, &c); } --- a/net/netfilter/nfnetlink_acct.c +++ b/net/netfilter/nfnetlink_acct.c @@ -238,29 +238,33 @@ static const struct nla_policy filter_po [NFACCT_FILTER_VALUE] = { .type = NLA_U32 }, }; -static struct nfacct_filter * -nfacct_filter_alloc(const struct nlattr * const attr) +static int nfnl_acct_start(struct netlink_callback *cb) { - struct nfacct_filter *filter; + const struct nlattr *const attr = cb->data; struct nlattr *tb[NFACCT_FILTER_MAX + 1]; + struct nfacct_filter *filter; int err; + if (!attr) + return 0; + err = nla_parse_nested(tb, NFACCT_FILTER_MAX, attr, filter_policy, NULL); if (err < 0) - return ERR_PTR(err); + return err; if (!tb[NFACCT_FILTER_MASK] || !tb[NFACCT_FILTER_VALUE]) - return ERR_PTR(-EINVAL); + return -EINVAL; filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL); if (!filter) - return ERR_PTR(-ENOMEM); + return -ENOMEM; filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK])); filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE])); + cb->data = filter; - return filter; + return 0; } static int nfnl_acct_get(struct net *net, struct sock *nfnl, @@ -275,18 +279,11 @@ static int nfnl_acct_get(struct net *net if (nlh->nlmsg_flags & NLM_F_DUMP) { struct netlink_dump_control c = { .dump = nfnl_acct_dump, + .start = nfnl_acct_start, .done = nfnl_acct_done, + .data = (void *)tb[NFACCT_FILTER], }; - if (tb[NFACCT_FILTER]) { - struct nfacct_filter *filter; - - filter = nfacct_filter_alloc(tb[NFACCT_FILTER]); - if (IS_ERR(filter)) - return PTR_ERR(filter); - - c.data = filter; - } return netlink_dump_start(nfnl, skb, nlh, &c); }