Skip to content

Commit 7b36e8e

Browse files
author
Marek Lindner
committed
batman-adv: Correct rcu refcounting for orig_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 7aadf88 commit 7b36e8e

9 files changed

Lines changed: 49 additions & 36 deletions

File tree

‎net/batman-adv/gateway_client.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ void *gw_get_selected(struct bat_priv *bat_priv)
5353
goto out;
5454

5555
orig_node = curr_gateway_tmp->orig_node;
56+
if (!orig_node)
57+
goto out;
5658

57-
if (orig_node)
58-
kref_get(&orig_node->refcount);
59+
if (!atomic_inc_not_zero(&orig_node->refcount))
60+
orig_node = NULL;
5961

6062
out:
6163
rcu_read_unlock();

‎net/batman-adv/icmp_socket.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
271271
if (neigh_node)
272272
neigh_node_free_ref(neigh_node);
273273
if (orig_node)
274-
kref_put(&orig_node->refcount, orig_node_free_ref);
274+
orig_node_free_ref(orig_node);
275275
return len;
276276
}
277277

‎net/batman-adv/originator.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
102102
return neigh_node;
103103
}
104104

105-
void orig_node_free_ref(struct kref *refcount)
105+
static void orig_node_free_rcu(struct rcu_head *rcu)
106106
{
107107
struct hlist_node *node, *node_tmp;
108108
struct neigh_node *neigh_node, *tmp_neigh_node;
109109
struct orig_node *orig_node;
110110

111-
orig_node = container_of(refcount, struct orig_node, refcount);
111+
orig_node = container_of(rcu, struct orig_node, rcu);
112112

113113
spin_lock_bh(&orig_node->neigh_list_lock);
114114

@@ -137,6 +137,12 @@ void orig_node_free_ref(struct kref *refcount)
137137
kfree(orig_node);
138138
}
139139

140+
void orig_node_free_ref(struct orig_node *orig_node)
141+
{
142+
if (atomic_dec_and_test(&orig_node->refcount))
143+
call_rcu(&orig_node->rcu, orig_node_free_rcu);
144+
}
145+
140146
void originator_free(struct bat_priv *bat_priv)
141147
{
142148
struct hashtable_t *hash = bat_priv->orig_hash;
@@ -163,7 +169,7 @@ void originator_free(struct bat_priv *bat_priv)
163169
head, hash_entry) {
164170

165171
hlist_del_rcu(node);
166-
kref_put(&orig_node->refcount, orig_node_free_ref);
172+
orig_node_free_ref(orig_node);
167173
}
168174
spin_unlock_bh(list_lock);
169175
}
@@ -196,7 +202,9 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
196202
spin_lock_init(&orig_node->ogm_cnt_lock);
197203
spin_lock_init(&orig_node->bcast_seqno_lock);
198204
spin_lock_init(&orig_node->neigh_list_lock);
199-
kref_init(&orig_node->refcount);
205+
206+
/* extra reference for return */
207+
atomic_set(&orig_node->refcount, 2);
200208

201209
orig_node->bat_priv = bat_priv;
202210
memcpy(orig_node->orig, addr, ETH_ALEN);
@@ -229,8 +237,6 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
229237
if (hash_added < 0)
230238
goto free_bcast_own_sum;
231239

232-
/* extra reference for return */
233-
kref_get(&orig_node->refcount);
234240
return orig_node;
235241
free_bcast_own_sum:
236242
kfree(orig_node->bcast_own_sum);
@@ -348,8 +354,7 @@ static void _purge_orig(struct bat_priv *bat_priv)
348354
if (orig_node->gw_flags)
349355
gw_node_delete(bat_priv, orig_node);
350356
hlist_del_rcu(node);
351-
kref_put(&orig_node->refcount,
352-
orig_node_free_ref);
357+
orig_node_free_ref(orig_node);
353358
continue;
354359
}
355360

‎net/batman-adv/originator.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
int originator_init(struct bat_priv *bat_priv);
2828
void originator_free(struct bat_priv *bat_priv);
2929
void purge_orig_ref(struct bat_priv *bat_priv);
30-
void orig_node_free_ref(struct kref *refcount);
30+
void orig_node_free_ref(struct orig_node *orig_node);
3131
struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr);
3232
struct neigh_node *create_neighbor(struct orig_node *orig_node,
3333
struct orig_node *orig_neigh_node,
@@ -88,8 +88,10 @@ static inline struct orig_node *orig_hash_find(struct bat_priv *bat_priv,
8888
if (!compare_eth(orig_node, data))
8989
continue;
9090

91+
if (!atomic_inc_not_zero(&orig_node->refcount))
92+
continue;
93+
9194
orig_node_tmp = orig_node;
92-
kref_get(&orig_node_tmp->refcount);
9395
break;
9496
}
9597
rcu_read_unlock();

‎net/batman-adv/routing.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static void update_orig(struct bat_priv *bat_priv,
420420
neigh_node = create_neighbor(orig_node, orig_tmp,
421421
ethhdr->h_source, if_incoming);
422422

423-
kref_put(&orig_tmp->refcount, orig_node_free_ref);
423+
orig_node_free_ref(orig_tmp);
424424
if (!neigh_node)
425425
goto unlock;
426426

@@ -604,7 +604,7 @@ static char count_real_packets(struct ethhdr *ethhdr,
604604

605605
out:
606606
spin_unlock_bh(&orig_node->ogm_cnt_lock);
607-
kref_put(&orig_node->refcount, orig_node_free_ref);
607+
orig_node_free_ref(orig_node);
608608
return ret;
609609
}
610610

@@ -730,7 +730,7 @@ void receive_bat_packet(struct ethhdr *ethhdr,
730730

731731
bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
732732
"originator packet from myself (via neighbor)\n");
733-
kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
733+
orig_node_free_ref(orig_neigh_node);
734734
return;
735735
}
736736

@@ -835,10 +835,10 @@ void receive_bat_packet(struct ethhdr *ethhdr,
835835
0, hna_buff_len, if_incoming);
836836

837837
out_neigh:
838-
if (!is_single_hop_neigh)
839-
kref_put(&orig_neigh_node->refcount, orig_node_free_ref);
838+
if ((orig_neigh_node) && (!is_single_hop_neigh))
839+
orig_node_free_ref(orig_neigh_node);
840840
out:
841-
kref_put(&orig_node->refcount, orig_node_free_ref);
841+
orig_node_free_ref(orig_node);
842842
}
843843

844844
int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
@@ -952,7 +952,7 @@ static int recv_my_icmp_packet(struct bat_priv *bat_priv,
952952
if (neigh_node)
953953
neigh_node_free_ref(neigh_node);
954954
if (orig_node)
955-
kref_put(&orig_node->refcount, orig_node_free_ref);
955+
orig_node_free_ref(orig_node);
956956
return ret;
957957
}
958958

@@ -1028,7 +1028,7 @@ static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
10281028
if (neigh_node)
10291029
neigh_node_free_ref(neigh_node);
10301030
if (orig_node)
1031-
kref_put(&orig_node->refcount, orig_node_free_ref);
1031+
orig_node_free_ref(orig_node);
10321032
return ret;
10331033
}
10341034

@@ -1134,7 +1134,7 @@ int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
11341134
if (neigh_node)
11351135
neigh_node_free_ref(neigh_node);
11361136
if (orig_node)
1137-
kref_put(&orig_node->refcount, orig_node_free_ref);
1137+
orig_node_free_ref(orig_node);
11381138
return ret;
11391139
}
11401140

@@ -1189,7 +1189,7 @@ struct neigh_node *find_router(struct bat_priv *bat_priv,
11891189
if (!primary_orig_node)
11901190
goto return_router;
11911191

1192-
kref_put(&primary_orig_node->refcount, orig_node_free_ref);
1192+
orig_node_free_ref(primary_orig_node);
11931193
}
11941194

11951195
/* with less than 2 candidates, we can't do any
@@ -1401,7 +1401,7 @@ int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
14011401
if (neigh_node)
14021402
neigh_node_free_ref(neigh_node);
14031403
if (orig_node)
1404-
kref_put(&orig_node->refcount, orig_node_free_ref);
1404+
orig_node_free_ref(orig_node);
14051405
return ret;
14061406
}
14071407

@@ -1543,7 +1543,7 @@ int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
15431543
spin_unlock_bh(&bat_priv->orig_hash_lock);
15441544
out:
15451545
if (orig_node)
1546-
kref_put(&orig_node->refcount, orig_node_free_ref);
1546+
orig_node_free_ref(orig_node);
15471547
return ret;
15481548
}
15491549

‎net/batman-adv/translation-table.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,20 @@ void hna_global_free(struct bat_priv *bat_priv)
589589
struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
590590
{
591591
struct hna_global_entry *hna_global_entry;
592+
struct orig_node *orig_node = NULL;
592593

593594
spin_lock_bh(&bat_priv->hna_ghash_lock);
594595
hna_global_entry = hna_global_hash_find(bat_priv, addr);
595596

596-
if (hna_global_entry)
597-
kref_get(&hna_global_entry->orig_node->refcount);
597+
if (!hna_global_entry)
598+
goto out;
598599

599-
spin_unlock_bh(&bat_priv->hna_ghash_lock);
600+
if (!atomic_inc_not_zero(&hna_global_entry->orig_node->refcount))
601+
goto out;
600602

601-
if (!hna_global_entry)
602-
return NULL;
603+
orig_node = hna_global_entry->orig_node;
603604

604-
return hna_global_entry->orig_node;
605+
out:
606+
spin_unlock_bh(&bat_priv->hna_ghash_lock);
607+
return orig_node;
605608
}

‎net/batman-adv/types.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ struct orig_node {
8484
struct hlist_head neigh_list;
8585
struct list_head frag_list;
8686
spinlock_t neigh_list_lock; /* protects neighbor list */
87-
struct kref refcount;
87+
atomic_t refcount;
88+
struct rcu_head rcu;
8889
struct hlist_node hash_entry;
8990
struct bat_priv *bat_priv;
9091
unsigned long last_frag_packet;

‎net/batman-adv/unicast.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
211211
spin_unlock_bh(&bat_priv->orig_hash_lock);
212212
out:
213213
if (orig_node)
214-
kref_put(&orig_node->refcount, orig_node_free_ref);
214+
orig_node_free_ref(orig_node);
215215
return ret;
216216
}
217217

@@ -280,7 +280,7 @@ int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
280280
{
281281
struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
282282
struct unicast_packet *unicast_packet;
283-
struct orig_node *orig_node = NULL;
283+
struct orig_node *orig_node;
284284
struct batman_if *batman_if;
285285
struct neigh_node *neigh_node;
286286
int data_len = skb->len;
@@ -347,7 +347,7 @@ int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
347347
if (neigh_node)
348348
neigh_node_free_ref(neigh_node);
349349
if (orig_node)
350-
kref_put(&orig_node->refcount, orig_node_free_ref);
350+
orig_node_free_ref(orig_node);
351351
if (ret == 1)
352352
kfree_skb(skb);
353353
return ret;

‎net/batman-adv/vis.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ static void unicast_vis_packet(struct bat_priv *bat_priv,
826826
if (neigh_node)
827827
neigh_node_free_ref(neigh_node);
828828
if (orig_node)
829-
kref_put(&orig_node->refcount, orig_node_free_ref);
829+
orig_node_free_ref(orig_node);
830830
return;
831831
}
832832

0 commit comments

Comments
 (0)