git: 2137bad49a6f - main - exec_new_vmspace(): drop altroot for native processes if setuid

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 15:10:48 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2137bad49a6f4336cbc6c4045fdaf408dcc96f06

commit 2137bad49a6f4336cbc6c4045fdaf408dcc96f06
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-28 21:53:05 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-20 15:10:26 +0000

    exec_new_vmspace(): drop altroot for native processes if setuid
    
    Noted and reviewed by:  markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D60035
---
 sys/kern/kern_exec.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index 7871ac9b86fa..390f4cdaf3ed 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -1265,6 +1265,20 @@ exec_new_vmspace(struct image_params *imgp, struct sysentvec *sv)
 	imgp->vmspace_destroyed = true;
 	imgp->sysent = sv;
 
+	/*
+	 * Drop altroot if setuid.  It is done so late to avoid
+	 * clearing altroot until there is no way for execve(2) to
+	 * return to the calling image.
+	 *
+	 * Setuid is ignored for P2_NO_NEW_PRIVS processes, but
+	 * credential_setid is only cleared by execve_nosetid() after
+	 * the image activator returns, so check the flag here.
+	 */
+	if (imgp->credential_setid &&
+	    (p->p_flag2 & P2_NO_NEW_PRIVS) == 0 &&
+	    (sv->sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)
+		pwd_altroot(td, NULL);
+
 	if (p->p_sysent->sv_onexec_old != NULL)
 		p->p_sysent->sv_onexec_old(td);
 	itimers_exec(p);