[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAAVpQUAvDMCLySn_gW+abifVpt_=tH771ZzOxVM8mYsO6mgaMQ@mail.gmail.com>
Date: Fri, 21 Nov 2025 18:14:21 -0800
From: Kuniyuki Iwashima <kuniyu@...gle.com>
To: Gabriel Krisman Bertazi <krisman@...e.de>
Cc: Jens Axboe <axboe@...nel.dk>, netdev@...r.kernel.org, io-uring@...r.kernel.org,
Jakub Kicinski <kuba@...nel.org>, "David S. Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>, Paolo Abeni <pabeni@...hat.com>,
Willem de Bruijn <willemb@...gle.com>, Simon Horman <horms@...nel.org>
Subject: Re: [PATCH v2 1/3] socket: Unify getsockname and getpeername implementation
On Fri, Nov 21, 2025 at 8:10 AM Gabriel Krisman Bertazi <krisman@...e.de> wrote:
>
> They are already implemented by the same get_name hook in the protocol
> level. Bring the unification one level up to reduce code duplication
> in preparation to supporting these as io_uring operations.
>
> Signed-off-by: Gabriel Krisman Bertazi <krisman@...e.de>
> ---
> include/linux/socket.h | 4 +--
> net/compat.c | 4 +--
> net/socket.c | 55 ++++++++++--------------------------------
> 3 files changed, 16 insertions(+), 47 deletions(-)
>
> diff --git a/include/linux/socket.h b/include/linux/socket.h
> index 3b262487ec06..937fe331ff1e 100644
> --- a/include/linux/socket.h
> +++ b/include/linux/socket.h
> @@ -454,9 +454,7 @@ extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
> extern int __sys_listen(int fd, int backlog);
> extern int __sys_listen_socket(struct socket *sock, int backlog);
> extern int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
> - int __user *usockaddr_len);
> -extern int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
> - int __user *usockaddr_len);
> + int __user *usockaddr_len, int peer);
> extern int __sys_socketpair(int family, int type, int protocol,
> int __user *usockvec);
> extern int __sys_shutdown_sock(struct socket *sock, int how);
> diff --git a/net/compat.c b/net/compat.c
> index 485db8ee9b28..2c9bd0edac99 100644
> --- a/net/compat.c
> +++ b/net/compat.c
> @@ -460,10 +460,10 @@ COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
> ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
> break;
> case SYS_GETSOCKNAME:
> - ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
> + ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
> break;
> case SYS_GETPEERNAME:
> - ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
> + ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]), 1);
> break;
> case SYS_SOCKETPAIR:
> ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
> diff --git a/net/socket.c b/net/socket.c
> index e8892b218708..ee438b9425da 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -2128,12 +2128,11 @@ SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
> }
>
> /*
> - * Get the local address ('name') of a socket object. Move the obtained
> - * name to user space.
> + * Get the address (remote or local ('name')) of a socket object. Move the
nit: Get the remote or local address ('name')
Otherwise, looks good.
Reviewed-by: Kuniyuki Iwashima <kuniyu@...gle.com>
> + * obtained name to user space.
> */
> -
> int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
> - int __user *usockaddr_len)
> + int __user *usockaddr_len, int peer)
> {
> struct socket *sock;
> struct sockaddr_storage address;
> @@ -2146,11 +2145,14 @@ int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
> if (unlikely(!sock))
> return -ENOTSOCK;
>
> - err = security_socket_getsockname(sock);
> + if (peer)
> + err = security_socket_getpeername(sock);
> + else
> + err = security_socket_getsockname(sock);
> if (err)
> return err;
>
> - err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 0);
> + err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer);
> if (err < 0)
> return err;
>
> @@ -2161,44 +2163,13 @@ int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
> SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
> int __user *, usockaddr_len)
> {
> - return __sys_getsockname(fd, usockaddr, usockaddr_len);
> -}
> -
> -/*
> - * Get the remote address ('name') of a socket object. Move the obtained
> - * name to user space.
> - */
> -
> -int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
> - int __user *usockaddr_len)
> -{
> - struct socket *sock;
> - struct sockaddr_storage address;
> - CLASS(fd, f)(fd);
> - int err;
> -
> - if (fd_empty(f))
> - return -EBADF;
> - sock = sock_from_file(fd_file(f));
> - if (unlikely(!sock))
> - return -ENOTSOCK;
> -
> - err = security_socket_getpeername(sock);
> - if (err)
> - return err;
> -
> - err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 1);
> - if (err < 0)
> - return err;
> -
> - /* "err" is actually length in this case */
> - return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
> + return __sys_getsockname(fd, usockaddr, usockaddr_len, 0);
> }
>
> SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
> int __user *, usockaddr_len)
> {
> - return __sys_getpeername(fd, usockaddr, usockaddr_len);
> + return __sys_getsockname(fd, usockaddr, usockaddr_len, 1);
> }
>
> /*
> @@ -3162,12 +3133,12 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
> case SYS_GETSOCKNAME:
> err =
> __sys_getsockname(a0, (struct sockaddr __user *)a1,
> - (int __user *)a[2]);
> + (int __user *)a[2], 0);
> break;
> case SYS_GETPEERNAME:
> err =
> - __sys_getpeername(a0, (struct sockaddr __user *)a1,
> - (int __user *)a[2]);
> + __sys_getsockname(a0, (struct sockaddr __user *)a1,
> + (int __user *)a[2], 1);
> break;
> case SYS_SOCKETPAIR:
> err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
> --
> 2.51.0
>
Powered by blists - more mailing lists