netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: shaochun chen <cscnull@gmail.com>
To: Florian Westphal <fw@strlen.de>
Cc: David Miller <davem@davemloft.net>, pablo <pablo@netfilter.org>,
	kadlec <kadlec@blackhole.kfki.hu>,
	"johannes.berg" <johannes.berg@intel.com>,
	jason <Jason@zx2c4.com>, ktkhai <ktkhai@virtuozzo.com>,
	"lucien.xin" <lucien.xin@gmail.com>,
	"xiyou.wangcong" <xiyou.wangcong@gmail.com>,
	dsahern <dsahern@gmail.com>,
	netfilter-devel <netfilter-devel@vger.kernel.org>,
	tom <tom@quantonium.net>, netdev <netdev@vger.kernel.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] netlink: fix memory leak of dump
Date: Mon, 23 Jul 2018 10:05:18 +0800	[thread overview]
Message-ID: <CAKDdixA8x=xKuoVCYE5SRL8P9i-uUmcYSsyQrcvM9rnaaubnuw@mail.gmail.com> (raw)
In-Reply-To: <20180722180910.wcwhantwpm2nfxet@breakpoint.cc>

[-- Attachment #1: Type: text/plain, Size: 3400 bytes --]

allocate memory in cb->start(), which means passing 'static' variable
through control->data,
then allocate memory in cb->start() according to cb->data (cb->data is
equal to control->data now),
and set the memory back to cb->data which will be used in cb->dump().
It's a bit complicated, please see nf_tables_getset.

2018-07-23 2:09 GMT+08:00 Florian Westphal <fw@strlen.de>:

> David Miller <davem@davemloft.net> wrote:
> > From: Florian Westphal <fw@strlen.de>
> > Date: Sun, 22 Jul 2018 18:39:25 +0200
> >
> > > 3. change meaning of ->done() so its always called once ->start()
> > >    was invoked (and returned 0), this requires audit of all
> > >    places that provide .done to make sure they won't trip.
> > >
> > > 3) seems to be what Tom intended when he added .start, so probably
> > > best to investigate that first.
> >
> > Hmmm...
> >
> > Any time ->start() succeeds, we set cb_running to true.
>
> Right.
>
> > From that point forward, ->done() will be called at some point at all
> > of the locations that check if cb_running is true and set it to false.
>
> Also right, thanks for pointing this out, I missed fact that netlink
> core restarts a dump after this.
>
> So 3) is already true which means we should try to see if we can move
> all dump-related extra magic into ->start().
>
> Shaochun, can you see if this is possible?
>
> Something along these lines (totally untested), which makes this
> a netfilter fix:
>
> 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;
> +       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;
> +       return 0;
> +}
> +
>  /* 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,
>                 };
>
> -               if (nla[NFTA_OBJ_TABLE] ||
> -                   nla[NFTA_OBJ_TYPE]) {
> -                       struct nft_obj_filter *filter;
> -
> -                       filter = nft_obj_filter_alloc(nla);
> -                       if (IS_ERR(filter))
> -                               return -ENOMEM;
> -
> -                       c.data = filter;
> -               }
>                 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
>         }
>
>

[-- Attachment #2: Type: text/html, Size: 4575 bytes --]

  reply	other threads:[~2018-07-23  2:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-22 14:33 [PATCH] netlink: fix memory leak of dump Shaochun Chen
2018-07-22 16:39 ` Florian Westphal
2018-07-22 17:07   ` David Miller
2018-07-22 18:09     ` Florian Westphal
2018-07-23  2:05       ` shaochun chen [this message]
2018-07-23  9:15       ` Pablo Neira Ayuso
2018-07-23  9:28         ` Florian Westphal
2018-07-23  9:38           ` Pablo Neira Ayuso
2018-07-23  9:42             ` Florian Westphal
2018-07-23  9:47               ` Pablo Neira Ayuso
2018-07-23 10:51                 ` Florian Westphal
2018-07-23  9:52           ` shaochun chen
2018-07-23 10:34             ` shaochun chen
2018-07-23 10:43               ` Pablo Neira Ayuso
2018-07-23 10:59               ` Florian Westphal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKDdixA8x=xKuoVCYE5SRL8P9i-uUmcYSsyQrcvM9rnaaubnuw@mail.gmail.com' \
    --to=cscnull@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=fw@strlen.de \
    --cc=johannes.berg@intel.com \
    --cc=kadlec@blackhole.kfki.hu \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=tom@quantonium.net \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).