net_sched: convert idrinfo->lock from spinlock to a mutex
Subsystems (3)
PATCH 1/1 net_sched: convert idrinfo->lock from spinlock to a mutex
In commit ec3ed293e766 ("net_sched: change tcf_del_walker() to take idrinfo->lock") we move fl_hw_destroy_tmplt() to a workqueue to avoid blocking
with the spinlock held. Unfortunately, this causes a lot of
troubles here:
1. tcf_chain_destroy() could be called right after we queue the work but before the work runs. This is a use-after-free.
2. The chain refcnt is already 0, we can't even just hold it again. We can check refcnt==1 but it is ugly.
3. The chain with refcnt 0 is still visible in its block, which means it could be still found and used!
4. The block has a refcnt too, we can't hold it without introducing a proper API either.
We can make it working but the end result is ugly. Instead of wasting time on reviewing it, let's just convert the troubling spinlock to a mutex, which allows us to use non-atomic allocations too.
--- include/net/act_api.h | 4 ++-- net/sched/act_api.c | 44 ++++++++++++++++++++++---------------------- net/sched/cls_flower.c | 13 ++----------- 3 files changed, 26 insertions(+), 35 deletions(-)
include/net/act_api.h +2 -2
@@ -13,7 +13,7 @@#include <net/netns/generic.h> struct tcf_idrinfo { -spinlock_t lock; +struct mutex lock;struct idr action_idr; };
@@ -117,7 +117,7 @@ int tc_action_net_init(struct tc_action_net *tn,if (!tn->idrinfo)return -ENOMEM ; tn->ops = ops; -spin_lock_init (&tn->idrinfo->lock); +mutex_init (&tn->idrinfo->lock);idr_init (&tn->idrinfo->action_idr);return err; }
net/sched/act_api.c +22 -22
155 lines not on this page · raw message
net/sched/cls_flower.c +2 -11
@@ -79,7 +79,6 @@ struct fl_flow_tmplt {struct fl_flow_key mask;struct flow_dissector dissector;struct tcf_chain *chain; -struct rcu_work rwork; };struct cls_fl_head {
@@ -1438,20 +1437,12 @@ static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,return ERR_PTR (err); } -static void fl_tmplt_destroy_work (struct work_struct *work) -{ -struct fl_flow_tmplt *tmplt =container_of (to_rcu_work (work), - struct fl_flow_tmplt, rwork); - -fl_hw_destroy_tmplt (tmplt->chain, tmplt); -kfree (tmplt); -} -static void fl_tmplt_destroy (void *tmplt_priv) {struct fl_flow_tmplt *tmplt = tmplt_priv; -tcf_queue_work (&tmplt->rwork, fl_tmplt_destroy_work); +fl_hw_destroy_tmplt (tmplt->chain, tmplt); +kfree (tmplt); } static int fl_dump_key_val(struct sk_buff *skb,
Signature · 1 line
2.14.4
Quoted 21 lines · Cong Wang
In commit
ec3ed293e766("net_sched: changetcf_del_walker()to takeidrinfo->lock") we movefl_hw_destroy_tmplt()to a workqueue to avoid blocking
with the spinlock held. Unfortunately, this causes a lot of
troubles here:1.
tcf_chain_destroy()could be called right after we queue the work but before the work runs. This is a use-after-free.2. The chain refcnt is already 0, we can't even just hold it again. We can check refcnt==1 but it is ugly.
3. The chain with refcnt 0 is still visible in its block, which means it could be still found and used!
4. The block has a refcnt too, we can't hold it without introducing a proper API either.
We can make it working but the end result is ugly. Instead of wasting time on reviewing it, let's just convert the troubling spinlock to a mutex, which allows us to use non-atomic allocations too.
Quoted 6 lines
Fixes ec3ed293e766 ("net_sched: change tcf_del_walker() to take idrinfo->lock")Reported-by Ido Schimmel <[email protected]>Cc Jamal Hadi Salim <[email protected]>Cc Vlad Buslov <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>
Thanks a lot!
From: Cong Wang <[email protected]>
Date: Tue, 2 Oct 2018 12:50:19 -0700
Quoted 21 lines
In commit
ec3ed293e766("net_sched: changetcf_del_walker()to takeidrinfo->lock") we movefl_hw_destroy_tmplt()to a workqueue to avoid blocking
with the spinlock held. Unfortunately, this causes a lot of
troubles here:1.
tcf_chain_destroy()could be called right after we queue the work but before the work runs. This is a use-after-free.2. The chain refcnt is already 0, we can't even just hold it again. We can check refcnt==1 but it is ugly.
3. The chain with refcnt 0 is still visible in its block, which means it could be still found and used!
4. The block has a refcnt too, we can't hold it without introducing a proper API either.
We can make it working but the end result is ugly. Instead of wasting time on reviewing it, let's just convert the troubling spinlock to a mutex, which allows us to use non-atomic allocations too.
Quoted 6 lines
Fixes ec3ed293e766 ("net_sched: change tcf_del_walker() to take idrinfo->lock")Reported-by Ido Schimmel <[email protected]>Cc Jamal Hadi Salim <[email protected]>Cc Vlad Buslov <[email protected]>Cc Jiri Pirko <[email protected]>Signed-off-by Cong Wang <[email protected]>
Applied.