tipc: switch to rhashtable iterator
Subsystems (3)
PATCH 1/1 tipc: switch to rhashtable iterator
syzbot reported a use-after-free in tipc_group_fill_sock_diag(), where tipc_group_fill_sock_diag() still reads tsk->group meanwhile tipc_group_delete() just deletes it in tipc_release().
tipc_nl_sk_walk() aims to lock this sock when walking each sock in the hash table to close race conditions with sock changes like this one, by acquiring tsk->sk.sk_lock.slock spinlock, unfortunately this doesn't work at all. All non-BH call path should take lock_sock() instead to make it work.
tipc_nl_sk_walk() brutally iterates with raw rht_for_each_entry_rcu() where RCU read lock is required, this is the reason why lock_sock() can't be taken on this path. This could be resolved by switching to rhashtable iterator API's, where taking a sleepable lock is possible. Also, the iterator API's are friendly for restartable calls like diag dump, the last position is remembered behind the scence, all we need to do here is saving the iterator into cb->args[].
I tested this with parallel tipc diag dump and thousands of tipc socket creation and release, no crash or memory leak.
--- net/tipc/diag.c | 2 ++ net/tipc/netlink.c | 2 ++ net/tipc/socket.c | 76 +++++++++++++++++++++++++++++++++++------------------- net/tipc/socket.h | 2 ++ 4 files changed, 56 insertions(+), 26 deletions(-)
net/tipc/diag.c +2 -0
@@ -84,7 +84,9 @@ static int tipc_sock_diag_handler_dump(struct sk_buff *skb,if (h->nlmsg_flags &NLM_F_DUMP ) {struct netlink_dump_control c = { + .start = tipc_dump_start, .dump = tipc_diag_dump, + .done = tipc_dump_done, };netlink_dump_start (net->diag_nlsk, skb, h, &c);return 0 ;
net/tipc/netlink.c +2 -0
@@ -167,7 +167,9 @@ static const struct genl_ops tipc_genl_v2_ops[] = { }, { .cmd = TIPC_NL_SOCK_GET, + .start = tipc_dump_start, .dumpit = tipc_nl_sk_dump, + .done = tipc_dump_done, .policy = tipc_nl_policy, }, {
net/tipc/socket.c +50 -26
95 lines not on this page · raw message
net/tipc/socket.h +2 -0
@@ -68,4 +68,6 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,int (*skb_handler)(struct sk_buff *skb,struct netlink_callback *cb, struct tipc_sock *tsk)); +int tipc_dump_start (struct netlink_callback *cb); +int tipc_dump_done (struct netlink_callback *cb);#endif
Signature · 1 line
2.14.4
David Miller
maintainerFrom: Cong Wang <[email protected]>
Date: Fri, 24 Aug 2018 12:28:06 -0700
Quoted 18 lines
syzbot reported a use-after-free in
tipc_group_fill_sock_diag(), wheretipc_group_fill_sock_diag()still readstsk->groupmeanwhiletipc_group_delete()just deletes it intipc_release().
tipc_nl_sk_walk()aims to lock this sock when walking each sock in the hash table to close race conditions with sock changes like this one, by acquiringtsk->sk.sk_lock.slockspinlock, unfortunately this doesn't work at all. All non-BH call path should takelock_sock()instead to make it work.
tipc_nl_sk_walk()brutally iterates with rawrht_for_each_entry_rcu()where RCU read lock is required, this is the reason whylock_sock()can't be taken on this path. This could be resolved by switching to rhashtable iterator API's, where taking a sleepable lock is possible. Also, the iterator API's are friendly for restartable calls like diag dump, the last position is remembered behind the scence, all we need to do here is saving the iterator into cb->args[].
Quoted 7 lines
I tested this with parallel tipc diag dump and thousands of tipc socket creation and release, no crash or memory leak.
Reported-by [email protected]Cc Jon Maloy <[email protected]>Cc Ying Xue <[email protected]>Signed-off-by Cong Wang <[email protected]>
Applied and queued up for -stable, thanks Cong.