[Patch net] net_sched: fix a memory leak of filter chain

Subsystems: networking [general], tc subsystem, the rest

STALE3290d

3 messages, 2 authors, 2017-09-06 · open the first message on its own page

[Patch net] net_sched: fix a memory leak of filter chain

From: Cong Wang <hidden>
Date: 2017-09-06 05:03:18

tcf_chain_destroy() is called by tcf_block_put() and tcf_chain_put().
tcf_chain_put() is refcn'ed and paired with tcf_chain_get(),
but tcf_block_put() is not, it should be paired with tcf_block_get()
and we still need to decrease the refcnt. However, tcf_block_put()
is special, it stores the chains too, we have to detach them if
it is not the last user.

What's more, index 0 is not special at all, it should be treated
like other chains. This also makes the code more readable.

Fixes: 744a4cf63e52 ("net: sched: fix use after free when tcf_chain_destroy is called multiple times")
Reported-by: Jakub Kicinski <redacted>
Cc: Jiri Pirko <redacted>
Signed-off-by: Cong Wang <redacted>
---
 net/sched/cls_api.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
index 6c5ea84d2682..c6d25b29bcd4 100644
--- a/net/sched/cls_api.c
+++ b/net/sched/cls_api.c
@@ -213,17 +213,17 @@ static void tcf_chain_flush(struct tcf_chain *chain)
 	}
 }
 
-static void tcf_chain_destroy(struct tcf_chain *chain)
+static void tcf_chain_detach(struct tcf_chain *chain)
 {
 	/* May be already removed from the list by the previous call. */
 	if (!list_empty(&chain->list))
 		list_del_init(&chain->list);
+}
 
-	/* There might still be a reference held when we got here from
-	 * tcf_block_put. Wait for the user to drop reference before free.
-	 */
-	if (!chain->refcnt)
-		kfree(chain);
+static void tcf_chain_destroy(struct tcf_chain *chain)
+{
+	tcf_chain_detach(chain);
+	kfree(chain);
 }
 
 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
@@ -246,10 +246,7 @@ EXPORT_SYMBOL(tcf_chain_get);
 
 void tcf_chain_put(struct tcf_chain *chain)
 {
-	/* Destroy unused chain, with exception of chain 0, which is the
-	 * default one and has to be always present.
-	 */
-	if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
+	if (--chain->refcnt == 0)
 		tcf_chain_destroy(chain);
 }
 EXPORT_SYMBOL(tcf_chain_put);
@@ -296,8 +293,11 @@ void tcf_block_put(struct tcf_block *block)
 
 	list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
 		tcf_chain_flush(chain);
-		tcf_chain_destroy(chain);
+		tcf_chain_put(chain);
 	}
+	/* If tc actions still hold the chain, just detach it. */
+	list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
+		tcf_chain_detach(chain);
 	kfree(block);
 }
 EXPORT_SYMBOL(tcf_block_put);
-- 
2.13.0

Re: [Patch net] net_sched: fix a memory leak of filter chain

From: Jiri Pirko <jiri@resnulli.us>
Date: 2017-09-06 07:38:42

Wed, Sep 06, 2017 at 07:03:10AM CEST, xiyou.wangcong@gmail.com wrote:
tcf_chain_destroy() is called by tcf_block_put() and tcf_chain_put().
tcf_chain_put() is refcn'ed and paired with tcf_chain_get(),
but tcf_block_put() is not, it should be paired with tcf_block_get()
and we still need to decrease the refcnt. However, tcf_block_put()
is special, it stores the chains too, we have to detach them if
it is not the last user.
You don't describe the original issue, or I am missing that from your
description.

What's more, index 0 is not special at all, it should be treated
like other chains. This also makes the code more readable.
[...]

quoted hunk
@@ -246,10 +246,7 @@ EXPORT_SYMBOL(tcf_chain_get);
void tcf_chain_put(struct tcf_chain *chain)
{
-	/* Destroy unused chain, with exception of chain 0, which is the
-	 * default one and has to be always present.
-	 */
-	if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
+	if (--chain->refcnt == 0)
The refcounting is only done for actions holding reference to the chain.
You still need to check is the filter chain is not empty.
See tc_ctl_tfilter.

Also, chain 0 is created by default on a block creation. It has to be
present always for a reason. Please see tcf_block_get. The pointer to
chain 0 is assigned to the qdisc filter list pointer.


quoted hunk
		tcf_chain_destroy(chain);
}
EXPORT_SYMBOL(tcf_chain_put);
@@ -296,8 +293,11 @@ void tcf_block_put(struct tcf_block *block)
	list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
		tcf_chain_flush(chain);
-		tcf_chain_destroy(chain);
+		tcf_chain_put(chain);
	}
+	/* If tc actions still hold the chain, just detach it. */
+	list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
+		tcf_chain_detach(chain);
	kfree(block);
}
EXPORT_SYMBOL(tcf_block_put);
-- 
2.13.0

Re: [Patch net] net_sched: fix a memory leak of filter chain

From: Cong Wang <hidden>
Date: 2017-09-06 17:26:12

On Wed, Sep 6, 2017 at 12:38 AM, Jiri Pirko [off-list ref] wrote:
Wed, Sep 06, 2017 at 07:03:10AM CEST, xiyou.wangcong@gmail.com wrote:
quoted
tcf_chain_destroy() is called by tcf_block_put() and tcf_chain_put().
tcf_chain_put() is refcn'ed and paired with tcf_chain_get(),
but tcf_block_put() is not, it should be paired with tcf_block_get()
and we still need to decrease the refcnt. However, tcf_block_put()
is special, it stores the chains too, we have to detach them if
it is not the last user.
You don't describe the original issue, or I am missing that from your
description.
The original issue is the mismatch of tcf_block_put() and tcf_block_get()
w.r.t. refcnt. Think it in this way: if you call tcf_bock_put() immediately
after tcf_block_get(), would you get effectively a nop?

quoted
What's more, index 0 is not special at all, it should be treated
like other chains. This also makes the code more readable.
[...]

quoted
@@ -246,10 +246,7 @@ EXPORT_SYMBOL(tcf_chain_get);
void tcf_chain_put(struct tcf_chain *chain)
{
-      /* Destroy unused chain, with exception of chain 0, which is the
-       * default one and has to be always present.
-       */
-      if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
+      if (--chain->refcnt == 0)
The refcounting is only done for actions holding reference to the chain.
You still need to check is the filter chain is not empty.
See tc_ctl_tfilter.
With my patch refcnt is done for block too, if you notice the
tcf_chain_put() in tcf_block_put().

Also, chain 0 is created by default on a block creation. It has to be
present always for a reason. Please see tcf_block_get. The pointer to
chain 0 is assigned to the qdisc filter list pointer.
Sure, this is why block holds a refcnt to chain (not just chain 0) with
my patch, aka why the initial refcnt is 1 rather than 0.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help