git: dba2eb1dc02d - stable/13 - unix: Fix locking in uipc_peeraddr()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 11 May 2023 13:32:57 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=dba2eb1dc02d6ef876aa38b7b9612703dde5d635

commit dba2eb1dc02d6ef876aa38b7b9612703dde5d635
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-05-10 13:18:16 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-05-11 13:32:32 +0000

    unix: Fix locking in uipc_peeraddr()
    
    After the locking protocol changed in commit 75a67bf3d00d ("AF_UNIX:
    make unix socket locking finer grained"), uipc_peeraddr() was not
    updated accordingly.
    
    The link lock now only protects global socket lists.  The PCB lock is
    used to protect the link between connected PCBs, so use that.  Remove an
    old comment which appears to be noting that unp_conn is not set for
    connected SOCK_DGRAM sockets (in one direction anyway).
    
    Reviewed by:    glebius
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D39855
    
    (cherry picked from commit e8f6e5b2d969fdf7e8f0a9a0a87eede357618fe9)
---
 sys/kern/uipc_usrreq.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index 9c46e8588ed4..4d602341f514 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -921,15 +921,10 @@ uipc_peeraddr(struct socket *so, struct sockaddr **nam)
 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
 
 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
-	UNP_LINK_RLOCK();
-	/*
-	 * XXX: It seems that this test always fails even when connection is
-	 * established.  So, this else clause is added as workaround to
-	 * return PF_LOCAL sockaddr.
-	 */
-	unp2 = unp->unp_conn;
+
+	UNP_PCB_LOCK(unp);
+	unp2 = unp_pcb_lock_peer(unp);
 	if (unp2 != NULL) {
-		UNP_PCB_LOCK(unp2);
 		if (unp2->unp_addr != NULL)
 			sa = (struct sockaddr *) unp2->unp_addr;
 		else
@@ -940,7 +935,7 @@ uipc_peeraddr(struct socket *so, struct sockaddr **nam)
 		sa = &sun_noname;
 		bcopy(sa, *nam, sa->sa_len);
 	}
-	UNP_LINK_RUNLOCK();
+	UNP_PCB_UNLOCK(unp);
 	return (0);
 }