The race occurs between network namespace removal and PSP device unregistration. When a netns is deleted while a PSP device associated with that netns is concurrently being removed, psp_dev_unregister() triggers psp_nl_notify_dev() to send a device change notification. Concurrently, cleanup_net() running in the netns workqueue calls genl_pernet_exit(), which sets net->genl_sock to NULL. If genl_pernet_exit() wins the race, two sites in psp_nl_multicast_per_ns() then dereference the NULL socket and crash: CPU 0 (netns teardown) CPU 1 (PSP device unregister) ====================== ============================= cleanup_net [workqueue] genl_pernet_exit() psp_dev_unregister() net->genl_sock = NULL psp_nl_notify_dev() psp_nl_multicast_per_ns() build_ntf() -> netlink_has_listeners(NULL) /* crash */ genlmsg_multicast_netns() -> nlmsg_multicast_filtered(NULL) /* crash */ Fix by replacing the bare dev_net() calls with maybe_get_net(). maybe_get_net() returns NULL if the namespace is already dying. Holding the reference ensures genl_sock remains valid across both the build_ntf() and genlmsg_multicast_netns() calls.
Unified · Split
| @@ -62,7 +62,14 @@ psp_nl_multicast_per_ns(struct psp_dev *psd, unsigned int group, · 61 unmodified lines | |||
| 62 | struct net *main_net; | 62 | struct net *main_net; |
| 63 | struct sk_buff *ntf; | 63 | struct sk_buff *ntf; |
| 64 | 64 | ||
| 65 | main_net = dev_net(psd->main_netdev); | 65 | /* device may be changing netns in parallel */ |
| 66 | rcu_read_lock(); | ||
| 67 | main_net = maybe_get_net(dev_net_rcu(psd->main_netdev)); | ||
| 68 | rcu_read_unlock(); | ||
| 69 | |||
| 70 | if (!main_net) | ||
| 71 | return; | ||
| 72 | |||
| 66 | xa_init(&sent_nets); | 73 | xa_init(&sent_nets); |
| 67 | 74 | ||
| 68 | list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { | 75 | list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { |
| @@ -88,10 +95,10 @@ psp_nl_multicast_per_ns(struct psp_dev *psd, unsigned int group, · 19 unmodified lines | |||
| 88 | 95 | ||
| 89 | /* Send to main device netns */ | 96 | /* Send to main device netns */ |
| 90 | ntf = build_ntf(psd, main_net, ctx); | 97 | ntf = build_ntf(psd, main_net, ctx); |
| 91 | if (!ntf) | 98 | if (ntf) |
| 92 | return; | 99 | genlmsg_multicast_netns(&psp_nl_family, main_net, ntf, 0, group, |
| 93 | genlmsg_multicast_netns(&psp_nl_family, main_net, ntf, 0, group, | 100 | GFP_KERNEL); |
| 94 | GFP_KERNEL); | 101 | put_net(main_net); |
| 95 | } | 102 | } |
| 96 | 103 | ||
| 97 | static struct sk_buff *psp_nl_clone_ntf(struct psp_dev *psd, struct net *net, | 104 | static struct sk_buff *psp_nl_clone_ntf(struct psp_dev *psd, struct net *net, |