git: dc94d15a7136 - stable/14 - execve_block(): a mechanism for mutual exclusion with execve() on the process
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 28 Jun 2026 00:30:02 UTC
The branch stable/14 has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=dc94d15a7136c4cfe3342c005d8a319f0583ed9a
commit dc94d15a7136c4cfe3342c005d8a319f0583ed9a
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-05-26 17:36:20 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-06-28 00:28:57 +0000
execve_block(): a mechanism for mutual exclusion with execve() on the process
(cherry picked from commit e1a84b7708c2514769625c2af6c5034694013b6a)
---
sys/kern/kern_exec.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
sys/kern/kern_exit.c | 1 +
sys/kern/kern_fork.c | 1 +
sys/sys/imgact.h | 4 +++
sys/sys/proc.h | 3 ++
5 files changed, 88 insertions(+)
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index 349e13915b29..4461b9e4dc11 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -370,6 +370,77 @@ execve_nosetid(struct image_params *imgp)
}
}
+/*
+ * Returns true if the execblock was obtained, in this case the
+ * process lock is kept. Returns false if the execblock was not
+ * obtained, but the function slept and the lock was dropped.
+ */
+bool
+execve_block(struct thread *td, struct proc *p)
+{
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+ MPASS(td == curthread);
+ MPASS(p != td->td_proc || (p->p_flag & P_INEXEC) == 0);
+
+ if (p != td->td_proc && (p->p_flag & P_INEXEC) != 0) {
+ p->p_flag2 |= P2_INEXEC_WAIT;
+ msleep(&p->p_execblock, &p->p_mtx, PDROP, "inexec", 0);
+ return (false);
+ }
+ MPASS(p->p_execblock < UINT_MAX);
+ p->p_execblock++;
+ return (true);
+}
+
+/*
+ * Might drop the process lock internally, callers must re-check the
+ * invariants afterward.
+ */
+void
+execve_block_wait(struct thread *td, struct proc *p)
+{
+ bool first;
+
+ PROC_ASSERT_HELD(p);
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+
+ for (first = true;; first = false) {
+ if (!first)
+ PROC_LOCK(p);
+ if (execve_block(td, p))
+ return;
+ }
+}
+
+void
+execve_unblock(struct thread *td, struct proc *p)
+{
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+ MPASS(td == curthread);
+
+ MPASS(p->p_execblock > 0);
+ p->p_execblock--;
+ if (p->p_execblock == 0 && (p->p_flag2 & P2_INEXEC_WAIT) != 0) {
+ p->p_flag2 &= ~P2_INEXEC_WAIT;
+ wakeup(&p->p_execblock);
+ }
+}
+
+void
+execve_block_pass(struct thread *td)
+{
+ struct proc *p;
+
+ MPASS(td == curthread);
+ p = td->td_proc;
+ PROC_LOCK_ASSERT(p, MA_OWNED);
+
+ while (p->p_execblock != 0) {
+ p->p_flag2 |= P2_INEXEC_WAIT;
+ msleep(&p->p_execblock, &p->p_mtx, 0, "exeblk", 0);
+ }
+}
+
/*
* In-kernel implementation of execve(). All arguments are assumed to be
* userspace pointers from the passed thread.
@@ -425,6 +496,7 @@ do_execve(struct thread *td, struct image_args *args, struct mac *mac_p,
PROC_LOCK(p);
KASSERT((p->p_flag & P_INEXEC) == 0,
("%s(): process already has P_INEXEC flag", __func__));
+ execve_block_pass(td);
p->p_flag |= P_INEXEC;
PROC_UNLOCK(p);
@@ -893,7 +965,11 @@ interpret:
* as we're now a bona fide freshly-execed process.
*/
KNOTE_LOCKED(p->p_klist, NOTE_EXEC);
+ MPASS(p->p_execblock == 0);
+ if ((p->p_flag2 & P2_INEXEC_WAIT) != 0)
+ wakeup(&p->p_execblock);
p->p_flag &= ~P_INEXEC;
+ p->p_flag2 &= ~P2_INEXEC_WAIT;
/* clear "fork but no exec" flag, as we _are_ execing */
p->p_acflag &= ~AFORK;
@@ -975,7 +1051,10 @@ exec_fail_dealloc:
exec_fail:
/* we're done here, clear P_INEXEC */
PROC_LOCK(p);
+ if ((p->p_flag2 & P2_INEXEC_WAIT) != 0)
+ wakeup(&p->p_execblock);
p->p_flag &= ~P_INEXEC;
+ p->p_flag2 &= ~P2_INEXEC_WAIT;
PROC_UNLOCK(p);
SDT_PROBE1(proc, , , exec__failure, error);
diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c
index bbd30dfb9480..baa5cad2bdc8 100644
--- a/sys/kern/kern_exit.c
+++ b/sys/kern/kern_exit.c
@@ -325,6 +325,7 @@ exit1(struct thread *td, int rval, int signo)
while (p->p_lock > 0)
msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
+ MPASS(p->p_execblock == 0);
PROC_UNLOCK(p);
/* Drain the limit callout while we don't have the proc locked */
callout_drain(&p->p_limco);
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 7731ae4d14f3..d8578f01677b 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -384,6 +384,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread *
bzero(&p2->p_startzero,
__rangeof(struct proc, p_startzero, p_endzero));
+ p2->p_execblock = 0;
/* Tell the prison that we exist. */
prison_proc_hold(p2->p_ucred->cr_prison);
diff --git a/sys/sys/imgact.h b/sys/sys/imgact.h
index c1c94a2eabfd..d35f85620ac9 100644
--- a/sys/sys/imgact.h
+++ b/sys/sys/imgact.h
@@ -122,6 +122,10 @@ int exec_shell_imgact(struct image_params *);
int exec_copyin_args(struct image_args *, const char *, char **, char **);
int pre_execve(struct thread *td, struct vmspace **oldvmspace);
void post_execve(struct thread *td, int error, struct vmspace *oldvmspace);
+bool execve_block(struct thread *td, struct proc *p);
+void execve_block_wait(struct thread *td, struct proc *p);
+void execve_unblock(struct thread *td, struct proc *p);
+void execve_block_pass(struct thread *td);
#endif
#endif /* !_SYS_IMGACT_H_ */
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 95a279742300..66b6a35c8d95 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -777,6 +777,7 @@ struct proc {
TAILQ_HEAD(, kq_timer_cb_data) p_kqtim_stop; /* (c) */
LIST_ENTRY(proc) p_jaillist; /* (d) Jail process linkage. */
+ u_int p_execblock; /* (c) Blockers for execve. */
};
#define p_session p_pgrp->pg_session
@@ -890,6 +891,8 @@ struct proc {
sync core registered */
#define P2_MEMBAR_GLOBE 0x00400000 /* membar global expedited
registered */
+#define P2_INEXEC_WAIT 0x80000000 /* Not same as in HEAD.
+ Waiters for P_INEXEC/p_execblock */
/* Flags protected by proctree_lock, kept in p_treeflags. */
#define P_TREE_ORPHANED 0x00000001 /* Reparented, on orphan list */