Skip to content

Commit 44524fc

Browse files
author
Marek Lindner
committed
batman-adv: Correct rcu refcounting for neigh_node
It might be possible that 2 threads access the same data in the same rcu grace period. The first thread calls call_rcu() to decrement the refcount and free the data while the second thread increases the refcount to use the data. To avoid this race condition all refcount operations have to be atomic. Reported-by: Sven Eckelmann <sven@narfation.org> Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent a4c135c commit 44524fc

7 files changed

Lines changed: 313 additions & 174 deletions

File tree

‎net/batman-adv/icmp_socket.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
156156
struct sk_buff *skb;
157157
struct icmp_packet_rr *icmp_packet;
158158

159-
struct orig_node *orig_node;
159+
struct orig_node *orig_node = NULL;
160+
struct neigh_node *neigh_node = NULL;
160161
struct batman_if *batman_if;
161162
size_t packet_len = sizeof(struct icmp_packet);
162163
uint8_t dstaddr[ETH_ALEN];
@@ -224,17 +225,25 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
224225
orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
225226
compare_orig, choose_orig,
226227
icmp_packet->dst));
227-
rcu_read_unlock();
228228

229229
if (!orig_node)
230230
goto unlock;
231231

232-
if (!orig_node->router)
232+
kref_get(&orig_node->refcount);
233+
neigh_node = orig_node->router;
234+
235+
if (!neigh_node)
236+
goto unlock;
237+
238+
if (!atomic_inc_not_zero(&neigh_node->refcount)) {
239+
neigh_node = NULL;
233240
goto unlock;
241+
}
242+
243+
rcu_read_unlock();
234244

235245
batman_if = orig_node->router->if_incoming;
236246
memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
237-
238247
spin_unlock_bh(&bat_priv->orig_hash_lock);
239248

240249
if (!batman_if)
@@ -247,21 +256,25 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
247256
bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
248257

249258
if (packet_len == sizeof(struct icmp_packet_rr))
250-
memcpy(icmp_packet->rr, batman_if->net_dev->dev_addr, ETH_ALEN);
251-
259+
memcpy(icmp_packet->rr,
260+
batman_if->net_dev->dev_addr, ETH_ALEN);
252261

253262
send_skb_packet(skb, batman_if, dstaddr);
254-
255263
goto out;
256264

257265
unlock:
266+
rcu_read_unlock();
258267
spin_unlock_bh(&bat_priv->orig_hash_lock);
259268
dst_unreach:
260269
icmp_packet->msg_type = DESTINATION_UNREACHABLE;
261270
bat_socket_add_packet(socket_client, icmp_packet, packet_len);
262271
free_skb:
263272
kfree_skb(skb);
264273
out:
274+
if (neigh_node)
275+
neigh_node_free_ref(neigh_node);
276+
if (orig_node)
277+
kref_put(&orig_node->refcount, orig_node_free_ref);
265278
return len;
266279
}
267280

‎net/batman-adv/originator.c‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,18 @@ int originator_init(struct bat_priv *bat_priv)
5959
return 0;
6060
}
6161

62-
void neigh_node_free_ref(struct kref *refcount)
63-
{
64-
struct neigh_node *neigh_node;
65-
66-
neigh_node = container_of(refcount, struct neigh_node, refcount);
67-
kfree(neigh_node);
68-
}
69-
7062
static void neigh_node_free_rcu(struct rcu_head *rcu)
7163
{
7264
struct neigh_node *neigh_node;
7365

7466
neigh_node = container_of(rcu, struct neigh_node, rcu);
75-
kref_put(&neigh_node->refcount, neigh_node_free_ref);
67+
kfree(neigh_node);
7668
}
7769

78-
void neigh_node_free_rcu_bond(struct rcu_head *rcu)
70+
void neigh_node_free_ref(struct neigh_node *neigh_node)
7971
{
80-
struct neigh_node *neigh_node;
81-
82-
neigh_node = container_of(rcu, struct neigh_node, rcu_bond);
83-
kref_put(&neigh_node->refcount, neigh_node_free_ref);
72+
if (atomic_dec_and_test(&neigh_node->refcount))
73+
call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
8474
}
8575

8676
struct neigh_node *create_neighbor(struct orig_node *orig_node,
@@ -104,7 +94,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
10494
memcpy(neigh_node->addr, neigh, ETH_ALEN);
10595
neigh_node->orig_node = orig_neigh_node;
10696
neigh_node->if_incoming = if_incoming;
107-
kref_init(&neigh_node->refcount);
97+
atomic_set(&neigh_node->refcount, 1);
10898

10999
spin_lock_bh(&orig_node->neigh_list_lock);
110100
hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
@@ -126,14 +116,14 @@ void orig_node_free_ref(struct kref *refcount)
126116
list_for_each_entry_safe(neigh_node, tmp_neigh_node,
127117
&orig_node->bond_list, bonding_list) {
128118
list_del_rcu(&neigh_node->bonding_list);
129-
call_rcu(&neigh_node->rcu_bond, neigh_node_free_rcu_bond);
119+
neigh_node_free_ref(neigh_node);
130120
}
131121

132122
/* for all neighbors towards this originator ... */
133123
hlist_for_each_entry_safe(neigh_node, node, node_tmp,
134124
&orig_node->neigh_list, list) {
135125
hlist_del_rcu(&neigh_node->list);
136-
call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
126+
neigh_node_free_ref(neigh_node);
137127
}
138128

139129
spin_unlock_bh(&orig_node->neigh_list_lock);
@@ -315,7 +305,7 @@ static bool purge_orig_neighbors(struct bat_priv *bat_priv,
315305

316306
hlist_del_rcu(&neigh_node->list);
317307
bonding_candidate_del(orig_node, neigh_node);
318-
call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
308+
neigh_node_free_ref(neigh_node);
319309
} else {
320310
if ((!*best_neigh_node) ||
321311
(neigh_node->tq_avg > (*best_neigh_node)->tq_avg))

‎net/batman-adv/originator.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ int originator_init(struct bat_priv *bat_priv);
2626
void originator_free(struct bat_priv *bat_priv);
2727
void purge_orig_ref(struct bat_priv *bat_priv);
2828
void orig_node_free_ref(struct kref *refcount);
29-
void neigh_node_free_rcu_bond(struct rcu_head *rcu);
3029
struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr);
3130
struct neigh_node *create_neighbor(struct orig_node *orig_node,
3231
struct orig_node *orig_neigh_node,
3332
uint8_t *neigh,
3433
struct batman_if *if_incoming);
35-
void neigh_node_free_ref(struct kref *refcount);
34+
void neigh_node_free_ref(struct neigh_node *neigh_node);
3635
int orig_seq_print_text(struct seq_file *seq, void *offset);
3736
int orig_hash_add_if(struct batman_if *batman_if, int max_if_num);
3837
int orig_hash_del_if(struct batman_if *batman_if, int max_if_num);

0 commit comments

Comments
 (0)