Skip to contentSkip to conversation

batman-adv: fix NULL pointer deref in batadv_find_best_neighbor

PATCH Superseded Simon Wunderlich 2 participants 2 messages Dec 17, 2013 – Dec 25, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batman-adv: fix NULL pointer deref in batadv_find_best_neighbor

If there is no best neighbor, don't dereference the NULL pointer.

Introduced by 9bb33b8d88e318c4879d37d06ad28e3e018b9036 ("batman-adv:
split tq information in neigh_node struct")

---
 originator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
originator.c +1 -1
index 2243003..9fcde58 100644
@@ -789,7 +789,7 @@ batadv_find_best_neighbor(struct batadv_priv *bat_priv,
 					best, if_outgoing) <= 0))
 			best = neigh;
 
-	if (!atomic_inc_not_zero(&best->refcount))
+	if (best && !atomic_inc_not_zero(&best->refcount))
 		best = NULL;
 	rcu_read_unlock();
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 15 lines · On Tuesday 17 December 2013 16:35:13 Simon Wunderlich wrote:

If there is no best neighbor, don't dereference the NULL pointer.

Introduced by 9bb33b8d88e318c4879d37d06ad28e3e018b9036 ("batman-adv:
split tq information in neigh_node struct")

---
 originator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
originator.c +0 -0
index 2243003..9fcde58 100644

originator.c batadv_find_best_neighbor(struct batadv_priv *bat_priv, · line 794

Quoted 7 lines
originator.c +1 -1
@@ -789,7 +789,7 @@ batadv_find_best_neighbor(struct batadv_priv *bat_priv,
 					best, if_outgoing) <= 0))
 			best = neigh;
 
-	if (!atomic_inc_not_zero(&best->refcount))
+	if (best && !atomic_inc_not_zero(&best->refcount))
 		best = NULL;
 	rcu_read_unlock();

Although this is a valid fix the approach could be improved. The for-loop should check whether the counter can be increased. Otherwise we might select a best-neighbor that is being purged and end up with no best despite having alternatives.

Cheers,
Marek