Skip to content

Commit 40f9f43

Browse files
herbertxdavem330
authored andcommitted
tipc: Fix tipc_sk_reinit race conditions
There are two problems with the function tipc_sk_reinit. Firstly it's doing a manual walk over an rhashtable. This is broken as an rhashtable can be resized and if you manually walk over it during a resize then you may miss entries. Secondly it's missing memory barriers as previously the code used spinlocks which provide the barriers implicitly. This patch fixes both problems. Fixes: 07f6c4b ("tipc: convert tipc reference table to...") Signed-off-by: Herbert Xu <[email protected]> Acked-by: Ying Xue <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 98687f4 commit 40f9f43

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

net/tipc/net.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ int tipc_net_start(struct net *net, u32 addr)
110110
char addr_string[16];
111111

112112
tn->own_addr = addr;
113+
114+
/* Ensure that the new address is visible before we reinit. */
115+
smp_mb();
116+
113117
tipc_named_reinit(net);
114118
tipc_sk_reinit(net);
115119

net/tipc/socket.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,6 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
430430
INIT_LIST_HEAD(&tsk->cong_links);
431431
msg = &tsk->phdr;
432432
tn = net_generic(sock_net(sk), tipc_net_id);
433-
tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
434-
NAMED_H_SIZE, 0);
435433

436434
/* Finish initializing socket data structures */
437435
sock->ops = ops;
@@ -441,6 +439,13 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
441439
pr_warn("Socket create failed; port number exhausted\n");
442440
return -EINVAL;
443441
}
442+
443+
/* Ensure tsk is visible before we read own_addr. */
444+
smp_mb();
445+
446+
tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
447+
NAMED_H_SIZE, 0);
448+
444449
msg_set_origport(msg, tsk->portid);
445450
setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
446451
sk->sk_shutdown = 0;
@@ -2234,24 +2239,27 @@ static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
22342239
void tipc_sk_reinit(struct net *net)
22352240
{
22362241
struct tipc_net *tn = net_generic(net, tipc_net_id);
2237-
const struct bucket_table *tbl;
2238-
struct rhash_head *pos;
2242+
struct rhashtable_iter iter;
22392243
struct tipc_sock *tsk;
22402244
struct tipc_msg *msg;
2241-
int i;
22422245

2243-
rcu_read_lock();
2244-
tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2245-
for (i = 0; i < tbl->size; i++) {
2246-
rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2246+
rhashtable_walk_enter(&tn->sk_rht, &iter);
2247+
2248+
do {
2249+
tsk = ERR_PTR(rhashtable_walk_start(&iter));
2250+
if (tsk)
2251+
continue;
2252+
2253+
while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
22472254
spin_lock_bh(&tsk->sk.sk_lock.slock);
22482255
msg = &tsk->phdr;
22492256
msg_set_prevnode(msg, tn->own_addr);
22502257
msg_set_orignode(msg, tn->own_addr);
22512258
spin_unlock_bh(&tsk->sk.sk_lock.slock);
22522259
}
2253-
}
2254-
rcu_read_unlock();
2260+
2261+
rhashtable_walk_stop(&iter);
2262+
} while (tsk == ERR_PTR(-EAGAIN));
22552263
}
22562264

22572265
static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)

0 commit comments

Comments
 (0)