From nobody Mon Apr 13 02:56:39 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4fvBrY3S85z6ZMny for ; Mon, 13 Apr 2026 02:56:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4fvBrY1ByLz3pQP for ; Mon, 13 Apr 2026 02:56:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776049005; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=H+R39vLGNavh42iAeebN+aJKmkAjlznAkSdrd1e01Bc=; b=PDUecgR7Yer+tcx30Hel5X56kevgpUUn4PHaVIRxfO922vrygMLLGu/6ppLCkSPBLtqmkx Ce223duN2z1EMcfFCBVR36sVJVR/2nScELjDHJeMbFDmiZ8ffvnfD0jWTGSa4Mtqzm0SUQ tydpQdQeDi8QlEdbv0yTd3rBcmInq/NB66yTEc6UVGKM769fupjWaQoimsP9QfS7UYH94W GvbJywiPkl0Ou1qQP4+dkBWMkUBKK5qN6ejBk0UZ7UrnHv0MCMus89klNz+zw+7UZIun2i xeDh8keQkVTt+lSfjdewL2mstZA6vbNjLuFbqfoOIrL6WxGDWsKLL+6MdnDJsw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1776049005; a=rsa-sha256; cv=none; b=L/2UidGkOgLFmF6T4ZQg5RS4lfqd+WPg5210ZHedaRQaXHL06H98utJUyOt+R+bNFdBHSh 6euxGSUf58ZRihmgHIRjb9AmNLRaF++Oit6r1laYmtC/6FdDZa42wjD5gOPxzWrKWrareC STysmz0IgYonjfWzGd0B2aQOZcI7IC1UXLrdb6qpoho491ZIC4NUFqn3CMxLoKK/KPaOMI kYjyRThwPgPUZbBaJrlkyS8fMZEpIrP47iMUI9EdbzRj1495t5d49Hda7anNiZLbozaYec iX0mUM4pbBYGQWpiaAlUNVkKGqARdbcVzI6sNjUuesKwAah0wU+SIJC5xcbdfg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776049005; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=H+R39vLGNavh42iAeebN+aJKmkAjlznAkSdrd1e01Bc=; b=HDLA94g2KjZbJACtnHlCq2+JCgEWQ1sRpTSmDAXK8WwqVnn5m2ZrNRvRe9mT3bUGFS3sK5 1/gx+jnuMilPl59pqlnmjGSzbGxY9lFq8ODhuy0/Bdp9PXe3YdgnrT2gjbz9UeC/GDRWc3 x2Mp2rFZ3HJOHMdWOBvygJYMi4Ne+pzs8qMevg2k990jX67FCkEdW8OXJX+2XkcskusBEU 2mLvNQshxL7XW+8y+xWTLbq2YMNTVbR8QPxpuMCiH8xtNkZfQgOdGV/kbEJfiiql1i5rb1 4cYXHQjhTPKBaOAxQRjrWQVx0A7IrCS3ff8B20x0ZUDCOi4ziTi+bOj8XwMOQQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fvBrY0Htwzm84 for ; Mon, 13 Apr 2026 02:56:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 44d44 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 13 Apr 2026 02:56:39 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 88535adba95d - stable/14 - kqueue: Fix a race when adding an fd-based knote to a queue List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 88535adba95d019557b4281c52daa9feccade2fe Auto-Submitted: auto-generated Date: Mon, 13 Apr 2026 02:56:39 +0000 Message-Id: <69dc5b67.44d44.27fb06e6@gitrepo.freebsd.org> The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=88535adba95d019557b4281c52daa9feccade2fe commit 88535adba95d019557b4281c52daa9feccade2fe Author: Mark Johnston AuthorDate: 2026-03-27 00:24:18 +0000 Commit: Mark Johnston CommitDate: 2026-04-13 02:53:26 +0000 kqueue: Fix a race when adding an fd-based knote to a queue When registering a new kevent backed by a file descriptor, we first look up the file description with fget(), then lock the kqueue, then see if a corresponding knote is already registered. If not, and KN_ADD is specified, we add the knote to the kqueue. closefp_impl() interlocks with this process by calling knote_fdclose(), which locks each kqueue and checks to see if the fd is registered with a knote. But, if userspace closes an fd while a different thread is registering it, i.e., after fget() succeeds but before the kqueue is locked, then we may end up with a mismatch in the knote table, where the knote kn_fp field points to a different file description than the knote ident. Fix the problem by double-checking before registering a knote. Add a new fget_noref_unlocked() helper for this purpose. It is a clone of fget_noref(). We could simply use fget_noref(), but I like having an explicit unlocked variant. PR: 293382 Reviewed by: kib MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D55852 (cherry picked from commit 8f3227f527567aef53da845ab78da8e16d9051c1) --- sys/kern/kern_event.c | 14 +++++++++++++- sys/sys/filedesc.h | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index 920256f8c1bd..df509e7a8492 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -28,7 +28,6 @@ * SUCH DAMAGE. */ -#include #include "opt_ktrace.h" #include "opt_kqueue.h" @@ -1654,6 +1653,19 @@ findkn: error = ENOMEM; goto done; } + + /* + * Now that the kqueue is locked, make sure the fd + * didn't change out from under us. + */ + if (fops->f_isfd && + fget_noref_unlocked(td->td_proc->p_fd, + kev->ident) != fp) { + KQ_UNLOCK(kq); + tkn = kn; + error = EBADF; + goto done; + } kn->kn_fp = fp; kn->kn_kq = kq; kn->kn_fop = fops; diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index 440c5d3d15f9..18077d11150d 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -213,6 +213,8 @@ struct filedesc_to_leader { #ifdef _KERNEL +#include + /* Operation types for kern_dup(). */ enum { FDDUP_NORMAL, /* dup() behavior. */ @@ -303,6 +305,21 @@ int fget_only_user(struct filedesc *fdp, int fd, MPASS(refcount_load(&fp->f_count) > 0); \ }) +/* + * Look up a file description without requiring a lock. In general the result + * may be immediately invalidated after the function returns, the caller must + * handle this. + */ +static inline struct file * +fget_noref_unlocked(struct filedesc *fdp, int fd) +{ + if (__predict_false( + (u_int)fd >= (u_int)atomic_load_int(&fdp->fd_nfiles))) + return (NULL); + + return (atomic_load_ptr(&fdp->fd_ofiles[fd].fde_file)); +} + /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */ static __inline struct file * fget_noref(struct filedesc *fdp, int fd)