Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5593290imm; Mon, 23 Jul 2018 02:30:02 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfWv7xahSmsqGTV5S73YSRJ7wNN9Z93VKHb/PSx9IFJTkTAV/c+abgFK25GEcw2kpgyNdwC X-Received: by 2002:a62:a312:: with SMTP id s18-v6mr12279892pfe.13.1532338202531; Mon, 23 Jul 2018 02:30:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532338202; cv=none; d=google.com; s=arc-20160816; b=KKXvbUUXk03/GNI8DHnuApILz6zOS2c+I7xD3tj6p+BXwvYq0LiJcY1hMFV01XplT8 Q8M22SOWUqzPz6/hoi2ez9W0UMRKFraUtNedCLyWKCpOwKTNWMDYlFLNnN+hxQDEudOO yoz8UnPgD88uV+bcphQi/WXFa8RU0anewOibvfu0XAT4OpsGyC0ru1j2IpcsDD2CX/Qd +aTJfUFVSx57Qxi/PUPk29yX4Ve2AlQK0C3L1cpl9nj3KwbPWX3em2ZYt91Oi1X+9BEa tMjXP8REy2JkeeFl6hRPtj8l8DDk/ss2J4YUbYaBIMQj8oDDD4q2pWEx3b0YFa5xt5Lm tMoA== 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=VGlucEf07Mx3rOSd2iXCpU6DlmaP2luYGzOdER1jb2U=; b=fW7P/wieBZrnZJj+2d8k6Viu7Ho4lU5jm2sm7LSULZ03I9ex5jGHvdz06//4T0bOp2 DhiURk3bmj1N66lCXxlhG2Q/Gey8/K+XMqVnWRupMZeFz5FS2r/cizpy+Da7zDPVkgNh aoVbgFhVwMkWVbgywROS3WSNu+kxhPfllIIiBsFndxo6/7zXQcV8LPohSlmB1fDI3WPI nnTqReWiC+pNd2UfWIq8MqT2yWsySM4GzfWi2WQMn21jI7453JkvYjfoC/HuHUAaBvsd Ah6oC0Jj/HCCZlePrLLPRVgNncc87/vLL3Lh4D4aruHTVdy4iQpSAMiMzIk4P45hrrdL Uvvg== 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 ay1-v6si493673plb.266.2018.07.23.02.29.48; Mon, 23 Jul 2018 02:30:02 -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 S2388259AbeGWK2s (ORCPT + 99 others); Mon, 23 Jul 2018 06:28:48 -0400 Received: from Chamillionaire.breakpoint.cc ([146.0.238.67]:35068 "EHLO Chamillionaire.breakpoint.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388062AbeGWK2r (ORCPT ); Mon, 23 Jul 2018 06:28:47 -0400 Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.89) (envelope-from ) id 1fhX8c-0007mK-Cv; Mon, 23 Jul 2018 11:28:18 +0200 Date: Mon, 23 Jul 2018 11:28:18 +0200 From: Florian Westphal To: Pablo Neira Ayuso Cc: Florian Westphal , David Miller , cscnull@gmail.com, 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: <20180723092818.ztsfsnqzxgzrauim@breakpoint.cc> References: <20180722143354.23722-1-cscnull@gmail.com> <20180722163925.gdfkndldatsoae6x@breakpoint.cc> <20180722.100755.19840167505550163.davem@davemloft.net> <20180722180910.wcwhantwpm2nfxet@breakpoint.cc> <20180723091551.mwhltw4ujm4bylvj@salvia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180723091551.mwhltw4ujm4bylvj@salvia> 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 Pablo Neira Ayuso wrote: > > 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; On-Stack input. I can't see how its wrong, ->start() happens from same context as netlink_dump_start so its valid. > > + 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; And this replaced the on-stack input with dynamically allocated one, which will be free'd via ->done(). > > /* 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, > > You cannot do this. > > nla is allocated in this stack. Yes. > the nla will not be available in the second recv(), it won't be there. Its replaced in ->start(). As David pointed out, once ->start() returns 0 we set cb_running, i.e. only after successful ->start() netlink core will call ->dump() again. So I see no problem setting ->data to onstack cookie and then duplicating it to heap via kmemdup in ->start(). As far as I can see netlink core offers all functionality already, so we only need to switch netfilter to make use of it. If you disagree please let me know, otherwise I will cook up a patch along this pattern for net/netfilter/*. Thanks.