git: 77552467648a - main - execve(): unshare pwd

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 00:16:02 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=77552467648aa06b8c9ac95fb45099630c2a769b

commit 77552467648aa06b8c9ac95fb45099630c2a769b
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-21 04:02:31 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 00:10:23 +0000

    execve(): unshare pwd
    
    Ensure that pwd is not shared when execing into any ABI, so rforked
    siblings are not affected.  The altroot changes made during exec, by
    linux_on_exec() and by the setuid altroot clear, affected rforked
    siblings sharing the pwd.
    
    Remove now always no-op pdunshare() call next to fdunshare().
    
    Reviewed by:    markj
    Fixes:  fd745e1db6b5 ("linux(4): Use pwd_altroot() to tell namei() about ABI root path")
    Fixes:  2137bad49a6f ("exec_new_vmspace(): drop altroot for native processes if setuid")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61858
---
 sys/kern/kern_exec.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index df6f04dfecbe..d081ce5dd879 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -858,11 +858,11 @@ interpret:
 	}
 
 	/*
-	 * For security and other reasons, the file descriptor table cannot be
-	 * shared after an exec.
+	 * For security and other reasons, the file descriptor table
+	 * cannot be shared after an exec.  pwd should not be shared
+	 * either, this was handled earlier in exec_new_vmspace().
 	 */
 	fdunshare(td);
-	pdunshare(td);
 	/* close files on exec */
 	fdcloseexec(td);
 
@@ -1265,6 +1265,13 @@ exec_new_vmspace(struct image_params *imgp, struct sysentvec *sv)
 	imgp->vmspace_destroyed = true;
 	imgp->sysent = sv;
 
+	/*
+	 * Prevent an rforked process, possibly sharing pwd with
+	 * us, from being affected by altroot.  Exec is already past
+	 * the point of no return.
+	 */
+	pdunshare(td);
+
 	/*
 	 * Drop altroot if setuid.  It is done so late to avoid
 	 * clearing altroot until there is no way for execve(2) to