* [PATCH net] net: fix a resource leak in copy_net_ns() error handling path
@ 2026-08-22 8:18 Tetsuo Handa
2026-08-25 10:40 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 2+ messages in thread
From: Tetsuo Handa @ 2026-08-22 8:18 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Network Development, LKML
Currently, preinit_net() does two things:
(1) call ns_common_init() which might fail
(2) initialize resources which does not fail
However, preinit_net() is returning early when (1) fails, and copy_net_ns()
is jumping to the dec_ucounts: label. As a result, resources allocated by
net_alloc() are leaking. We need to call key_remove_domain() and
net_passive_dec() in order to release resources allocated by net_alloc().
We cannot simply jump to the put_userns: label when preinit_net() failed,
for (2) is not yet done. But we can reorder (1) and (2), for there is no
dependency between (1) and (2). Therefore, this patch decouples (1) from
preinit_net() and changes preinit_net() back to a void function, and calls
ns_common_init() after preinit_net() succeeded. Then, we can jump to
immediately after ns_common_free() of the put_userns: label.
Reported-by: sashiko (no mail address)
Closes: https://sashiko.dev/#/patchset/af7dabf3-d0d7-46dc-a878-e1715b3c9ac6%40I-love.SAKURA.ne.jp
Fixes: 08027f6b790b ("net: use ns_common_init()")
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
net/core/net_namespace.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 9166f467293e..da5f881fbd3b 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -400,14 +400,8 @@ static __net_init void preinit_net_sysctl(struct net *net)
}
/* init code that must occur even if setup_net() is not called. */
-static __net_init int preinit_net(struct net *net, struct user_namespace *user_ns)
+static __net_init void preinit_net(struct net *net, struct user_namespace *user_ns)
{
- int ret;
-
- ret = ns_common_init(net);
- if (ret)
- return ret;
-
refcount_set(&net->passive, 1);
ref_tracker_dir_init(&net->refcnt_tracker, 128, "net_refcnt");
ref_tracker_dir_init(&net->notrefcnt_tracker, 128, "net_notrefcnt");
@@ -431,7 +425,6 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
INIT_LIST_HEAD(&net->ptype_all);
INIT_LIST_HEAD(&net->ptype_specific);
preinit_net_sysctl(net);
- return 0;
}
/*
@@ -574,12 +567,14 @@ struct net *copy_net_ns(u64 flags,
goto dec_ucounts;
}
- rv = preinit_net(net, user_ns);
- if (rv < 0)
- goto dec_ucounts;
+ preinit_net(net, user_ns);
net->ucounts = ucounts;
get_user_ns(user_ns);
+ rv = ns_common_init(net);
+ if (rv)
+ goto put_userns_no_common;
+
rv = down_read_killable(&pernet_ops_rwsem);
if (rv < 0)
goto put_userns;
@@ -591,6 +586,7 @@ struct net *copy_net_ns(u64 flags,
if (rv < 0) {
put_userns:
ns_common_free(net);
+put_userns_no_common:
#ifdef CONFIG_KEYS
key_remove_domain(net->key_domain);
#endif
@@ -1293,7 +1289,8 @@ void __init net_ns_init(void)
* This currently cannot fail as the initial network namespace
* has a static inode number.
*/
- if (preinit_net(&init_net, &init_user_ns))
+ preinit_net(&init_net, &init_user_ns);
+ if (ns_common_init(&init_net))
panic("Could not preinitialize the initial network namespace");
down_write(&pernet_ops_rwsem);
--
2.52.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net] net: fix a resource leak in copy_net_ns() error handling path
2026-08-22 8:18 [PATCH net] net: fix a resource leak in copy_net_ns() error handling path Tetsuo Handa
@ 2026-08-25 10:40 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-08-25 10:40 UTC (permalink / raw)
To: Tetsuo Handa; +Cc: davem, edumazet, kuba, pabeni, horms, netdev, linux-kernel
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:
On Sat, 22 Aug 2026 17:18:31 +0900 you wrote:
> Currently, preinit_net() does two things:
>
> (1) call ns_common_init() which might fail
> (2) initialize resources which does not fail
>
> However, preinit_net() is returning early when (1) fails, and copy_net_ns()
> is jumping to the dec_ucounts: label. As a result, resources allocated by
> net_alloc() are leaking. We need to call key_remove_domain() and
> net_passive_dec() in order to release resources allocated by net_alloc().
>
> [...]
Here is the summary with links:
- [net] net: fix a resource leak in copy_net_ns() error handling path
https://git.kernel.org/netdev/net/c/3220b62fbb8a
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-08-25 10:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-22 8:18 [PATCH net] net: fix a resource leak in copy_net_ns() error handling path Tetsuo Handa
2026-08-25 10:40 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox