git: c350c69c92b3 - releng/15.2 - fget_remote(9): recheck remote process fdp after holding the file

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:10:03 UTC
The branch releng/15.2 has been updated by markj:

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

commit c350c69c92b3f8ee6d7abcf2e5dc9547b1752ff3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-14 00:30:36 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 17:09:01 +0000

    fget_remote(9): recheck remote process fdp after holding the file
    
    kern_pddupfd() grants access to the process named by a process
    descriptor with p_candebug(), then calls fget_remote() to duplicate a
    descriptor from that process's current filedesc. The check remains
    stable against an execve() by that one process, but the implementation
    does not keep the held filedesc semantically bound to that process.
    
    If the authorized process P shares its descriptor table with another
    process Q, P can detach from the table with rfork(RFCFDG) after
    fget_remote() has held the old table but before it acquires the table
    lock. After P detaches, Q is the sole process-reference owner of the
    old table; fget_remote() retains only a separate lifetime hold. When
    Q executes a setuid program, the normal fdunshare() call observes
    fd_refcnt == 1 and does not copy the table. Privileged descriptors are
    therefore installed in the held old table. The waiting pddupfd() call
    accepts that table because its process reference count is nonzero and
    can return a descriptor opened by Q even though the security check was
    performed against P.
    
    Recheck the file descriptor table of the remote process after the hold,
    and refuse to dup if the table changed.
    
    Approved by:    re (cperciva)
    Reported by:    Reo Shiseki working with TrendAI Zero Day Initiative
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D61520
    
    (cherry picked from commit 6f5458cc9a75e220a5af9d864c007bbf71a4bb0f)
    (cherry picked from commit cfd6ff1fe5ca4fe238a764b3a9594fe316b313d1)
---
 sys/kern/kern_descrip.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 764368b36cd3..6f289a7eca25 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -3197,6 +3197,7 @@ fget_remote(struct thread *td, struct proc *p, int fd, struct filecaps *fcaps,
 	struct file *fp;
 	int error;
 	bool copied __diagused;
+	bool drop_fp;
 
 	/*
 	 * Both fcaps and fd_flags must be either requested together,
@@ -3212,10 +3213,21 @@ fget_remote(struct thread *td, struct proc *p, int fd, struct filecaps *fcaps,
 	PROC_UNLOCK(p);
 	if (fdp == NULL)
 		return (ENOENT);
+	drop_fp = false;
 	FILEDESC_SLOCK(fdp);
 	if (refcount_load(&fdp->fd_refcnt) != 0) {
 		fp = fget_noref(fdp, fd);
-		if (fp != NULL && fhold(fp)) {
+		error = fp != NULL && fhold(fp) ? 0 : EBADF;
+	} else {
+		error = ENOENT;
+	}
+	if (error == 0) {
+		PROC_LOCK(p);
+		/* Guard against a race with rfork(RFCFDG). */
+		if (fdp != atomic_load_ptr(&p->p_fd))
+			error = EBADF;
+		PROC_UNLOCK(p);
+		if (error == 0) {
 			*fpp = fp;
 			if (fd_flags != NULL) {
 				*fd_flags = fde_to_fd_flags(fdp->fd_ofiles[fd].
@@ -3226,14 +3238,13 @@ fget_remote(struct thread *td, struct proc *p, int fd, struct filecaps *fcaps,
 				    &fdp->fd_ofiles[fd].fde_caps, fcaps, true);
 				MPASS(copied);
 			}
-			error = 0;
 		} else {
-			error = EBADF;
+			drop_fp = true;
 		}
-	} else {
-		error = ENOENT;
 	}
 	FILEDESC_SUNLOCK(fdp);
+	if (drop_fp)
+		fdrop(fp, td);
 	fddrop(fdp);
 	return (error);
 }