Date: Sun, 05 Jul 2026 05:13:47 +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: 530ee2980c50 - stable/14 - jail: clean up locking around do_jail_attach Message-ID: <6a49e80b.22a25.226d169f@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=530ee2980c50d3639e01e21a7fed64eed2741ebf commit 530ee2980c50d3639e01e21a7fed64eed2741ebf Author: Jamie Gritton <jamie@FreeBSD.org> AuthorDate: 2026-06-25 03:21:18 +0000 Commit: Jamie Gritton <jamie@FreeBSD.org> CommitDate: 2026-07-05 03:26:04 +0000 jail: clean up locking around do_jail_attach jail_attach_jd passed PD_DEREF to do_jail_attach, assuming it would take care of freeing the held prison. This is not true, as do_jail_attach immediately cleared that flag, leaving the jail stock in dying state when it is later removed. Reported by: markj Reviewed by: markj Differential Revision: <https://reviews.freebsd.org/D57674> (cherry picked from commit 3584cde63e416d150214192721cead4b735ca0b5) --- sys/kern/kern_jail.c | 57 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index a18da4472947..f40e7c4e176c 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -149,7 +149,7 @@ LIST_HEAD(, prison_racct) allprison_racct; int lastprid = 0; static int get_next_prid(struct prison **insprp); -static int do_jail_attach(struct thread *td, struct prison *pr, int drflags); +static int do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp); 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); @@ -2134,11 +2134,12 @@ kern_jail_set(struct thread *td, struct uio *optuio, int flags) pr->pr_state = PRISON_STATE_ALIVE; } - /* Attach this process to the prison if requested. */ + /* + * Attach this process to the prison if requested. This will + * unlock allprison_lock, meaning changes are now user-visible. + */ if (flags & JAIL_ATTACH) { - error = do_jail_attach(td, pr, - prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); - drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); + error = do_jail_attach(td, pr, &drflags); if (error) { vfs_opterror(opts, "attach failed"); goto done_deref; @@ -2631,39 +2632,48 @@ int sys_jail_attach(struct thread *td, struct jail_attach_args *uap) { struct prison *pr; - int error; + int drflags, error; error = priv_check(td, PRIV_JAIL_ATTACH); if (error) return (error); sx_slock(&allprison_lock); + drflags = PD_LIST_SLOCKED; pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); if (pr == NULL) { - sx_sunlock(&allprison_lock); - return (EINVAL); + error = EINVAL; + goto done; } + drflags |= PD_LOCKED; /* Do not allow a process to attach to a prison that is not alive. */ if (!prison_isalive(pr)) { - mtx_unlock(&pr->pr_mtx); - sx_sunlock(&allprison_lock); - return (EINVAL); + error = EINVAL; + goto done; } - return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); + error = do_jail_attach(td, pr, &drflags); + + done: + prison_deref(pr, drflags); + return (error); } +/* + * Attach the current process to a prison. On entry, the allprison + * lock should be at least shared. On exit, both it and the prison + * itself will be unlocked, which will be refelected in *drflagsp. + */ static int -do_jail_attach(struct thread *td, struct prison *pr, int drflags) +do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp) { struct proc *p; struct ucred *newcred, *oldcred; - int error; + int drflags, error; - mtx_assert(&pr->pr_mtx, MA_OWNED); sx_assert(&allprison_lock, SX_LOCKED); - drflags &= PD_LOCK_FLAGS; + 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 @@ -2672,13 +2682,22 @@ do_jail_attach(struct thread *td, struct prison *pr, int drflags) * 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 + * dropped (which isn't true now, but will be after OSD calls). + */ prison_hold(pr); refcount_acquire(&pr->pr_uref); - drflags |= PD_DEREF | PD_DEUREF; - mtx_unlock(&pr->pr_mtx); - drflags &= ~PD_LOCKED; + drflags = PD_DEREF | PD_DEUREF | (*drflagsp & PD_LOCK_FLAGS); + *drflagsp &= PD_OP_FLAGS; /* Let modules do whatever they need to prepare for attaching. */ + if (drflags & PD_LOCKED) { + mtx_unlock(&pr->pr_mtx); + drflags &= ~PD_LOCKED; + } error = osd_jail_call(pr, PR_METHOD_ATTACH, td); if (error) goto e_revert_osd;home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a49e80b.22a25.226d169f>
