Skip to contentSkip to conversation

bonding: fix two race conditions in bond_store_updelay/downdelay

PATCH In review Nikolay Aleksandrov 5 participants 8 messages Nov 13, 2013 – Nov 14, 2013

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

PATCH 1/1 bonding: fix two race conditions in bond_store_updelay/downdelay

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

CC Jay Vosburgh <[email protected]>
CC Andy Gospodarek <[email protected]>
CC Veaceslav Falico <[email protected]>
---
 drivers/net/bonding/bond_sysfs.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/bonding/bond_sysfs.c +6 -0
index bc8fd36..8f8a607 100644
@@ -701,6 +701,8 @@ static ssize_t bonding_store_downdelay(struct device *d,
 	int new_value, ret = count;
 	struct bonding *bond = to_bond(d);
 
+	if (!rtnl_trylock())
+		return restart_syscall();
 	if (!(bond->params.miimon)) {
 		pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
 		       bond->dev->name);
@@ -734,6 +736,7 @@ static ssize_t bonding_store_downdelay(struct device *d,
 	}
 
 out:
+	rtnl_unlock();
 	return ret;
 }
 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
@@ -756,6 +759,8 @@ static ssize_t bonding_store_updelay(struct device *d,
 	int new_value, ret = count;
 	struct bonding *bond = to_bond(d);
 
+	if (!rtnl_trylock())
+		return restart_syscall();
 	if (!(bond->params.miimon)) {
 		pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
 		       bond->dev->name);
@@ -789,6 +794,7 @@ static ssize_t bonding_store_updelay(struct device *d,
 	}
 
 out:
+	rtnl_unlock();
 	return ret;
 }
 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 7 lines · Nikolay Aleksandrov

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

Quoted 5 lines
CC Jay Vosburgh <[email protected]>
CC Andy Gospodarek <[email protected]>
CC Veaceslav Falico <[email protected]>

---

A little addition: these can also race with anything that sets miimon to 0, currently beside store_miimon only store_arp_interval does that. But that is also fixed because store_arp_interval uses rtnl.

Cheers,
Nik

#2 lore raw
In reply to Nikolay Aleksandrov
Quoted 6 lines · Nikolay Aleksandrov

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

It seems a bit heavy duty to take rtnl for this.

Using ACCESS_ONCE() in bonding_store_updelay()/bonding_store_downdelay() should be enough ?

int miimon = ACCESS_ONCE(bond->params.miimon);

#3 lore raw
Quoted 13 lines · Eric Dumazet
Quoted 6 lines · Nikolay Aleksandrov

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

It seems a bit heavy duty to take rtnl for this.

Using ACCESS_ONCE() in bonding_store_updelay()/bonding_store_downdelay() should be enough ?

Quoted 9 lines

int miimon = ACCESS_ONCE(bond->params.miimon);

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Hi Eric,
I thought about this version too, but downdelay/updelay can be changed in other places (e.g., store_miimon) and the resulting downdelay/updelay value might not be the right one.
Correct me if I'm wrong, but this is what I have in mind (miimon = 100, updelay = 200):
set miimon to 300 and concurrently set updelay to 400, we might endup leaving updelay to 400 because the old value of miimon is used in the calculation in store_updelay even though when changing miimon updelay/downdelay get adjusted, they might get adjusted by store_updelay/downdelay to a wrong value afterwards.

Nik

#4 lore raw
Quoted 9 lines · Nikolay Aleksandrov

I thought about this version too, but downdelay/updelay can be changed in other places (e.g., store_miimon) and the resulting downdelay/updelay value might not be the right one.
Correct me if I'm wrong, but this is what I have in mind (miimon = 100, updelay = 200):
set miimon to 300 and concurrently set updelay to 400, we might endup leaving updelay to 400 because the old value of miimon is used in the calculation in store_updelay even though when changing miimon updelay/downdelay get adjusted, they might get adjusted by store_updelay/downdelay to a wrong value afterwards.

OK then ;)

#5 lore raw
In reply to Nikolay Aleksandrov
Quoted 34 lines · Nikolay Aleksandrov
Quoted 13 lines · Eric Dumazet
Quoted 6 lines · Nikolay Aleksandrov

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

It seems a bit heavy duty to take rtnl for this.

Using ACCESS_ONCE() in bonding_store_updelay()/bonding_store_downdelay() should be enough ?

Quoted 9 lines

int miimon = ACCESS_ONCE(bond->params.miimon);

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Hi Eric,
I thought about this version too, but downdelay/updelay can be changed in other places (e.g., store_miimon) and the resulting downdelay/updelay value might not be the right one.
Correct me if I'm wrong, but this is what I have in mind (miimon = 100, updelay = 200):
set miimon to 300 and concurrently set updelay to 400, we might endup leaving updelay to 400 because the old value of miimon is used in the calculation in store_updelay even though when changing miimon updelay/downdelay get adjusted, they might get adjusted by store_updelay/downdelay to a wrong value afterwards.

Quoted 3 lines

Nik

agree, set miimon and set updelay may conflict.

Quoted 8 lines

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Acked-by on v1 1/1
#6 lore raw
In reply to Nikolay Aleksandrov
Quoted 11 lines · Nikolay Aleksandrov

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

CC Jay Vosburgh <[email protected]>
CC Andy Gospodarek <[email protected]>
CC Veaceslav Falico <[email protected]>

Nice, thanks! :)

Quoted 45 lines

---
drivers/net/bonding/bond_sysfs.c | 6 ++++++

1 file changed, 6 insertions(+)
drivers/net/bonding/bond_sysfs.c +2 -0
index bc8fd36..8f8a607 100644
@@ -701,6 +701,8 @@ static ssize_t bonding_store_downdelay(struct device *d,
 	int new_value, ret = count;
 	struct bonding *bond = to_bond(d);
 
+	if (!rtnl_trylock())
+		return restart_syscall();
 	if (!(bond->params.miimon)) {
 		pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
 		       bond->dev->name);
@@ -734,6 +736,7 @@ static ssize_t bonding_store_downdelay(struct device *d,
 	}
 

out:

drivers/net/bonding/bond_sysfs.c +1 -0
+	rtnl_unlock();
	return ret;
}

static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,

drivers/net/bonding/bond_sysfs.c +2 -0
@@ -756,6 +759,8 @@ static ssize_t bonding_store_updelay(struct device *d,
 	int new_value, ret = count;
 	struct bonding *bond = to_bond(d);
 
+	if (!rtnl_trylock())
+		return restart_syscall();
 	if (!(bond->params.miimon)) {
 		pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
 		       bond->dev->name);
@@ -789,6 +794,7 @@ static ssize_t bonding_store_updelay(struct device *d,
 	}
 

out:

drivers/net/bonding/bond_sysfs.c +1 -0
+	rtnl_unlock();
	return ret;
}

static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,

Signature · 1 line
1.8.1.4
maintainer
#7 lore raw
In reply to Nikolay Aleksandrov

From: Nikolay Aleksandrov <[email protected]>
Date: Wed, 13 Nov 2013 17:07:46 +0100

Quoted 11 lines

This patch fixes two race conditions between bond_store_updelay/downdelay and bond_store_miimon which could lead to division by zero as miimon can be set to 0 while either updelay/downdelay are being set and thus miss the zero check in the beginning, the zero div happens because updelay/downdelay are stored as new_value / bond->params.miimon. Use rtnl to synchronize with miimon setting.

CC Jay Vosburgh <[email protected]>
CC Andy Gospodarek <[email protected]>
CC Veaceslav Falico <[email protected]>

Applied and queued up for -stable, thanks.