net: pin protocol module before inet socket allocation
Subsystems (3)
PATCH v4 1/1 net: pin protocol module before inet socket allocation
inet_create() and inet6_create() look up the protocol under RCU, then drop RCU before using the resulting proto. A concurrent module unload can unregister the protosw, wait for the RCU readers, and destroy the proto's slab before sk_alloc() uses it. sk_alloc() may sleep, so the RCU read-side critical section cannot simply be extended across the allocation.
The reported failure occurred during concurrent L2TP socket creation and module unload:
Oops: general protection fault, probably for non-canonical address
0xffff1108974182a0
KASAN: maybe wild-memory-access in range
[0xfff8a844ba0c1500-0xfff8a844ba0c1507]
RIP: kmem_cache_alloc_noprof+0x63/0x370
Call Trace:
sk_prot_alloc+0x74/0x2c0
sk_alloc+0x2b/0x6c0
inet_create+0x2cd/0xd40
__sock_create+0x1c3/0x430
__sys_socket+0x116/0x1d0Cache the proto and its owner under RCU and acquire a temporary module reference before dropping RCU. This prevents normal module unload while the proto is in use. sk_prot_alloc() takes the socket's own reference; drop the temporary reference on every exit after allocation. Use the cached proto for the IPv6 backlog callback as well.
Use try_module_get() with its existing initialization semantics. SCTP creates its kernel control socket during module initialization, so rejecting MODULE_STATE_COMING would prevent modular SCTP from loading. Failed module initialization can still unwind published protocols despite module references; that separate publication/unwind lifetime issue is not fixed here.
---
Changes in v4:
- Remove the MODULE_STATE_COMING rejection. SCTP creates its kernel control socket from module init and must be allowed to use the published protosw.
- Keep the owner cached under RCU and use the cached proto for IPv6 backlog setup, retaining those v3 changes.
- Scope the module-reference guarantee to normal module unload. The earlier review's initialization-failure unwind concern remains a separate issue; rejecting all initializing modules is not a compatible solution.
- Rebase onto net/main at a7bfaba4823e.
v3: https://lore.kernel.org/r/[email protected]/
Review: https://lore.kernel.org/r/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/
net/ipv4/af_inet.c | 14 +++++++++++--- net/ipv6/af_inet6.c | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-)
net/ipv4/af_inet.c +11 -3
@@ -263,6 +263,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,struct inet_protosw *answer;struct inet_sock *inet;struct proto *answer_prot; +struct module *answer_owner;unsigned char answer_flags;int try_loading_module =0 ;int err;
@@ -322,9 +323,14 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, !ns_capable(net->user_ns, CAP_NET_RAW)) goto out_rcu_unlock; - sock->ops = answer->ops; answer_prot = answer->prot; + answer_owner = answer_prot->owner; answer_flags = answer->flags; +if (!try_module_get (answer_owner)) { + err = -EPROTONOSUPPORT ; + goto out_rcu_unlock; + } + sock->ops = answer->ops;rcu_read_unlock ();WARN_ON (!answer_prot->slab);
@@ -332,7 +338,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, err = -ENOMEM ; sk =sk_alloc (net,PF_INET ,GFP_KERNEL , answer_prot, kern);if (!sk) - goto out; + goto out_module_put; err =0 ;if (INET_PROTOSW_REUSE & answer_flags)
@@ -398,6 +404,8 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,if (err) goto out_sk_release; } +out_module_put: +module_put (answer_owner); out:return err; out_rcu_unlock:
@@ -406,7 +414,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, out_sk_release:sk_common_release (sk); sock->sk =NULL ; - goto out; + goto out_module_put; }
net/ipv6/af_inet6.c +12 -4
@@ -110,6 +110,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,struct sock *sk;struct inet_protosw *answer;struct proto *answer_prot; +struct module *answer_owner;unsigned char answer_flags;int try_loading_module =0 ;int err;
@@ -167,9 +168,14 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, !ns_capable(net->user_ns, CAP_NET_RAW)) goto out_rcu_unlock; - sock->ops = answer->ops; answer_prot = answer->prot; + answer_owner = answer_prot->owner; answer_flags = answer->flags; +if (!try_module_get (answer_owner)) { + err = -EPROTONOSUPPORT ; + goto out_rcu_unlock; + } + sock->ops = answer->ops;rcu_read_unlock ();WARN_ON (!answer_prot->slab);
@@ -177,7 +183,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, err = -ENOBUFS ; sk =sk_alloc (net,PF_INET6 ,GFP_KERNEL , answer_prot, kern);if (!sk) - goto out; + goto out_module_put;sock_init_data (sock, sk);
@@ -201,7 +207,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, sk->sk_family =PF_INET6 ; sk->sk_protocol = protocol; - sk->sk_backlog_rcv = answer->prot->backlog_rcv; + sk->sk_backlog_rcv = answer_prot->backlog_rcv;inet_sk (sk)->pinet6 = np =inet6_sk_generic (sk); np->hop_limit =-1 ;
@@ -251,6 +257,8 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,if (err) goto out_sk_release; } +out_module_put: +module_put (answer_owner); out:return err; out_rcu_unlock:
@@ -259,7 +267,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, out_sk_release:sk_common_release (sk); sock->sk =NULL ; - goto out; + goto out_module_put; } int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
base-commit: a7bfaba4823e3c165bb2004c74eff7c096672bc7
Signature · 1 line
2.43.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Jakub Kicinski
maintainerQuoted 6 lines · On Fri, 02 Oct 2026 17:44:13 +0000 [email protected] wrote:
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
You have to answer the bot's question.
Quoted 10 lines · Jakub Kicinski
Quoted 6 lines · On Fri, 02 Oct 2026 17:44:13 +0000 [email protected] wrote:
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
You have to answer the bot's question.
An LLM-based scan of the networking source initially identified the issue, I then reproduced it with l2tp_ip by racing socket (AF_INET, SOCK_DGRAM, IPPROTO_L2TP) against module unload on a KASAN-enabled kernel on QEMU.
The reproduction used a temporary 50 ms delay between reading prot->slab and calling kmem_cache_alloc() to widen the reported window proto_unregister() destroyed the same cache before the allocation used it.
Best regards,
Chengfeng
From: Chengfeng Ye <[email protected]>
Date: Sat, 3 Oct 2026 01:38:44 +0800
Quoted 34 lines
inet_create()andinet6_create()look up the protocol under RCU, then drop RCU before using the resulting proto. A concurrent module unload can unregister the protosw, wait for the RCU readers, and destroy the proto's slab beforesk_alloc()uses it.sk_alloc()may sleep, so the RCU read-side critical section cannot simply be extended across the allocation.The reported failure occurred during concurrent L2TP socket creation and module unload:
Oops: general protection fault, probably for non-canonical address 0xffff1108974182a0 KASAN: maybe wild-memory-access in range [0xfff8a844ba0c1500-0xfff8a844ba0c1507] RIP: kmem_cache_alloc_noprof+0x63/0x370 Call Trace: sk_prot_alloc+0x74/0x2c0 sk_alloc+0x2b/0x6c0 inet_create+0x2cd/0xd40 __sock_create+0x1c3/0x430 __sys_socket+0x116/0x1d0Cache the proto and its owner under RCU and acquire a temporary module reference before dropping RCU. This prevents normal module unload while the proto is in use.
sk_prot_alloc()takes the socket's own reference; drop the temporary reference on every exit after allocation. Use the cached proto for the IPv6 backlog callback as well.Use
try_module_get()with its existing initialization semantics. SCTP creates its kernel control socket during module initialization, so rejectingMODULE_STATE_COMINGwould prevent modular SCTP from loading. Failed module initialization can still unwind published protocols despite module references; that separate publication/unwind lifetime issue is not fixed here.
Quoted 8 lines
Fixes a79af59efd20 ("[NET]: Fix module reference counts for loadable protocol modules")Assisted-by GPT-6-AstraSigned-off-by Chengfeng Ye <[email protected]>---
Changes in v4:
- Remove theMODULE_STATE_COMINGrejection. SCTP creates its kernel control socket from module init and must be allowed to use the published protosw.
You can filter the case by @kern.
sockets created from __init_net functions always have @kern=true otherwise we cannot tear down netns.
Quoted 139 lines
- Keep the owner cached under RCU and use the cached proto for IPv6 backlog setup, retaining those v3 changes.
- Scope the module-reference guarantee to normal module unload. The earlier review's initialization-failure unwind concern remains a separate issue; rejecting all initializing modules is not a compatible solution.
- Rebase onto net/main ata7bfaba4823e.v3: https://lore.kernel.org/r/[email protected]/
Review: https://lore.kernel.org/r/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/net/ipv4/af_inet.c | 14 +++++++++++--- net/ipv6/af_inet6.c | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-)net/ipv4/af_inet.c +11
-3@@ -263,6 +263,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,struct inet_protosw *answer;struct inet_sock *inet;struct proto *answer_prot; +struct module *answer_owner;unsigned char answer_flags;int try_loading_module =0 ;int err;@@ -322,9 +323,14 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, !ns_capable(net->user_ns, CAP_NET_RAW)) goto out_rcu_unlock; - sock->ops = answer->ops; answer_prot = answer->prot; + answer_owner = answer_prot->owner; answer_flags = answer->flags; +if (!try_module_get (answer_owner)) { + err = -EPROTONOSUPPORT ; + goto out_rcu_unlock; + } + sock->ops = answer->ops;rcu_read_unlock ();WARN_ON (!answer_prot->slab);@@ -332,7 +338,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, err = -ENOMEM ; sk =sk_alloc (net,PF_INET ,GFP_KERNEL , answer_prot, kern);if (!sk) - goto out; + goto out_module_put; err =0 ;if (INET_PROTOSW_REUSE & answer_flags)@@ -398,6 +404,8 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,if (err) goto out_sk_release; } +out_module_put: +module_put (answer_owner); out:return err; out_rcu_unlock:@@ -406,7 +414,7 @@ static int inet_create(struct net *net, struct socket *sock, int protocol, out_sk_release:sk_common_release (sk); sock->sk =NULL ; - goto out; + goto out_module_put; }net/ipv6/af_inet6.c +12
-4@@ -110,6 +110,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,struct sock *sk;struct inet_protosw *answer;struct proto *answer_prot; +struct module *answer_owner;unsigned char answer_flags;int try_loading_module =0 ;int err;@@ -167,9 +168,14 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, !ns_capable(net->user_ns, CAP_NET_RAW)) goto out_rcu_unlock; - sock->ops = answer->ops; answer_prot = answer->prot; + answer_owner = answer_prot->owner; answer_flags = answer->flags; +if (!try_module_get (answer_owner)) { + err = -EPROTONOSUPPORT ; + goto out_rcu_unlock; + } + sock->ops = answer->ops;rcu_read_unlock ();WARN_ON (!answer_prot->slab);@@ -177,7 +183,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, err = -ENOBUFS ; sk =sk_alloc (net,PF_INET6 ,GFP_KERNEL , answer_prot, kern);if (!sk) - goto out; + goto out_module_put;sock_init_data (sock, sk);@@ -201,7 +207,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, sk->sk_family =PF_INET6 ; sk->sk_protocol = protocol; - sk->sk_backlog_rcv = answer->prot->backlog_rcv; + sk->sk_backlog_rcv = answer_prot->backlog_rcv;inet_sk (sk)->pinet6 = np =inet6_sk_generic (sk); np->hop_limit =-1 ;@@ -251,6 +257,8 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,if (err) goto out_sk_release; } +out_module_put: +module_put (answer_owner); out:return err; out_rcu_unlock:@@ -259,7 +267,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, out_sk_release:sk_common_release (sk); sock->sk =NULL ; - goto out; + goto out_module_put; } int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,base-commit:
a7bfaba4823e3c165bb2004c74eff7c096672bc7Signature · 1 line
2.43.0
Hi Kuniyuki
Quoted 46 lines · Kuniyuki Iwashima
From: Chengfeng Ye <[email protected]>
Date: Sat, 3 Oct 2026 01:38:44 +0800Quoted 34 lines
inet_create()andinet6_create()look up the protocol under RCU, then drop RCU before using the resulting proto. A concurrent module unload can unregister the protosw, wait for the RCU readers, and destroy the proto's slab beforesk_alloc()uses it.sk_alloc()may sleep, so the RCU read-side critical section cannot simply be extended across the allocation.The reported failure occurred during concurrent L2TP socket creation and module unload:
Oops: general protection fault, probably for non-canonical address 0xffff1108974182a0 KASAN: maybe wild-memory-access in range [0xfff8a844ba0c1500-0xfff8a844ba0c1507] RIP: kmem_cache_alloc_noprof+0x63/0x370 Call Trace: sk_prot_alloc+0x74/0x2c0 sk_alloc+0x2b/0x6c0 inet_create+0x2cd/0xd40 __sock_create+0x1c3/0x430 __sys_socket+0x116/0x1d0Cache the proto and its owner under RCU and acquire a temporary module reference before dropping RCU. This prevents normal module unload while the proto is in use.
sk_prot_alloc()takes the socket's own reference; drop the temporary reference on every exit after allocation. Use the cached proto for the IPv6 backlog callback as well.Use
try_module_get()with its existing initialization semantics. SCTP creates its kernel control socket during module initialization, so rejectingMODULE_STATE_COMINGwould prevent modular SCTP from loading. Failed module initialization can still unwind published protocols despite module references; that separate publication/unwind lifetime issue is not fixed here.Quoted 8 lines
Fixes a79af59efd20 ("[NET]: Fix module reference counts for loadable protocol modules")Assisted-by GPT-6-AstraSigned-off-by Chengfeng Ye <[email protected]>---
Changes in v4:
- Remove theMODULE_STATE_COMINGrejection. SCTP creates its kernel control socket from module init and must be allowed to use the published protosw.
Quoted 5 lines
You can filter the case by @kern.
sockets created from
__init_netfunctions always have @kern=true otherwise we cannot tear down netns.
Thanks for the review and suggestion, v5 adds that @kern checking and adds back the STATE_COMING rejection in v3
https://lore.kernel.org/netdev/[email protected]/T/#u
Best regards,
Chengfeng