mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid()
@ 2026-11-19 10:04 Ziheng Wei
  2026-11-19 10:07 ` Eric Dumazet
  2026-11-21 19:01 ` Kuniyuki Iwashima
  0 siblings, 2 replies; 3+ messages in thread
From: Ziheng Wei @ 2026-11-19 10:04 UTC (permalink / raw)
  To: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Kuniyuki Iwashima, Willem de Bruijn, Simon Horman, netdev, linux-kernel

Since commit 5d6b58c932ec ("net: lockless sock_i_ino()"),
sk_set_socket() reads i_uid and i_ino through SOCK_INODE(sock).
sock_init_data_uid() calls sk_set_socket(), but its only direct users
are tun and tap, which pass &tfile->socket and &q->sock. Those sockets
are embedded in struct tun_file and struct tap_queue, not in a struct
socket_alloc, so SOCK_INODE() points at whatever follows the socket in
those structures and both reads return unrelated data.

sk_uid is overwritten a few lines later with the uid passed in, and
nothing reads sk_ino of a tun or tap socket today, so the result is
not used. The reads themselves are still out of type, and with
CONFIG_RANDSTRUCT the inode fields can land beyond the end of struct
tap_queue.

sock_init_data_uid() was added so that tun and tap would not use
SOCK_INODE() on these sockets. Set sk_socket directly there, clear
sk_ino, and let sock_init_data(), whose sockets always come from
sockfs, fill in sk_ino.

Found by LLM-assisted source review and confirmed by code inspection
only; the out-of-type reads were not triggered at runtime. The fix was
build tested only; it was not run against selftests.

Fixes: 5d6b58c932ec ("net: lockless sock_i_ino()")
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---

Notes:
    Changes in v2:
    - Use plain assignments for sk_socket and sk_ino during socket
      initialization, before the socket is published (suggested by Kuniyuki
      Iwashima)
    - v1: https://lore.kernel.org/all/20261115161820.941166-1-zihengw03@163.com/
    
    The changelog was drafted with an AI assistant from my analysis;
    I have reviewed the code, the fix and the text.

 net/core/sock.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/net/core/sock.c b/net/core/sock.c
index ad414878d..ce7e1cf0d 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3831,7 +3831,11 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
 	sk->sk_sndbuf		=	READ_ONCE(sysctl_wmem_default);
 	sk->sk_state		=	TCP_CLOSE;
 	sk->sk_use_task_frag	=	true;
-	sk_set_socket(sk, sock);
+	/* tun and tap pass a socket that is not part of a socket_alloc, so
+	 * SOCK_INODE() must not be used here. sock_init_data() sets sk_ino.
+	 */
+	sk->sk_socket = sock;
+	sk->sk_ino = 0;
 
 	sock_set_flag(sk, SOCK_ZAPPED);
 
@@ -3897,6 +3901,8 @@ void sock_init_data(struct socket *sock, struct sock *sk)
 		make_kuid(sock_net(sk)->user_ns, 0);
 
 	sock_init_data_uid(sock, sk, uid);
+	if (sock)
+		sk->sk_ino = SOCK_INODE(sock)->i_ino;
 }
 EXPORT_SYMBOL(sock_init_data);
 
base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0


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

* Re: [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid()
  2026-11-19 10:04 [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid() Ziheng Wei
@ 2026-11-19 10:07 ` Eric Dumazet
  2026-11-21 19:01 ` Kuniyuki Iwashima
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-11-19 10:07 UTC (permalink / raw)
  To: Ziheng Wei
  Cc: Andrew Lunn, David S . Miller, Jakub Kicinski, Paolo Abeni,
	Kuniyuki Iwashima, Willem de Bruijn, Simon Horman, netdev,
	linux-kernel

Le jeu. 19 nov. 2026 à 11:04, Ziheng Wei <zihengw03@163.com> a écrit :
>
> Since commit 5d6b58c932ec ("net: lockless sock_i_ino()"),
> sk_set_socket() reads i_uid and i_ino through SOCK_INODE(sock).
> sock_init_data_uid() calls sk_set_socket(), but its only direct users
> are tun and tap, which pass &tfile->socket and &q->sock. Those sockets
> are embedded in struct tun_file and struct tap_queue, not in a struct
> socket_alloc, so SOCK_INODE() points at whatever follows the socket in
> those structures and both reads return unrelated data.
>
> sk_uid is overwritten a few lines later with the uid passed in, and
> nothing reads sk_ino of a tun or tap socket today, so the result is
> not used. The reads themselves are still out of type, and with
> CONFIG_RANDSTRUCT the inode fields can land beyond the end of struct
> tap_queue.
>
> sock_init_data_uid() was added so that tun and tap would not use
> SOCK_INODE() on these sockets. Set sk_socket directly there, clear
> sk_ino, and let sock_init_data(), whose sockets always come from
> sockfs, fill in sk_ino.
>
> Found by LLM-assisted source review and confirmed by code inspection
> only; the out-of-type reads were not triggered at runtime. The fix was
> build tested only; it was not run against selftests.
>
> Fixes: 5d6b58c932ec ("net: lockless sock_i_ino()")
> Signed-off-by: Ziheng Wei <zihengw03@163.com>
> ---

Reviewed-by: Eric Dumazet <edumazet@kernel.org>

Thanks.

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

* Re: [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid()
  2026-11-19 10:04 [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid() Ziheng Wei
  2026-11-19 10:07 ` Eric Dumazet
@ 2026-11-21 19:01 ` Kuniyuki Iwashima
  1 sibling, 0 replies; 3+ messages in thread
From: Kuniyuki Iwashima @ 2026-11-21 19:01 UTC (permalink / raw)
  To: Ziheng Wei
  Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman, netdev,
	linux-kernel

On Thu, 19 Nov 2026 18:04:14 +0800, Ziheng Wei wrote:
> Since commit 5d6b58c932ec ("net: lockless sock_i_ino()"),
> sk_set_socket() reads i_uid and i_ino through SOCK_INODE(sock).
> sock_init_data_uid() calls sk_set_socket(), but its only direct users
> are tun and tap, which pass &tfile->socket and &q->sock. Those sockets
> are embedded in struct tun_file and struct tap_queue, not in a struct
> socket_alloc, so SOCK_INODE() points at whatever follows the socket in
> those structures and both reads return unrelated data.
> 
> sk_uid is overwritten a few lines later with the uid passed in, and
> nothing reads sk_ino of a tun or tap socket today, so the result is
> not used. The reads themselves are still out of type, and with
> CONFIG_RANDSTRUCT the inode fields can land beyond the end of struct
> tap_queue.
> 
> sock_init_data_uid() was added so that tun and tap would not use
> SOCK_INODE() on these sockets. Set sk_socket directly there, clear
> sk_ino, and let sock_init_data(), whose sockets always come from
> sockfs, fill in sk_ino.
> 
> Found by LLM-assisted source review and confirmed by code inspection
> only; the out-of-type reads were not triggered at runtime. The fix was
> build tested only; it was not run against selftests.
> 
> Fixes: 5d6b58c932ec ("net: lockless sock_i_ino()")
> Signed-off-by: Ziheng Wei <zihengw03@163.com>

Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>

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

end of thread, other threads:[~2026-11-21 19:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 10:04 [PATCH net-next v2] net: do not use SOCK_INODE() in sock_init_data_uid() Ziheng Wei
2026-11-19 10:07 ` Eric Dumazet
2026-11-21 19:01 ` Kuniyuki Iwashima

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®