git: a79aca4b320b - stable/15 - kern/vfs_syscalls.c: extract privilege check from kern_chroot()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 11 Nov 2026 01:06:58 UTC
The branch stable/15 has been updated by kib:

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

commit a79aca4b320bf31e6b72eff900760326201e5cae
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-26 16:31:21 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-10 19:53:56 +0000

    kern/vfs_syscalls.c: extract privilege check from kern_chroot()
    
    (cherry picked from commit f809179ba9811decc518cb5694a289d169a5d6ae)
---
 sys/kern/vfs_syscalls.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 620fbcc1def9..1aecd7f7d5a8 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -969,31 +969,31 @@ SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
     &unprivileged_chroot, 0,
     "Unprivileged processes can use chroot(2)");
 
+static int
+chroot_check_priv(struct thread *td)
+{
+	if (priv_check(td, PRIV_VFS_CHROOT) == 0)
+		return (0);
+	if (unprivileged_chroot == 0)
+		return (EXTERROR(EPERM,
+		    "security.bsd.unprivileged_chroot sysctl not enabled"));
+	if ((td->td_proc->p_flag2 & P2_NO_NEW_PRIVS) == 0)
+		return (EXTERROR(EPERM,
+		    "PROC_NO_NEW_PRIVS not enabled"));
+	return (0);
+}
+
 /*
  * Takes locked vnode, unlocks it before returning.
  */
 static int
 kern_chroot(struct thread *td, struct vnode *vp)
 {
-	struct proc *p;
 	int error;
 
-	error = priv_check(td, PRIV_VFS_CHROOT);
-	if (error != 0) {
-		p = td->td_proc;
-		if (unprivileged_chroot == 0) {
-			error = EXTERROR(EPERM,
-		    "security.bsd.unprivileged_chroot sysctl not enabled");
-			goto e_vunlock;
-		}
-		if ((p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
-			error = EXTERROR(EPERM,
-			    "PROC_NO_NEW_PRIVS not enabled");
-			goto e_vunlock;
-		}
-	}
-
-	error = change_dir(vp, td);
+	error = chroot_check_priv(td);
+	if (error == 0)
+		error = change_dir(vp, td);
 	if (error != 0)
 		goto e_vunlock;
 #ifdef MAC