[1/5] batman-adv: Prevent duplicated gateway_node entry

Message ID 20180812190445.28013-2-sven@narfation.org (mailing list archive)
State Superseded, archived
Delegated to: Simon Wunderlich
Headers
Series batman-adv: Missing list checks for *list_add* |

Commit Message

Sven Eckelmann Aug. 12, 2018, 7:04 p.m. UTC
  The function batadv_gw_node_add is responsible for adding new gw_node to
the gateway_list. It is expecting that the caller already checked that
there is not already an entry with the same key or not.

But the lock for the list is only held when the list is really modified.
This could lead to duplicated entries because another context could create
an entry with the same key between the check and the list manipulation.

The check and the manipulation of the list must therefore be in the same
locked code section.

Fixes: bc3538cabac5 ("batman-adv: adding gateway functionality")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 net/batman-adv/gateway_client.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
  

Comments

Marek Lindner Sept. 6, 2018, 11:41 a.m. UTC | #1
On Monday, 13 August 2018 03:04:41 HKT Sven Eckelmann wrote:
> @@ -355,6 +356,8 @@ static void batadv_gw_node_add(struct batadv_priv
> *bat_priv, {
>         struct batadv_gw_node *gw_node;
> 
> +       lockdep_assert_held(&bat_priv->gw.list_lock);
> +
>         if (gateway->bandwidth_down == 0)
>                 return;
> 
> @@ -369,10 +372,8 @@ static void batadv_gw_node_add(struct batadv_priv
> *bat_priv, gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
>         gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
> 
> -       spin_lock_bh(&bat_priv->gw.list_lock);
>         kref_get(&gw_node->refcount);
>         hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
> -       spin_unlock_bh(&bat_priv->gw.list_lock);

Not mandatory but in other cases the function kernel doc received a hint that 
$some lock needs to be acquired before the function should be called.

Acked-by: Marek Lindner <mareklindner@neomailbox.ch>

Cheers,
Marek
  

Patch

diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index 8b198ee7..0df4e87d 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -32,6 +32,7 @@ 
 #include <linux/kernel.h>
 #include <linux/kref.h>
 #include <linux/list.h>
+#include <linux/lockdep.h>
 #include <linux/netdevice.h>
 #include <linux/netlink.h>
 #include <linux/rculist.h>
@@ -355,6 +356,8 @@  static void batadv_gw_node_add(struct batadv_priv *bat_priv,
 {
 	struct batadv_gw_node *gw_node;
 
+	lockdep_assert_held(&bat_priv->gw.list_lock);
+
 	if (gateway->bandwidth_down == 0)
 		return;
 
@@ -369,10 +372,8 @@  static void batadv_gw_node_add(struct batadv_priv *bat_priv,
 	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
 	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
 
-	spin_lock_bh(&bat_priv->gw.list_lock);
 	kref_get(&gw_node->refcount);
 	hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
-	spin_unlock_bh(&bat_priv->gw.list_lock);
 
 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
@@ -428,11 +429,14 @@  void batadv_gw_node_update(struct batadv_priv *bat_priv,
 {
 	struct batadv_gw_node *gw_node, *curr_gw = NULL;
 
+	spin_lock_bh(&bat_priv->gw.list_lock);
 	gw_node = batadv_gw_node_get(bat_priv, orig_node);
 	if (!gw_node) {
 		batadv_gw_node_add(bat_priv, orig_node, gateway);
+		spin_unlock_bh(&bat_priv->gw.list_lock);
 		goto out;
 	}
+	spin_unlock_bh(&bat_priv->gw.list_lock);
 
 	if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
 	    gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))