netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: "Denis V. Lunev" <den@openvz.org>,
	Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>
Subject: [PATCH v1 net] af_unix: Add error handling in af_unix_init().
Date: Wed, 14 Dec 2022 18:20:08 +0900	[thread overview]
Message-ID: <20221214092008.47330-1-kuniyu@amazon.com> (raw)

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


             reply	other threads:[~2022-12-14  9:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14  9:20 Kuniyuki Iwashima [this message]
2022-12-14 19:18 ` [PATCH v1 net] af_unix: Add error handling in af_unix_init() 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221214092008.47330-1-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=den@openvz.org \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).