Re: [PATCH] netlink: fix memory leak of dump

From: shaochun chen
Date: Mon Jul 23 2018 - 06:34:41 EST


I have a question: we will try_module_get inÂ__netlink_dump_start(),
but why we need to callÂtry_module_get again in Ânft_netlink_dump_startÂ??

2018-07-23 17:52 GMT+08:00 shaochun chen <cscnull@xxxxxxxxx>:
allocate memory in ->start(), it's not convenient for users.
if call ->done() isn't ok for clean memory when netlink_dump_start() fail,
maybe we should have another function ->clean() to clean memory.

2018-07-23 17:28 GMT+08:00 Florian Westphal <fw@xxxxxxxxx>:
Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> 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 = "">
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 = "" *)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.