Sysfs and SIOCGIFHWADDR hold dev_addr_sem for reading while copying the primary device address. Drivers that publish a new address in response to firmware can bypass the write-side lock taken by the userspace MAC setter. Add a helper for callers that already hold RTNL. Take the semaphore around dev_addr_set() so the primary address and its list bookkeeping are updated through the normal path. Require callers to enter before taking the device instance lock, and leave notification and hardware programming to the caller. Signed-off-by: Satish Kharat --- include/linux/netdevice.h | 2 ++ net/core/dev_api.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index b5427b953229..5f3f761e821f 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -5139,6 +5139,8 @@ static inline void dev_addr_set(struct net_device *dev, const u8 *addr) __dev_addr_set(dev, addr, dev->addr_len); } +void dev_addr_set_sync(struct net_device *dev, const u8 *addr); + int dev_addr_add(struct net_device *dev, const unsigned char *addr, unsigned char addr_type); int dev_addr_del(struct net_device *dev, const unsigned char *addr, diff --git a/net/core/dev_api.c b/net/core/dev_api.c index 437947dd08ed..081e9cb063b6 100644 --- a/net/core/dev_api.c +++ b/net/core/dev_api.c @@ -85,6 +85,27 @@ void dev_set_group(struct net_device *dev, int new_group) netdev_unlock_ops(dev); } +/** + * dev_addr_set_sync() - publish a driver-selected device address + * @dev: device whose address is being updated + * @addr: new address, with dev->addr_len bytes + * + * Serialize the update with sysfs and SIOCGIFHWADDR readers. + * The caller holds RTNL and must hold neither dev_addr_sem nor the + * device instance lock. + */ +void dev_addr_set_sync(struct net_device *dev, const u8 *addr) +{ + ASSERT_RTNL(); + lockdep_assert_not_held(&dev_addr_sem); + lockdep_assert_not_held(&dev->lock); + + down_write(&dev_addr_sem); + dev_addr_set(dev, addr); + up_write(&dev_addr_sem); +} +EXPORT_SYMBOL_GPL(dev_addr_set_sync); + int dev_set_mac_address_user(struct net_device *dev, struct sockaddr_storage *ss, struct netlink_ext_ack *extack) -- 2.43.0