fdcopy(): properly update fd_freefile when skipping non-copyable fd

fd_freefile is maintained to fulfill the POSIX guarantee that the lowest
possible file descriptor is allocated on open.  If we skip copying an
fd, it is a possible candidate for the lowest index.

First, the file descriptor installed with a kqueue created
without KQUEUE_CPONFORK missed this handling [*].

Second, fd_freefile should be updated only when the current fd index is
below it.  A skipped fd above fd_freefile could raise it past a free slot.

PR:	300234 [*]
Reported by:	Andrea Venturoli <[email protected]>
Reviewed by:	markj
Fixes:	1265516c5c ("struct file: add DFLAG_FORK, indicate copying on fork")
Fixes:	809f3121be ("fd: assign fd_freefile early when copying")
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D61054
This commit is contained in:
Konstantin Belousov committed 2026-10-30 21:07:39 +02:00
1 parent eb0f28db1b
commit 99a58803f9
1 file changed
+5 -2
+5 -2
View File
@@ -2644,13 +2644,16 @@ fdcopy(struct filedesc *fdp, struct proc *p1)
if ((ops->fo_flags & DFLAG_FORK) != 0 &&
(ofde->fde_flags & UF_FOCLOSE) == 0) {
if (ops->fo_fork(newfdp, ofde->fde_file, &fp, p1,
curthread) != 0)
curthread) != 0) {
if (newfdp->fd_freefile > i)
newfdp->fd_freefile = i;
continue;
}
fork_pass = true;
} else if ((ops->fo_flags & DFLAG_PASSABLE) == 0 ||
(ofde->fde_flags & UF_FOCLOSE) != 0 ||
!fhold(ofde->fde_file)) {
if (newfdp->fd_freefile == fdp->fd_freefile)
if (newfdp->fd_freefile > i)
newfdp->fd_freefile = i;
continue;
}