Skip to contentSkip to conversation

xsk: fix init race causing NPD/UAF in xsk_create()

PATCH New Kery Qi 2 participants 2 messages Jan 8

Subsystems (3)
lore
Share & tools
lore

Kery Qi

author
#0 lore raw

PATCH 1/1 xsk: fix init race causing NPD/UAF in xsk_create()

xsk_init() registers the PF_XDP socket family before xsk_net_ops. This exposes .create = xsk_create() to user space while per-netns state (net->xdp.lock/list) is still uninitialized.

A task with CAP_NET_RAW can trigger this during boot/module load by calling socket(PF_XDP/AF_XDP, SOCK_RAW, 0) concurrently with xsk_init(), leading to NULL deref, list/lock corruption or use-after-free.

Register the pernet subsystem (and prerequisites) first and only then sock_register() PF_XDP. Update the error unwind to match the new order.

Similar to CVE-2024-26793 (GTP init ordering bug).

Fixes c0c77d8fb787 ("xsk: add user memory registration support sockopt")
---
 net/xdp/xsk.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
net/xdp/xsk.c +6 -6
index f093c3453f64..63b48e4b8b65 100644
@@ -1922,10 +1922,6 @@ static int __init xsk_init(void)
 	if (err)
 		goto out;
 
-	err = sock_register(&xsk_family_ops);
-	if (err)
-		goto out_proto;
-
 	err = register_pernet_subsys(&xsk_net_ops);
 	if (err)
 		goto out_sk;
@@ -1942,16 +1938,20 @@ static int __init xsk_init(void)
 		goto out_unreg_notif;
 	}
 
+	err = sock_register(&xsk_family_ops);
+	if (err)
+		goto out_proto;
+
 	return 0;
 
 out_unreg_notif:
 	unregister_netdevice_notifier(&xsk_netdev_notifier);
 out_pernet:
 	unregister_pernet_subsys(&xsk_net_ops);
-out_sk:
-	sock_unregister(PF_XDP);
 out_proto:
 	proto_unregister(&xsk_proto);
+out_sk:
+	sock_unregister(PF_XDP);
 out:
 	return err;
 }
Signature · 1 line
2.34.1
#1 lore raw
The error unwind path looks incorrect. If register_pernet_subsys() fails, the code jumps to out_sk which now calls sock_unregister(PF_XDP). However, at that point sock_register() has not been called…
Quoted 15 lines
net/xdp/xsk.c +0 -4
index f093c3453f64..63b48e4b8b65 100644
@@ -1922,10 +1922,6 @@ static int __init xsk_init(void)
 	if (err)
 		goto out;
 
-	err = sock_register(&xsk_family_ops);
-	if (err)
-		goto out_proto;
-
 	err = register_pernet_subsys(&xsk_net_ops);
 	if (err)
 		goto out_sk;

[ ... ]

Quoted 10 lines
net/xdp/xsk.c +4 -0
@@ -1942,16 +1938,20 @@ static int __init xsk_init(void)
 		goto out_unreg_notif;
 	}
 
+	err = sock_register(&xsk_family_ops);
+	if (err)
+		goto out_proto;
+
 	return 0;
 

net/xdp/xsk.c static int __init xsk_init(void) · line 1954

Quoted 10 lines
net/xdp/xsk.c +2 -2
 out_unreg_notif:
 	unregister_netdevice_notifier(&xsk_netdev_notifier);
 out_pernet:
 	unregister_pernet_subsys(&xsk_net_ops);
-out_sk:
-	sock_unregister(PF_XDP);
 out_proto:
 	proto_unregister(&xsk_proto);
+out_sk:
+	sock_unregister(PF_XDP);

The error unwind path looks incorrect. If register_pernet_subsys() fails, the code jumps to out_sk which now calls sock_unregister(PF_XDP). However, at that point sock_register() has not been called yet since it was moved to after kmem_cache_create().

Looking at the execution path:

    proto_register() succeeds
    register_pernet_subsys() fails
    goto out_sk
    -> out_sk: sock_unregister(PF_XDP)  // sock_register() was never called

The same issue affects register_netdevice_notifier() and kmem_cache_create() failures - they all eventually fall through to out_sk and call sock_unregister() before sock_register() was ever called.

Should out_sk be placed before out_proto in the error unwind section, or should the goto targets be updated? The original code had the error unwind labels in the correct order relative to the registration sequence.

net/xdp/xsk.c static int __init xsk_init(void) · line 1957

Quoted 3 lines
net/xdp/xsk.c +0 -0
 out:
 	return err;
 }

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20814824025