[PATCH 01/10] sock: move sock_init_peercred() from af_unix

From: Marc-André Lureau
Date: Thu Oct 21 2021 - 08:37:32 EST


SO_PEERCRED can be made to work with other kind of sockets.

Signed-off-by: Marc-André Lureau <marcandre.lureau@xxxxxxxxxx>
---
include/net/sock.h | 3 +++
net/core/sock.c | 17 +++++++++++++++++
net/unix/af_unix.c | 24 ++++--------------------
3 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/include/net/sock.h b/include/net/sock.h
index ea6fbc88c8f9..8b12953752e6 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1816,6 +1816,9 @@ void sk_common_release(struct sock *sk);
/* Initialise core socket variables */
void sock_init_data(struct socket *sock, struct sock *sk);

+/* Set socket peer PID and credentials with current process. */
+void sock_init_peercred(struct sock *sk);
+
/*
* Socket reference counting postulates.
*
diff --git a/net/core/sock.c b/net/core/sock.c
index c1601f75ec4b..997e8d256e2f 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3197,6 +3197,23 @@ void sock_init_data(struct socket *sock, struct sock *sk)
}
EXPORT_SYMBOL(sock_init_data);

+void sock_init_peercred(struct sock *sk)
+{
+ const struct cred *old_cred;
+ struct pid *old_pid;
+
+ spin_lock(&sk->sk_peer_lock);
+ old_pid = sk->sk_peer_pid;
+ old_cred = sk->sk_peer_cred;
+ sk->sk_peer_pid = get_pid(task_tgid(current));
+ sk->sk_peer_cred = get_current_cred();
+ spin_unlock(&sk->sk_peer_lock);
+
+ put_pid(old_pid);
+ put_cred(old_cred);
+}
+EXPORT_SYMBOL(sock_init_peercred);
+
void lock_sock_nested(struct sock *sk, int subclass)
{
/* The sk_lock has mutex_lock() semantics here. */
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 89f9e85ae970..e56f320dff20 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -606,22 +606,6 @@ static void unix_release_sock(struct sock *sk, int embrion)
unix_gc(); /* Garbage collect fds */
}

-static void init_peercred(struct sock *sk)
-{
- const struct cred *old_cred;
- struct pid *old_pid;
-
- spin_lock(&sk->sk_peer_lock);
- old_pid = sk->sk_peer_pid;
- old_cred = sk->sk_peer_cred;
- sk->sk_peer_pid = get_pid(task_tgid(current));
- sk->sk_peer_cred = get_current_cred();
- spin_unlock(&sk->sk_peer_lock);
-
- put_pid(old_pid);
- put_cred(old_cred);
-}
-
static void copy_peercred(struct sock *sk, struct sock *peersk)
{
const struct cred *old_cred;
@@ -666,7 +650,7 @@ static int unix_listen(struct socket *sock, int backlog)
sk->sk_max_ack_backlog = backlog;
sk->sk_state = TCP_LISTEN;
/* set credentials so connect can copy them */
- init_peercred(sk);
+ sock_init_peercred(sk);
err = 0;

out_unlock:
@@ -1446,7 +1430,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
unix_peer(newsk) = sk;
newsk->sk_state = TCP_ESTABLISHED;
newsk->sk_type = sk->sk_type;
- init_peercred(newsk);
+ sock_init_peercred(newsk);
newu = unix_sk(newsk);
RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
otheru = unix_sk(other);
@@ -1518,8 +1502,8 @@ static int unix_socketpair(struct socket *socka, struct socket *sockb)
sock_hold(skb);
unix_peer(ska) = skb;
unix_peer(skb) = ska;
- init_peercred(ska);
- init_peercred(skb);
+ sock_init_peercred(ska);
+ sock_init_peercred(skb);

ska->sk_state = TCP_ESTABLISHED;
skb->sk_state = TCP_ESTABLISHED;
--
2.33.0.721.g106298f7f9