From nobody Mon Apr 13 02:56:39 2026 X-Original-To: dev-commits-src-branches@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 4fvBrS2Zj0z6ZMnt for ; Mon, 13 Apr 2026 02:56:40 +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 4fvBrR723pz3pHY for ; Mon, 13 Apr 2026 02:56:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776049000; 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=SQTnf6ZANApnZQowFrY3r7A4eQtQfm7t9NkDxvz4veRieUN1d3TmsgM7PNE8fM1QKFQ7jX Hi9cLf7qQC4XsOnedinjlEjddRybKYZ8vyhExDtMFzRWu76ajG6f8vKdDj7cN5zI0JxqOy 2q3aE7VfIvNhGZjaKvkTwwJoC9PTDuEy3hnlwwoeKOfEjuoQMQ+2pwuk5IxOMRpCSBI17X Ma7cdUzKVdAyRa4iMMbWmGh6cFHMAA9mV4vksJhz0bOziB5eEaLz5weXMRWYRRRToWJ+Qv GK+Pbi2h4448GfpQ7+YE8wHX6XPOwln3Z0ZxkvnkWLiLM+4t0JsO0G0ZqA3DDw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1776049000; a=rsa-sha256; cv=none; b=K5lt7oao95A2hL8A3Zx9UzciTk0PdZAC0fM4eBWZ8jHnxRik7dl76puaVYbSrDr4/+cPyt HywvRaP0mP44NwOMt4Ik6fuM2t2p+nDabc+FJRvQcZqwOggZaaBo8G3uSEP3jUb1/gknAQ wRnjGedXqqXRg1UCwrDkznVW+DNFBpWgJXlVj1cdLJhx3j8PlJOFdlX20I401cUOKnLqAY DP3gwwX7AUM7eJZKW1Gjv1SHhhd0mq6bAuqJGSluFXVfMgJkzyN/3S3mUjAwSoPiPCvIwp 7Poa1Ds5anY96VUGAhK0XBwHRbE0cPhMEw3VB3LFRbrVyiKAgmLlVpR2aYOGMw== 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=1776049000; 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=EM36QoP8q3CpMUgXbv6bHwqj2Cos72wmQN2zdtpD3evJMfMcByc/09sz7kJb35O70VV6VA uQD1h7vJx6a+8dAbSA/BzE7a9LaeZVh1sF9GcbLMxTDaz6r7OxbbE8HYfIfhunMzk+F4BS rP4olYQvcYYp+tO5mwthLhlwpSv+gAmA+qsKuxpj5Ntu+I+MrdHzFWWDtS38j6V1+9JkPT 48vrTXYAzlQFSWX/3WBZaNpvYW2FKApcUNghE+F7/EzJRY6UVCM01B9vhkHqm8P06yp+wF LfNCJaEKNui0HN1ln7wJnPX1bIUUqBWPnyC7wYq3JL1Sku5C6sDuutjWV+n7lg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fvBrR6H4tzlP5 for ; Mon, 13 Apr 2026 02:56:39 +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: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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)