git: 06c455dd9bbb - main - vfs: guard against altroot pointing to not a directory

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 15:10:43 UTC
The branch main has been updated by kib:

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

commit 06c455dd9bbbb2560ae78414949b8bd3be3cbab6
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-25 23:36:56 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-20 15:10:26 +0000

    vfs: guard against altroot pointing to not a directory
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D60035
---
 sys/compat/linux/linux_util.c | 9 +++++++--
 sys/kern/kern_descrip.c       | 5 ++++-
 sys/sys/filedesc.h            | 2 +-
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/sys/compat/linux/linux_util.c b/sys/compat/linux/linux_util.c
index 910800489874..f4701fac87fa 100644
--- a/sys/compat/linux/linux_util.c
+++ b/sys/compat/linux/linux_util.c
@@ -74,6 +74,10 @@ SYSCTL_STRING(_compat_linux, OID_AUTO, emul_path, CTLFLAG_RWTUN,
     linux_emul_path, sizeof(linux_emul_path),
     "Linux runtime environment path");
 
+/*
+ * Do not prevent execution if altroot is non-existent or is not a
+ * directory.
+ */
 int
 linux_pwd_onexec(struct thread *td)
 {
@@ -83,12 +87,13 @@ linux_pwd_onexec(struct thread *td)
 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path);
 	error = namei(&nd);
 	if (error != 0) {
-		/* Do not prevent execution if altroot is non-existent. */
 		pwd_altroot(td, NULL);
 		return (0);
 	}
 	NDFREE_PNBUF(&nd);
-	pwd_altroot(td, nd.ni_vp);
+	error = pwd_altroot(td, nd.ni_vp);
+	if (error != 0)
+		pwd_altroot(td, NULL);
 	vrele(nd.ni_vp);
 	return (0);
 }
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 629ecdb57e47..de49d738c6d0 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -4411,12 +4411,14 @@ pwd_chdir(struct thread *td, struct vnode *vp)
 /*
  * Process is transitioning to/from a non-native ABI.
  */
-void
+int
 pwd_altroot(struct thread *td, struct vnode *altroot_vp)
 {
 	struct pwddesc *pdp;
 	struct pwd *newpwd, *oldpwd;
 
+	if (altroot_vp != NULL && altroot_vp->v_type != VDIR)
+		return (ENOTDIR);
 	newpwd = pwd_alloc();
 	pdp = td->td_proc->p_pd;
 	PWDDESC_XLOCK(pdp);
@@ -4440,6 +4442,7 @@ pwd_altroot(struct thread *td, struct vnode *altroot_vp)
 	pwd_set(pdp, newpwd);
 	PWDDESC_XUNLOCK(pdp);
 	pwd_drop(oldpwd);
+	return (0);
 }
 
 /*
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index d985641eb475..077ffa8a9daa 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -372,7 +372,7 @@ struct pwddesc *pdinit(struct pwddesc *pdp, bool keeplock);
 struct pwddesc *pdshare(struct pwddesc *pdp);
 void	pdunshare(struct thread *td);
 
-void	pwd_altroot(struct thread *td, struct vnode *altroot_vp);
+int	pwd_altroot(struct thread *td, struct vnode *altroot_vp);
 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);