git: cfd6ff1fe5ca - stable/15 - fget_remote(9): recheck remote process fdp after holding the file
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 17 Nov 2026 17:06:22 UTC
The branch stable/15 has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=cfd6ff1fe5ca4fe238a764b3a9594fe316b313d1
commit cfd6ff1fe5ca4fe238a764b3a9594fe316b313d1
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:02:15 +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.
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)
---
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 ea033a7f598a..b5799a827e6d 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -3204,6 +3204,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,
@@ -3219,10 +3220,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].
@@ -3233,14 +3245,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);
}