Skip to content

Commit d8d85ef

Browse files
Stanislav FomichevPaolo Abeni
authored andcommitted
af_packet: move notifier's packet_dev_mc out of rcu critical section
Syzkaller reports the following issue: BUG: sleeping function called from invalid context at kernel/locking/mutex.c:578 __mutex_lock+0x106/0xe80 kernel/locking/mutex.c:746 team_change_rx_flags+0x38/0x220 drivers/net/team/team_core.c:1781 dev_change_rx_flags net/core/dev.c:9145 [inline] __dev_set_promiscuity+0x3f8/0x590 net/core/dev.c:9189 netif_set_promiscuity+0x50/0xe0 net/core/dev.c:9201 dev_set_promiscuity+0x126/0x260 net/core/dev_api.c:286 packet_dev_mc net/packet/af_packet.c:3698 [inline] packet_dev_mclist_delete net/packet/af_packet.c:3722 [inline] packet_notifier+0x292/0xa60 net/packet/af_packet.c:4247 notifier_call_chain+0x1b3/0x3e0 kernel/notifier.c:85 call_netdevice_notifiers_extack net/core/dev.c:2214 [inline] call_netdevice_notifiers net/core/dev.c:2228 [inline] unregister_netdevice_many_notify+0x15d8/0x2330 net/core/dev.c:11972 rtnl_delete_link net/core/rtnetlink.c:3522 [inline] rtnl_dellink+0x488/0x710 net/core/rtnetlink.c:3564 rtnetlink_rcv_msg+0x7cf/0xb70 net/core/rtnetlink.c:6955 netlink_rcv_skb+0x219/0x490 net/netlink/af_netlink.c:2534 Calling `PACKET_ADD_MEMBERSHIP` on an ops-locked device can trigger the `NETDEV_UNREGISTER` notifier, which may require disabling promiscuous and/or allmulti mode. Both of these operations require acquiring the netdev instance lock. Move the call to `packet_dev_mc` outside of the RCU critical section. The `mclist` modifications (add, del, flush, unregister) are protected by the RTNL, not the RCU. The RCU only protects the `sklist` and its associated `sks`. The delayed operation on the `mclist` entry remains within the RTNL. Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=b191b5ccad8d7a986286 Fixes: ad7c7b2 ("net: hold netdev instance lock during sysfs operations") Signed-off-by: Stanislav Fomichev <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 3237423 commit d8d85ef

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

net/packet/af_packet.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,15 +3713,15 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
37133713
}
37143714

37153715
static void packet_dev_mclist_delete(struct net_device *dev,
3716-
struct packet_mclist **mlp)
3716+
struct packet_mclist **mlp,
3717+
struct list_head *list)
37173718
{
37183719
struct packet_mclist *ml;
37193720

37203721
while ((ml = *mlp) != NULL) {
37213722
if (ml->ifindex == dev->ifindex) {
3722-
packet_dev_mc(dev, ml, -1);
3723+
list_add(&ml->remove_list, list);
37233724
*mlp = ml->next;
3724-
kfree(ml);
37253725
} else
37263726
mlp = &ml->next;
37273727
}
@@ -3769,6 +3769,7 @@ static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
37693769
memcpy(i->addr, mreq->mr_address, i->alen);
37703770
memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
37713771
i->count = 1;
3772+
INIT_LIST_HEAD(&i->remove_list);
37723773
i->next = po->mclist;
37733774
po->mclist = i;
37743775
err = packet_dev_mc(dev, i, 1);
@@ -4233,9 +4234,11 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
42334234
static int packet_notifier(struct notifier_block *this,
42344235
unsigned long msg, void *ptr)
42354236
{
4236-
struct sock *sk;
42374237
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
42384238
struct net *net = dev_net(dev);
4239+
struct packet_mclist *ml, *tmp;
4240+
LIST_HEAD(mclist);
4241+
struct sock *sk;
42394242

42404243
rcu_read_lock();
42414244
sk_for_each_rcu(sk, &net->packet.sklist) {
@@ -4244,7 +4247,8 @@ static int packet_notifier(struct notifier_block *this,
42444247
switch (msg) {
42454248
case NETDEV_UNREGISTER:
42464249
if (po->mclist)
4247-
packet_dev_mclist_delete(dev, &po->mclist);
4250+
packet_dev_mclist_delete(dev, &po->mclist,
4251+
&mclist);
42484252
fallthrough;
42494253

42504254
case NETDEV_DOWN:
@@ -4277,6 +4281,13 @@ static int packet_notifier(struct notifier_block *this,
42774281
}
42784282
}
42794283
rcu_read_unlock();
4284+
4285+
/* packet_dev_mc might grab instance locks so can't run under rcu */
4286+
list_for_each_entry_safe(ml, tmp, &mclist, remove_list) {
4287+
packet_dev_mc(dev, ml, -1);
4288+
kfree(ml);
4289+
}
4290+
42804291
return NOTIFY_DONE;
42814292
}
42824293

net/packet/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct packet_mclist {
1111
unsigned short type;
1212
unsigned short alen;
1313
unsigned char addr[MAX_ADDR_LEN];
14+
struct list_head remove_list;
1415
};
1516

1617
/* kbdq - kernel block descriptor queue */

0 commit comments

Comments
 (0)