Messages in this thread Patch in this message | | | From | shaochun chen <> | Date | Mon, 23 Jul 2018 17:52:44 +0800 | Subject | Re: [PATCH] netlink: fix memory leak of dump |
| |
<div dir="ltr">allocate memory in ->start(), it's not convenient for users.<div>if call ->done() isn't ok for clean memory when netlink_dump_start() fail,</div><div>maybe we should have another function ->clean() to clean memory.</div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-07-23 17:28 GMT+08:00 Florian Westphal <span dir="ltr"><<a href="mailto:fw@strlen.de" target="_blank">fw@strlen.de</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Pablo Neira Ayuso <<a href="mailto:pablo@netfilter.org">pablo@netfilter.org</a>> wrote:<br> > > diff --git a/net/netfilter/nf_tables_api.<wbr>c b/net/netfilter/nf_tables_api.<wbr>c<br> > > --- a/net/netfilter/nf_tables_api.<wbr>c<br> > > +++ b/net/netfilter/nf_tables_api.<wbr>c<br> > > @@ -5010,6 +5013,22 @@ nft_obj_filter_alloc(const struct nlattr * const nla[])<br> > > return filter;<br> > > }<br> > > <br> > > +static int nf_tables_dump_obj_start(<wbr>struct netlink_callback *cb)<br> > > +{<br> > > + const struct nlattr * const *nla = cb->data;<br> <br> </span>On-Stack input.<br> I can't see how its wrong, ->start() happens from same context as<br> netlink_dump_start so its valid.<br> <span class=""><br> > > + struct nft_obj_filter *filter = NULL;<br> > > +<br> > > + if (nla[NFTA_OBJ_TABLE] ||<br> > > + nla[NFTA_OBJ_TYPE]) {<br> > > + filter = nft_obj_filter_alloc(nla);<br> > > + if (IS_ERR(filter))<br> > > + return -ENOMEM;<br> > > + }<br> > > +<br> > > + cb->data = filter;<br> <br> </span>And this replaced the on-stack input with dynamically<br> allocated one, which will be free'd via ->done().<br> <span class=""><br> > > /* called with rcu_read_lock held */<br> > > static int nf_tables_getobj(struct net *net, struct sock *nlsk,<br> > > struct sk_buff *skb, const struct nlmsghdr *nlh,<br> > > @@ -5028,21 +5047,13 @@ static int nf_tables_getobj(struct net *net, struct sock *nlsk,<br> > > <br> > > if (nlh->nlmsg_flags & NLM_F_DUMP) {<br> > > struct netlink_dump_control c = {<br> > > + .start = nf_tables_dump_obj_start,<br> > > .dump = nf_tables_dump_obj,<br> > > .done = nf_tables_dump_obj_done,<br> > > .module = THIS_MODULE,<br> > > + .data = (void *)nla,<br> > <br> > You cannot do this.<br> > <br> > nla is allocated in this stack.<br> <br> </span>Yes.<br> <span class=""><br> > the nla will not be available in the second recv(), it won't be there.<br> <br> </span>Its replaced in ->start().<br> <br> As David pointed out, once ->start() returns 0 we set cb_running, i.e.<br> only after successful ->start() netlink core will call ->dump() again.<br> <br> So I see no problem setting ->data to onstack cookie and then<br> duplicating it to heap via kmemdup in ->start().<br> <br> As far as I can see netlink core offers all functionality already,<br> so we only need to switch netfilter to make use of it.<br> <br> If you disagree please let me know, otherwise I will cook up<br> a patch along this pattern for net/netfilter/*.<br> <br> Thanks.<br> </blockquote></div><br></div>
| |