bridge: fix br_stp_set_bridge_priority race conditions
authorNikolay Aleksandrov <razor@blackwall.org>
Mon, 15 Jun 2015 17:28:51 +0000 (20:28 +0300)
committerDavid S. Miller <davem@davemloft.net>
Thu, 18 Jun 2015 10:29:47 +0000 (03:29 -0700)
After the ->set() spinlocks were removed br_stp_set_bridge_priority
was left running without any protection when used via sysfs. It can
race with port add/del and could result in use-after-free cases and
corrupted lists. Tested by running port add/del in a loop with stp
enabled while setting priority in a loop, crashes are easily
reproducible.
The spinlocks around sysfs ->set() were removed in commit:
14f98f258f19 ("bridge: range check STP parameters")
There's also a race condition in the netlink priority support that is
fixed by this change, but it was introduced recently and the fixes tag
covers it, just in case it's needed the commit is:
af615762e972 ("bridge: add ageing_time, stp_state, priority over netlink")

Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
Fixes: 14f98f258f19 ("bridge: range check STP parameters")
Signed-off-by: David S. Miller <davem@davemloft.net>
net/bridge/br_ioctl.c
net/bridge/br_stp_if.c

index a9a4a1b7863d11c1972c13ba0767c0ccdcfcfe35..8d423bc649b9c4a2829499dffb0ba88d952af128 100644 (file)
@@ -247,9 +247,7 @@ static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                if (!ns_capable(dev_net(dev)->user_ns, CAP_NET_ADMIN))
                        return -EPERM;
 
-               spin_lock_bh(&br->lock);
                br_stp_set_bridge_priority(br, args[1]);
-               spin_unlock_bh(&br->lock);
                return 0;
 
        case BRCTL_SET_PORT_PRIORITY:
index 41146872c1b47ca6ee3ac48c14e672a9743273d4..7832d07f48f66c86e4787364d1de86a189b5c842 100644 (file)
@@ -243,12 +243,13 @@ bool br_stp_recalculate_bridge_id(struct net_bridge *br)
        return true;
 }
 
-/* called under bridge lock */
+/* Acquires and releases bridge lock */
 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
 {
        struct net_bridge_port *p;
        int wasroot;
 
+       spin_lock_bh(&br->lock);
        wasroot = br_is_root_bridge(br);
 
        list_for_each_entry(p, &br->port_list, list) {
@@ -266,6 +267,7 @@ void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
        br_port_state_selection(br);
        if (br_is_root_bridge(br) && !wasroot)
                br_become_root_bridge(br);
+       spin_unlock_bh(&br->lock);
 }
 
 /* called under bridge lock */