Thread (18 messages) flat view 18 messages, 3 authors, 2021-11-05
STALE1751d

[PATCH 04/10] sock: add sock_swap_peercred

From: Marc-André Lureau <hidden>
Date: 2021-10-21 12:37:48
Also in: lkml
Subsystem: networking [general], networking [sockets], the rest · Maintainers: "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima, Willem de Bruijn, Linus Torvalds

Signed-off-by: Marc-André Lureau <redacted>
---
 include/net/sock.h |  3 +++
 net/core/sock.c    | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
diff --git a/include/net/sock.h b/include/net/sock.h
index d6877df26200..635d270c3a65 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1822,6 +1822,9 @@ void sock_init_peercred(struct sock *sk);
 /* Copy peer credentials from peersk */
 void sock_copy_peercred(struct sock *sk, struct sock *peersk);
 
+/* Swap socket credentials */
+void sock_swap_peercred(struct sock *sk, struct sock *peersk);
+
 /*
  * Socket reference counting postulates.
  *
diff --git a/net/core/sock.c b/net/core/sock.c
index f6b2662824df..365b63afa915 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3239,6 +3239,30 @@ void sock_copy_peercred(struct sock *sk, struct sock *peersk)
 }
 EXPORT_SYMBOL(sock_copy_peercred);
 
+void sock_swap_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  = peersk->sk_peer_pid;
+	sk->sk_peer_cred = peersk->sk_peer_cred;
+	peersk->sk_peer_pid = old_pid;
+	peersk->sk_peer_cred = old_cred;
+
+	spin_unlock(&sk->sk_peer_lock);
+	spin_unlock(&peersk->sk_peer_lock);
+}
+EXPORT_SYMBOL(sock_swap_peercred);
+
 void lock_sock_nested(struct sock *sk, int subclass)
 {
 	/* The sk_lock has mutex_lock() semantics here. */
-- 
2.33.0.721.g106298f7f9
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help