Miroslav Franc 980b26
From: Pietro Borrello <borrello@diag.uniroma1.it>
Miroslav Franc 980b26
Date: Sat, 4 Feb 2023 17:39:20 +0000
Miroslav Franc 980b26
Subject: net: add sock_init_data_uid()
Miroslav Franc 980b26
Git-commit: 584f3742890e966d2f0a1f3c418c9ead70b2d99e
Miroslav Franc 980b26
Patch-mainline: v6.3-rc1
Miroslav Franc 980b26
References: CVE-2023-1076 bsc#1208599
Miroslav Franc 980b26
Miroslav Franc 980b26
Add sock_init_data_uid() to explicitly initialize the socket uid.
Miroslav Franc 980b26
To initialise the socket uid, sock_init_data() assumes a the struct
Miroslav Franc 980b26
socket* sock is always embedded in a struct socket_alloc, used to
Miroslav Franc 980b26
access the corresponding inode uid. This may not be true.
Miroslav Franc 980b26
Examples are sockets created in tun_chr_open() and tap_open().
Miroslav Franc 980b26
Miroslav Franc 980b26
Fixes: 86741ec25462 ("net: core: Add a UID field to struct sock.")
Miroslav Franc 980b26
Signed-off-by: Pietro Borrello <borrello@diag.uniroma1.it>
Miroslav Franc 980b26
Reviewed-by: Eric Dumazet <edumazet@google.com>
Miroslav Franc 980b26
Signed-off-by: David S. Miller <davem@davemloft.net>
Miroslav Franc 980b26
Acked-by: Miroslav Franc <mfranc@suse.cz>
Miroslav Franc 980b26
---
Miroslav Franc 980b26
 include/net/sock.h |  7 ++++++-
Miroslav Franc 980b26
 net/core/sock.c    | 15 ++++++++++++---
Miroslav Franc 980b26
 2 files changed, 18 insertions(+), 4 deletions(-)
Miroslav Franc 980b26
Miroslav Franc 980b26
diff --git a/include/net/sock.h b/include/net/sock.h
Miroslav Franc 980b26
index 22d50dabdc9d..da758f2273e4 100644
Miroslav Franc 980b26
--- a/include/net/sock.h
Miroslav Franc 980b26
+++ b/include/net/sock.h
Miroslav Franc 980b26
@@ -1739,7 +1739,12 @@ void sk_common_release(struct sock *sk);
Miroslav Franc 980b26
  *	Default socket callbacks and setup code
Miroslav Franc 980b26
  */
Miroslav Franc 980b26
 
Miroslav Franc 980b26
-/* Initialise core socket variables */
Miroslav Franc 980b26
+/* Initialise core socket variables using an explicit uid. */
Miroslav Franc 980b26
+void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid);
Miroslav Franc 980b26
+
Miroslav Franc 980b26
+/* Initialise core socket variables.
Miroslav Franc 980b26
+ * Assumes struct socket *sock is embedded in a struct socket_alloc.
Miroslav Franc 980b26
+ */
Miroslav Franc 980b26
 void sock_init_data(struct socket *sock, struct sock *sk);
Miroslav Franc 980b26
 
Miroslav Franc 980b26
 /*
Miroslav Franc 980b26
diff --git a/net/core/sock.c b/net/core/sock.c
Miroslav Franc 980b26
index 652913df588d..28382b23afe2 100644
Miroslav Franc 980b26
--- a/net/core/sock.c
Miroslav Franc 980b26
+++ b/net/core/sock.c
Miroslav Franc 980b26
@@ -2878,7 +2878,7 @@ void sk_stop_timer(struct sock *sk, struct timer_list* timer)
Miroslav Franc 980b26
 }
Miroslav Franc 980b26
 EXPORT_SYMBOL(sk_stop_timer);
Miroslav Franc 980b26
 
Miroslav Franc 980b26
-void sock_init_data(struct socket *sock, struct sock *sk)
Miroslav Franc 980b26
+void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
Miroslav Franc 980b26
 {
Miroslav Franc 980b26
 	sk_init_common(sk);
Miroslav Franc 980b26
 	sk->sk_send_head	=	NULL;
Miroslav Franc 980b26
@@ -2897,11 +2897,10 @@ void sock_init_data(struct socket *sock, struct sock *sk)
Miroslav Franc 980b26
 		sk->sk_type	=	sock->type;
Miroslav Franc 980b26
 		RCU_INIT_POINTER(sk->sk_wq, &sock->wq);
Miroslav Franc 980b26
 		sock->sk	=	sk;
Miroslav Franc 980b26
-		sk->sk_uid	=	SOCK_INODE(sock)->i_uid;
Miroslav Franc 980b26
 	} else {
Miroslav Franc 980b26
 		RCU_INIT_POINTER(sk->sk_wq, NULL);
Miroslav Franc 980b26
-		sk->sk_uid	=	make_kuid(sock_net(sk)->user_ns, 0);
Miroslav Franc 980b26
 	}
Miroslav Franc 980b26
+	sk->sk_uid	=	uid;
Miroslav Franc 980b26
 
Miroslav Franc 980b26
 	rwlock_init(&sk->sk_callback_lock);
Miroslav Franc 980b26
 	if (sk->sk_kern_sock)
Miroslav Franc 980b26
@@ -2959,6 +2958,16 @@ void sock_init_data(struct socket *sock, struct sock *sk)
Miroslav Franc 980b26
 	refcount_set(&sk->sk_refcnt, 1);
Miroslav Franc 980b26
 	atomic_set(&sk->sk_drops, 0);
Miroslav Franc 980b26
 }
Miroslav Franc 980b26
+EXPORT_SYMBOL(sock_init_data_uid);
Miroslav Franc 980b26
+
Miroslav Franc 980b26
+void sock_init_data(struct socket *sock, struct sock *sk)
Miroslav Franc 980b26
+{
Miroslav Franc 980b26
+	kuid_t uid = sock ?
Miroslav Franc 980b26
+		SOCK_INODE(sock)->i_uid :
Miroslav Franc 980b26
+		make_kuid(sock_net(sk)->user_ns, 0);
Miroslav Franc 980b26
+
Miroslav Franc 980b26
+	sock_init_data_uid(sock, sk, uid);
Miroslav Franc 980b26
+}
Miroslav Franc 980b26
 EXPORT_SYMBOL(sock_init_data);
Miroslav Franc 980b26
 
Miroslav Franc 980b26
 void lock_sock_nested(struct sock *sk, int subclass)