Prevent premature device free in interface_claim_device
authorFelix Fietkau <nbd@nbd.name>
Thu, 11 Aug 2016 17:36:09 +0000 (19:36 +0200)
committerFelix Fietkau <nbd@nbd.name>
Thu, 11 Aug 2016 17:36:12 +0000 (19:36 +0200)
interface_set_device_config can trigger a device free (for example
if the device is here only present in a bridge), which renders dev
invalid and leads to segfault. Add a lock to prevent this and
clean-up the code for readability.

Signed-off-by: Gino Peeters <peeters.gino@gmail.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
interface.c

index 0b9893ccad49fc4b84b698aa9d735203ebd30960..71e2ecc282d52a97baba83ebcd7aa47d863ef6b1 100644 (file)
@@ -586,6 +586,8 @@ interface_claim_device(struct interface *iface)
        if (iface->parent_iface.iface)
                interface_remove_user(&iface->parent_iface);
 
+       device_lock();
+
        if (iface->parent_ifname) {
                parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
                iface->parent_iface.cb = interface_alias_cb;
@@ -601,6 +603,8 @@ interface_claim_device(struct interface *iface)
        if (dev)
                interface_set_main_dev(iface, dev);
 
+       device_unlock();
+
        if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
                interface_set_available(iface, true);
 }