Messages in this thread Patch in this message | | | From | shaochun chen <> | Date | Mon, 23 Jul 2018 10:05:18 +0800 | Subject | Re: [PATCH] netlink: fix memory leak of dump |
| |
<div dir="ltr">allocate memory in cb->start(), which means passing 'static' variable through control->data, <div>then allocate memory in cb->start() according to cb->data (cb->data is equal to control->data now),</div><div>and set the memory back to cb->data which will be used in cb->dump(). </div><div>It's a bit complicated, please see nf_tables_getset.</div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-07-23 2:09 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="">David Miller <<a href="mailto:davem@davemloft.net">davem@davemloft.net</a>> wrote:<br> > From: Florian Westphal <<a href="mailto:fw@strlen.de">fw@strlen.de</a>><br> > Date: Sun, 22 Jul 2018 18:39:25 +0200<br> > <br> > > 3. change meaning of ->done() so its always called once ->start()<br> > > was invoked (and returned 0), this requires audit of all<br> > > places that provide .done to make sure they won't trip.<br> > > <br> > > 3) seems to be what Tom intended when he added .start, so probably<br> > > best to investigate that first.<br> > <br> > Hmmm...<br> > <br> > Any time ->start() succeeds, we set cb_running to true.<br> <br> </span>Right.<br> <span class=""><br> > From that point forward, ->done() will be called at some point at all<br> > of the locations that check if cb_running is true and set it to false.<br> <br> </span>Also right, thanks for pointing this out, I missed fact that netlink<br> core restarts a dump after this.<br> <br> So 3) is already true which means we should try to see if we can move<br> all dump-related extra magic into ->start().<br> <br> Shaochun, can you see if this is possible?<br> <br> Something along these lines (totally untested), which makes this<br> a netfilter fix:<br> <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> <span class="">+ struct nft_obj_filter *filter = NULL;<br> </span>+<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> + return 0;<br> +}<br> +<br> /* called with rcu_read_lock held */<br> <span class=""> static int nf_tables_getobj(struct net *net, struct sock *nlsk,<br> </span> 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> <span class=""> .done = nf_tables_dump_obj_done,<br> .module = THIS_MODULE,<br> </span>+ .data = (void *)nla,<br> };<br> <br> - if (nla[NFTA_OBJ_TABLE] ||<br> - nla[NFTA_OBJ_TYPE]) {<br> <span class="">- struct nft_obj_filter *filter;<br> -<br> </span>- filter = nft_obj_filter_alloc(nla);<br> - if (IS_ERR(filter))<br> - return -ENOMEM;<br> -<br> - c.data = filter;<br> <div class="HOEnZb"><div class="h5">- }<br> return nft_netlink_dump_start_rcu(<wbr>nlsk, skb, nlh, &c);<br> }<br> <br> </div></div></blockquote></div><br></div>
| |