Messages in this thread Patch in this message | | | From | shaochun chen <> | Date | Mon, 23 Jul 2018 18:34:36 +0800 | Subject | Re: [PATCH] netlink: fix memory leak of dump |
| |
<div dir="ltr">I have a question: we will try_module_get in <span style="color:rgb(0,0,0);font-size:13.3333px">__netlink_dump_start(),</span><div><span style="color:rgb(0,0,0);font-size:13.3333px">but why we need to call <span style="color:rgb(34,34,34);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span>try_module_get again in
<span style="color:rgb(0,0,0);font-size:13.3333px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span> </span></span><span style="text-decoration-style:initial;text-decoration-color:initial;background-color:rgb(255,255,255);float:none;display:inline">nft_netlink_dump_start<span> </span></span>??</span></span></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-07-23 17:52 GMT+08:00 shaochun chen <span dir="ltr"><<a href="mailto:cscnull@gmail.com" target="_blank">cscnull@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="HOEnZb"><div class="h5"><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>Pablo Neira Ayuso <<a href="mailto:pablo@netfilter.org" target="_blank">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(struc<wbr>t 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><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><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><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> </div></div></blockquote></div><br></div>
| |