Readers of dev->mc use dev->addr_len to know how many bytes of ha->addr to print. This is safe under the address list lock. Drivers that change dev->addr_len (tun, bonding, team) flush dev->uc and dev->mc first, and the flush takes the same lock. A lockless RCU reader can still walk the old entries after the flush and see the new, larger dev->addr_len. Entries come from kmalloc(), and only addr_len bytes of ha->addr are written, so the reader would copy uninitialized slab bytes to user space. Store the length in each entry instead. Set it when an entry is created. Also set it when a snapshot entry is refilled from the cache, because a snapshot entry can be moved back onto the real list. The new field fits in an existing hole, so the struct does not grow. Calling synchronize_rcu() in the flush would also work, but it adds a grace period per device during batched unregister. No user of the field yet. It is used by a later patch. Signed-off-by: Yuyang Huang --- include/linux/netdevice.h | 1 + net/core/dev_addr_lists.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 694b6d29f680..50369a062fb1 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -244,6 +244,7 @@ struct netdev_hw_addr { #define NETDEV_HW_ADDR_T_UNICAST 3 #define NETDEV_HW_ADDR_T_MULTICAST 4 bool global_use; + unsigned char addr_len; int sync_cnt; int refcount; int synced; diff --git a/net/core/dev_addr_lists.c b/net/core/dev_addr_lists.c index 700d9247ac0c..849d63e51e2a 100644 --- a/net/core/dev_addr_lists.c +++ b/net/core/dev_addr_lists.c @@ -106,6 +106,7 @@ __hw_addr_create(const unsigned char *addr, int addr_len, if (!ha) return NULL; memcpy(ha->addr, addr, addr_len); + ha->addr_len = addr_len; ha->type = addr_type; ha->refcount = 1; ha->global_use = global; @@ -582,6 +583,7 @@ int __hw_addr_list_snapshot(struct netdev_hw_addr_list *snap, list_del(&entry->list); __hw_addr_count_dec(cache); memcpy(entry->addr, ha->addr, addr_len); + entry->addr_len = addr_len; entry->type = ha->type; entry->global_use = false; entry->synced = 0; -- 2.56.1.915.g69d5ec7298-goog ha->refcount and ha->global_use are changed in place, under the address list lock, on entries that readers can see. A later patch reads them without that lock. Use WRITE_ONCE() for these in-place writes. Writes to entries that are not yet published, or to private snapshot entries, stay plain. i40e writes ha->refcount directly in netdev_hw_addr_refcnt() without the address list lock. That is an existing driver issue and is left for a separate patch. No functional change. Signed-off-by: Yuyang Huang --- net/core/dev_addr_lists.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/net/core/dev_addr_lists.c b/net/core/dev_addr_lists.c index 849d63e51e2a..93765997c28d 100644 --- a/net/core/dev_addr_lists.c +++ b/net/core/dev_addr_lists.c @@ -148,7 +148,7 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list, if (ha->global_use) return 0; else - ha->global_use = true; + WRITE_ONCE(ha->global_use, true); } if (sync) { if (ha->synced && sync_count) @@ -156,7 +156,7 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list, else ha->synced++; } - ha->refcount++; + WRITE_ONCE(ha->refcount, ha->refcount + 1); return 0; } } @@ -193,12 +193,13 @@ static int __hw_addr_del_entry(struct netdev_hw_addr_list *list, return -ENOENT; if (global) - ha->global_use = false; + WRITE_ONCE(ha->global_use, false); if (sync) ha->synced--; - if (--ha->refcount) + WRITE_ONCE(ha->refcount, ha->refcount - 1); + if (ha->refcount) return 0; rb_erase(&ha->node, &list->tree); @@ -266,7 +267,7 @@ static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list, if (!err) { ha->sync_cnt++; - ha->refcount++; + WRITE_ONCE(ha->refcount, ha->refcount + 1); } return 0; @@ -390,7 +391,7 @@ int __hw_addr_sync_dev(struct netdev_hw_addr_list *list, return err; ha->sync_cnt++; - ha->refcount++; + WRITE_ONCE(ha->refcount, ha->refcount + 1); } return 0; @@ -434,7 +435,7 @@ int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list, if (unsync && unsync(dev, ha->addr, ref_cnt)) continue; - ha->refcount = (ref_cnt << 1) + 1; + WRITE_ONCE(ha->refcount, (ref_cnt << 1) + 1); ha->sync_cnt = ref_cnt; __hw_addr_del_entry(list, ha, false, false); } @@ -450,7 +451,7 @@ int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list, if (err) return err; - ha->refcount = ref_cnt << 1; + WRITE_ONCE(ha->refcount, ref_cnt << 1); ha->sync_cnt = ref_cnt; } @@ -487,7 +488,7 @@ void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list, if (unsync && unsync(dev, ha->addr, ha->sync_cnt)) continue; - ha->refcount -= ha->sync_cnt - 1; + WRITE_ONCE(ha->refcount, ha->refcount - (ha->sync_cnt - 1)); ha->sync_cnt = 0; __hw_addr_del_entry(list, ha, false, false); } @@ -663,7 +664,7 @@ void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list, } real_ha->sync_cnt += delta; - real_ha->refcount += delta; + WRITE_ONCE(real_ha->refcount, real_ha->refcount + delta); if (!real_ha->refcount) { rb_erase(&real_ha->node, &real_list->tree); list_del_rcu(&real_ha->list); -- 2.56.1.915.g69d5ec7298-goog The AF_PACKET RTM_GETMULTICAST dump and /proc/net/dev_mcast both run under rcu_read_lock() already. Still, they take the address list spinlock with BH disabled while they format every entry of a device. Both can be triggered by unprivileged users. Entries are already added and freed in an RCU safe way, so walk dev->mc with list_for_each_entry_rcu() and drop the lock. Read refcount and global_use with READ_ONCE(), and use ha->addr_len for the address length. An entry whose refcount reached 0 is about to be unlinked. Skip it, so we never report 0 users. In the dump, the skip comes before the resume index check, so the index only counts live entries. One round can now see entries that never existed together at the same time. It can also see a refcount in the middle of an update, e.g. from __hw_addr_ref_sync_dev(). Changes between rounds are still flagged with NLM_F_DUMP_INTR through dev_mc_genid. The output format does not change. Signed-off-by: Yuyang Huang --- net/core/dev_addr_lists.c | 24 +++++++++++++++--------- net/core/net-procfs.c | 13 ++++++++----- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/net/core/dev_addr_lists.c b/net/core/dev_addr_lists.c index 93765997c28d..2861d9795d44 100644 --- a/net/core/dev_addr_lists.c +++ b/net/core/dev_addr_lists.c @@ -1229,8 +1229,9 @@ void dev_mc_init(struct net_device *dev) EXPORT_SYMBOL(dev_mc_init); static int dev_mc_fill_addr(struct sk_buff *skb, const struct net_device *dev, - const struct netdev_hw_addr *ha, u32 portid, - u32 seq, unsigned int flags, int netnsid) + const struct netdev_hw_addr *ha, int refcount, + u32 portid, u32 seq, unsigned int flags, + int netnsid) { struct ifaddrmsg *ifm; struct nlmsghdr *nlh; @@ -1249,9 +1250,10 @@ static int dev_mc_fill_addr(struct sk_buff *skb, const struct net_device *dev, if ((netnsid >= 0 && nla_put_s32(skb, IFA_TARGET_NETNSID, netnsid)) || - nla_put(skb, IFA_MULTICAST, dev->addr_len, ha->addr) || - nla_put_u32(skb, IFA_MC_USERS, ha->refcount) || - nla_put_u32(skb, IFA_FLAGS, ha->global_use ? IFA_F_GLOBAL : 0)) { + nla_put(skb, IFA_MULTICAST, ha->addr_len, ha->addr) || + nla_put_u32(skb, IFA_MC_USERS, refcount) || + nla_put_u32(skb, IFA_FLAGS, + READ_ONCE(ha->global_use) ? IFA_F_GLOBAL : 0)) { nlmsg_cancel(skb, nlh); return -EMSGSIZE; } @@ -1280,21 +1282,25 @@ static int dev_mc_dump_dev(struct net_device *dev, struct sk_buff *skb, { struct netdev_hw_addr *ha; int addr_idx = 0; + int refcount; int err = 0; - netif_addr_lock_bh(dev); - netdev_for_each_mc_addr(ha, dev) { + list_for_each_entry_rcu(ha, &dev->mc.list, list) { + /* The last user is gone, the entry is about to be unlinked */ + refcount = READ_ONCE(ha->refcount); + if (refcount <= 0) + continue; if (addr_idx < *s_addr_idx) { addr_idx++; continue; } - err = dev_mc_fill_addr(skb, dev, ha, NETLINK_CB(cb->skb).portid, + err = dev_mc_fill_addr(skb, dev, ha, refcount, + NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, flags, netnsid); if (err < 0) break; addr_idx++; } - netif_addr_unlock_bh(dev); *s_addr_idx = err < 0 ? addr_idx : 0; diff --git a/net/core/net-procfs.c b/net/core/net-procfs.c index 7dbfa6109f0b..915beefa2a08 100644 --- a/net/core/net-procfs.c +++ b/net/core/net-procfs.c @@ -368,18 +368,21 @@ static int dev_mc_seq_show(struct seq_file *seq, void *v) { struct netdev_hw_addr *ha; struct net_device *dev = v; + int refcount; if (v == SEQ_START_TOKEN) return 0; - netif_addr_lock_bh(dev); - netdev_for_each_mc_addr(ha, dev) { + /* dev_seq_start() holds rcu_read_lock() */ + list_for_each_entry_rcu(ha, &dev->mc.list, list) { + refcount = READ_ONCE(ha->refcount); + if (refcount <= 0) + continue; seq_printf(seq, "%-4d %-15s %-5d %-5d %*phN\n", dev->ifindex, dev->name, - ha->refcount, ha->global_use, - (int)dev->addr_len, ha->addr); + refcount, READ_ONCE(ha->global_use), + (int)ha->addr_len, ha->addr); } - netif_addr_unlock_bh(dev); return 0; } -- 2.56.1.915.g69d5ec7298-goog