git: 9757c0cf98e4 - main - chaltroot(2) and fchaltroot(2): new syscalls

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

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

commit 9757c0cf98e4fde6b2f2be6ae6095798fbdb95cc
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-25 23:49:37 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-20 15:10:26 +0000

    chaltroot(2) and fchaltroot(2): new syscalls
    
    to change the altroot AKA ABI root for native processes
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D60035
---
 sys/kern/syscalls.master | 10 ++++++
 sys/kern/vfs_syscalls.c  | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
 sys/sys/unistd.h         |  2 ++
 3 files changed, 97 insertions(+)

diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index bf7ae079c6b9..d49e5843ac12 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3452,5 +3452,15 @@
 			int data
 		);
 	}
+606	AUE_CHALTROOT	STD {
+		int chaltroot(
+		    _In_z_opt_ const char *path
+		);
+	}
+607	AUE_FCHALTROOT	STD {
+		int fchaltroot(
+		    int fd
+		);
+	}
 
 ; vim: syntax=off
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 225dd09e6c25..484802b3e29d 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -5424,3 +5424,88 @@ user_copy_file_range(struct thread *td, int infd, off_t *uinoffp, int outfd,
 		error = copyout(outoffp, uoutoffp, sizeof(off_t));
 	return (error);
 }
+
+static int
+disable_altdir(struct thread *td)
+{
+#ifdef MAC
+	int error;
+
+	error = mac_vnode_check_chaltroot(td->td_ucred, NULL);
+	if (error != 0)
+		return (error);
+#endif
+	return (pwd_altroot(td, NULL));
+}
+
+static int
+change_altdir(struct thread *td, struct vnode *vp)
+{
+	int error;
+
+	if (vp->v_type != VDIR) {
+		error = ENOTDIR;
+	} else {
+#ifdef MAC
+		error = mac_vnode_check_chaltroot(td->td_ucred, vp);
+		if (error == 0)
+#endif
+			error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
+	}
+	VOP_UNLOCK(vp);
+	if (error == 0)
+		error = pwd_altroot(td, vp);
+	vrele(vp);
+	return (error);
+}
+
+int
+sys_chaltroot(struct thread *td, struct chaltroot_args *uap)
+{
+	struct nameidata nd;
+	int error;
+
+	error = chroot_check_priv(td);
+	if (error != 0)
+		return (error);
+
+	if (uap->path == NULL)
+		return (disable_altdir(td));
+
+	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | LOCKSHARED | AUDITVNODE1,
+	    UIO_USERSPACE, uap->path);
+	error = namei(&nd);
+	if (error != 0)
+		return (error);
+	NDFREE_PNBUF(&nd);
+	return (change_altdir(td, nd.ni_vp));
+}
+
+int
+sys_fchaltroot(struct thread *td, struct fchaltroot_args *uap)
+{
+	struct vnode *vp;
+	struct file *fp;
+	int error;
+	uint8_t fdflags;
+
+	error = chroot_check_priv(td);
+	if (error != 0)
+		return (error);
+
+	if (uap->fd == FCHALTROOT_DISABLE)
+		return (disable_altdir(td));
+
+	error = getvnode_path(td, uap->fd, &cap_fchroot_rights, &fdflags, &fp);
+	if (error != 0)
+		return (error);
+	if ((fdflags & UF_RESOLVE_BENEATH) != 0) {
+		fdrop(fp, td);
+		return (ENOTCAPABLE);
+	}
+	vp = fp->f_vnode;
+	vrefact(vp);
+	fdrop(fp, td);
+	vn_lock(vp, LK_SHARED | LK_RETRY);
+	return (change_altdir(td, vp));
+}
diff --git a/sys/sys/unistd.h b/sys/sys/unistd.h
index 29adf6b59425..2d71212b74c7 100644
--- a/sys/sys/unistd.h
+++ b/sys/sys/unistd.h
@@ -226,6 +226,8 @@
 #define	COPY_FILE_RANGE_CLONE		0x00800000	/* Require cloning. */
 #define	COPY_FILE_RANGE_USERFLAGS	(COPY_FILE_RANGE_CLONE)
 
+#define	FCHALTROOT_DISABLE	(-110)	/* Disable altroot by fchaltroot(2). */
+
 #endif /* __BSD_VISIBLE */
 
 #endif /* !_SYS_UNISTD_H_ */