socket: Make sock_create_kern() robust against misuse.
Subsystems (43)
v2 cover socket: Make sock_create_kern() robust against misuse.
There are a bunch of weird usages of sock_create() and friends due to poor documentation.
1) some subsystems use __sock_create(), but all of them can be replaced with sock_create_kern()
2) some subsystems use sock_create(), but most of the sockets are not tied to userspace processes nor exposed via file descriptors but are (most likely unintentionally) exposed to some BPF hooks (infiniband, ISDN, iscsi, Xen PV call, ocfs2, smbd)
3) some subsystems use sock_create_kern() and convert the sockets to hold netns refcnt (cifs, mptcp, nvme, rds, smc, and sunrpc)
The primary goal is to sort out such confusion and provide enough documentation for future developers to choose an appropriate API.
Before commit 26abe14379f8 ("net: Modify sk_alloc to not reference count the netns of kernel sockets."), sock_create_kern() held the netns refcnt, and each caller dropped it if unnecessary:
sock_create_kern(&init_net, ..., &sock); sk_change_net(sock->sk, net);
But that implicit API change ended up causing a lot of use-after-free and finally introduced another helper:
sock_create_kern(net, ..., &sock); sk_net_refcnt_upgrade(sock->sk);
Patch 2 renames sock_create_kern() to __sock_create_kern() to mark it as a special-purpose API, and Patch 3 restores the original sock_create_kern() that holds the netns refcnt.
Now, we can simply use sock_create_kern() or __sock_create_kern() depending on the use case (except for rds).
Changes
v2:
patch 3: s/ret/err/ in sock_create_kern() for clarity patch 4: newly added
patch 5: drop unnecessary change for sunrpc and updated changelog
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (7):
socket: Un-export __sock_create().
socket: Rename sock_create_kern() to __sock_create_kern().
socket: Restore sock_create_kern().
smb: client: Add missing net_passive_dec().
socket: Remove kernel socket conversion except for net/rds/.
socket: Replace most sock_create() calls with sock_create_kern().
socket: Clean up kdoc for sock_create() and sock_create_lite().
drivers/block/drbd/drbd_receiver.c | 12 +- drivers/infiniband/hw/erdma/erdma_cm.c | 6 +- drivers/infiniband/sw/rxe/rxe_qp.c | 2 +- drivers/infiniband/sw/siw/siw_cm.c | 6 +- drivers/isdn/mISDN/l1oip_core.c | 3 +- drivers/nvme/host/tcp.c | 5 +- drivers/nvme/target/tcp.c | 5 +- drivers/soc/qcom/qmi_interface.c | 4 +- drivers/target/iscsi/iscsi_target_login.c | 7 +- drivers/xen/pvcalls-back.c | 6 +- fs/afs/rxrpc.c | 2 +- fs/dlm/lowcomms.c | 8 +- fs/ocfs2/cluster/tcp.c | 8 +- fs/smb/client/connect.c | 11 +- fs/smb/server/transport_tcp.c | 7 +- include/linux/net.h | 7 +- net/9p/trans_fd.c | 9 +- net/bluetooth/rfcomm/core.c | 3 +- net/ceph/messenger.c | 6 +- net/handshake/handshake-test.c | 32 ++-- net/ipv4/af_inet.c | 2 +- net/ipv4/udp_tunnel_core.c | 2 +- net/ipv6/ip6_udp_tunnel.c | 2 +- net/l2tp/l2tp_core.c | 8 +- net/mctp/test/route-test.c | 6 +- net/mptcp/pm_kernel.c | 4 +- net/mptcp/subflow.c | 7 +- net/netfilter/ipvs/ip_vs_sync.c | 8 +- net/qrtr/ns.c | 6 +- net/rds/tcp_connect.c | 8 +- net/rds/tcp_listen.c | 4 +- net/rxrpc/rxperf.c | 4 +- net/sctp/socket.c | 2 +- net/smc/af_smc.c | 18 +-- net/smc/smc_inet.c | 2 +- net/socket.c | 138 ++++++++++++------ net/sunrpc/clnt.c | 4 +- net/sunrpc/svcsock.c | 6 +- net/sunrpc/xprtsock.c | 12 +- net/tipc/topsrv.c | 4 +- net/wireless/nl80211.c | 4 +- .../selftests/bpf/test_kmods/bpf_testmod.c | 4 +- 42 files changed, 219 insertions(+), 185 deletions(-)
Signature · 1 line
2.49.0
PATCH v2 1/7 socket: Un-export __sock_create().
Since commit eeb1bd5c40ed ("net: Add a struct net parameter to sock_create_kern"), we no longer need to export __sock_create() and can replace all non-core users with sock_create_kern().
Let's convert them and un-export __sock_create().
--- fs/smb/client/connect.c | 4 ++-- include/linux/net.h | 2 -- net/9p/trans_fd.c | 9 +++++---- net/handshake/handshake-test.c | 32 ++++++++++++++------------------ net/socket.c | 3 +-- net/sunrpc/clnt.c | 4 ++-- net/sunrpc/svcsock.c | 2 +- net/sunrpc/xprtsock.c | 6 +++--- net/wireless/nl80211.c | 4 ++-- 9 files changed, 30 insertions(+), 36 deletions(-)
fs/smb/client/connect.c +2 -2
@@ -3350,8 +3350,8 @@ generic_ip_connect(struct TCP_Server_Info *server)struct net *net =cifs_net_ns (server);struct sock *sk; - rc =__sock_create (net, sfamily,SOCK_STREAM , -IPPROTO_TCP , &server->ssocket,1 ); + rc =sock_create_kern (net, sfamily,SOCK_STREAM , +IPPROTO_TCP , &server->ssocket);if (rc <0 ) {cifs_server_dbg (VFS ,"Error %d creating socket\n" , rc);return rc;
include/linux/net.h +0 -2
@@ -251,8 +251,6 @@ int sock_wake_async(struct socket_wq *sk_wq, int how, int band);int sock_register (const struct net_proto_family *fam);void sock_unregister (int family);bool sock_is_registered (int family); -int __sock_create (struct net *net,int family,int type,int proto, -struct socket **res,int kern);int sock_create (int family,int type,int proto,struct socket **res);int sock_create_kern (struct net *net,int family,int type,int proto,struct socket **res);int sock_create_lite (int family,int type,int proto,struct socket **res);
net/9p/trans_fd.c +5 -4
@@ -1006,8 +1006,9 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) client->trans_opts.tcp.port = opts.port; client->trans_opts.tcp.privport = opts.privport; - err =__sock_create (current->nsproxy->net_ns, stor.ss_family, -SOCK_STREAM ,IPPROTO_TCP , &csocket,1 ); + + err =sock_create_kern (current->nsproxy->net_ns, stor.ss_family, +SOCK_STREAM ,IPPROTO_TCP , &csocket);if (err) {pr_err ("%s (%d): problem creating socket\n" , __func__,task_pid_nr (current));
@@ -1057,8 +1058,8 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) sun_server.sun_family =PF_UNIX ;strcpy (sun_server.sun_path, addr); - err =__sock_create (current->nsproxy->net_ns,PF_UNIX , -SOCK_STREAM ,0 , &csocket,1 ); + err =sock_create_kern (current->nsproxy->net_ns,PF_UNIX , +SOCK_STREAM ,0 , &csocket);if (err <0 ) {pr_err ("%s (%d): problem creating socket\n" , __func__,task_pid_nr (current));
net/handshake/handshake-test.c +14 -18
@@ -143,14 +143,18 @@ static void handshake_req_alloc_case(struct kunit *test)kfree (result); } +static int handshake_sock_create (struct socket **sock) +{ +return sock_create_kern (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , sock); +} +static void handshake_req_submit_test1 (struct kunit *test) {struct socket *sock;int err, result;/* Arrange */ - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 );/* Act */
@@ -190,8 +194,7 @@ static void handshake_req_submit_test3(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); sock->file =NULL ;
@@ -216,8 +219,7 @@ static void handshake_req_submit_test4(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, filp);
@@ -251,8 +253,7 @@ static void handshake_req_submit_test5(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, filp);
@@ -289,8 +290,7 @@ static void handshake_req_submit_test6(struct kunit *test) req2 =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req2); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, filp);
@@ -321,8 +321,7 @@ static void handshake_req_cancel_test1(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );
@@ -357,8 +356,7 @@ static void handshake_req_cancel_test2(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );
@@ -399,8 +397,7 @@ static void handshake_req_cancel_test3(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_good,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );
@@ -457,8 +454,7 @@ static void handshake_req_destroy_test1(struct kunit *test) req =handshake_req_alloc (&handshake_req_alloc_proto_destroy,GFP_KERNEL );KUNIT_ASSERT_NOT_NULL (test, req); - err =__sock_create (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &sock,1 ); + err =handshake_sock_create (&sock);KUNIT_ASSERT_EQ (test, err,0 ); filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );
net/socket.c +1 -2
@@ -1467,7 +1467,7 @@ EXPORT_SYMBOL(sock_wake_async);* This function internally uses GFP_KERNEL. */ -int __sock_create (struct net *net,int family,int type,int protocol, +static int __sock_create (struct net *net,int family,int type,int protocol,struct socket **res,int kern) {int err;
@@ -1581,7 +1581,6 @@ int __sock_create(struct net *net, int family, int type, int protocol,rcu_read_unlock (); goto out_sock_release; } -EXPORT_SYMBOL (__sock_create);/** * sock_create - creates a socket
net/sunrpc/clnt.c +2 -2
@@ -1455,8 +1455,8 @@ static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,struct socket *sock;int err; - err =__sock_create (net, sap->sa_family, -SOCK_DGRAM ,IPPROTO_UDP , &sock,1 ); + err =sock_create_kern (net, sap->sa_family, +SOCK_DGRAM ,IPPROTO_UDP , &sock);if (err <0 ) {dprintk ("RPC: can't create UDP socket (%d)\n" , err); goto out;
net/sunrpc/svcsock.c +1 -1
@@ -1516,7 +1516,7 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,return ERR_PTR (-EINVAL ); } - error =__sock_create (net, family, type, protocol, &sock,1 ); + error =sock_create_kern (net, family, type, protocol, &sock);if (error <0 )return ERR_PTR (error);
net/sunrpc/xprtsock.c +3 -3
@@ -1924,7 +1924,7 @@ static struct socket *xs_create_sock(struct rpc_xprt *xprt,struct socket *sock;int err; - err =__sock_create (xprt->xprt_net, family, type, protocol, &sock,1 ); + err =sock_create_kern (xprt->xprt_net, family, type, protocol, &sock);if (err <0 ) {dprintk ("RPC: can't create %d transport socket (%d).\n" , protocol, -err);
@@ -1999,8 +1999,8 @@ static int xs_local_setup_socket(struct sock_xprt *transport)struct socket *sock;int status; - status =__sock_create (xprt->xprt_net,AF_LOCAL , -SOCK_STREAM ,0 , &sock,1 ); + status =sock_create_kern (xprt->xprt_net,AF_LOCAL , +SOCK_STREAM ,0 , &sock);if (status <0 ) {dprintk ("RPC: can't create AF_LOCAL " "transport socket (%d).\n" , -status);
net/wireless/nl80211.c +2 -2
@@ -13750,8 +13750,8 @@ static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev, port =nla_get_u16_default (tb[NL80211_WOWLAN_TCP_SRC_PORT ],0 );#ifdef CONFIG_INET /* allocate a socket and port for it and use it */ - err =__sock_create (wiphy_net (&rdev->wiphy),PF_INET ,SOCK_STREAM , -IPPROTO_TCP , &cfg->sock,1 ); + err =sock_create_kern (wiphy_net (&rdev->wiphy),PF_INET ,SOCK_STREAM , +IPPROTO_TCP , &cfg->sock);if (err) {kfree (cfg);return err;
Signature · 1 line
2.49.0
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 1/7] socket: Un-export __sock_create().
Quoted 5 lines · Kuniyuki Iwashima
Since commit
eeb1bd5c40ed("net: Add a struct net parameter tosock_create_kern"), we no longer need to export__sock_create()and can replace all non-core users withsock_create_kern().Let's convert them and un-export
__sock_create().
The changes looks good, but the commit log including subject line is rather confusing. What you do is to replace all uses of __sock_create with sock_create_kern, which works because sock_create_kern just calls __sock_create with the last argument set to 1 as those callers do it. This then allows marking __sock_create static because all outside users are gone.
Please state that, i.e.
Subect: use sock_create_kern insteadf of opencoding it
Replace all callers of __sock_create that set the kernel argument to 1 with sock_create_kern, which is the improve interface for that. Mark __sock_create static now that all users outside of socket.c are gone.
Re: [PATCH v2 net-next 1/7] socket: Un-export __sock_create().
Quoted 14 lines · On Mon, 26 May 2025 07:29:07 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 5 lines · Kuniyuki Iwashima
Since commit
eeb1bd5c40ed("net: Add a struct net parameter tosock_create_kern"), we no longer need to export__sock_create()and can replace all non-core users withsock_create_kern().Let's convert them and un-export
__sock_create().The changes looks good, but the commit log including subject line is rather confusing. What you do is to replace all uses of
__sock_createwithsock_create_kern, which works becausesock_create_kernjust calls__sock_createwith the last argument set to 1 as those callers do it. This then allows marking__sock_createstatic because all outside users are gone.
Quoted 8 lines
Please state that, i.e.
Subect: use
sock_create_kerninsteadf of opencoding itReplace all callers of
__sock_createthat set the kernel argument to 1 withsock_create_kern, which is the improve interface for that. Mark__sock_createstatic now that all users outside ofsocket.care gone.
I'd also like to see an explicit statement on all these patches about whether the created sockets hold a reference to the namespace.
I know it is documented in the function definitions, but the issue has always been that the callers get it wrong.
From what I remember, as this point in the patch series sock_create_kern() doesn't holds a reference, but by the end of the series it does. That just has to be a recipe for disaster and pretty much requires the changes all go through the same tree in one merge window.
But the code touches multiple areas and the changes would normally go through multiple trees.
So it's going to be hard to get all the acks and the patch accepted. (Unless you persuade Linus to 'just apply the changes'.
I think you need to look at three merge windows.
1) Add new function(s) for creating user/kernel sockets with/without holding a namespace reference.
2) Update all the callers to use the new functions.
3) Delete the old functions.
There is no point modifying the callers twice, and the commits need to explicitly state whether they want the namespace held or not.
David
Re: [PATCH v2 net-next 1/7] socket: Un-export __sock_create().
In reply to Christoph HellwigFrom: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:29:07 +0200
Quoted 7 lines · Kuniyuki Iwashima
Quoted 5 lines
Since commit
eeb1bd5c40ed("net: Add a struct net parameter tosock_create_kern"), we no longer need to export__sock_create()and can replace all non-core users withsock_create_kern().Let's convert them and un-export
__sock_create().
Quoted 8 lines
The changes looks good, but the commit log including subject line is rather confusing. What you do is to replace all uses of
__sock_createwithsock_create_kern, which works becausesock_create_kernjust calls__sock_createwith the last argument set to 1 as those callers do it. This then allows marking__sock_createstatic because all outside users are gone.Please state that, i.e.
Will do so.
Thanks!
Quoted 7 lines
Subect: use
sock_create_kerninsteadf of opencoding itReplace all callers of
__sock_createthat set the kernel argument to 1 withsock_create_kern, which is the improve interface for that. Mark__sock_createstatic now that all users outside ofsocket.care gone.
PATCH v2 2/7 socket: Rename sock_create_kern() to __sock_create_kern().
In reply to Kuniyuki Iwashimasock_create_kern() is a catchy name and often chosen by non-networking developers to create kernel sockets. But due to its poor documentation, it has caused a bunch of netns use-after-free:
* commit ef7134c7fc48 ("smb: client: Fix use-after-free of network namespace.")
* commit b013b817f32f ("nvme-tcp: fix use-after-free of netns by kernel TCP socket.")
.. and more in NFS, SMC, MPTCP, RDS
Some non-networking maintainers mentioned that the socket API should be more robust to prevent this type of issues. [0]
The current sock_create_kern() doesn't hold a reference to the netns, which allows the netns to be removed while the socket is still around. This is useful when the socket is used as the backend for a networking device.
But, this is rather a special case, where netdev folks should use a dedicated API, and we should provide sock_create_kern() as the standard API for general in-kernel use cases.
In fact, we did so before commit 26abe14379f8 ("net: Modify sk_alloc to not reference count the netns of kernel sockets."),
sock_create_kern(&init_net, ..., &sock) sk_change_net(sock->sk, net);
but that implicit API change ended up causing a lot of problems.
Let's rename sock_create_kern() to __sock_create_kern() as a special API and add a fat documentation.
The next patch will add sock_create_kern() that holds netns refcnt.
--- drivers/block/drbd/drbd_receiver.c | 12 +++--- drivers/infiniband/sw/rxe/rxe_qp.c | 2 +- drivers/nvme/host/tcp.c | 6 +-- drivers/soc/qcom/qmi_interface.c | 4 +- fs/afs/rxrpc.c | 2 +- fs/dlm/lowcomms.c | 8 ++-- fs/smb/client/connect.c | 4 +- include/linux/net.h | 3 +- net/9p/trans_fd.c | 8 ++-- net/bluetooth/rfcomm/core.c | 3 +- net/ceph/messenger.c | 6 +-- net/handshake/handshake-test.c | 2 +- net/ipv4/af_inet.c | 2 +- net/ipv4/udp_tunnel_core.c | 2 +- net/ipv6/ip6_udp_tunnel.c | 2 +- net/l2tp/l2tp_core.c | 8 ++-- net/mctp/test/route-test.c | 6 +-- net/mptcp/pm_kernel.c | 4 +- net/mptcp/subflow.c | 4 +- net/netfilter/ipvs/ip_vs_sync.c | 8 ++-- net/qrtr/ns.c | 6 +-- net/rds/tcp_connect.c | 8 ++-- net/rds/tcp_listen.c | 4 +- net/rxrpc/rxperf.c | 4 +- net/sctp/socket.c | 2 +- net/smc/af_smc.c | 4 +- net/smc/smc_inet.c | 2 +- net/socket.c | 37 +++++++++++++------ net/sunrpc/clnt.c | 4 +- net/sunrpc/svcsock.c | 2 +- net/sunrpc/xprtsock.c | 6 +-- net/tipc/topsrv.c | 4 +- net/wireless/nl80211.c | 4 +- .../selftests/bpf/test_kmods/bpf_testmod.c | 4 +- 34 files changed, 102 insertions(+), 85 deletions(-)
drivers/block/drbd/drbd_receiver.c +6 -6
@@ -618,9 +618,9 @@ static struct socket *drbd_try_connect(struct drbd_connection *connection) peer_addr_len =min_t (int, connection->peer_addr_len,sizeof (src_in6));memcpy (&peer_in6, &connection->peer_addr, peer_addr_len); - what ="sock_create_kern" ; - err =sock_create_kern (&init_net, ((struct sockaddr *)&src_in6)->sa_family, -SOCK_STREAM ,IPPROTO_TCP , &sock); + what ="__sock_create_kern" ; + err =__sock_create_kern (&init_net, ((struct sockaddr *)&src_in6)->sa_family, +SOCK_STREAM ,IPPROTO_TCP , &sock);if (err <0 ) { sock =NULL ; goto out;
@@ -713,9 +713,9 @@ static int prepare_listen_socket(struct drbd_connection *connection, struct acce my_addr_len =min_t (int, connection->my_addr_len,sizeof (struct sockaddr_in6 ));memcpy (&my_addr, &connection->my_addr, my_addr_len); - what ="sock_create_kern" ; - err =sock_create_kern (&init_net, ((struct sockaddr *)&my_addr)->sa_family, -SOCK_STREAM ,IPPROTO_TCP , &s_listen); + what ="__sock_create_kern" ; + err =__sock_create_kern (&init_net, ((struct sockaddr *)&my_addr)->sa_family, +SOCK_STREAM ,IPPROTO_TCP , &s_listen);if (err) { s_listen =NULL ; goto out;
drivers/infiniband/sw/rxe/rxe_qp.c +1 -1
@@ -241,7 +241,7 @@ static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,/* if we don't finish qp create make sure queue is valid */ skb_queue_head_init (&qp->req_pkts); - err =sock_create_kern (&init_net,AF_INET ,SOCK_DGRAM ,0 , &qp->sk); + err =__sock_create_kern (&init_net,AF_INET ,SOCK_DGRAM ,0 , &qp->sk);if (err <0 )return err; qp->sk->sk->sk_user_data = (void *)(uintptr_t )qp->elem.index;
drivers/nvme/host/tcp.c +3 -3
@@ -1756,9 +1756,9 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, queue->cmnd_capsule_len =sizeof (struct nvme_command ) +NVME_TCP_ADMIN_CCSZ ; - ret =sock_create_kern (current->nsproxy->net_ns, - ctrl->addr.ss_family,SOCK_STREAM , -IPPROTO_TCP , &queue->sock); + ret =__sock_create_kern (current->nsproxy->net_ns, + ctrl->addr.ss_family,SOCK_STREAM , +IPPROTO_TCP , &queue->sock);if (ret) {dev_err (nctrl->device,"failed to create socket: %d\n" , ret);
drivers/soc/qcom/qmi_interface.c +2 -2
@@ -588,8 +588,8 @@ static struct socket *qmi_sock_create(struct qmi_handle *qmi,struct socket *sock;int ret; - ret =sock_create_kern (&init_net,AF_QIPCRTR ,SOCK_DGRAM , -PF_QIPCRTR , &sock); + ret =__sock_create_kern (&init_net,AF_QIPCRTR ,SOCK_DGRAM , +PF_QIPCRTR , &sock);if (ret <0 )return ERR_PTR (ret);
fs/afs/rxrpc.c +1 -1
@@ -53,7 +53,7 @@ int afs_open_socket(struct afs_net *net)_enter ("" ); - ret =sock_create_kern (net->net,AF_RXRPC ,SOCK_DGRAM ,PF_INET6 , &socket); + ret =__sock_create_kern (net->net,AF_RXRPC ,SOCK_DGRAM ,PF_INET6 , &socket);if (ret <0 ) goto error_1;
fs/dlm/lowcomms.c +4 -4
@@ -1580,8 +1580,8 @@ static int dlm_connect(struct connection *con) }/* Create a socket to communicate with */ - result =sock_create_kern (&init_net, dlm_local_addr[0 ].ss_family, -SOCK_STREAM , dlm_proto_ops->proto, &sock); + result =__sock_create_kern (&init_net, dlm_local_addr[0 ].ss_family, +SOCK_STREAM , dlm_proto_ops->proto, &sock);if (result <0 )return result;
@@ -1761,8 +1761,8 @@ static int dlm_listen_for_all(void)if (result <0 )return result; - result =sock_create_kern (&init_net, dlm_local_addr[0 ].ss_family, -SOCK_STREAM , dlm_proto_ops->proto, &sock); + result =__sock_create_kern (&init_net, dlm_local_addr[0 ].ss_family, +SOCK_STREAM , dlm_proto_ops->proto, &sock);if (result <0 ) {log_print ("Can't create comms socket: %d" , result);return result;
fs/smb/client/connect.c +2 -2
@@ -3350,8 +3350,8 @@ generic_ip_connect(struct TCP_Server_Info *server)struct net *net =cifs_net_ns (server);struct sock *sk; - rc =sock_create_kern (net, sfamily,SOCK_STREAM , -IPPROTO_TCP , &server->ssocket); + rc =__sock_create_kern (net, sfamily,SOCK_STREAM , +IPPROTO_TCP , &server->ssocket);if (rc <0 ) {cifs_server_dbg (VFS ,"Error %d creating socket\n" , rc);return rc;
include/linux/net.h +2 -1
@@ -252,7 +252,8 @@ int sock_register(const struct net_proto_family *fam);void sock_unregister (int family);bool sock_is_registered (int family);int sock_create (int family,int type,int proto,struct socket **res); -int sock_create_kern (struct net *net,int family,int type,int proto,struct socket **res); +int __sock_create_kern (struct net *net,int family,int type,int proto, +struct socket **res);int sock_create_lite (int family,int type,int proto,struct socket **res);struct socket *sock_alloc (void );void sock_release (struct socket *sock);
net/9p/trans_fd.c +4 -4
@@ -1007,8 +1007,8 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) client->trans_opts.tcp.port = opts.port; client->trans_opts.tcp.privport = opts.privport; - err =sock_create_kern (current->nsproxy->net_ns, stor.ss_family, -SOCK_STREAM ,IPPROTO_TCP , &csocket); + err =__sock_create_kern (current->nsproxy->net_ns, stor.ss_family, +SOCK_STREAM ,IPPROTO_TCP , &csocket);if (err) {pr_err ("%s (%d): problem creating socket\n" , __func__,task_pid_nr (current));
@@ -1058,8 +1058,8 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) sun_server.sun_family =PF_UNIX ;strcpy (sun_server.sun_path, addr); - err =sock_create_kern (current->nsproxy->net_ns,PF_UNIX , -SOCK_STREAM ,0 , &csocket); + err =__sock_create_kern (current->nsproxy->net_ns,PF_UNIX , +SOCK_STREAM ,0 , &csocket);if (err <0 ) {pr_err ("%s (%d): problem creating socket\n" , __func__,task_pid_nr (current));
net/bluetooth/rfcomm/core.c +2 -1
@@ -200,7 +200,8 @@ static int rfcomm_l2sock_create(struct socket **sock)BT_DBG ("" ); - err =sock_create_kern (&init_net,PF_BLUETOOTH ,SOCK_SEQPACKET ,BTPROTO_L2CAP , sock); + err =__sock_create_kern (&init_net,PF_BLUETOOTH ,SOCK_SEQPACKET , +BTPROTO_L2CAP , sock);if (!err) {struct sock *sk = (*sock)->sk; sk->sk_data_ready = rfcomm_l2data_ready;
net/ceph/messenger.c +3 -3
@@ -442,10 +442,10 @@ int ceph_tcp_connect(struct ceph_connection *con) ceph_pr_addr(&con->peer_addr));BUG_ON (con->sock); -/* sock_create_kern() allocates with GFP_KERNEL */ +/* __sock_create_kern() allocates with GFP_KERNEL */ noio_flag =memalloc_noio_save (); - ret =sock_create_kern (read_pnet (&con->msgr->net), ss.ss_family, -SOCK_STREAM ,IPPROTO_TCP , &sock); + ret =__sock_create_kern (read_pnet (&con->msgr->net), ss.ss_family, +SOCK_STREAM ,IPPROTO_TCP , &sock);memalloc_noio_restore (noio_flag);if (ret)return ret;
net/handshake/handshake-test.c +1 -1
@@ -145,7 +145,7 @@ static void handshake_req_alloc_case(struct kunit *test)static int handshake_sock_create (struct socket **sock) { -return sock_create_kern (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , sock); +return __sock_create_kern (&init_net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , sock); }static void handshake_req_submit_test1 (struct kunit *test)
net/ipv4/af_inet.c +1 -1
@@ -1631,7 +1631,7 @@ int inet_ctl_sock_create(struct sock **sk, unsigned short family, struct net *net) {struct socket *sock; -int rc =sock_create_kern (net, family, type, protocol, &sock); +int rc =__sock_create_kern (net, family, type, protocol, &sock);if (rc ==0 ) { *sk = sock->sk;
net/ipv4/udp_tunnel_core.c +1 -1
@@ -15,7 +15,7 @@ int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,struct socket *sock =NULL ;struct sockaddr_in udp_addr; - err =sock_create_kern (net,AF_INET ,SOCK_DGRAM ,0 , &sock); + err =__sock_create_kern (net,AF_INET ,SOCK_DGRAM ,0 , &sock);if (err <0 ) goto error;
net/ipv6/ip6_udp_tunnel.c +1 -1
@@ -21,7 +21,7 @@ int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,int err;struct socket *sock =NULL ; - err =sock_create_kern (net,AF_INET6 ,SOCK_DGRAM ,0 , &sock); + err =__sock_create_kern (net,AF_INET6 ,SOCK_DGRAM ,0 , &sock);if (err <0 ) goto error;
net/l2tp/l2tp_core.c +4 -4
@@ -1494,8 +1494,8 @@ static int l2tp_tunnel_sock_create(struct net *net,if (cfg->local_ip6 && cfg->peer_ip6) {struct sockaddr_l2tpip6 ip6_addr = {0 }; - err =sock_create_kern (net,AF_INET6 ,SOCK_DGRAM , -IPPROTO_L2TP , &sock); + err =__sock_create_kern (net,AF_INET6 ,SOCK_DGRAM , +IPPROTO_L2TP , &sock);if (err <0 ) goto out;
@@ -1522,8 +1522,8 @@ static int l2tp_tunnel_sock_create(struct net *net, {struct sockaddr_l2tpip ip_addr = {0 }; - err =sock_create_kern (net,AF_INET ,SOCK_DGRAM , -IPPROTO_L2TP , &sock); + err =__sock_create_kern (net,AF_INET ,SOCK_DGRAM , +IPPROTO_L2TP , &sock);if (err <0 ) goto out;
net/mctp/test/route-test.c +3 -3
@@ -310,7 +310,7 @@ static void __mctp_route_test_init(struct kunit *test, rt =mctp_test_create_route (&init_net, dev->mdev,8 ,68 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt); - rc =sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock); + rc =__sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock);KUNIT_ASSERT_EQ (test, rc,0 ); addr.smctp_family =AF_MCTP ;
@@ -568,7 +568,7 @@ static void mctp_test_route_input_sk_keys(struct kunit *test) rt =mctp_test_create_route (&init_net, dev->mdev,8 ,68 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt); - rc =sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock); + rc =__sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock);KUNIT_ASSERT_EQ (test, rc,0 ); msk = container_of(sock->sk, struct mctp_sock, sk);
@@ -1186,7 +1186,7 @@ static void mctp_test_route_output_key_create(struct kunit *test) rt =mctp_test_create_route (&init_net, dev->mdev, dst,68 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt); - rc =sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock); + rc =__sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock);KUNIT_ASSERT_EQ (test, rc,0 ); dev->mdev->addrs =kmalloc (sizeof (u8),GFP_KERNEL );
net/mptcp/pm_kernel.c +2 -2
@@ -637,8 +637,8 @@ static int mptcp_pm_nl_create_listen_socket(struct sock *sk,int backlog =1024 ;int err; - err =sock_create_kern (sock_net (sk), entry->addr.family, -SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk); + err =__sock_create_kern (sock_net (sk), entry->addr.family, +SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk);if (err)return err;
net/mptcp/subflow.c +2 -2
@@ -1757,7 +1757,7 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,if (unlikely (!sk->sk_socket))return -EINVAL ; - err =sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , &sf); + err =__sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , &sf);if (err)return err;
@@ -1948,7 +1948,7 @@ static int subflow_ulp_init(struct sock *sk)int err =0 ;/* disallow attaching ULP to a socket unless it has been -* created with sock_create_kern() +* created with __sock_create_kern() */ if (!sk->sk_kern_sock) { err = -EOPNOTSUPP ;
net/netfilter/ipvs/ip_vs_sync.c +4 -4
@@ -1470,8 +1470,8 @@ static int make_send_sock(struct netns_ipvs *ipvs, int id,int result, salen;/* First create a socket */ - result =sock_create_kern (ipvs->net, ipvs->mcfg.mcast_af,SOCK_DGRAM , -IPPROTO_UDP , &sock); + result =__sock_create_kern (ipvs->net, ipvs->mcfg.mcast_af,SOCK_DGRAM , +IPPROTO_UDP , &sock);if (result <0 ) {pr_err ("Error during creation of socket; terminating\n" ); goto error;
@@ -1527,8 +1527,8 @@ static int make_receive_sock(struct netns_ipvs *ipvs, int id,int result, salen;/* First create a socket */ - result =sock_create_kern (ipvs->net, ipvs->bcfg.mcast_af,SOCK_DGRAM , -IPPROTO_UDP , &sock); + result =__sock_create_kern (ipvs->net, ipvs->bcfg.mcast_af,SOCK_DGRAM , +IPPROTO_UDP , &sock);if (result <0 ) {pr_err ("Error during creation of socket; terminating\n" ); goto error;
net/qrtr/ns.c +3 -3
@@ -692,8 +692,8 @@ int qrtr_ns_init(void)INIT_LIST_HEAD (&qrtr_ns.lookups);INIT_WORK (&qrtr_ns.work, qrtr_ns_worker); - ret =sock_create_kern (&init_net,AF_QIPCRTR ,SOCK_DGRAM , -PF_QIPCRTR , &qrtr_ns.sock); + ret =__sock_create_kern (&init_net,AF_QIPCRTR ,SOCK_DGRAM , +PF_QIPCRTR , &qrtr_ns.sock);if (ret <0 )return ret;
@@ -735,7 +735,7 @@ int qrtr_ns_init(void)* qrtr module is inserted successfully. * * However, the reference count is increased twice in -* sock_create_kern(): one is to increase the reference count of owner +* __sock_create_kern(): one is to increase the reference count of owner * of qrtr socket's proto_ops struct; another is to increment the * reference count of owner of qrtr proto struct. Therefore, we must * decrement the module reference count twice to ensure that it keeps
net/rds/tcp_connect.c +4 -4
@@ -112,12 +112,12 @@ int rds_tcp_conn_path_connect(struct rds_conn_path *cp)return 0 ; }if (ipv6_addr_v4mapped (&conn->c_laddr)) { - ret =sock_create_kern (rds_conn_net (conn),PF_INET , -SOCK_STREAM ,IPPROTO_TCP , &sock); + ret =__sock_create_kern (rds_conn_net (conn),PF_INET , +SOCK_STREAM ,IPPROTO_TCP , &sock); isv6 = false; }else { - ret =sock_create_kern (rds_conn_net (conn),PF_INET6 , -SOCK_STREAM ,IPPROTO_TCP , &sock); + ret =__sock_create_kern (rds_conn_net (conn),PF_INET6 , +SOCK_STREAM ,IPPROTO_TCP , &sock); isv6 = true; }
net/rds/tcp_listen.c +2 -2
@@ -278,8 +278,8 @@ struct socket *rds_tcp_listen_init(struct net *net, bool isv6)int addr_len;int ret; - ret =sock_create_kern (net, isv6 ?PF_INET6 :PF_INET ,SOCK_STREAM , -IPPROTO_TCP , &sock); + ret =__sock_create_kern (net, isv6 ?PF_INET6 :PF_INET ,SOCK_STREAM , +IPPROTO_TCP , &sock);if (ret <0 ) {rdsdebug ("could not create %s listener socket: %d\n" , isv6 ?"IPv6" :"IPv4" , ret);
net/rxrpc/rxperf.c +2 -2
@@ -188,8 +188,8 @@ static int rxperf_open_socket(void)struct socket *socket;int ret; - ret =sock_create_kern (&init_net,AF_RXRPC ,SOCK_DGRAM ,PF_INET6 , - &socket); + ret =__sock_create_kern (&init_net,AF_RXRPC ,SOCK_DGRAM ,PF_INET6 , + &socket);if (ret <0 ) goto error_1;
net/sctp/socket.c +1 -1
@@ -1329,7 +1329,7 @@ static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs,return err;/* in-kernel sockets don't generally have a file allocated to them -* if all they do is call sock_create_kern(). +* if all they do is call __sock_create_kern(). */ if (sk->sk_socket->file) flags = sk->sk_socket->file->f_flags;
net/smc/af_smc.c +2 -2
@@ -3331,8 +3331,8 @@ int smc_create_clcsk(struct net *net, struct sock *sk, int family)struct smc_sock *smc =smc_sk (sk);int rc; - rc =sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , - &smc->clcsock); + rc =__sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , + &smc->clcsock);if (rc)return rc;
net/smc/smc_inet.c +1 -1
@@ -111,7 +111,7 @@ static struct inet_protosw smc_inet6_protosw = {static unsigned int smc_sync_mss (struct sock *sk,u32 pmtu) {/* No need pass it through to clcsock, mss can always be set by -* sock_create_kern or smc_setsockopt. +* __sock_create_kern or smc_setsockopt. */ return 0 ; }
net/socket.c +26 -11
@@ -1600,22 +1600,37 @@ int sock_create(int family, int type, int protocol, struct socket **res)EXPORT_SYMBOL (sock_create);/** -* sock_create_kern - creates a socket (kernel space) -* @net: net namespace -* @family: protocol family (AF_INET, ...) -* @type: communication type (SOCK_STREAM, ...) -* @protocol: protocol (0, ...) -* @res: new socket +* __sock_create_kern - creates a socket for kernel space * -* A wrapper around __sock_create(). -* Returns 0 or an error. This function internally uses GFP_KERNEL. +* @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 **DOES NOT** hold a reference count of @net to allow +* it to be removed; the caller MUST ensure that the socket is always +* freed before @net. +* +* @net MUST be alive as of calling __sock_create_kern(). +* +* 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 __sock_create_kern (struct net *net,int family,int type,int protocol,struct socket **res) {return __sock_create (net, family, type, protocol, res,1 ); } -EXPORT_SYMBOL (sock_create_kern); +EXPORT_SYMBOL (__sock_create_kern);static struct socket *__sys_socket_create (int family,int type,int protocol) {
net/sunrpc/clnt.c +2 -2
@@ -1455,8 +1455,8 @@ static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,struct socket *sock;int err; - err =sock_create_kern (net, sap->sa_family, -SOCK_DGRAM ,IPPROTO_UDP , &sock); + err =__sock_create_kern (net, sap->sa_family, +SOCK_DGRAM ,IPPROTO_UDP , &sock);if (err <0 ) {dprintk ("RPC: can't create UDP socket (%d)\n" , err); goto out;
net/sunrpc/svcsock.c +1 -1
@@ -1516,7 +1516,7 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,return ERR_PTR (-EINVAL ); } - error =sock_create_kern (net, family, type, protocol, &sock); + error =__sock_create_kern (net, family, type, protocol, &sock);if (error <0 )return ERR_PTR (error);
net/sunrpc/xprtsock.c +3 -3
@@ -1924,7 +1924,7 @@ static struct socket *xs_create_sock(struct rpc_xprt *xprt,struct socket *sock;int err; - err =sock_create_kern (xprt->xprt_net, family, type, protocol, &sock); + err =__sock_create_kern (xprt->xprt_net, family, type, protocol, &sock);if (err <0 ) {dprintk ("RPC: can't create %d transport socket (%d).\n" , protocol, -err);
@@ -1999,8 +1999,8 @@ static int xs_local_setup_socket(struct sock_xprt *transport)struct socket *sock;int status; - status =sock_create_kern (xprt->xprt_net,AF_LOCAL , -SOCK_STREAM ,0 , &sock); + status =__sock_create_kern (xprt->xprt_net,AF_LOCAL , +SOCK_STREAM ,0 , &sock);if (status <0 ) {dprintk ("RPC: can't create AF_LOCAL " "transport socket (%d).\n" , -status);
net/tipc/topsrv.c +2 -2
@@ -515,7 +515,7 @@ static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)struct sock *sk;int rc; - rc =sock_create_kern (srv->net,AF_TIPC ,SOCK_SEQPACKET ,0 , &lsock); + rc =__sock_create_kern (srv->net,AF_TIPC ,SOCK_SEQPACKET ,0 , &lsock);if (rc <0 )return rc;
@@ -553,7 +553,7 @@ static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)* after TIPC module is inserted successfully. * * However, the reference count is ever increased twice in -* sock_create_kern(): one is to increase the reference count of owner +* __sock_create_kern(): one is to increase the reference count of owner * of TIPC socket's proto_ops struct; another is to increment the * reference count of owner of TIPC proto struct. Therefore, we must * decrement the module reference count twice to ensure that it keeps
net/wireless/nl80211.c +2 -2
@@ -13750,8 +13750,8 @@ static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev, port =nla_get_u16_default (tb[NL80211_WOWLAN_TCP_SRC_PORT ],0 );#ifdef CONFIG_INET /* allocate a socket and port for it and use it */ - err =sock_create_kern (wiphy_net (&rdev->wiphy),PF_INET ,SOCK_STREAM , -IPPROTO_TCP , &cfg->sock); + err =__sock_create_kern (wiphy_net (&rdev->wiphy),PF_INET ,SOCK_STREAM , +IPPROTO_TCP , &cfg->sock);if (err) {kfree (cfg);return err;
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +2 -2
@@ -804,8 +804,8 @@ __bpf_kfunc int bpf_kfunc_init_sock(struct init_sock_args *args) goto out; } - err =sock_create_kern (current->nsproxy->net_ns, args->af, args->type, - proto, &sock); + err =__sock_create_kern (current->nsproxy->net_ns, args->af, args->type, + proto, &sock);if (!err)/* Set timeout for call to kernel_connect() to prevent it from hanging,
Signature · 1 line
2.49.0
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 2/7] socket: Rename sock_create_kern() to __sock_create_kern().
Quoted 4 lines · Kuniyuki Iwashima
Let's rename
sock_create_kern()to__sock_create_kern()as a special API and add a fat documentation.The next patch will add
sock_create_kern()that holds netns refcnt.
Maybe do this before patch 1 to reduce the churn of just touching a lot of the same callers again?
Re: [PATCH v2 net-next 2/7] socket: Rename sock_create_kern() to __sock_create_kern().
Quoted 8 lines · On Mon, 26 May 2025 07:30:13 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · Kuniyuki Iwashima
Let's rename
sock_create_kern()to__sock_create_kern()as a special API and add a fat documentation.The next patch will add
sock_create_kern()that holds netns refcnt.Maybe do this before patch 1 to reduce the churn of just touching a lot of the same callers again?
You also really want untouched source files to fail to compile. If nothing else it'll stop backports going badly awry.
David
Re: [PATCH v2 net-next 2/7] socket: Rename sock_create_kern() to __sock_create_kern().
From: David Laight <[email protected]>
Date: Thu, 29 May 2025 22:29:11 +0100
Quoted 7 lines
Quoted 4 lines · On Mon, 26 May 2025 07:30:13 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 3 lines · Kuniyuki Iwashima
Let's rename
sock_create_kern()to__sock_create_kern()as a special API and add a fat documentation.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
The next patch will add
sock_create_kern()that holds netns refcnt.Maybe do this before patch 1 to reduce the churn of just touching a lot of the same callers again?
You also really want untouched source files to fail to compile. If nothing else it'll stop backports going badly awry.
I didn't get what you wanted to say, but I remember the series passed make all{yes,mod}config.
Re: [PATCH v2 net-next 2/7] socket: Rename sock_create_kern() to __sock_create_kern().
Quoted 11 lines · On Thu, 29 May 2025 20:05:32 -0700 Kuniyuki Iwashima <[email protected]> wrote:
From: David Laight <[email protected]>
Date: Thu, 29 May 2025 22:29:11 +0100Quoted 9 lines
Quoted 6 lines · On Mon, 26 May 2025 07:30:13 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · Kuniyuki Iwashima
Let's rename
sock_create_kern()to__sock_create_kern()as a special API and add a fat documentation.The next patch will add
sock_create_kern()that holds netns refcnt.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Maybe do this before patch 1 to reduce the churn of just touching a lot of the same callers again?
You also really want untouched source files to fail to compile. If nothing else it'll stop backports going badly awry.
I didn't get what you wanted to say, but I remember the series passed make all{yes,mod}config.
One effect of the series seems to be changing sock_create_kern() so that it 'holds' the network namespace.
Now if I backport one of the changed files to an old kernel version it will still compile but won't work properly.
(Maybe you've removed the call where it acquired the 'hold'.)
So while the patch series bisects (assuming it all goes through one tree - and it really needs to go through several) you are relying on any backports picking up the changes.
(And also the changes to sock_create_kern() not being picked up without all the other changes.)
Now backports ought to pick up the required dependant patches, but it is much better to generate compile fails when patches are missing.
Obscure run-time backport issues are annoying.
David
Re: [PATCH v2 net-next 2/7] socket: Rename sock_create_kern() to __sock_create_kern().
In reply to Christoph HellwigFrom: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:30:13 +0200
Quoted 8 lines · Kuniyuki Iwashima
Quoted 4 lines
Let's rename
sock_create_kern()to__sock_create_kern()as a special API and add a fat documentation.The next patch will add
sock_create_kern()that holds netns refcnt.Maybe do this before patch 1 to reduce the churn of just touching a lot of the same callers again?
Makes sense, will do.
PATCH v2 3/7 socket: Restore sock_create_kern().
In reply to Kuniyuki IwashimaLet'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.
---
v2: s/ret/err/ in sock_create_kern() for clarity
--- include/linux/net.h | 2 ++ net/socket.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
include/linux/net.h +2 -0
@@ -254,6 +254,8 @@ bool sock_is_registered(int family);int sock_create (int family,int type,int proto,struct socket **res);int __sock_create_kern (struct net *net,int family,int type,int proto,struct socket **res); +int sock_create_kern (struct net *net,int family,int type,int proto, +struct socket **res);int sock_create_lite (int family,int type,int proto,struct socket **res);struct socket *sock_alloc (void );void sock_release (struct socket *sock);
net/socket.c +42 -0
48 lines not on this page · raw message
Signature · 1 line
2.49.0
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 3/7] socket: Restore sock_create_kern().
Quoted 9 lines · Kuniyuki Iwashima
Let's restore
sock_create_kern()that holds a netns reference.Now, it's the same as the version before commit
26abe14379f8("net:
Modifysk_allocto not reference count the netns of kernel sockets.").Back then, after creating a socket in
init_net, we usedsk_change_net()to drop the netns ref and switch to another netns, but now we can simply use__sock_create_kern()instead.
Quoted 5 lines
$ git blame -L:sk_change_net include/net/sock.h 26abe14379f8~
DEBUG_NET_WARN_ON_ONCE()is to catch a path callingsock_create_kern()from__net_initfunctions, since doing so would leak the netns as__net_exitfunctions cannot run until the socket is removed.
Is reusing the name as the old sock_create_kern a good idea? It can lead to bugs by people used to the old semantics. It's also not really an all that descriptive name for either variant. I'm not really a net stack or namespace expert, but maybe we can come up with more descriptive version for both this new sock_create_kern and the old sock_create_kern/__sock_create_kern?
Re: [PATCH v2 net-next 3/7] socket: Restore sock_create_kern().
From: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:32:27 +0200
Quoted 10 lines · Kuniyuki Iwashima
Quoted 9 lines
Let's restore
sock_create_kern()that holds a netns reference.Now, it's the same as the version before commit
26abe14379f8("net:
Modifysk_allocto not reference count the netns of kernel sockets.").Back then, after creating a socket in
init_net, we usedsk_change_net()to drop the netns ref and switch to another netns, but now we can simply use__sock_create_kern()instead.
Quoted 8 lines
Quoted 5 lines
$ git blame -L:sk_change_net include/net/sock.h 26abe14379f8~
DEBUG_NET_WARN_ON_ONCE()is to catch a path callingsock_create_kern()from__net_initfunctions, since doing so would leak the netns as__net_exitfunctions cannot run until the socket is removed.Is reusing the name as the old
sock_create_kerna good idea? It can lead to bugs by people used to the old semantics.
In the old days, sock_create_kern() did take a ref to netns, but an implicit change that avoids taking the ref has caused a lot of problems for people who used to the old semantics.
This series rather rolls back the change, so I think using the same name here is better than leaving the catchy sock_create_kern() error-prone.
Quoted 5 lines
It's also
not really an all that descriptive name for either variant. I'm not really a net stack or namespace expert, but maybe we can come up with more descriptive version for both this newsock_create_kernand the old sock_create_kern/__sock_create_kern?
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 3/7] socket: Restore sock_create_kern().
Quoted 7 lines · Kuniyuki Iwashima
In the old days,
sock_create_kern()did take a ref to netns, but an implicit change that avoids taking the ref has caused a lot of problems for people who used to the old semantics.This series rather rolls back the change, so I think using the same name here is better than leaving the catchy
sock_create_kern()error-prone.
Ok.
Re: [PATCH v2 net-next 3/7] socket: Restore sock_create_kern().
Quoted 4 lines · On Mon, 2 Jun 2025 07:08:17 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 3 lines · Kuniyuki Iwashima
In the old days,
sock_create_kern()did take a ref to netns, but an implicit change that avoids taking the ref has caused a lot of problems for people who used to the old semantics.
That must have been a long time ago.
Was it even long after the namespace code was added? (I don't have a system with the git tree up at the moment)
Quoted 6 lines
Quoted 4 lines
This series rather rolls back the change, so I think using the same name here is better than leaving the catchy
sock_create_kern()error-prone.Ok.
Except that you are changing the semantics again.
So you end up with the same problem the other way around. I can imagine code ending up with an extra reference to the ns.
The obvious name a a function for general driver use would be kernel_socket() - matching the other functions that were added when set_fs(KERNEL_DS) was removed.
I definitely aim to end up where the existing code fails to compile - just to ensure all the code is found.
David
Re: [PATCH v2 net-next 3/7] socket: Restore sock_create_kern().
From: David Laight <[email protected]>
Date: Tue, 3 Jun 2025 22:30:20 +0100
Quoted 11 lines
Quoted 4 lines · On Mon, 2 Jun 2025 07:08:17 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 3 lines · Kuniyuki Iwashima
In the old days,
sock_create_kern()did take a ref to netns, but an implicit change that avoids taking the ref has caused a lot of problems for people who used to the old semantics.That must have been a long time ago.
Was it even long after the namespace code was added? (I don't have a system with the git tree up at the moment)
2007: 1b8d7ae42d02 ("[NET]: Make socket creation namespace safe.")
2015: 26abe14379f8 ("net: Modify sk_alloc to not reference count the netns of kernel sockets.")
It's been long since the implicit change, but it's only _recently_ that people started to notice the issue thanks?/due to k8s use cases, e.g. fs mounted in netns (ef7134c7fc48, 1be52169c348 + b013b817f32f, etc).
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
This series rather rolls back the change, so I think using the same name here is better than leaving the catchy
sock_create_kern()error-prone.Ok.
Except that you are changing the semantics again.
So you end up with the same problem the other way around. I can imagine code ending up with an extra reference to the ns.
I don't think so because it's rare case where we want to use the no-refcnt version and it usually happens under net/ or drivers/net.
Now we have SOCKET entry in MAINTAINERS so I can add sock_create there so that we are always CCed to prevent such issues.
Quoted 4 lines
The obvious name a a function for general driver use would be
kernel_socket()- matching the other functions that were added whenset_fs(KERNEL_DS)was removed.
kernel_socket() doesn't fit here as kernel_XXX() takes struct socket, not struct sock.
Quoted 3 lines
I definitely aim to end up where the existing code fails to compile - just to ensure all the code is found.
You can see the patch 2 renaming sock_create_kern() to __sock_create_kern() does the job to find all users with the help of compilers.
PATCH v2 4/7 smb: client: Add missing net_passive_dec().
In reply to Kuniyuki IwashimaWhile reverting commit e9f2517a3e18 ("smb: client: fix TCP timers deadlock after rmmod"), I should have added net_passive_dec(), which was added between the original commit and the revert by commit 5c70eb5c593d ("net:
better track kernel sockets lifetime").
Let's call net_passive_dec() in generic_ip_connect().
Note that this commit is only needed for 6.14+.
--- fs/smb/client/connect.c | 1 + 1 file changed, 1 insertion(+)
fs/smb/client/connect.c +1 -0
@@ -3359,6 +3359,7 @@ generic_ip_connect(struct TCP_Server_Info *server) sk = server->ssocket->sk;__netns_tracker_free (net, &sk->ns_tracker, false); +net_passive_dec (net); sk->sk_net_refcnt =1 ;get_net_track (net, &sk->ns_tracker,GFP_KERNEL );sock_inuse_add (net,1 );
Signature · 1 line
2.49.0
PATCH v2 5/7 socket: Remove kernel socket conversion except for net/rds/.
In reply to Kuniyuki IwashimaSince commit 26abe14379f8 ("net: Modify sk_alloc to not reference count the netns of kernel sockets."), TCP kernel socket has caused many UAF.
We have converted such sockets to hold netns refcnt, and we have the same pattern in cifs, mptcp, nvme, rds, smc, and sunrpc.
__sock_create_kern(..., &sock); sk_net_refcnt_upgrade(sock->sk);
Let's drop the conversion and use sock_create_kern() instead.
The changes for cifs, mptcp, nvme, and smc are straightforward.
For sunrpc, we call sk_net_refcnt_upgrade() for IPPROTO_TCP only so we use sock_create_kern() for TCP and use __sock_create_kern() for others.
For rds, we cannot drop sk_net_refcnt_upgrade() for accept()ed sockets.
---
v2: Drop unnecessary change for sunrpc and updated changelog for sunrpc
--- drivers/nvme/host/tcp.c | 7 +++---- fs/smb/client/connect.c | 12 ++---------- net/mptcp/subflow.c | 7 +------ net/smc/af_smc.c | 18 ++---------------- net/sunrpc/svcsock.c | 6 ++++-- net/sunrpc/xprtsock.c | 8 ++++---- 6 files changed, 16 insertions(+), 42 deletions(-)
drivers/nvme/host/tcp.c +3 -4
@@ -1756,9 +1756,9 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, queue->cmnd_capsule_len =sizeof (struct nvme_command ) +NVME_TCP_ADMIN_CCSZ ; - ret =__sock_create_kern (current->nsproxy->net_ns, - ctrl->addr.ss_family,SOCK_STREAM , -IPPROTO_TCP , &queue->sock); + ret =sock_create_kern (current->nsproxy->net_ns, + ctrl->addr.ss_family,SOCK_STREAM , +IPPROTO_TCP , &queue->sock);if (ret) {dev_err (nctrl->device,"failed to create socket: %d\n" , ret);
@@ -1771,7 +1771,6 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, goto err_destroy_mutex; } -sk_net_refcnt_upgrade (queue->sock->sk);nvme_tcp_reclassify_socket (queue->sock);/* Single syn retry */
fs/smb/client/connect.c +2 -10
@@ -3348,22 +3348,14 @@ generic_ip_connect(struct TCP_Server_Info *server) socket = server->ssocket; }else {struct net *net =cifs_net_ns (server); -struct sock *sk; - rc =__sock_create_kern (net, sfamily,SOCK_STREAM , -IPPROTO_TCP , &server->ssocket); + rc =sock_create_kern (net, sfamily,SOCK_STREAM , +IPPROTO_TCP , &server->ssocket);if (rc <0 ) {cifs_server_dbg (VFS ,"Error %d creating socket\n" , rc);return rc; } - sk = server->ssocket->sk; -__netns_tracker_free (net, &sk->ns_tracker, false); -net_passive_dec (net); - sk->sk_net_refcnt =1 ; -get_net_track (net, &sk->ns_tracker,GFP_KERNEL ); -sock_inuse_add (net,1 ); -/* BB other socket options to set KEEPALIVE, NODELAY? */ cifs_dbg (FYI ,"Socket created\n" ); socket = server->ssocket;
net/mptcp/subflow.c +1 -6
@@ -1757,7 +1757,7 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,if (unlikely (!sk->sk_socket))return -EINVAL ; - err =__sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , &sf); + err =sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , &sf);if (err)return err;
@@ -1770,11 +1770,6 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,/* the newly created socket has to be in the same cgroup as its parent */ mptcp_attach_cgroup (sk, sf->sk); -/* kernel sockets do not by default acquire net ref, but TCP timer -* needs it. -* Update ns_tracker to current stack trace and refcounted tracker. -*/ -sk_net_refcnt_upgrade (sf->sk); err =tcp_set_ulp (sf->sk,"mptcp" );if (err) goto err_free;
net/smc/af_smc.c +2 -16
@@ -3328,22 +3328,8 @@ static const struct proto_ops smc_sock_ops = {int smc_create_clcsk (struct net *net,struct sock *sk,int family) { -struct smc_sock *smc =smc_sk (sk); -int rc; - - rc =__sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , - &smc->clcsock); -if (rc) -return rc; - -/* smc_clcsock_release() does not wait smc->clcsock->sk's -* destruction; its sk_state might not be TCP_CLOSE after -* smc->sk is close()d, and TCP timers can be fired later, -* which need net ref. -*/ - sk = smc->clcsock->sk; -sk_net_refcnt_upgrade (sk); -return 0 ; +return sock_create_kern (net, family,SOCK_STREAM ,IPPROTO_TCP , + &smc_sk (sk)->clcsock); } static int __smc_create(struct net *net, struct socket *sock, int protocol,
net/sunrpc/svcsock.c +4 -2
@@ -1516,7 +1516,10 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,return ERR_PTR (-EINVAL ); } - error =__sock_create_kern (net, family, type, protocol, &sock); +if (protocol ==IPPROTO_TCP ) + error =sock_create_kern (net, family, type, protocol, &sock); +else + error =__sock_create_kern (net, family, type, protocol, &sock);if (error <0 )return ERR_PTR (error);
@@ -1541,7 +1544,6 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv, newlen = error;if (protocol ==IPPROTO_TCP ) { -sk_net_refcnt_upgrade (sock->sk);if ((error =kernel_listen (sock,64 )) <0 ) goto bummer; }
net/sunrpc/xprtsock.c +4 -4
@@ -1924,7 +1924,10 @@ static struct socket *xs_create_sock(struct rpc_xprt *xprt,struct socket *sock;int err; - err =__sock_create_kern (xprt->xprt_net, family, type, protocol, &sock); +if (protocol ==IPPROTO_TCP ) + err =sock_create_kern (xprt->xprt_net, family, type, protocol, &sock); +else + err =__sock_create_kern (xprt->xprt_net, family, type, protocol, &sock);if (err <0 ) {dprintk ("RPC: can't create %d transport socket (%d).\n" , protocol, -err);
@@ -1941,9 +1944,6 @@ static struct socket *xs_create_sock(struct rpc_xprt *xprt, goto out; } -if (protocol ==IPPROTO_TCP ) -sk_net_refcnt_upgrade (sock->sk); - filp =sock_alloc_file (sock,O_NONBLOCK ,NULL );if (IS_ERR (filp))return ERR_CAST (filp);
Signature · 1 line
2.49.0
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 5/7] socket: Remove kernel socket conversion except for net/rds/.
Quoted 1 line · Kuniyuki Iwashima
Let's drop the conversion and use
sock_create_kern()instead.
Please send a patch per subsystem that is converted to make the commit log better and help with bisectability.
Re: [PATCH v2 net-next 5/7] socket: Remove kernel socket conversion except for net/rds/.
From: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:33:21 +0200
Quoted 5 lines · Kuniyuki Iwashima
Quoted 1 line
Let's drop the conversion and use
sock_create_kern()instead.Please send a patch per subsystem that is converted to make the commit log better and help with bisectability.
Do you mean splitting this patch into per-subsystem patches within the same series or sending non-netdev patches separately ?
The former is fine, but I think this change should be done in the same series as the main goal of the series is the changes in this patch, making kernel TCP sockets hold netns ref in a single place.
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 5/7] socket: Remove kernel socket conversion except for net/rds/.
Quoted 10 lines · Kuniyuki Iwashima
From: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:33:21 +0200Quoted 5 lines · Kuniyuki Iwashima
Quoted 1 line
Let's drop the conversion and use
sock_create_kern()instead.Please send a patch per subsystem that is converted to make the commit log better and help with bisectability.
Do you mean splitting this patch into per-subsystem patches within the same series or sending non-netdev patches separately ?
Please send them in the same series for now. I think they can go in together, but without splitting them out it's hard to even get all the reuqired attention.
PATCH v2 6/7 socket: Replace most sock_create() calls with sock_create_kern().
In reply to Kuniyuki IwashimaExcept for only one user, sctp_do_peeloff(), all sockets created by drivers and fs are not tied to userspace processes nor exposed via file descriptors.
Let's use sock_create_kern() for such in-kernel use cases as CIFS client and NFS.
--- drivers/infiniband/hw/erdma/erdma_cm.c | 6 ++++-- drivers/infiniband/sw/siw/siw_cm.c | 6 ++++-- drivers/isdn/mISDN/l1oip_core.c | 3 ++- drivers/nvme/target/tcp.c | 5 +++-- drivers/target/iscsi/iscsi_target_login.c | 7 ++++--- drivers/xen/pvcalls-back.c | 6 ++++-- fs/ocfs2/cluster/tcp.c | 8 +++++--- fs/smb/server/transport_tcp.c | 7 ++++--- 8 files changed, 30 insertions(+), 18 deletions(-)
drivers/infiniband/hw/erdma/erdma_cm.c +4 -2
@@ -1026,7 +1026,8 @@ int erdma_connect(struct iw_cm_id *id, struct iw_cm_conn_param *params)return -ENOENT ;erdma_qp_get (qp); - ret =sock_create (AF_INET ,SOCK_STREAM ,IPPROTO_TCP , &s); + ret =sock_create_kern (current->nsproxy->net_ns, +AF_INET ,SOCK_STREAM ,IPPROTO_TCP , &s);if (ret <0 ) goto error_put_qp;
@@ -1305,7 +1306,8 @@ int erdma_create_listen(struct iw_cm_id *id, int backlog)if (addr_family !=AF_INET )return -EAFNOSUPPORT ; - ret =sock_create (addr_family,SOCK_STREAM ,IPPROTO_TCP , &s); + ret =sock_create_kern (current->nsproxy->net_ns, + addr_family,SOCK_STREAM ,IPPROTO_TCP , &s);if (ret <0 )return ret;
drivers/infiniband/sw/siw/siw_cm.c +4 -2
@@ -1391,7 +1391,8 @@ int siw_connect(struct iw_cm_id *id, struct iw_cm_conn_param *params)siw_dbg_qp (qp,"pd_len %d, laddr %pISp, raddr %pISp\n" , pd_len, laddr, raddr); - rv =sock_create (v4 ?AF_INET :AF_INET6 ,SOCK_STREAM ,IPPROTO_TCP , &s); + rv =sock_create_kern (current->nsproxy->net_ns, + v4 ?AF_INET :AF_INET6 ,SOCK_STREAM ,IPPROTO_TCP , &s);if (rv <0 ) goto error;
@@ -1767,7 +1768,8 @@ int siw_create_listen(struct iw_cm_id *id, int backlog)if (addr_family !=AF_INET && addr_family !=AF_INET6 )return -EAFNOSUPPORT ; - rv =sock_create (addr_family,SOCK_STREAM ,IPPROTO_TCP , &s); + rv =sock_create_kern (current->nsproxy->net_ns, + addr_family,SOCK_STREAM ,IPPROTO_TCP , &s);if (rv <0 )return rv;
drivers/isdn/mISDN/l1oip_core.c +2 -1
@@ -659,7 +659,8 @@ l1oip_socket_thread(void *data)allow_signal (SIGTERM );/* create socket */ -if (sock_create (PF_INET ,SOCK_DGRAM ,IPPROTO_UDP , &socket)) { +if (sock_create_kern (current->nsproxy->net_ns, +PF_INET ,SOCK_DGRAM ,IPPROTO_UDP , &socket)) {printk (KERN_ERR "%s: Failed to create socket.\n" , __func__); ret = -EIO ; goto fail;
drivers/nvme/target/tcp.c +3 -2
@@ -2078,8 +2078,9 @@ static int nvmet_tcp_add_port(struct nvmet_port *nport)if (port->nport->inline_data_size <0 ) port->nport->inline_data_size =NVMET_TCP_DEF_INLINE_DATA_SIZE ; - ret =sock_create (port->addr.ss_family,SOCK_STREAM , -IPPROTO_TCP , &port->sock); + ret =sock_create_kern (current->nsproxy->net_ns, + port->addr.ss_family,SOCK_STREAM , +IPPROTO_TCP , &port->sock);if (ret) {pr_err ("failed to create a socket\n" ); goto err_port;
drivers/target/iscsi/iscsi_target_login.c +4 -3
@@ -796,10 +796,11 @@ int iscsit_setup_np(return -EINVAL ; } - ret =sock_create (sockaddr->ss_family, np->np_sock_type, - np->np_ip_proto, &sock); + ret =sock_create_kern (current->nsproxy->net_ns, + sockaddr->ss_family, np->np_sock_type, + np->np_ip_proto, &sock);if (ret <0 ) { -pr_err ("sock_create() failed.\n" ); +pr_err ("sock_create_kern() failed.\n" );return ret; } np->np_socket = sock;
drivers/xen/pvcalls-back.c +4 -2
@@ -406,7 +406,8 @@ static int pvcalls_back_connect(struct xenbus_device *dev, sa->sa_family != AF_INET) goto out; - ret =sock_create (AF_INET ,SOCK_STREAM ,0 , &sock); + ret =sock_create_kern (current->nsproxy->net_ns, +AF_INET ,SOCK_STREAM ,0 , &sock);if (ret <0 ) goto out; ret =inet_stream_connect (sock, sa, req->u.connect.len,0 );
@@ -646,7 +647,8 @@ static int pvcalls_back_bind(struct xenbus_device *dev, goto out; } - ret =sock_create (AF_INET ,SOCK_STREAM ,0 , &map->sock); + ret =sock_create_kern (current->nsproxy->net_ns, +AF_INET ,SOCK_STREAM ,0 , &map->sock);if (ret <0 ) goto out;
fs/ocfs2/cluster/tcp.c +5 -3
@@ -1558,7 +1558,7 @@ static void o2net_start_connect(struct work_struct *work)unsigned int nofs_flag;/* -* sock_create allocates the sock with GFP_KERNEL. We must +* sock_create_kern() allocates the sock with GFP_KERNEL. We must * prevent the filesystem from being reentered by memory reclaim. */ nofs_flag =memalloc_nofs_save ();
@@ -1600,7 +1600,8 @@ static void o2net_start_connect(struct work_struct *work) goto out; } - ret =sock_create (PF_INET ,SOCK_STREAM ,IPPROTO_TCP , &sock); + ret =sock_create_kern (current->nsproxy->net_ns, +PF_INET ,SOCK_STREAM ,IPPROTO_TCP , &sock);if (ret <0 ) {mlog (0 ,"can't create socket: %d\n" , ret); goto out;
@@ -1984,7 +1985,8 @@ static int o2net_open_listening_sock(__be32 addr, __be16 port) .sin_port = port, }; - ret =sock_create (PF_INET ,SOCK_STREAM ,IPPROTO_TCP , &sock); + ret =sock_create_kern (current->nsproxy->net_ns, +PF_INET ,SOCK_STREAM ,IPPROTO_TCP , &sock);if (ret <0 ) {printk (KERN_ERR "o2net: Error %d while creating socket\n" , ret); goto out;
fs/smb/server/transport_tcp.c +4 -3
@@ -427,18 +427,19 @@ static void tcp_destroy_socket(struct socket *ksmbd_socket)*/ static int create_socket (struct interface *iface) { +struct net *net = current->nsproxy->net_ns;int ret;struct sockaddr_in6 sin6;struct sockaddr_in sin;struct socket *ksmbd_socket;bool ipv4 = false; - ret =sock_create (PF_INET6 ,SOCK_STREAM ,IPPROTO_TCP , &ksmbd_socket); + ret =sock_create_kern (net,PF_INET6 ,SOCK_STREAM ,IPPROTO_TCP , &ksmbd_socket);if (ret) {if (ret != -EAFNOSUPPORT )pr_err ("Can't create socket for ipv6, fallback to ipv4: %d\n" , ret); - ret =sock_create (PF_INET ,SOCK_STREAM ,IPPROTO_TCP , - &ksmbd_socket); + ret =sock_create_kern (net,PF_INET ,SOCK_STREAM ,IPPROTO_TCP , + &ksmbd_socket);if (ret) {pr_err ("Can't create socket for ipv4: %d\n" , ret); goto out_clear;
Signature · 1 line
2.49.0
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
Quoted 6 lines · Kuniyuki Iwashima
Except for only one user,
sctp_do_peeloff(), all sockets created by drivers and fs are not tied to userspace processes nor exposed via file descriptors.Let's use
sock_create_kern()for such in-kernel use cases as CIFS client and NFS.
Same thing, one patch per subsystem please.
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
In reply to Kuniyuki IwashimaQuoted 6 lines · Kuniyuki Iwashima
Except for only one user,
sctp_do_peeloff(), all sockets created by drivers and fs are not tied to userspace processes nor exposed via file descriptors.Let's use
sock_create_kern()for such in-kernel use cases as CIFS client and NFS.
So if sock_create is now almost unused and the special case, should it also be renamed to make that explicit and make people not accidentally use it by default?
Re: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
From: Christoph Hellwig <[email protected]>
Date: Mon, 26 May 2025 07:35:55 +0200
Quoted 11 lines · Kuniyuki Iwashima
Quoted 6 lines
Except for only one user,
sctp_do_peeloff(), all sockets created by drivers and fs are not tied to userspace processes nor exposed via file descriptors.Let's use
sock_create_kern()for such in-kernel use cases as CIFS client and NFS.So if
sock_createis now almost unused and the special case, should it also be renamed to make that explicit and make people not accidentally use it by default?
I actually tried to to do so as sock_create_user() in the previous series but was advised to avoid rename as the benefit against LoC was low.
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
Quoted 3 lines · Kuniyuki Iwashima
I actually tried to to do so as
sock_create_user()in the previous series but was advised to avoid rename as the benefit against LoC was low.
I can't really parse this. What is the 'benefit against LoC'?
Re: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
From: Christoph Hellwig <[email protected]>
Date: Mon, 2 Jun 2025 07:09:49 +0200
Quoted 6 lines · Kuniyuki Iwashima
Quoted 3 lines
I actually tried to to do so as
sock_create_user()in the previous series but was advised to avoid rename as the benefit against LoC was low.I can't really parse this. What is the 'benefit against LoC'?
It was a kind of subjective opinion whether the amount of changes was worth or not.
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
Quoted 11 lines · Kuniyuki Iwashima
From: Christoph Hellwig <[email protected]>
Date: Mon, 2 Jun 2025 07:09:49 +0200Quoted 6 lines · Kuniyuki Iwashima
Quoted 3 lines
I actually tried to to do so as
sock_create_user()in the previous series but was advised to avoid rename as the benefit against LoC was low.I can't really parse this. What is the 'benefit against LoC'?
It was a kind of subjective opinion whether the amount of changes was worth or not.
So the simple scripted renaming was not worth it. Maybe I misunderstand, but based on the reading we should basically have about a handful callers of the non-__kern variant left. Or is it a lot more?
Re: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
From: Christoph Hellwig <[email protected]>
Date: Tue, 3 Jun 2025 06:50:21 +0200
Quoted 8 lines · Kuniyuki Iwashima
Quoted 7 lines
From: Christoph Hellwig <[email protected]>
Date: Mon, 2 Jun 2025 07:09:49 +0200Quoted 5 lines · Kuniyuki Iwashima
Quoted 3 lines
I actually tried to to do so as
sock_create_user()in the previous series but was advised to avoid rename as the benefit against LoC was low.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
I can't really parse this. What is the 'benefit against LoC'?
It was a kind of subjective opinion whether the amount of changes was worth or not.
So the simple scripted renaming was not worth it. Maybe I misunderstand, but based on the reading we should basically have about a handful callers of the non-__kern variant left. Or is it a lot more?
Yes, after this series, only 2 sock_create() left, one in sctp and another in core.
Christoph Hellwig
maintainerRe: [PATCH v2 net-next 6/7] socket: Replace most sock_create() calls with sock_create_kern().
Quoted 6 lines · Kuniyuki Iwashima
Quoted 3 lines
So the simple scripted renaming was not worth it. Maybe I misunderstand, but based on the reading we should basically have about a handful callers of the non-__kern variant left. Or is it a lot more?
Yes, after this series, only 2
sock_create()left, one in sctp and another in core.
Sounds easy enough to rename then, and doing so is useful go guide people away from it.
PATCH v2 7/7 socket: Clean up kdoc for sock_create() and sock_create_lite().
In reply to Kuniyuki Iwashima__sock_create() is now static and the same doc exists on sock_create() and sock_create_kern().
Also, __sock_create() says "On failure @res is set to %NULL.", but this is always false.
In addition, the old style kdoc is a bit corrupted and we can't see the DESCRIPTION section:
$ scripts/kernel-doc -man net/socket.c | scripts/split-man.pl /tmp/man $ man /tmp/man/sock_create.9
Let's clean them up.
--- net/socket.c | 58 ++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 33 deletions(-)
net/socket.c +25 -33
@@ -1315,18 +1315,20 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) }/** -* sock_create_lite - creates a socket -* @family: protocol family (AF_INET, ...) -* @type: communication type (SOCK_STREAM, ...) -* @protocol: protocol (0, ...) -* @res: new socket +* sock_create_lite - creates a socket * -* Creates a new socket and assigns it to @res, passing through LSM. -* The new socket initialization is not complete, see kernel_accept(). -* Returns 0 or an error. On failure @res is set to %NULL. -* This function internally uses GFP_KERNEL. +* @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, passing through LSM. +* +* The new socket initialization is not complete, see kernel_accept(). +* +* Context: Process context. This function internally uses GFP_KERNEL. +* Return: 0 or an error. On failure @res is set to %NULL. */ -int sock_create_lite (int family,int type,int protocol,struct socket **res) {int err;
@@ -1452,21 +1454,6 @@ int sock_wake_async(struct socket_wq *wq, int how, int band) }EXPORT_SYMBOL (sock_wake_async); -/** -* __sock_create - creates a socket -* @net: net namespace -* @family: protocol family (AF_INET, ...) -* @type: communication type (SOCK_STREAM, ...) -* @protocol: protocol (0, ...) -* @res: new socket -* @kern: boolean for kernel space sockets -* -* Creates a new socket and assigns it to @res, passing through LSM. -* Returns 0 or an error. On failure @res is set to %NULL. @kern must -* be set to true if the socket resides in kernel space. -* This function internally uses GFP_KERNEL. -*/ -static int __sock_create (struct net *net,int family,int type,int protocol,struct socket **res,int kern) {
@@ -1583,16 +1570,21 @@ static int __sock_create(struct net *net, int family, int type, int protocol, }/** -* sock_create - creates a socket -* @family: protocol family (AF_INET, ...) -* @type: communication type (SOCK_STREAM, ...) -* @protocol: protocol (0, ...) -* @res: new socket +* sock_create - creates a socket for userspace +* +* @family: protocol family (AF_INET, ...) +* @type: communication type (SOCK_STREAM, ...) +* @protocol: protocol (0, ...) +* @res: new socket * -* A wrapper around __sock_create(). -* Returns 0 or an error. This function internally uses GFP_KERNEL. +* Creates a new socket and assigns it to @res, passing through LSM. +* +* The socket is for userspace and should be exposed via a file +* descriptor and BPF hooks (see inet_create(), inet_release(), etc). +* +* Context: Process context. This function internally uses GFP_KERNEL. +* Return: 0 or an error. */ -int sock_create (int family,int type,int protocol,struct socket **res) {return __sock_create (current->nsproxy->net_ns, family, type, protocol, res,0 );
Signature · 1 line
2.49.0