Skip to contentSkip to conversation

psp: fix NULL genl_sock deref race with concurrent netns teardown

PATCH Applied Kiran Kella 2 participants 2 messages Jul 27 – Jul 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 psp: fix NULL genl_sock deref race with concurrent netns teardown

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.

Fixes 00c94ca2b99e ("psp: base PSP device support")
Fixes 06c2dce2d0f6 ("psp: add new netlink cmd for dev-assoc and dev-disassoc")

---
v4:
Fixes done based on feedback from Jakub Kicinski
- maybe_get_net() logic removed for assoc_dev_list
- Added RCU protection on main netns before invoking maybe_get_net()

v3:
Fixed typo in the commit log
https://lore.kernel.org/all/[email protected]/

v2:
get rid of the extra struct net *net, by doing (!maybe_get_net(assoc_net)) directly (as suggested by Daniel Zahka)
https://lore.kernel.org/all/[email protected]/

v1: https://lore.kernel.org/all/[email protected]/

 net/psp/psp_nl.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
net/psp/psp_nl.c +12 -5
index 9610d8c456ff..43b066353c65 100644
@@ -62,7 +62,14 @@ psp_nl_multicast_per_ns(struct psp_dev *psd, unsigned int group,
 	struct net *main_net;
 	struct sk_buff *ntf;
 
-	main_net = dev_net(psd->main_netdev);
+	/* device may be changing netns in parallel */
+	rcu_read_lock();
+	main_net = maybe_get_net(dev_net_rcu(psd->main_netdev));
+	rcu_read_unlock();
+
+	if (!main_net)
+		return;
+
 	xa_init(&sent_nets);
 
 	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,
 
 	/* Send to main device netns */
 	ntf = build_ntf(psd, main_net, ctx);
-	if (!ntf)
-		return;
-	genlmsg_multicast_netns(&psp_nl_family, main_net, ntf, 0, group,
-				GFP_KERNEL);
+	if (ntf)
+		genlmsg_multicast_netns(&psp_nl_family, main_net, ntf, 0, group,
+					GFP_KERNEL);
+	put_net(main_net);
 }
 
 static struct sk_buff *psp_nl_clone_ntf(struct psp_dev *psd, struct net *net,
Signature · 1 line
2.54.0
#1 lore raw
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Mon, 27 Jul 2026 03:16:28 -0700 you wrote:

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:

[...]

Here is the summary with links:
- [net,v4] psp: fix NULL genl_sock deref race with concurrent netns teardown
https://git.kernel.org/netdev/net/c/fc9c7ca5fcbf

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html