From nobody Fri Jun 26 22:20:44 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gn99T358Cz6j2hD for ; Fri, 26 Jun 2026 22:20:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gn99T0gwyz3mJ5 for ; Fri, 26 Jun 2026 22:20:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782512445; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=usilsFiOaQMx4Q1MoTXclvTPZn2LyhPhp2KUzpQhhOM=; b=qpzzh+dpq4faCHhkG7qZSCSKhkNXmBbzy+F6R5r9KAH02ppt8gaieCyNL71Mq1UEqYKcbA zDclhA4Del8GdonazlmDU0rGyhsTUm0tONcDrJxkJi3FTdBlYfZ80yM9hXdeVNsbyHU8Ip lRGV9fLXGtydj5VhdQkQpDunUzGai+GQor+Yl5gs5bA+msJRNSy0HhtiSvqSJ5pc14hyw5 M09RCZx7vIjXm+XiM5IX9C03WvN+HPm0RXsgSCbXzuK0L+YWEqGFvrVQgmpaZ0e7yP+kQz wON7sxs4wq6yxLTZJ9yQXiHAMjFmBsOIsBPcAlhVKqGiQo/2NvT0f7hhGxzSuQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1782512445; a=rsa-sha256; cv=none; b=TaRinaOws0yZpoyPbYakFxOJ8n0dlQlsaen5M5nhJYKzssfEB6h8KLFU1Dh/0AVHiBhjbU uYDR6kImEIn5hI+I1TwfOaMg+c/+xOAqnZ8WPginx3SxyNrjuN7tDxmJd00c/3I6MJjPAM o5a5+rPkNhCHqoXeCe+d31BIo8VtdGRQN61isvKg0TiR+XEC6FQ33CRuCGuBlCCy5vsxvv V5ooqHK1543mR5oqOuWokSfUG2p/3FwmjLMscqFAOKbWbg4iEKpKHtB3001oGMrphw/qR3 /Wx6dkpoZZCo+94cuffkXeM3Imklmwrp8qFjCvbr20c6tOJTQUwQBgDW00QpyQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782512445; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=usilsFiOaQMx4Q1MoTXclvTPZn2LyhPhp2KUzpQhhOM=; b=CGoKkeLlPjUDbbv/Gxox/10EWao5207k7TV4GvMABEFz92syv9EkyIg6F4Vs1fUl5mkSbr r9fEJ3U82UDjSKUtGHXLRRht8U9p+BDNhVhpY2xbKHCAkTue1aOMw0vXwTUj4/KdHRbMhB ZeKJdOzAPpkN+R+nwb+7wYle55e8+iXEDEecCi6C91/2v4FFB+UuV9fZ+1XA5IbJbxacKa QEgaDtew7+jHQSXYPStH5rZ+Sl/Kxf7WocaR9l0tV6v3hOy8UFJubAAi5GfR9D38XEOdzv l+n5uv49K0m7y/aCPHhQaVhwPXBXHLcTrjd5SZ2clvOX2k96X27EmProe8IbaQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gn99S6vFPzxJt for ; Fri, 26 Jun 2026 22:20:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 272c8 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 26 Jun 2026 22:20:44 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: a80e40ce9ee0 - stable/15 - execve_block(): a mechanism for mutual exclusion with execve() on the process List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: a80e40ce9ee0a0641927730a193ff1f5641e71b6 Auto-Submitted: auto-generated Date: Fri, 26 Jun 2026 22:20:44 +0000 Message-Id: <6a3efb3c.272c8.a59c457@gitrepo.freebsd.org> The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=a80e40ce9ee0a0641927730a193ff1f5641e71b6 commit a80e40ce9ee0a0641927730a193ff1f5641e71b6 Author: Konstantin Belousov AuthorDate: 2026-05-26 17:36:20 +0000 Commit: Konstantin Belousov CommitDate: 2026-06-26 22:16:03 +0000 execve_block(): a mechanism for mutual exclusion with execve() on the process (cherry picked from commit e1a84b7708c2514769625c2af6c5034694013b6a) --- sys/kern/kern_exec.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++-- sys/kern/kern_exit.c | 1 + sys/kern/kern_fork.c | 1 + sys/sys/imgact.h | 3 +++ sys/sys/proc.h | 3 ++- 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index e7552bab6005..ed7534af21bf 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -385,6 +385,62 @@ 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_flag |= P_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_flag & P_INEXEC_WAIT) != 0) { + p->p_flag &= ~P_INEXEC_WAIT; + wakeup(&p->p_execblock); + } +} + /* * In-kernel implementation of execve(). All arguments are assumed to be * userspace pointers from the passed thread. @@ -440,6 +496,10 @@ 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__)); + while (p->p_execblock != 0) { + p->p_flag |= P_INEXEC_WAIT; + msleep(&p->p_execblock, &p->p_mtx, 0, "exeblk", 0); + } p->p_flag |= P_INEXEC; PROC_UNLOCK(p); @@ -910,7 +970,10 @@ interpret: * as we're now a bona fide freshly-execed process. */ KNOTE_LOCKED(p->p_klist, NOTE_EXEC); - p->p_flag &= ~P_INEXEC; + MPASS(p->p_execblock == 0); + if ((p->p_flag & P_INEXEC_WAIT) != 0) + wakeup(&p->p_execblock); + p->p_flag &= ~(P_INEXEC | P_INEXEC_WAIT); /* clear "fork but no exec" flag, as we _are_ execing */ p->p_acflag &= ~AFORK; @@ -1006,7 +1069,9 @@ exec_fail_dealloc: exec_fail: /* we're done here, clear P_INEXEC */ PROC_LOCK(p); - p->p_flag &= ~P_INEXEC; + if ((p->p_flag & P_INEXEC_WAIT) != 0) + wakeup(&p->p_execblock); + p->p_flag &= ~(P_INEXEC | P_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 2f94386c9101..8f9007752b3b 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -365,6 +365,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 3de2a6224834..313fdef86fd8 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -431,6 +431,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 51cda4406cad..8c91b3a54bfa 100644 --- a/sys/sys/imgact.h +++ b/sys/sys/imgact.h @@ -123,6 +123,9 @@ 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); #endif #endif /* !_SYS_IMGACT_H_ */ diff --git a/sys/sys/proc.h b/sys/sys/proc.h index fd5c366ab95d..4e423c3ad0c7 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -780,6 +780,7 @@ struct proc { LIST_ENTRY(proc) p_jaillist; /* (d) Jail process linkage. */ u_int p_asig; /* (c) ASYNCEXIT pending signal. */ u_int p_tree_refcnt; /* (e) proctree refcount */ + u_int p_execblock; /* (c) Blockers for execve. */ }; #define p_session p_pgrp->pg_session @@ -852,7 +853,7 @@ struct proc { #define P_STATCHILD 0x08000000 /* Child process stopped or exited. */ #define P_INMEM 0x10000000 /* Loaded into memory, always set. */ #define P_ASYNC_EXIT 0x20000000 /* XXX */ -#define P_UNUSED2 0x40000000 /* --available-- */ +#define P_INEXEC_WAIT 0x40000000 /* Waiters for P_INEXEC/p_execblock */ #define P_PPTRACE 0x80000000 /* PT_TRACEME by vforked child. */ #define P_STOPPED (P_STOPPED_SIG|P_STOPPED_SINGLE|P_STOPPED_TRACE)