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

From: shaochun chen
Date: Sun Jul 22 2018 - 22:05:22 EST


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@xxxxxxxxx>:
David Miller <davem@xxxxxxxxxxxxx> wrote:
> From: Florian Westphal <fw@xxxxxxxxx>
> 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 = ""> +Â Â Â Â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 = "" *)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 = "">
-Â Â Â Â Â Â Â Â}
        return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
    }