netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 net] af_unix: Add error handling in af_unix_init().
@ 2022-12-14  9:20 Kuniyuki Iwashima
  2022-12-14 19:18 ` Alexander H Duyck
  2022-12-14 20:55 ` Jakub Kicinski
  0 siblings, 2 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2022-12-14  9:20 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Denis V. Lunev, Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

sock_register() and register_pernet_subsys() in af_unix_init() could
fail.

For example, after loading another PF_UNIX module (it's improbable
though), loading unix.ko fails at sock_register() and leaks slab
memory.  Also, register_pernet_subsys() fails when running out of
memory.

Let's handle errors appropriately.

Fixes: 097e66c57845 ("[NET]: Make AF_UNIX per network namespace safe [v2]")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/unix/af_unix.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index ede2b2a140a4..5352f30850f1 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -3720,7 +3720,7 @@ static void __init bpf_iter_register(void)
 
 static int __init af_unix_init(void)
 {
-	int i, rc = -1;
+	int i, rc;
 
 	BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
 
@@ -3730,20 +3730,25 @@ static int __init af_unix_init(void)
 	}
 
 	rc = proto_register(&unix_dgram_proto, 1);
-	if (rc != 0) {
+	if (rc) {
 		pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
 		goto out;
 	}
 
 	rc = proto_register(&unix_stream_proto, 1);
-	if (rc != 0) {
+	if (rc) {
 		pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
-		proto_unregister(&unix_dgram_proto);
-		goto out;
+		goto err_proto_register;
 	}
 
-	sock_register(&unix_family_ops);
-	register_pernet_subsys(&unix_net_ops);
+	rc = sock_register(&unix_family_ops);
+	if (rc)
+		goto err_sock_register;
+
+	rc = register_pernet_subsys(&unix_net_ops);
+	if (rc)
+		goto err_register_pernet_subsys;
+
 	unix_bpf_build_proto();
 
 #if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
@@ -3752,6 +3757,15 @@ static int __init af_unix_init(void)
 
 out:
 	return rc;
+
+err_register_pernet_subsys:
+	sock_unregister(PF_UNIX);
+err_sock_register:
+	proto_unregister(&unix_stream_proto);
+err_proto_register:
+	proto_unregister(&unix_dgram_proto);
+
+	goto out;
 }
 
 static void __exit af_unix_exit(void)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-12-14 22:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-14  9:20 [PATCH v1 net] af_unix: Add error handling in af_unix_init() Kuniyuki Iwashima
2022-12-14 19:18 ` Alexander H Duyck
     [not found]   ` <20221214130131.17555240@kernel.org>
2022-12-14 22:27     ` Alexander Duyck
2022-12-14 22:36       ` Jakub Kicinski
2022-12-14 20:55 ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).