mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, sgarzare@redhat.com,
	davem@davemloft.net, kuba@kernel.org,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [PATCH 02/10] sock: move sock_copy_peercred() from af_unix
Date: Thu, 21 Oct 2021 16:37:06 +0400	[thread overview]
Message-ID: <20211021123714.1125384-3-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20211021123714.1125384-1-marcandre.lureau@redhat.com>

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

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/net/sock.h |  3 +++
 net/core/sock.c    | 25 +++++++++++++++++++++++++
 net/unix/af_unix.c | 26 +-------------------------
 3 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/include/net/sock.h b/include/net/sock.h
index 8b12953752e6..d6877df26200 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1819,6 +1819,9 @@ 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);
 
+/* Copy peer credentials from peersk */
+void sock_copy_peercred(struct sock *sk, struct sock *peersk);
+
 /*
  * Socket reference counting postulates.
  *
diff --git a/net/core/sock.c b/net/core/sock.c
index 997e8d256e2f..f6b2662824df 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3214,6 +3214,31 @@ void sock_init_peercred(struct sock *sk)
 }
 EXPORT_SYMBOL(sock_init_peercred);
 
+void sock_copy_peercred(struct sock *sk, struct sock *peersk)
+{
+	const struct cred *old_cred;
+	struct pid *old_pid;
+
+	if (sk < peersk) {
+		spin_lock(&sk->sk_peer_lock);
+		spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING);
+	} else {
+		spin_lock(&peersk->sk_peer_lock);
+		spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);
+	}
+	old_pid = sk->sk_peer_pid;
+	old_cred = sk->sk_peer_cred;
+	sk->sk_peer_pid  = get_pid(peersk->sk_peer_pid);
+	sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
+
+	spin_unlock(&sk->sk_peer_lock);
+	spin_unlock(&peersk->sk_peer_lock);
+
+	put_pid(old_pid);
+	put_cred(old_cred);
+}
+EXPORT_SYMBOL(sock_copy_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 e56f320dff20..9109df1efdb6 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -606,30 +606,6 @@ static void unix_release_sock(struct sock *sk, int embrion)
 		unix_gc();		/* Garbage collect fds */
 }
 
-static void copy_peercred(struct sock *sk, struct sock *peersk)
-{
-	const struct cred *old_cred;
-	struct pid *old_pid;
-
-	if (sk < peersk) {
-		spin_lock(&sk->sk_peer_lock);
-		spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING);
-	} else {
-		spin_lock(&peersk->sk_peer_lock);
-		spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);
-	}
-	old_pid = sk->sk_peer_pid;
-	old_cred = sk->sk_peer_cred;
-	sk->sk_peer_pid  = get_pid(peersk->sk_peer_pid);
-	sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
-
-	spin_unlock(&sk->sk_peer_lock);
-	spin_unlock(&peersk->sk_peer_lock);
-
-	put_pid(old_pid);
-	put_cred(old_cred);
-}
-
 static int unix_listen(struct socket *sock, int backlog)
 {
 	int err;
@@ -1460,7 +1436,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
 	smp_store_release(&newu->addr, otheru->addr);
 
 	/* Set credentials */
-	copy_peercred(sk, other);
+	sock_copy_peercred(sk, other);
 
 	sock->state	= SS_CONNECTED;
 	sk->sk_state	= TCP_ESTABLISHED;
-- 
2.33.0.721.g106298f7f9


  parent reply	other threads:[~2021-10-21 12:37 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-21 12:37 [PATCH 00/10] RFC: SO_PEERCRED for AF_VSOCK Marc-André Lureau
2021-10-21 12:37 ` [PATCH 01/10] sock: move sock_init_peercred() from af_unix Marc-André Lureau
2021-10-21 12:37 ` Marc-André Lureau [this message]
2021-10-21 12:37 ` [PATCH 03/10] vsock: owner field is specific to VMCI Marc-André Lureau
2021-10-26 11:16   ` Stefano Garzarella
2021-10-27  8:13     ` Jorgen Hansen
2021-11-05  8:21       ` Marc-André Lureau
2021-10-21 12:37 ` [PATCH 04/10] sock: add sock_swap_peercred Marc-André Lureau
2021-10-21 12:37 ` [PATCH 05/10] virtio/vsock: add copy_peercred() to virtio_transport Marc-André Lureau
2021-10-26 11:17   ` Stefano Garzarella
2021-10-21 12:37 ` [PATCH 06/10] vsock: set socket peercred Marc-André Lureau
2021-10-26 11:18   ` Stefano Garzarella
2021-10-21 12:37 ` [PATCH 07/10] vsock/loopback: implement copy_peercred() Marc-André Lureau
2021-10-26 11:18   ` Stefano Garzarella
2021-10-21 12:37 ` [PATCH 08/10] vhost/vsock: save owner pid & creds Marc-André Lureau
2021-10-21 12:37 ` [PATCH 09/10] vhost/vsock: implement copy_peercred Marc-André Lureau
2021-10-21 12:37 ` [PATCH 10/10] vsock/virtio: clear peer creds on connect Marc-André Lureau
2021-10-21 13:34 ` [PATCH 00/10] RFC: SO_PEERCRED for AF_VSOCK Stefano Garzarella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211021123714.1125384-3-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=sgarzare@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®