git: ef7c9c88e99d - main - kern: add pwd_altroot_clear() helper

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 00:16:00 UTC
The branch main has been updated by kib:

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

commit ef7c9c88e99dbaaad718a2fb302354042c54260e
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-22 15:29:46 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 00:10:23 +0000

    kern: add pwd_altroot_clear() helper
    
    Suggested and reviewed by:      markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61858
---
 sys/compat/linux/linux_util.c | 6 +++---
 sys/kern/kern_descrip.c       | 9 +++++++++
 sys/kern/kern_exec.c          | 2 +-
 sys/kern/sys_capability.c     | 4 +---
 sys/kern/vfs_syscalls.c       | 3 ++-
 sys/sys/filedesc.h            | 1 +
 6 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/sys/compat/linux/linux_util.c b/sys/compat/linux/linux_util.c
index f4701fac87fa..d9f652e55cb2 100644
--- a/sys/compat/linux/linux_util.c
+++ b/sys/compat/linux/linux_util.c
@@ -87,13 +87,13 @@ linux_pwd_onexec(struct thread *td)
 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
 	error = namei(&nd);
 	if (error != 0) {
-		pwd_altroot(td, NULL);
+		pwd_altroot_clear(td);
 		return (0);
 	}
 	NDFREE_PNBUF(&nd);
 	error = pwd_altroot(td, nd.ni_vp);
 	if (error != 0)
-		pwd_altroot(td, NULL);
+		pwd_altroot_clear(td);
 	vrele(nd.ni_vp);
 	return (0);
 }
@@ -102,7 +102,7 @@ void
 linux_pwd_onexec_native(struct thread *td)
 {
 
-	pwd_altroot(td, NULL);
+	pwd_altroot_clear(td);
 }
 
 void
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 9066340543d9..ae32f9cffc97 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -4446,6 +4446,15 @@ pwd_altroot(struct thread *td, struct vnode *altroot_vp)
 	return (0);
 }
 
+void
+pwd_altroot_clear(struct thread *td)
+{
+	int error __diagused;
+
+	error = pwd_altroot(td, NULL);
+	KASSERT(error == 0, ("%s: pwd_altroot failed: %d", __func__, error));
+}
+
 /*
  * jail_attach(2) changes both root and working directories.
  */
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index 390f4cdaf3ed..df6f04dfecbe 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -1277,7 +1277,7 @@ exec_new_vmspace(struct image_params *imgp, struct sysentvec *sv)
 	if (imgp->credential_setid &&
 	    (p->p_flag2 & P2_NO_NEW_PRIVS) == 0 &&
 	    (sv->sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)
-		pwd_altroot(td, NULL);
+		pwd_altroot_clear(td);
 
 	if (p->p_sysent->sv_onexec_old != NULL)
 		p->p_sysent->sv_onexec_old(td);
diff --git a/sys/kern/sys_capability.c b/sys/kern/sys_capability.c
index 3bcfffd1c8fd..43cc89183606 100644
--- a/sys/kern/sys_capability.c
+++ b/sys/kern/sys_capability.c
@@ -122,10 +122,8 @@ sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
 		goto out;
 	}
 	PROC_UNLOCK(p);
-	error = pwd_altroot(td, NULL);
+	pwd_altroot_clear(td);
 	PROC_LOCK(p);
-	if (error != 0)
-		goto out;
 	oldcred = crcopysafe(p, newcred);
 	newcred->cr_flags |= CRED_FLAG_CAPMODE;
 	proc_set_cred(p, newcred);
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 484802b3e29d..1dd29a081a60 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -5435,7 +5435,8 @@ disable_altdir(struct thread *td)
 	if (error != 0)
 		return (error);
 #endif
-	return (pwd_altroot(td, NULL));
+	pwd_altroot_clear(td);
+	return (0);
 }
 
 static int
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index 077ffa8a9daa..3dfac6f0ab14 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -373,6 +373,7 @@ struct pwddesc *pdshare(struct pwddesc *pdp);
 void	pdunshare(struct thread *td);
 
 int	pwd_altroot(struct thread *td, struct vnode *altroot_vp);
+void	pwd_altroot_clear(struct thread *td);
 void	pwd_chdir(struct thread *td, struct vnode *vp);
 int	pwd_chroot(struct thread *td, struct vnode *vp);
 int	pwd_chroot_chdir(struct thread *td, struct vnode *vp);