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.

Reported-by: syzbot+b9c8f3ab2994b7cd1625@syzkaller.appspotmail.com
Cc: Jon Maloy <jon.maloy@ericsson.com>
Cc: Ying Xue <ying.xue@windriver.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Cong Wang 2018-08-24 12:28:06 -07:00 committed by David S. Miller
parent e5133f2f12
commit 9a07efa9ae
4 changed files with 58 additions and 28 deletions

View File

@ -84,7 +84,9 @@ static int tipc_sock_diag_handler_dump(struct sk_buff *skb,
if (h->nlmsg_flags & NLM_F_DUMP) { if (h->nlmsg_flags & NLM_F_DUMP) {
struct netlink_dump_control c = { struct netlink_dump_control c = {
.start = tipc_dump_start,
.dump = tipc_diag_dump, .dump = tipc_diag_dump,
.done = tipc_dump_done,
}; };
netlink_dump_start(net->diag_nlsk, skb, h, &c); netlink_dump_start(net->diag_nlsk, skb, h, &c);
return 0; return 0;

View File

@ -167,7 +167,9 @@ static const struct genl_ops tipc_genl_v2_ops[] = {
}, },
{ {
.cmd = TIPC_NL_SOCK_GET, .cmd = TIPC_NL_SOCK_GET,
.start = tipc_dump_start,
.dumpit = tipc_nl_sk_dump, .dumpit = tipc_nl_sk_dump,
.done = tipc_dump_done,
.policy = tipc_nl_policy, .policy = tipc_nl_policy,
}, },
{ {

View File

@ -3229,45 +3229,69 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
struct netlink_callback *cb, struct netlink_callback *cb,
struct tipc_sock *tsk)) struct tipc_sock *tsk))
{ {
struct net *net = sock_net(skb->sk); struct rhashtable_iter *iter = (void *)cb->args[0];
struct tipc_net *tn = tipc_net(net);
const struct bucket_table *tbl;
u32 prev_portid = cb->args[1];
u32 tbl_id = cb->args[0];
struct rhash_head *pos;
struct tipc_sock *tsk; struct tipc_sock *tsk;
int err; int err;
rcu_read_lock(); rhashtable_walk_start(iter);
tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht); while ((tsk = rhashtable_walk_next(iter)) != NULL) {
for (; tbl_id < tbl->size; tbl_id++) { if (IS_ERR(tsk)) {
rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) { err = PTR_ERR(tsk);
spin_lock_bh(&tsk->sk.sk_lock.slock); if (err == -EAGAIN) {
if (prev_portid && prev_portid != tsk->portid) { err = 0;
spin_unlock_bh(&tsk->sk.sk_lock.slock);
continue; continue;
} }
break;
err = skb_handler(skb, cb, tsk);
if (err) {
prev_portid = tsk->portid;
spin_unlock_bh(&tsk->sk.sk_lock.slock);
goto out;
}
prev_portid = 0;
spin_unlock_bh(&tsk->sk.sk_lock.slock);
} }
}
out:
rcu_read_unlock();
cb->args[0] = tbl_id;
cb->args[1] = prev_portid;
sock_hold(&tsk->sk);
rhashtable_walk_stop(iter);
lock_sock(&tsk->sk);
err = skb_handler(skb, cb, tsk);
if (err) {
release_sock(&tsk->sk);
sock_put(&tsk->sk);
goto out;
}
release_sock(&tsk->sk);
rhashtable_walk_start(iter);
sock_put(&tsk->sk);
}
rhashtable_walk_stop(iter);
out:
return skb->len; return skb->len;
} }
EXPORT_SYMBOL(tipc_nl_sk_walk); EXPORT_SYMBOL(tipc_nl_sk_walk);
int tipc_dump_start(struct netlink_callback *cb)
{
struct rhashtable_iter *iter = (void *)cb->args[0];
struct net *net = sock_net(cb->skb->sk);
struct tipc_net *tn = tipc_net(net);
if (!iter) {
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
if (!iter)
return -ENOMEM;
cb->args[0] = (long)iter;
}
rhashtable_walk_enter(&tn->sk_rht, iter);
return 0;
}
EXPORT_SYMBOL(tipc_dump_start);
int tipc_dump_done(struct netlink_callback *cb)
{
struct rhashtable_iter *hti = (void *)cb->args[0];
rhashtable_walk_exit(hti);
kfree(hti);
return 0;
}
EXPORT_SYMBOL(tipc_dump_done);
int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb, int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
struct tipc_sock *tsk, u32 sk_filter_state, struct tipc_sock *tsk, u32 sk_filter_state,
u64 (*tipc_diag_gen_cookie)(struct sock *sk)) u64 (*tipc_diag_gen_cookie)(struct sock *sk))

View File

@ -68,4 +68,6 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
int (*skb_handler)(struct sk_buff *skb, int (*skb_handler)(struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
struct tipc_sock *tsk)); struct tipc_sock *tsk));
int tipc_dump_start(struct netlink_callback *cb);
int tipc_dump_done(struct netlink_callback *cb);
#endif #endif