Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5022569imm; Sun, 22 Jul 2018 11:10:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpclzANx+mb5C5ekpkyq26cB4PEYT3vUex21mQgphBM/AQ5jOfRmRnyRDV87OrocU3ICeE3G X-Received: by 2002:a17:902:bb8d:: with SMTP id m13-v6mr9774310pls.46.1532283040303; Sun, 22 Jul 2018 11:10:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532283040; cv=none; d=google.com; s=arc-20160816; b=VuSrme2MHadjnmK74HK9Vsp3drX6ASXodtXfHVBLnJjMPloJ1dRUKtJ9rn6PeTuSi3 n2XwnMnf9coHN81tKh1Ht8U7+8CAnlaHXkRsl7dU17tuUwlX9kmuSBt7K08gV0mbOpFW 5YCUf7JGoWT33bKWxp+bMuhVDpxheNrMSMgi2fqoqFmfcMn/GdymyWkh40nUHBWbe64E rOisjble/q/CG6w43J+p4k2tP5oWOGt8z8OreIAGNcoRm1iP4EkvOXLfpZIpc2bcoNy7 Sm7xY+iclkHB03Jn5mY3DOcTrnru/DbM5Qt89/S+qsoe0BKTBTV5MKDZuSO2lz36NXCA 212g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=EmDpUbHvKddeK2bydiwMs/+9ovhk/kwRDabgoD4WGcM=; b=si680XcF5pZY8waUigwgR1XsdLhqBUxDxyOTVhhXjxxJn7bJ7Y4Gh25IzNBmDLYRAE 9eHw2EW8s/EjTqjR1REIXjbEz9/u2F1+2/VuYGBRvPycXLidAW6bPNhrduJVHM1rAbXF fJuMTUgEB3MmzR8bSQUCYTAvmTc0OQP3kd08mlKKVVgfJ+0Ke5igwcKzmdkADxY15dzu sQ6BXfGLPMILIXIeJkCdyydcv6MPshGd3EGK7K+WBbKUdQFq+Ubj+h/QcLCdRPY37GaY V1J+0E41hfJlhxJoShU0xktRVdjYTzdwAs+SBcETlbJHEX358xLHRPD6h5aFG/EAOFd1 GWSQ== 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 d13-v6si6318969plr.196.2018.07.22.11.10.25; Sun, 22 Jul 2018 11:10:40 -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 S1730576AbeGVTG7 (ORCPT + 99 others); Sun, 22 Jul 2018 15:06:59 -0400 Received: from Chamillionaire.breakpoint.cc ([146.0.238.67]:33656 "EHLO Chamillionaire.breakpoint.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729826AbeGVTG7 (ORCPT ); Sun, 22 Jul 2018 15:06:59 -0400 Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.89) (envelope-from ) id 1fhIn8-0004EF-9V; Sun, 22 Jul 2018 20:09:10 +0200 Date: Sun, 22 Jul 2018 20:09:10 +0200 From: Florian Westphal To: David Miller Cc: fw@strlen.de, cscnull@gmail.com, pablo@netfilter.org, kadlec@blackhole.kfki.hu, johannes.berg@intel.com, Jason@zx2c4.com, ktkhai@virtuozzo.com, lucien.xin@gmail.com, xiyou.wangcong@gmail.com, dsahern@gmail.com, netfilter-devel@vger.kernel.org, tom@quantonium.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] netlink: fix memory leak of dump Message-ID: <20180722180910.wcwhantwpm2nfxet@breakpoint.cc> References: <20180722143354.23722-1-cscnull@gmail.com> <20180722163925.gdfkndldatsoae6x@breakpoint.cc> <20180722.100755.19840167505550163.davem@davemloft.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180722.100755.19840167505550163.davem@davemloft.net> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org David Miller wrote: > From: Florian Westphal > Date: Sun, 22 Jul 2018 18:39:25 +0200 > > > 3. change meaning of ->done() so its always called once ->start() > > was invoked (and returned 0), this requires audit of all > > places that provide .done to make sure they won't trip. > > > > 3) seems to be what Tom intended when he added .start, so probably > > best to investigate that first. > > Hmmm... > > Any time ->start() succeeds, we set cb_running to true. Right. > From that point forward, ->done() will be called at some point at all > of the locations that check if cb_running is true and set it to false. Also right, thanks for pointing this out, I missed fact that netlink core restarts a dump after this. So 3) is already true which means we should try to see if we can move all dump-related extra magic into ->start(). Shaochun, can you see if this is possible? Something along these lines (totally untested), which makes this a netfilter fix: diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -5010,6 +5013,22 @@ nft_obj_filter_alloc(const struct nlattr * const nla[]) return filter; } +static int nf_tables_dump_obj_start(struct netlink_callback *cb) +{ + const struct nlattr * const *nla = cb->data; + struct nft_obj_filter *filter = NULL; + + if (nla[NFTA_OBJ_TABLE] || + nla[NFTA_OBJ_TYPE]) { + filter = nft_obj_filter_alloc(nla); + if (IS_ERR(filter)) + return -ENOMEM; + } + + cb->data = filter; + return 0; +} + /* called with rcu_read_lock held */ static int nf_tables_getobj(struct net *net, struct sock *nlsk, struct sk_buff *skb, const struct nlmsghdr *nlh, @@ -5028,21 +5047,13 @@ static int nf_tables_getobj(struct net *net, struct sock *nlsk, if (nlh->nlmsg_flags & NLM_F_DUMP) { struct netlink_dump_control c = { + .start = nf_tables_dump_obj_start, .dump = nf_tables_dump_obj, .done = nf_tables_dump_obj_done, .module = THIS_MODULE, + .data = (void *)nla, }; - if (nla[NFTA_OBJ_TABLE] || - nla[NFTA_OBJ_TYPE]) { - struct nft_obj_filter *filter; - - filter = nft_obj_filter_alloc(nla); - if (IS_ERR(filter)) - return -ENOMEM; - - c.data = filter; - } return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); }