git: e8f6e5b2d969 - main - unix: Fix locking in uipc_peeraddr()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 03 May 2023 16:00:48 UTC
The branch main has been updated by markj:

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

commit e8f6e5b2d969fdf7e8f0a9a0a87eede357618fe9
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-05-03 15:56:46 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-05-03 15:56:46 +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
---
 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 294ef807416c..bd7a2cca245f 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -885,15 +885,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
@@ -904,7 +899,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);
 }