Date: Sun, 05 Jul 2026 05:13:48 +0000 From: Jamie Gritton <jamie@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 5ba2b4f773c2 - stable/14 - jail: prevent a race between jail_attach in different threads Message-ID: <6a49e80c.22848.48d188d3@gitrepo.freebsd.org>
index | next in thread | raw e-mail
The branch stable/14 has been updated by jamie: URL: https://cgit.FreeBSD.org/src/commit/?id=5ba2b4f773c27f139ce840ca93b7c5dad83d1984 commit 5ba2b4f773c27f139ce840ca93b7c5dad83d1984 Author: Jamie Gritton <jamie@FreeBSD.org> AuthorDate: 2026-07-02 22:48:07 +0000 Commit: Jamie Gritton <jamie@FreeBSD.org> CommitDate: 2026-07-05 05:13:27 +0000 jail: prevent a race between jail_attach in different threads Attaching to a jail changes its root directory and its process credentials. These operations both require unlocking the jail, and also need allprison_lock unlocked. That means that if two threads are trying to attach to different jails at the same time, it's possible for the process to end up with one jail's root directory but the other jail's credentials. Solve this by forcing the process into single-threaded mode during system calls that attach to a jail (jail_attach, jail_attach_jd, and sometimes jail_set). Reviewed by: kib, markj Differential Revision: https://reviews.freebsd.org/D57858 (cherry picked from commit d4e0f4dab2d7f4de46bb79db1ca7e6e8a2e34746) --- sys/kern/kern_jail.c | 87 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index f40e7c4e176c..5063410e63b1 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -150,6 +150,8 @@ int lastprid = 0; static int get_next_prid(struct prison **insprp); static int do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp); +static int prison_attach_thread_single(struct thread *td); +static void prison_attach_thread_single_end(struct thread *td); static void prison_complete(void *context, int pending); static void prison_deref(struct prison *pr, int flags); static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison); @@ -1032,6 +1034,20 @@ kern_jail_set(struct thread *td, struct uio *optuio, int flags) if (flags & ~JAIL_SET_MASK) return (EINVAL); + /* Only let a single thread in the process try to attach at a time. */ + if (flags & JAIL_ATTACH) { + error = prison_attach_thread_single(td); + if (error != 0) + return (error); + } + +#ifdef INET + ip4 = NULL; +#endif +#ifdef INET6 + ip6 = NULL; +#endif + g_path = NULL; /* * Check all the parameters before committing to anything. Not all * errors can be caught early, but we may as well try. Also, this @@ -1043,15 +1059,10 @@ kern_jail_set(struct thread *td, struct uio *optuio, int flags) * than duplicate it under a different name. */ error = vfs_buildopts(optuio, &opts); - if (error) - return (error); -#ifdef INET - ip4 = NULL; -#endif -#ifdef INET6 - ip6 = NULL; -#endif - g_path = NULL; + if (error) { + opts = NULL; + goto done_free; + } cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); if (!cuflags) { @@ -2203,7 +2214,10 @@ kern_jail_set(struct thread *td, struct uio *optuio, int flags) #endif if (g_path != NULL) free(g_path, M_TEMP); - vfs_freeopts(opts); + if (opts != NULL) + vfs_freeopts(opts); + if (flags & JAIL_ATTACH) + prison_attach_thread_single_end(td); return (error); } @@ -2637,6 +2651,10 @@ sys_jail_attach(struct thread *td, struct jail_attach_args *uap) error = priv_check(td, PRIV_JAIL_ATTACH); if (error) return (error); + /* Only let a single thread in the process try to attach at a time. */ + error = prison_attach_thread_single(td); + if (error != 0) + return (error); sx_slock(&allprison_lock); drflags = PD_LIST_SLOCKED; @@ -2657,6 +2675,7 @@ sys_jail_attach(struct thread *td, struct jail_attach_args *uap) done: prison_deref(pr, drflags); + prison_attach_thread_single_end(td); return (error); } @@ -2674,15 +2693,6 @@ do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp) sx_assert(&allprison_lock, SX_LOCKED); KASSERT(prison_isvalid(pr), ("Attaching to invalid prison %p", pr)); - /* - * XXX: Note that there is a slight race here if two threads - * in the same privileged process attempt to attach to two - * different jails at the same time. It is important for - * user processes not to do this, or they might end up with - * a process root from one prison, but attached to the jail - * of another. - */ - /* * Note the caller's locking state, but gain and track our own * references. The caller will see that locks have been @@ -2764,7 +2774,7 @@ do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp) e_unlock: VOP_UNLOCK(pr->pr_root); e_revert_osd: - /* Tell modules this thread is still in its old jail after all. */ + /* Tell modules this process is still in its old jail after all. */ if (!(drflags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) { sx_slock(&allprison_lock); drflags |= PD_LIST_SLOCKED; @@ -2774,6 +2784,43 @@ do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp) return (error); } +/* + * Only one thread in a process should try to attach to a jail, or + * they might end up with a process root from one prison, but attached + * to the jail of another. Enforce this by making the process run in + * single-threaded mode for the duration of the system call, which + * also prevents other related calls such as chroot. + */ +static int +prison_attach_thread_single(struct thread *td) +{ + struct proc *p; + int error; + + error = 0; + p = td->td_proc; + if ((atomic_load_int(&p->p_flag) & P_HADTHREADS) != 0) { + PROC_LOCK(p); + if (thread_single(p, SINGLE_BOUNDARY)) + error = ERESTART; + PROC_UNLOCK(p); + } + return (error); +} + +static void +prison_attach_thread_single_end(struct thread *td) +{ + struct proc *p; + + p = td->td_proc; + if ((atomic_load_int(&p->p_flag) & P_HADTHREADS) != 0) { + PROC_LOCK(p); + thread_single_end(p, SINGLE_BOUNDARY); + PROC_UNLOCK(p); + } +} + /* * Returns a locked prison instance, or NULL on failure. */home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a49e80c.22848.48d188d3>
