Skip to content

Commit 6862fa9

Browse files
Jon Paul Maloygregkh
authored andcommitted
tipc: fix crash during node removal
commit d25a01257e422a4bdeb426f69529d57c73b235fe upstream. When the TIPC module is unloaded, we have identified a race condition that allows a node reference counter to go to zero and the node instance being freed before the node timer is finished with accessing it. This leads to occasional crashes, especially in multi-namespace environments. The scenario goes as follows: CPU0:(node_stop) CPU1:(node_timeout) // ref == 2 1: if(!mod_timer()) 2: if (del_timer()) 3: tipc_node_put() // ref -> 1 4: tipc_node_put() // ref -> 0 5: kfree_rcu(node); 6: tipc_node_get(node) 7: // BOOM! We now clean up this functionality as follows: 1) We remove the node pointer from the node lookup table before we attempt deactivating the timer. This way, we reduce the risk that tipc_node_find() may obtain a valid pointer to an instance marked for deletion; a harmless but undesirable situation. 2) We use del_timer_sync() instead of del_timer() to safely deactivate the node timer without any risk that it might be reactivated by the timeout handler. There is no risk of deadlock here, since the two functions never touch the same spinlocks. 3: We remove a pointless tipc_node_get() + tipc_node_put() from the timeout handler. Reported-by: Zhijiang Hu <huzhijiang@gmail.com> Acked-by: Ying Xue <ying.xue@windriver.com> Signed-off-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6ddbac9 commit 6862fa9

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

net/tipc/node.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ static unsigned int tipc_hashfn(u32 addr)
102102

103103
static void tipc_node_kref_release(struct kref *kref)
104104
{
105-
struct tipc_node *node = container_of(kref, struct tipc_node, kref);
105+
struct tipc_node *n = container_of(kref, struct tipc_node, kref);
106106

107-
tipc_node_delete(node);
107+
kfree(n->bc_entry.link);
108+
kfree_rcu(n, rcu);
108109
}
109110

110111
void tipc_node_put(struct tipc_node *node)
@@ -216,21 +217,20 @@ static void tipc_node_delete(struct tipc_node *node)
216217
{
217218
list_del_rcu(&node->list);
218219
hlist_del_rcu(&node->hash);
219-
kfree(node->bc_entry.link);
220-
kfree_rcu(node, rcu);
220+
tipc_node_put(node);
221+
222+
del_timer_sync(&node->timer);
223+
tipc_node_put(node);
221224
}
222225

223226
void tipc_node_stop(struct net *net)
224227
{
225-
struct tipc_net *tn = net_generic(net, tipc_net_id);
228+
struct tipc_net *tn = tipc_net(net);
226229
struct tipc_node *node, *t_node;
227230

228231
spin_lock_bh(&tn->node_list_lock);
229-
list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
230-
if (del_timer(&node->timer))
231-
tipc_node_put(node);
232-
tipc_node_put(node);
233-
}
232+
list_for_each_entry_safe(node, t_node, &tn->node_list, list)
233+
tipc_node_delete(node);
234234
spin_unlock_bh(&tn->node_list_lock);
235235
}
236236

@@ -313,9 +313,7 @@ static void tipc_node_timeout(unsigned long data)
313313
if (rc & TIPC_LINK_DOWN_EVT)
314314
tipc_node_link_down(n, bearer_id, false);
315315
}
316-
if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
317-
tipc_node_get(n);
318-
tipc_node_put(n);
316+
mod_timer(&n->timer, jiffies + n->keepalive_intv);
319317
}
320318

321319
/**

0 commit comments

Comments
 (0)