git: 1e9bca400b9c - main - sockets: assert VNET set in sopeeraddr()

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 17 Jan 2025 02:31:54 UTC
The branch main has been updated by glebius:

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

commit 1e9bca400b9c7ae3dc3f31aa48df69cf2259e16c
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-01-17 02:31:15 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-01-17 02:31:15 +0000

    sockets: assert VNET set in sopeeraddr()
    
    Most callers already have the context set, it was only bare syscall that
    needs it to set.
---
 sys/kern/uipc_socket.c   | 4 ++--
 sys/kern/uipc_syscalls.c | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 426316ac6ce8..46a7053f2872 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -2030,11 +2030,11 @@ sopeeraddr(struct socket *so, struct sockaddr *sa)
 #endif
 	int error;
 
-	CURVNET_SET(so->so_vnet);
+	CURVNET_ASSERT_SET();
+
 	error = so->so_proto->pr_peeraddr(so, sa);
 	KASSERT(sa->sa_len <= len,
 	    ("%s: protocol %p sockaddr overflow", __func__, so->so_proto));
-	CURVNET_RESTORE();
 
 	return (error);
 }
diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c
index e0bb7ace92c6..91fcdf9a7a9d 100644
--- a/sys/kern/uipc_syscalls.c
+++ b/sys/kern/uipc_syscalls.c
@@ -1433,7 +1433,9 @@ kern_getpeername(struct thread *td, int fd, struct sockaddr *sa)
 		error = ENOTCONN;
 		goto done;
 	}
+	CURVNET_SET(so->so_vnet);
 	error = sopeeraddr(so, sa);
+	CURVNET_RESTORE();
 #ifdef KTRACE
 	if (error == 0 && KTRPOINT(td, KTR_STRUCT))
 		ktrsockaddr(sa);