Skip to content

Commit 94cb82f

Browse files
ecsvsimonwunderlich
authored andcommitted
batman-adv: Prevent duplicated softif_vlan entry
The function batadv_softif_vlan_get is responsible for adding new softif_vlan to the softif_vlan_list. It first checks whether the entry already is in the list or not. If it is, then the creation of a new entry is aborted. 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: 5d2c05b ("batman-adv: add per VLAN interface attribute framework") Signed-off-by: Sven Eckelmann <[email protected]> Signed-off-by: Simon Wunderlich <[email protected]>
1 parent fa122fe commit 94cb82f

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

net/batman-adv/soft-interface.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,33 +574,44 @@ int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
574574
struct batadv_softif_vlan *vlan;
575575
int err;
576576

577+
spin_lock_bh(&bat_priv->softif_vlan_list_lock);
578+
577579
vlan = batadv_softif_vlan_get(bat_priv, vid);
578580
if (vlan) {
579581
batadv_softif_vlan_put(vlan);
582+
spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
580583
return -EEXIST;
581584
}
582585

583586
vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
584-
if (!vlan)
587+
if (!vlan) {
588+
spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
585589
return -ENOMEM;
590+
}
586591

587592
vlan->bat_priv = bat_priv;
588593
vlan->vid = vid;
589594
kref_init(&vlan->refcount);
590595

591596
atomic_set(&vlan->ap_isolation, 0);
592597

598+
kref_get(&vlan->refcount);
599+
hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
600+
spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
601+
602+
/* batadv_sysfs_add_vlan cannot be in the spinlock section due to the
603+
* sleeping behavior of the sysfs functions and the fs_reclaim lock
604+
*/
593605
err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
594606
if (err) {
595-
kfree(vlan);
607+
/* ref for the function */
608+
batadv_softif_vlan_put(vlan);
609+
610+
/* ref for the list */
611+
batadv_softif_vlan_put(vlan);
596612
return err;
597613
}
598614

599-
spin_lock_bh(&bat_priv->softif_vlan_list_lock);
600-
kref_get(&vlan->refcount);
601-
hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
602-
spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
603-
604615
/* add a new TT local entry. This one will be marked with the NOPURGE
605616
* flag
606617
*/

0 commit comments

Comments
 (0)