net: sched: crash on blocks with goto chain action
Subsystems (3)
PATCH 1/1 net: sched: crash on blocks with goto chain action
tcf_block_put_ext has assumed that all filters (and thus their goto actions) are destroyed in RCU callback and thus can not race with our list iteration. However, that is not true during netns cleanup (see tcf_exts_get_net comment).
Prevent the user after free by holding the current list element we are iterating over (foreach_safe is not enough).
To reproduce, run the following in a netns and then delete the ns:
ip link add dtest type dummy
tc qdisc add dev dtest ingress
tc filter add dev dtest chain 1 parent ffff: handle 1 prio 1 flower action goto chain 2
---
The mail was original rejected by vger, this is a re-send to netdev@vger only (with the same message ID). Sorry for any confusion.
--- net/sched/cls_api.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
net/sched/cls_api.c +14 -4
@@ -344,16 +344,26 @@ static void tcf_block_put_final(struct work_struct *work) }/* XXX: Standalone actions are not allowed to jump to any chain, and bound -* actions should be all removed after flushing. However, filters are now -* destroyed in tc filter workqueue with RTNL lock, they can not race here. +* actions should be all removed after flushing. */ void tcf_block_put_ext (struct tcf_block *block,struct Qdisc *q,struct tcf_block_ext_info *ei) { -struct tcf_chain *chain, *tmp; +struct tcf_chain *chain, *last =NULL ; -list_for_each_entry_safe (chain, tmp, &block->chain_list, list) +list_for_each_entry (chain, &block->chain_list, list) { +if (last) +tcf_chain_put (last); +/* Flushing a chain may release any other chain in this block, +* so we have to hold on to the chain across flush to known +* which one comes next. +*/ +tcf_chain_hold (chain);tcf_chain_flush (chain); + last = chain; + } +if (last) +tcf_chain_put (last);tcf_block_offload_unbind (block, q, ei);
Signature · 1 line
2.15.0
Quoted 7 lines · Roman Kapl
tcf_block_put_exthas assumed that all filters (and thus their goto actions) are destroyed in RCU callback and thus can not race with our list iteration. However, that is not true during netns cleanup (seetcf_exts_get_netcomment).Prevent the user after free by holding the current list element we are iterating over (
foreach_safeis not enough).
Hmm...
Looks like we need to restore the trick we used previously, that is holding refcnt for all list entries before this list iteration.
Quoted 6 lines · Cong Wang
Quoted 5 lines · Roman Kapl
tcf_block_put_exthas assumed that all filters (and thus their goto actions) are destroyed in RCU callback and thus can not race with our list iteration. However, that is not true during netns cleanup (seetcf_exts_get_netcomment).
Quoted 6 lines
Quoted 2 lines
Prevent the user after free by holding the current list element we are iterating over (
foreach_safeis not enough).Hmm...
Looks like we need to restore the trick we used previously, that is holding refcnt for all list entries before this list iteration.
Was there a reason to hold all list entries in that trick? I thought that holding just the current element will be enough, but maybe not.
Quoted 12 lines · Roman Kapl
Quoted 11 lines · Cong Wang
Quoted 8 lines · Roman Kapl
tcf_block_put_exthas assumed that all filters (and thus their goto actions) are destroyed in RCU callback and thus can not race with our list iteration. However, that is not true during netns cleanup (seetcf_exts_get_netcomment).Prevent the user after free by holding the current list element we are iterating over (
foreach_safeis not enough).
Quoted 9 lines
Quoted 4 lines
Hmm...
Looks like we need to restore the trick we used previously, that is holding refcnt for all list entries before this list iteration.
Was there a reason to hold all list entries in that trick? I thought that holding just the current element will be enough, but maybe not.
Yes, let me quote Jiri's explanation:
"
The reason for the hold above was to avoid use after free in this loop. Consider following example:
chain1
1 filter with action goto_chain 2
chain2
empty
Now in your list_for_each_entry_safe loop, chain1 is flushed, action is removed and chain is put:tcf_action_goto_chain_fini->tcf_chain_put(2)
Given the fact chain2 is empty, this put would lead to tcf_chain_destroy(2)
Then in another iteration of list_for_each_entry_safe you are using already freed chain.
"
Quoted 20 lines · Cong Wang
Quoted 9 lines · Roman Kapl
Quoted 8 lines · Cong Wang
Quoted 7 lines · Roman Kapl
tcf_block_put_exthas assumed that all filters (and thus their goto actions) are destroyed in RCU callback and thus can not race with our list iteration. However, that is not true during netns cleanup (seetcf_exts_get_netcomment).Prevent the user after free by holding the current list element we are iterating over (
foreach_safeis not enough).Quoted 8 lines
Quoted 4 lines
Hmm...
Looks like we need to restore the trick we used previously, that is holding refcnt for all list entries before this list iteration.
Was there a reason to hold all list entries in that trick? I thought that holding just the current element will be enough, but maybe not.
Yes, let me quote Jiri's explanation:
Quoted 8 lines
"
The reason for the hold above was to avoid use after free in this loop. Consider following example:chain1
1 filter with actiongoto_chain2
chain2
empty
I believe the exact same example is part of the 'how to reproduce' part of commit and the patch helped me get rid of that crash.
Quoted 2 lines
Now in your
list_for_each_entry_safeloop,
Note that list_for_each_entry_safe was replaced by pure list_for_each_entry in my proposed patch.
Quoted 9 lines
chain1 is flushed, action is
removed and chain is put:tcf_action_goto_chain_fini->tcf_chain_put(2)Given the fact chain2 is empty, this put would lead to
tcf_chain_destroy(2)Then in another iteration of
list_for_each_entry_safeyou are using already freed chain.
"
No, I believe that the last iteration would simply stop, because at the point you reach second iteration, chain->next == head.
But maybe the "hold all chains" approach from 822e86d997 (net_sched: remove tcf_block_put_deferred()) is simpler to understand?
Quoted 4 lines · Roman Kapl
But maybe the "hold all chains" approach from
822e86d997(net_sched: removetcf_block_put_deferred()) is simpler to understand?
Yes, it is much easier to understand for me, probably for others too.