xsk: fix init race causing NPD/UAF in xsk_create()
Subsystems (3)
PATCH 1/1 xsk: fix init race causing NPD/UAF in xsk_create()
xsk_init() previously registered the PF_XDP socket family before the per-net subsystem and other prerequisites (netdevice notifier, caches) were fully initialized.
This exposed .create = xsk_create() to user space while per-netns state (net->xdp.lock/list) was still uninitialized. A task with CAP_NET_RAW could trigger this during boot/module load by calling socket(PF_XDP, SOCK_RAW, 0) concurrently with xsk_init(), leading to a NULL pointer dereference or use-after-free in the list manipulation.
To fix this, move sock_register() to the end of the initialization sequence, ensuring that all required kernel structures are ready before exposing the AF_XDP interface to userspace.
Accordingly, reorder the error unwind path to ensure proper cleanup in reverse order of initialization. Also, explicitly add kmem_cache_destroy() in the error path to prevent leaking xsk_tx_generic_cache if the registration fails.
--- net/xdp/xsk.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
net/xdp/xsk.c +8 -6
@@ -23,6 +23,7 @@#include <linux/netdevice.h> #include <linux/rculist.h> #include <linux/vmalloc.h> +#include <linux/slab.h> #include <net/xdp_sock_drv.h> #include <net/busy_poll.h> #include <net/netdev_lock.h>
@@ -1922,10 +1923,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 +1939,21 @@ static int __init xsk_init(void) goto out_unreg_notif; } + err =sock_register (&xsk_family_ops); +if (err) + goto out_proto; +return 0 ; +out_proto: +proto_unregister (&xsk_proto); +kmem_cache_destroy (xsk_tx_generic_cache); 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:return err; }
Signature · 1 line
2.34.1
[email protected]
AI reviewWith sock_register() now at the end of xsk_init(), the out_sk label is reached via goto from register_pernet_subsys(), register_netdevice_notifier(), or kmem_cache_create() failures - all of which…
Quoted 2 lines
net/xdp/xsk.c +0
-0
[ ... ]
Quoted 11 lines
net/xdp/xsk.c +0
-4@@ -1922,10 +1923,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 9 lines
net/xdp/xsk.c +4
-0@@ -1942,16 +1939,21 @@ 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 1956
Quoted 10 lines
net/xdp/xsk.c +3
-0+out_proto: +proto_unregister (&xsk_proto); +kmem_cache_destroy (xsk_tx_generic_cache); out_unreg_notif:unregister_netdevice_notifier (&xsk_netdev_notifier); out_pernet:unregister_pernet_subsys (&xsk_net_ops); out_sk:sock_unregister (PF_XDP );
^^^^^^^^^^^^^^^^
With sock_register() now at the end of xsk_init(), the out_sk label is reached via goto from register_pernet_subsys(), register_netdevice_notifier(), or kmem_cache_create() failures - all of which occur before sock_register() is called.
Shouldn't out_sk be removed entirely, and the gotos redirected? For example, if register_pernet_subsys() fails, we goto out_sk and call sock_unregister() on a family that was never registered. The same applies to the other error paths that fall through to out_sk.
While sock_unregister() on an unregistered family appears to be benign (it just sets NULL to NULL), it will print "NET: Unregistered PF_XDP protocol family" to the kernel log even though it was never registered in the first place.
net/xdp/xsk.c static int __init xsk_init(void) · line 1959
Quoted 5 lines
net/xdp/xsk.c +0
-2-out_proto: -proto_unregister (&xsk_proto); 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/20848468294