Skip to contentSkip to conversation

bridge: check iface upper dev when setting master via ioctl

PATCH Superseded Hangbin Liu 3 participants 8 messages Apr 26, 2018 – Apr 30, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bridge: check iface upper dev when setting master via ioctl

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. So it would be better to return as early as possible.

---
 net/bridge/br_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/bridge/br_if.c +2 -2
index 82c1a6f..176de8a9 100644
@@ -518,8 +518,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 		return -ELOOP;
 	}
 
-	/* Device is already being bridged */
-	if (br_port_exists(dev))
+	/* Device has master upper dev */
+	if (netdev_has_any_upper_dev(dev))
 		return -EBUSY;
 
 	/* No bridging devices that dislike that (e.g. wireless) */
Signature · 1 line
2.5.5
#1 lore raw
Quoted 18 lines · Hangbin Liu

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. So it would be better to return as early as possible.

---
 net/bridge/br_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/bridge/br_if.c +0 -0
index 82c1a6f..176de8a9 100644

net/bridge/br_if.c int br_add_if(struct net_bridge *br, struct net_device *dev, · line 525

Quoted 10 lines
net/bridge/br_if.c +2 -2
@@ -518,8 +518,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 		return -ELOOP;
 	}
 
-	/* Device is already being bridged */
-	if (br_port_exists(dev))
+	/* Device has master upper dev */
+	if (netdev_has_any_upper_dev(dev))
 		return -EBUSY;
 
 	/* No bridging devices that dislike that (e.g. wireless) */

Not all upper devs are masters. This can break some setups.

#2 lore raw
Quoted 27 lines · Nikolay Aleksandrov
Quoted 26 lines · Hangbin Liu

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. So it would be better to return as early as possible.

---
 net/bridge/br_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/bridge/br_if.c +2 -2
index 82c1a6f..176de8a9 100644
@@ -518,8 +518,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 		return -ELOOP;
 	}
 
-	/* Device is already being bridged */
-	if (br_port_exists(dev))
+	/* Device has master upper dev */
+	if (netdev_has_any_upper_dev(dev))
 		return -EBUSY;
 
Quoted 6 lines

net/bridge/br_if.c int br_add_if(struct net_bridge *br, struct net_device *dev, · line 525

Quoted 2 lines
net/bridge/br_if.c +0 -0
 	/* No bridging devices that dislike that (e.g. wireless) */

Not all upper devs are masters. This can break some setups.

Also it's not really a bug, the device begins to get initialized but it will get removed at netdev_master_upper_dev_link() anyway if there's already a master. Why would it be better ?
It's clearly wrong to try and enslave a device that already has a master via ioctl, rtnetlink already deals with that and the old ioctl interface will get an error, yes it will initialize some structs but they'll get freed later. This is common practice, check the bonding for example.

If anything do the check in the ioctl interface (add_del_if) only and maybe target net-next, there's really no bug fix here. IMO it's not needed even there, but it doesn't hurt either so up to you.

Thanks,
Nik

#3 lore raw

Hi Nikolay,

Thanks for the comments.

Quoted 1 line · Nikolay Aleksandrov
Quoted 1 line

Not all upper devs are masters. This can break some setups.

Ah, like vlan device.. So how about

+ if (netdev_master_upper_dev_get(dev))
return -EBUSY;

Quoted 11 lines
Quoted 2 lines

Also it's not really a bug, the device begins to get initialized but it will get removed at netdev_master_upper_dev_link() anyway if there's already a master. Why would it be better ?

It's clearly wrong to try and enslave a device that already has a master via ioctl, rtnetlink already deals with that and the old ioctl interface will get an error, yes it will initialize some structs but they'll get freed later. This is common practice, check the bonding for example.

Bonding use netdev_is_rx_handler_busy(slave_dev) to check if the slave already has a master, which is another solution.

Quoted 3 lines

If anything do the check in the ioctl interface (add_del_if) only and maybe target net-next, there's really no bug fix here. IMO it's not

What if someone do like

while true; do brctl addif br0 bond_slave &; done

I know this is stupid and almost no one will do that in real world. But syzbot run some similar test and get warn from kobject_add_internal() with -ENOMEM. That's why I think we should fix it before allocate any resource.

What do you think?

[1] https://syzkaller.appspot.com/bug?id=3e0339080acd6a2a350a900bc6533b03f5498490

Thanks
Hangbin

#4 lore raw

Review

Quoted 10 lines · Hangbin Liu

Hi Nikolay,

Thanks for the comments.

Quoted 1 line · Nikolay Aleksandrov
Quoted 1 line

Not all upper devs are masters. This can break some setups.

Ah, like vlan device.. So how about

(quoted diff) +1 -0
+	if (netdev_master_upper_dev_get(dev))
  		return -EBUSY;

That should be fine, yes.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Also it's not really a bug, the device begins to get initialized but it will get removed at netdev_master_upper_dev_link() anyway if there's already a master. Why would it be better ?

Quoted 7 lines
Quoted 4 lines

It's clearly wrong to try and enslave a device that already has a master via ioctl, rtnetlink already deals with that and the old ioctl interface will get an error, yes it will initialize some structs but they'll get freed later. This is common practice, check the bonding for example.

Bonding use netdev_is_rx_handler_busy(slave_dev) to check if the slave already has a master, which is another solution.

Some masters don't use rx_handlers and the bonding fails at linking them as a master which is still fine, it cleans up after the error like the bridge.

Quoted 6 lines
Quoted 3 lines

If anything do the check in the ioctl interface (add_del_if) only and maybe target net-next, there's really no bug fix here. IMO it's not

What if someone do like

Quoted 8 lines

while true; do brctl addif br0 bond_slave &; done

I know this is stupid and almost no one will do that in real world. But syzbot run some similar test and get warn from kobject_add_internal() with -ENOMEM. That's why I think we should fix it before allocate any resource.

What do you think?

The bridge code is only a symptom of what happened, that warn was placed to warn people against doing stupid things - it was literally in the commit message of some kobject patch. As long as the resources involved are cleaned up and it's returned to the bridge to cleanup after itself, it should be fine.

You can add the check if you feel like it, I don't have an objection against failing earlier. My main concern was the netdev_has_any_upper usage which can break some setups.

Cheers,
Nik

Quoted 6 lines

[1] https://syzkaller.appspot.com/bug?id=3e0339080acd6a2a350a900bc6533b03f5498490

Thanks
Hangbin

#5 lore raw

PATCH v2 1/1 bridge: check iface upper dev when setting master via ioctl

In reply to Hangbin Liu

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. It would be better to return as early as possible.

v1 -> v2:
use netdev_master_upper_dev_get() instead of netdev_has_any_upper_dev() to check if we have a master, because not all upper devs are masters, e.g. vlan device.

---
 net/bridge/br_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/bridge/br_if.c +2 -2
index 82c1a6f..5bb6681 100644
@@ -518,8 +518,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
 		return -ELOOP;
 	}
 
-	/* Device is already being bridged */
-	if (br_port_exists(dev))
+	/* Device has master upper dev */
+	if (netdev_master_upper_dev_get(dev))
 		return -EBUSY;
 
 	/* No bridging devices that dislike that (e.g. wireless) */
Signature · 1 line
2.5.5
Acked-by on v2 1/1
#6 lore raw
Quoted 23 lines · Hangbin Liu

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. It would be better to return as early as possible.

v1 -> v2:
use netdev_master_upper_dev_get() instead of netdev_has_any_upper_dev() to check if we have a master, because not all upper devs are masters, e.g. vlan device.

---
  net/bridge/br_if.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/bridge/br_if.c +0 -0
index 82c1a6f..5bb6681 100644

net/bridge/br_if.c int br_add_if(struct net_bridge *br, struct net_device *dev, · line 525

Quoted 10 lines
net/bridge/br_if.c +2 -2
@@ -518,8 +518,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev,
  		return -ELOOP;
  	}
 
-	/* Device is already being bridged */
-	if (br_port_exists(dev))
+	/* Device has master upper dev */
+	if (netdev_master_upper_dev_get(dev))
  		return -EBUSY;
 
  	/* No bridging devices that dislike that (e.g. wireless) */
maintainer
#7 lore raw
In reply to Hangbin Liu

From: Hangbin Liu <[email protected]>
Date: Fri, 27 Apr 2018 20:59:24 +0800

Quoted 6 lines

When we set a bond slave's master to bridge via ioctl, we only check the IFF_BRIDGE_PORT flag. Although we will find the slave's real master at netdev_master_upper_dev_link() later, it already does some settings and allocates some resources. It would be better to return as early as possible.

Quoted 7 lines

v1 -> v2:
use netdev_master_upper_dev_get() instead of netdev_has_any_upper_dev() to check if we have a master, because not all upper devs are masters, e.g. vlan device.

Applied and queued up for -stable, thanks.