batman-adv: Disallow regular interface as mesh device
authorSven Eckelmann <sven@narfation.org>
Fri, 4 Mar 2011 21:36:41 +0000 (21:36 +0000)
committerMarek Lindner <lindner_marek@yahoo.de>
Sat, 5 Mar 2011 11:52:09 +0000 (12:52 +0100)
When trying to associate a net_device with another net_device which
already exists, batman-adv assumes that this interface is a fully
initialized batman mesh interface without checking it. The behaviour
when accessing data behind netdev_priv of a random net_device is
undefined and potentially dangerous.

Reported-by: Linus Lüssing <linus.luessing@ascom.ch>
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
net/batman-adv/hard-interface.c
net/batman-adv/soft-interface.c
net/batman-adv/soft-interface.h

index 95a35b695700dd8ce8225449d2f33e56aca47d4d..b3058e46ee6ba9bb7bdfc7ef96860eedc9bb67f7 100644 (file)
@@ -79,13 +79,8 @@ static int is_valid_iface(struct net_device *net_dev)
                return 0;
 
        /* no batman over batman */
-#ifdef HAVE_NET_DEVICE_OPS
-       if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
+       if (softif_is_valid(net_dev))
                return 0;
-#else
-       if (net_dev->hard_start_xmit == interface_tx)
-               return 0;
-#endif
 
        /* Device is being bridged */
        /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
@@ -282,6 +277,8 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
 {
        struct bat_priv *bat_priv;
        struct batman_packet *batman_packet;
+       struct net_device *soft_iface;
+       int ret;
 
        if (hard_iface->if_status != IF_NOT_IN_USE)
                goto out;
@@ -289,18 +286,30 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
        if (!atomic_inc_not_zero(&hard_iface->refcount))
                goto out;
 
-       hard_iface->soft_iface = dev_get_by_name(&init_net, iface_name);
+       soft_iface = dev_get_by_name(&init_net, iface_name);
 
-       if (!hard_iface->soft_iface) {
-               hard_iface->soft_iface = softif_create(iface_name);
+       if (!soft_iface) {
+               soft_iface = softif_create(iface_name);
 
-               if (!hard_iface->soft_iface)
+               if (!soft_iface) {
+                       ret = -ENOMEM;
                        goto err;
+               }
 
                /* dev_get_by_name() increases the reference counter for us */
-               dev_hold(hard_iface->soft_iface);
+               dev_hold(soft_iface);
+       }
+
+       if (!softif_is_valid(soft_iface)) {
+               pr_err("Can't create batman mesh interface %s: "
+                      "already exists as regular interface\n",
+                      soft_iface->name);
+               dev_put(soft_iface);
+               ret = -EINVAL;
+               goto err;
        }
 
+       hard_iface->soft_iface = soft_iface;
        bat_priv = netdev_priv(hard_iface->soft_iface);
        hard_iface->packet_len = BAT_PACKET_LEN;
        hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
@@ -308,6 +317,7 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
        if (!hard_iface->packet_buff) {
                bat_err(hard_iface->soft_iface, "Can't add interface packet "
                        "(%s): out of memory\n", hard_iface->net_dev->name);
+               ret = -ENOMEM;
                goto err;
        }
 
@@ -370,7 +380,7 @@ out:
 
 err:
        hardif_free_ref(hard_iface);
-       return -ENOMEM;
+       return ret;
 }
 
 void hardif_disable_interface(struct hard_iface *hard_iface)
index 6b514ecee5a2b8ce4a322d6a1ad9fb62887a3262..9ed26140a2696aa19d87534e15b4609d5901b66b 100644 (file)
@@ -622,6 +622,19 @@ void softif_destroy(struct net_device *soft_iface)
        unregister_netdevice(soft_iface);
 }
 
+int softif_is_valid(struct net_device *net_dev)
+{
+#ifdef HAVE_NET_DEVICE_OPS
+       if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
+               return 1;
+#else
+       if (net_dev->hard_start_xmit == interface_tx)
+               return 1;
+#endif
+
+       return 0;
+}
+
 /* ethtool */
 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
index 80a3607df18622ba954da2817d8884551d4211a0..4789b6f2a0b3f9cdcd393d380d48ea8f95618b62 100644 (file)
@@ -31,5 +31,6 @@ void interface_rx(struct net_device *soft_iface,
                  int hdr_size);
 struct net_device *softif_create(char *name);
 void softif_destroy(struct net_device *soft_iface);
+int softif_is_valid(struct net_device *net_dev);
 
 #endif /* _NET_BATMAN_ADV_SOFT_INTERFACE_H_ */