From: Matthieu Baerts <matttbe@kernel.org>
To: Kuniyuki Iwashima <kuniyu@amazon.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>
Cc: Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
netdev@vger.kernel.org
Subject: Re: [PATCH v1 net-next 3/6] socket: Restore sock_create_kern().
Date: Thu, 22 May 2025 17:02:45 +0200 [thread overview]
Message-ID: <a5654334-b0fc-419d-adbd-91eca9437e28@kernel.org> (raw)
In-Reply-To: <20250517035120.55560-4-kuniyu@amazon.com>
Hi Kuniyuki,
On 17/05/2025 05:50, Kuniyuki Iwashima wrote:
> Let's restore sock_create_kern() that holds a netns reference.
>
> Now, it's the same as the version before commit 26abe14379f8 ("net:
> Modify sk_alloc to not reference count the netns of kernel sockets.").
>
> Back then, after creating a socket in init_net, we used sk_change_net()
> to drop the netns ref and switch to another netns, but now we can
> simply use __sock_create_kern() instead.
>
> $ git blame -L:sk_change_net include/net/sock.h 26abe14379f8~
>
> DEBUG_NET_WARN_ON_ONCE() is to catch a path calling sock_create_kern()
> from __net_init functions, since doing so would leak the netns as
> __net_exit functions cannot run until the socket is removed.
Thank you for working on this!
(...)
> diff --git a/net/socket.c b/net/socket.c
> index 7c4474c966c0..aeece4c4bb08 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -1632,6 +1632,48 @@ int __sock_create_kern(struct net *net, int family, int type, int protocol, stru
> }
> EXPORT_SYMBOL(__sock_create_kern);
>
> +/**
> + * sock_create_kern - creates a socket for kernel space
> + *
> + * @net: net namespace
> + * @family: protocol family (AF_INET, ...)
> + * @type: communication type (SOCK_STREAM, ...)
> + * @protocol: protocol (0, ...)
> + * @res: new socket
> + *
> + * Creates a new socket and assigns it to @res.
> + *
> + * The socket is for kernel space and should not be exposed to
> + * userspace via a file descriptor nor BPF hooks except for LSM
> + * (see inet_create(), inet_release(), etc).
> + *
> + * The socket bypasses some LSMs that take care of @kern in
> + * security_socket_create() and security_socket_post_create().
> + *
> + * The socket holds a reference count of @net so that the caller
> + * does not need to care about @net's lifetime.
> + *
> + * This MUST NOT be called from the __net_init path and @net MUST
> + * be alive as of calling sock_create_net().
> + *
> + * Context: Process context. This function internally uses GFP_KERNEL.
> + * Return: 0 or an error.
> + */
> +int sock_create_kern(struct net *net, int family, int type, int protocol,
> + struct socket **res)
> +{
> + int ret;
> +
> + DEBUG_NET_WARN_ON_ONCE(!net_initialized(net));
> +
> + ret = __sock_create(net, family, type, protocol, res, 1);
> + if (!ret)
A small suggestion if you have to send a v2: when quickly reading the
code, I find it easy to interpret the code above as: "in case of error
with __sock_create(), the refcnt is upgraded" . It might be clearer to
simply rename "ret" to "err" or use "(ret < 0)".
Up to you, a small detail for those who didn't directly realise what
"ret" is :)
> + sk_net_refcnt_upgrade((*res)->sk);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(sock_create_kern);
> +
> static struct socket *__sys_socket_create(int family, int type, int protocol)
> {
> struct socket *sock;
Cheers,
Matt
--
Sponsored by the NGI0 Core fund.
next prev parent reply other threads:[~2025-05-22 15:02 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-17 3:50 [PATCH v1 net-next 0/6] socket: Make sock_create_kern() robust against misuse Kuniyuki Iwashima
2025-05-17 3:50 ` [PATCH v1 net-next 1/6] socket: Un-export __sock_create() Kuniyuki Iwashima
2025-05-19 12:16 ` David Laight
2025-05-19 17:16 ` Kuniyuki Iwashima
2025-05-17 3:50 ` [PATCH v1 net-next 2/6] socket: Rename sock_create_kern() to __sock_create_kern() Kuniyuki Iwashima
2025-05-22 15:01 ` Matthieu Baerts
2025-05-17 3:50 ` [PATCH v1 net-next 3/6] socket: Restore sock_create_kern() Kuniyuki Iwashima
2025-05-22 15:02 ` Matthieu Baerts [this message]
2025-05-22 16:23 ` Kuniyuki Iwashima
2025-05-17 3:50 ` [PATCH v1 net-next 4/6] socket: Remove kernel socket conversion except for net/rds/ Kuniyuki Iwashima
2025-05-22 8:55 ` Paolo Abeni
2025-05-22 16:12 ` Kuniyuki Iwashima
2025-05-22 16:38 ` Chuck Lever
2025-05-22 17:04 ` Kuniyuki Iwashima
2025-05-23 4:23 ` Christoph Hellwig
2025-05-22 15:03 ` Matthieu Baerts
2025-05-17 3:50 ` [PATCH v1 net-next 5/6] socket: Replace most sock_create() calls with sock_create_kern() Kuniyuki Iwashima
2025-05-22 9:11 ` Paolo Abeni
2025-05-17 3:50 ` [PATCH v1 net-next 6/6] socket: Clean up kdoc for sock_create() and sock_create_lite() Kuniyuki Iwashima
2025-05-19 12:43 ` David Laight
2025-05-19 17:28 ` Kuniyuki Iwashima
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=a5654334-b0fc-419d-adbd-91eca9437e28@kernel.org \
--to=matttbe@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuni1840@gmail.com \
--cc=kuniyu@amazon.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemb@google.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).