Skip to content

Commit a775eb8

Browse files
author
Marek Lindner
committed
batman-adv: protect neigh_nodes used outside of rcu_locks with refcounting
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent 1a241a5 commit a775eb8

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

‎net/batman-adv/routing.c‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
151151
struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
152152
struct hlist_node *node;
153153
unsigned char total_count;
154+
int ret = 0;
154155

155156
if (orig_node == orig_neigh_node) {
156157
rcu_read_lock();
@@ -162,7 +163,6 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
162163
(tmp_neigh_node->if_incoming == if_incoming))
163164
neigh_node = tmp_neigh_node;
164165
}
165-
rcu_read_unlock();
166166

167167
if (!neigh_node)
168168
neigh_node = create_neighbor(orig_node,
@@ -171,7 +171,10 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
171171
if_incoming);
172172
/* create_neighbor failed, return 0 */
173173
if (!neigh_node)
174-
return 0;
174+
goto unlock;
175+
176+
kref_get(&neigh_node->refcount);
177+
rcu_read_unlock();
175178

176179
neigh_node->last_valid = jiffies;
177180
} else {
@@ -185,7 +188,6 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
185188
(tmp_neigh_node->if_incoming == if_incoming))
186189
neigh_node = tmp_neigh_node;
187190
}
188-
rcu_read_unlock();
189191

190192
if (!neigh_node)
191193
neigh_node = create_neighbor(orig_neigh_node,
@@ -194,7 +196,10 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
194196
if_incoming);
195197
/* create_neighbor failed, return 0 */
196198
if (!neigh_node)
197-
return 0;
199+
goto unlock;
200+
201+
kref_get(&neigh_node->refcount);
202+
rcu_read_unlock();
198203
}
199204

200205
orig_node->last_valid = jiffies;
@@ -250,9 +255,16 @@ static int is_bidirectional_neigh(struct orig_node *orig_node,
250255
/* if link has the minimum required transmission quality
251256
* consider it bidirectional */
252257
if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
253-
return 1;
258+
ret = 1;
254259

255-
return 0;
260+
goto out;
261+
262+
unlock:
263+
rcu_read_unlock();
264+
out:
265+
if (neigh_node)
266+
kref_put(&neigh_node->refcount, neigh_node_free_ref);
267+
return ret;
256268
}
257269

258270
static void update_orig(struct bat_priv *bat_priv,
@@ -287,23 +299,25 @@ static void update_orig(struct bat_priv *bat_priv,
287299
tmp_neigh_node->tq_avg =
288300
ring_buffer_avg(tmp_neigh_node->tq_recv);
289301
}
290-
rcu_read_unlock();
291302

292303
if (!neigh_node) {
293304
struct orig_node *orig_tmp;
294305

295306
orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
296307
if (!orig_tmp)
297-
return;
308+
goto unlock;
298309

299310
neigh_node = create_neighbor(orig_node, orig_tmp,
300311
ethhdr->h_source, if_incoming);
301312
if (!neigh_node)
302-
return;
313+
goto unlock;
303314
} else
304315
bat_dbg(DBG_BATMAN, bat_priv,
305316
"Updating existing last-hop neighbor of originator\n");
306317

318+
kref_get(&neigh_node->refcount);
319+
rcu_read_unlock();
320+
307321
orig_node->flags = batman_packet->flags;
308322
neigh_node->last_valid = jiffies;
309323

@@ -357,6 +371,14 @@ static void update_orig(struct bat_priv *bat_priv,
357371
(atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
358372
(atomic_read(&bat_priv->gw_sel_class) > 2))
359373
gw_check_election(bat_priv, orig_node);
374+
375+
goto out;
376+
377+
unlock:
378+
rcu_read_unlock();
379+
out:
380+
if (neigh_node)
381+
kref_put(&neigh_node->refcount, neigh_node_free_ref);
360382
}
361383

362384
/* checks whether the host restarted and is in the protection time.

0 commit comments

Comments
 (0)