bonding: fix missed rcu protection
Subsystems (3)
PATCH v2 1/1 bonding: fix missed rcu protection
When removing the rcu_read_lock in bond_ethtool_get_ts_info() as discussed [1], I didn't notice it could be called via setsockopt, which doesn't hold rcu lock, as syzbot pointed:
Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 bond_option_active_slave_get_rcu include/net/bonding.h:353 [inline] bond_ethtool_get_ts_info+0x32c/0x3a0 drivers/net/bonding/bond_main.c:5595 __ethtool_get_ts_info+0x173/0x240 net/ethtool/common.c:554 ethtool_get_phc_vclocks+0x99/0x110 net/ethtool/common.c:568 sock_timestamping_bind_phc net/core/sock.c:869 [inline] sock_set_timestamping+0x3a3/0x7e0 net/core/sock.c:916 sock_setsockopt+0x543/0x2ec0 net/core/sock.c:1221 __sys_setsockopt+0x55e/0x6a0 net/socket.c:2223 __do_sys_setsockopt net/socket.c:2238 [inline] __se_sys_setsockopt net/socket.c:2235 [inline] __x64_sys_setsockopt+0xba/0x150 net/socket.c:2235 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7f8902c8eb39
Fix it by adding rcu_read_lock and take a ref on the real_dev.
[1] https://lore.kernel.org/netdev/27565.1642742439@famine/
---
v2: add ref on the real_dev as Jakub and Paolo suggested.
--- drivers/net/bonding/bond_main.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/net/bonding/bond_main.c +14 -3
@@ -5591,24 +5591,35 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond);if (real_dev) { +dev_hold (real_dev); +rcu_read_unlock (); ops = real_dev->ethtool_ops; phydev = real_dev->phydev;if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); + ret =phy_ts_info (phydev, info); + goto out; }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); + ret = ops->get_ts_info (real_dev, info); + goto out; } + }else { +rcu_read_unlock (); } info->so_timestamping =SOF_TIMESTAMPING_RX_SOFTWARE |SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; +out: +if (real_dev) +dev_put (real_dev); +return ret; }static const struct ethtool_ops bond_ethtool_ops = {
Signature · 1 line
2.35.1
---
RESEND, list still didn't receive my last version
The diffstat is slightly larger but IMO a slightly more readable version. When I was reading v2 I found myself jumping around.
I only compile tested it, so YMMV.
If this amount of change is too much v2 from Hangbin looks correct to me.
drivers/net/bonding/bond_main.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-)
drivers/net/bonding/bond_main.c +20 -11
@@ -5591,23 +5591,32 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond); -if (real_dev) { - ops = real_dev->ethtool_ops; - phydev = real_dev->phydev; - -if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); - }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); - } - } +if (real_dev) +dev_hold (real_dev); +rcu_read_unlock (); + +if (!real_dev) + goto software; + ops = real_dev->ethtool_ops; + phydev = real_dev->phydev; + +if (phy_has_tsinfo (phydev)) + ret =phy_ts_info (phydev, info); +else if (ops->get_ts_info) + ret = ops->get_ts_info (real_dev, info); + +dev_put (real_dev); +return ret; + +software: info->so_timestamping =SOF_TIMESTAMPING_RX_SOFTWARE |SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; }
Signature · 1 line
2.27.0
Quoted 6 lines · Jonathan Toppins
Signed-off-by Jonathan Toppins <[email protected]>---
RESEND, list still didn't receive my last versionThe diffstat is slightly larger but IMO a slightly more readable version. When I was reading v2 I found myself jumping around.
Hi Jon,
Thanks for the commit. But..
Quoted 26 lines
drivers/net/bonding/bond_main.c +6
-10@@ -5591,23 +5591,32 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond); -if (real_dev) { - ops = real_dev->ethtool_ops; - phydev = real_dev->phydev; - -if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); - }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); - } - } +if (real_dev) +dev_hold (real_dev); +rcu_read_unlock (); +
drivers/net/bonding/bond_main.c static int bond_ethtool_get_ts_info(struct net_device *bond_dev, · line 5611
Quoted 10 lines
drivers/net/bonding/bond_main.c +9
-0+if (!real_dev) + goto software; + ops = real_dev->ethtool_ops; + phydev = real_dev->phydev; + +if (phy_has_tsinfo (phydev)) + ret =phy_ts_info (phydev, info); +else if (ops->get_ts_info) + ret = ops->get_ts_info (real_dev, info);
else {
dev_put(real_dev);
goto software;
}Here we need another check and goto software if !phy_has_tsinfo() and no ops->get_ts_info. With this change we also have 2 goto and dev_put().
drivers/net/bonding/bond_main.c static int bond_ethtool_get_ts_info(struct net_device *bond_dev, · line 5621
Quoted 11 lines
drivers/net/bonding/bond_main.c +5
-1+ +dev_put (real_dev); +return ret; + +software: info->so_timestamping =SOF_TIMESTAMPING_RX_SOFTWARE |SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; }
As Jakub remind, dev_hold() and dev_put() can take NULL now. So how about this new patch:
drivers/net/bonding/bond_main.c +12 -3
@@ -5591,16 +5591,23 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond); +dev_hold (real_dev); +rcu_read_unlock (); +if (real_dev) { ops = real_dev->ethtool_ops; phydev = real_dev->phydev;if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); + ret =phy_ts_info (phydev, info); + goto out; }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); + ret = ops->get_ts_info (real_dev, info); + goto out; } }
@@ -5608,7 +5615,9 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; +out: +dev_put (real_dev); +return ret; }
Thanks
Hangbin
Quoted 53 lines · Hangbin Liu
Quoted 6 lines · Jonathan Toppins
Signed-off-by Jonathan Toppins <[email protected]>---
RESEND, list still didn't receive my last versionThe diffstat is slightly larger but IMO a slightly more readable version. When I was reading v2 I found myself jumping around.
Hi Jon,
Thanks for the commit. But..
Quoted 26 lines
drivers/net/bonding/bond_main.c +6
-10@@ -5591,23 +5591,32 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond); -if (real_dev) { - ops = real_dev->ethtool_ops; - phydev = real_dev->phydev; - -if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); - }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); - } - } +if (real_dev) +dev_hold (real_dev); +rcu_read_unlock (); +drivers/net/bonding/bond_main.c static int bond_ethtool_get_ts_info(struct net_device *bond_dev, · line 5611
Quoted 10 lines
drivers/net/bonding/bond_main.c +9
-0+if (!real_dev) + goto software; + ops = real_dev->ethtool_ops; + phydev = real_dev->phydev; + +if (phy_has_tsinfo (phydev)) + ret =phy_ts_info (phydev, info); +else if (ops->get_ts_info) + ret = ops->get_ts_info (real_dev, info);else { dev_put(real_dev); goto software; }
Quoted 2 lines
Here we need another check and goto software if !phy_has_tsinfo() and no
ops->get_ts_info. With this change we also have 2 goto anddev_put().
Ah yes. I cannot think of a way to make this simpler. The patch below looks good.
Quoted 59 lines
drivers/net/bonding/bond_main.c static int bond_ethtool_get_ts_info(struct net_device *bond_dev, · line 5621
Quoted 12 lines
drivers/net/bonding/bond_main.c +5
-1+ +dev_put (real_dev); +return ret; + +software: info->so_timestamping =SOF_TIMESTAMPING_RX_SOFTWARE |SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; }As Jakub remind,
dev_hold()anddev_put()can take NULL now. So how about this new patch:drivers/net/bonding/bond_main.c +12
-3@@ -5591,16 +5591,23 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,const struct ethtool_ops *ops;struct net_device *real_dev;struct phy_device *phydev; +int ret =0 ; +rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond); +dev_hold (real_dev); +rcu_read_unlock (); +if (real_dev) { ops = real_dev->ethtool_ops; phydev = real_dev->phydev;if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); + ret =phy_ts_info (phydev, info); + goto out; }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); + ret = ops->get_ts_info (real_dev, info); + goto out; } }@@ -5608,7 +5615,9 @@ static int bond_ethtool_get_ts_info(struct net_device *bond_dev,SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; +out: +dev_put (real_dev); +return ret; }Thanks
Hangbin
Quoted 10 lines · Jonathan Toppins
Signed-off-by Jonathan Toppins <[email protected]>---
RESEND, list still didn't receive my last versionThe diffstat is slightly larger but IMO a slightly more readable version. When I was reading v2 I found myself jumping around.
I only compile tested it, so YMMV.If this amount of change is too much v2 from Hangbin looks correct to me.
Seems to be too big of a change for what the issue is, yes, sorry.
Jakub Kicinski
maintainerQuoted 19 lines · On Tue, 17 May 2022 16:23:12 +0800 Hangbin Liu wrote:
drivers/net/bonding/bond_main.c +9
-2+rcu_read_lock (); real_dev =bond_option_active_slave_get_rcu (bond);if (real_dev) { +dev_hold (real_dev); +rcu_read_unlock (); ops = real_dev->ethtool_ops; phydev = real_dev->phydev;if (phy_has_tsinfo (phydev)) { -return phy_ts_info (phydev, info); + ret =phy_ts_info (phydev, info); + goto out; }else if (ops->get_ts_info) { -return ops->get_ts_info (real_dev, info); + ret = ops->get_ts_info (real_dev, info); + goto out; } + }else { +rcu_read_unlock ();
drivers/net/bonding/bond_main.c static int bond_ethtool_get_ts_info(struct net_device *bond_dev, · line 5621
Quoted 10 lines
drivers/net/bonding/bond_main.c +3
-1} info->so_timestamping =SOF_TIMESTAMPING_RX_SOFTWARE |SOF_TIMESTAMPING_SOFTWARE ; info->phc_index =-1 ; -return 0 ; +out: +if (real_dev) +dev_put (real_dev);
dev_hold() and dev_put() can take NULL these days, for better or worse. I think the code simplification is worth making use of that, even tho it will make the backport slightly more tricky (perhaps make a not of this in the commit message).
Quoted 4 lines · On Tue, 17 May 2022 10:54:45 -0700 Jakub Kicinski <[email protected]> wrote:
dev_hold()anddev_put()can take NULL these days, for better or worse. I think the code simplification is worth making use of that, even tho it will make the backport slightly more tricky (perhaps make a not of this in the commit message).
Since that is so, would be worth having coccinelle script to cleanup existing code.
See scripts/coccinelle/ifnullfree.cocci for similar example.