Skip to content

Commit e44d8fe

Browse files
ecsvMarek Lindner
authored andcommitted
batman-adv: Disallow regular interface as mesh device
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>
1 parent 7cefb14 commit e44d8fe

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

‎net/batman-adv/hard-interface.c‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,8 @@ static int is_valid_iface(struct net_device *net_dev)
7979
return 0;
8080

8181
/* no batman over batman */
82-
#ifdef HAVE_NET_DEVICE_OPS
83-
if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
82+
if (softif_is_valid(net_dev))
8483
return 0;
85-
#else
86-
if (net_dev->hard_start_xmit == interface_tx)
87-
return 0;
88-
#endif
8984

9085
/* Device is being bridged */
9186
/* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
@@ -282,32 +277,47 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
282277
{
283278
struct bat_priv *bat_priv;
284279
struct batman_packet *batman_packet;
280+
struct net_device *soft_iface;
281+
int ret;
285282

286283
if (hard_iface->if_status != IF_NOT_IN_USE)
287284
goto out;
288285

289286
if (!atomic_inc_not_zero(&hard_iface->refcount))
290287
goto out;
291288

292-
hard_iface->soft_iface = dev_get_by_name(&init_net, iface_name);
289+
soft_iface = dev_get_by_name(&init_net, iface_name);
293290

294-
if (!hard_iface->soft_iface) {
295-
hard_iface->soft_iface = softif_create(iface_name);
291+
if (!soft_iface) {
292+
soft_iface = softif_create(iface_name);
296293

297-
if (!hard_iface->soft_iface)
294+
if (!soft_iface) {
295+
ret = -ENOMEM;
298296
goto err;
297+
}
299298

300299
/* dev_get_by_name() increases the reference counter for us */
301-
dev_hold(hard_iface->soft_iface);
300+
dev_hold(soft_iface);
301+
}
302+
303+
if (!softif_is_valid(soft_iface)) {
304+
pr_err("Can't create batman mesh interface %s: "
305+
"already exists as regular interface\n",
306+
soft_iface->name);
307+
dev_put(soft_iface);
308+
ret = -EINVAL;
309+
goto err;
302310
}
303311

312+
hard_iface->soft_iface = soft_iface;
304313
bat_priv = netdev_priv(hard_iface->soft_iface);
305314
hard_iface->packet_len = BAT_PACKET_LEN;
306315
hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
307316

308317
if (!hard_iface->packet_buff) {
309318
bat_err(hard_iface->soft_iface, "Can't add interface packet "
310319
"(%s): out of memory\n", hard_iface->net_dev->name);
320+
ret = -ENOMEM;
311321
goto err;
312322
}
313323

@@ -370,7 +380,7 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
370380

371381
err:
372382
hardif_free_ref(hard_iface);
373-
return -ENOMEM;
383+
return ret;
374384
}
375385

376386
void hardif_disable_interface(struct hard_iface *hard_iface)

‎net/batman-adv/soft-interface.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@ void softif_destroy(struct net_device *soft_iface)
622622
unregister_netdevice(soft_iface);
623623
}
624624

625+
int softif_is_valid(struct net_device *net_dev)
626+
{
627+
#ifdef HAVE_NET_DEVICE_OPS
628+
if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
629+
return 1;
630+
#else
631+
if (net_dev->hard_start_xmit == interface_tx)
632+
return 1;
633+
#endif
634+
635+
return 0;
636+
}
637+
625638
/* ethtool */
626639
static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
627640
{

‎net/batman-adv/soft-interface.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ void interface_rx(struct net_device *soft_iface,
3131
int hdr_size);
3232
struct net_device *softif_create(char *name);
3333
void softif_destroy(struct net_device *soft_iface);
34+
int softif_is_valid(struct net_device *net_dev);
3435

3536
#endif /* _NET_BATMAN_ADV_SOFT_INTERFACE_H_ */

0 commit comments

Comments
 (0)