Commit c7d52737 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

net-sysfs: use dev_addr_sem to remove races in address_show()

Using dev_base_lock is not preventing from reading garbage.

Use dev_addr_sem instead.

v4: place dev_addr_sem extern in net/core/dev.h (Jakub Kicinski)
 Link: https://lore.kernel.org/netdev/20240212175845.10f6680a@kernel.org/Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 12692e3d
...@@ -8993,7 +8993,7 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, ...@@ -8993,7 +8993,7 @@ int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
} }
EXPORT_SYMBOL(dev_set_mac_address); EXPORT_SYMBOL(dev_set_mac_address);
static DECLARE_RWSEM(dev_addr_sem); DECLARE_RWSEM(dev_addr_sem);
int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa, int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#define _NET_CORE_DEV_H #define _NET_CORE_DEV_H
#include <linux/types.h> #include <linux/types.h>
#include <linux/rwsem.h>
struct net; struct net;
struct net_device; struct net_device;
...@@ -46,6 +47,8 @@ extern int weight_p; ...@@ -46,6 +47,8 @@ extern int weight_p;
extern int dev_weight_rx_bias; extern int dev_weight_rx_bias;
extern int dev_weight_tx_bias; extern int dev_weight_tx_bias;
extern struct rw_semaphore dev_addr_sem;
/* rtnl helpers */ /* rtnl helpers */
extern struct list_head net_todo_list; extern struct list_head net_todo_list;
void netdev_run_todo(void); void netdev_run_todo(void);
......
...@@ -142,17 +142,21 @@ static ssize_t name_assign_type_show(struct device *dev, ...@@ -142,17 +142,21 @@ static ssize_t name_assign_type_show(struct device *dev,
} }
static DEVICE_ATTR_RO(name_assign_type); static DEVICE_ATTR_RO(name_assign_type);
/* use same locking rules as GIFHWADDR ioctl's */ /* use same locking rules as GIFHWADDR ioctl's (dev_get_mac_address()) */
static ssize_t address_show(struct device *dev, struct device_attribute *attr, static ssize_t address_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct net_device *ndev = to_net_dev(dev); struct net_device *ndev = to_net_dev(dev);
ssize_t ret = -EINVAL; ssize_t ret = -EINVAL;
read_lock(&dev_base_lock); down_read(&dev_addr_sem);
rcu_read_lock();
if (dev_isalive(ndev)) if (dev_isalive(ndev))
ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len); ret = sysfs_format_mac(buf, ndev->dev_addr, ndev->addr_len);
read_unlock(&dev_base_lock); rcu_read_unlock();
up_read(&dev_addr_sem);
return ret; return ret;
} }
static DEVICE_ATTR_RO(address); static DEVICE_ATTR_RO(address);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment