git: e95e010fd0de - main - kern: do not allow unprivileged chroot when pwd is shared
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 04 Nov 2026 16:23:22 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=e95e010fd0de721ae0e87bb24e20ec53aec8629e
commit e95e010fd0de721ae0e87bb24e20ec53aec8629e
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-10-24 12:59:08 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-04 16:23:11 +0000
kern: do not allow unprivileged chroot when pwd is shared
Unprivileged chroot(2), fchroot(2), chaltroot(2), and fchaltroot(2)
rely on PROC_NO_NEW_PRIVS to make set-ID images harmless under the
new root. But the pwddesc can be shared by rfork(2) without RFFDG
with a process that has not enabled PROC_NO_NEW_PRIVS, and the
change of the root affects that process as well. It can then
execute a set-ID image whose ELF interpreter is resolved under the
root controlled by the caller.
Refuse the unprivileged operations when the pwddesc is shared.
Processes that start sharing it later are created by the caller and
inherit PROC_NO_NEW_PRIVS.
Reviewed by: markj
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D60963
---
sys/kern/vfs_syscalls.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 31a7894932bf..308c1c0297df 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -972,14 +972,34 @@ SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
static int
chroot_check_priv(struct thread *td)
{
+ struct proc *p;
+
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)
+ p = td->td_proc;
+ if ((p->p_flag2 & P2_NO_NEW_PRIVS) == 0)
return (EXTERROR(EPERM,
"PROC_NO_NEW_PRIVS not enabled"));
+
+ /*
+ * Refuse chroot() when the pwd is shared with other
+ * processes, since they might have been created before
+ * P2_NO_NEW_PRIVS was set.
+ *
+ * The check races with fork(), but the race is harmless. If
+ * pd_sharecount was observed as 1 and another thread forked
+ * in the meantime, the child inherited P2_NO_NEW_PRIVS. Then
+ * neither the parent nor the child can gain privileges by
+ * executing a set-ID image, which prevents the issues
+ * otherwise possible due to the pwd sharing.
+ */
+ atomic_thread_fence_acq();
+ if (refcount_load(&p->p_pd->pd_sharecount) > 1)
+ return (EXTERROR(EPERM,
+ "root/current directory shared with another process"));
return (0);
}