From nobody Fri Mar 27 00:30:54 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 4fhhQ67296z6WQX9 for ; Fri, 27 Mar 2026 00:30:54 +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 4fhhQ65zWSz3rMX for ; Fri, 27 Mar 2026 00:30:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1774571454; 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=/jhL+SLudsIxvGT8WL3wXtJw7QTFWRRZO+5FB+zZSA4=; b=cs915rRFtyKN1zECSDVy6NQQoCOnudHKh7OT5xh7Ws0lRTFAzaGLivGlzqyPTkt3DERlQe MiWFOGJSKllKNgZMFRML2/WyJmTsNp1q9/ywRTTtUIyl0LvHu497tvxtitFWizrwxXn/ib pVd1mIjzg8iGkhRPK+qc2Vxyvxk/N1Bfq9LpecQ0YtSmYCTeXCTbcawX5kmfBUXq38L7i4 PKbkrDysJD9ckqc4L54G468IR3xOaefaly9OdKO7u2fpGtfph8kH6mllb1AcArIfZ5zw+Z WbVIbudOSoJKp+O91I4PLWU2onpApcIOMgl9Jo7KaSCyAvqJyT9wCTQg8oFXEg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1774571454; a=rsa-sha256; cv=none; b=I5MHz/jnLFrZGSb7o5RnoYCmQhsHNXD71XfYhiOxtRL2HeZCduaxg4Z6XLSrL8zXIjbwVX nQ17PbZhtc0vFwc/Mcbh0PGagRzqWBSmXPACqqVXK1oGIFSYWjDQ7t8GsDrpdN1lxf0YQK QV1vNajOn1rOziMTF5F+oJftFRskpAMHr/UyNRMlejSVRqBx++/P+lSX6aMqUYc7vlgpEk LURymgMlAnDLuTPtLr6iYuDH2l2fVpQ3LER0tTquvjwHV08mZYGu7qnid/xM0vx6wK9Yld ApoKLucFP9n9JzpOJbmllL0d3VAM0Oj/D1gjiueCXbcjIM1fRPwaBIolDQHiFA== 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=1774571454; 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=/jhL+SLudsIxvGT8WL3wXtJw7QTFWRRZO+5FB+zZSA4=; b=Faky0D4U8fPeOZ0PawXBOJKdtsfK+xnaPQKetkLtTR7Zap9AuOyzO8dZzoDVpAPmqIm29j xD5kBMZpxsYUFe09cBzcddaf4hphFzGSAtU7k5hM4filk5blakl3lOAkkEUt9GLzNhWGye I/0rcjXf0EUIbGHI8seKj22o7cVSjHlZv2JbiJvlawwhlhcq/vKg/IeWQKPSZrGXT/soVl hDeFpSW0fm5eUWd7gFtUi2rQ908BC2cRMzFUEREyzVcM7OqBHZM/l+8+m96OcgCBYAgaSt mzw1THa76VoF/hgH1vfwStuUGfNdY4C88M/AtquuzQrpUIvwxkPkzN4TgoTgYg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fhhQ65WNDzhc for ; Fri, 27 Mar 2026 00:30:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 45a44 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 27 Mar 2026 00:30:54 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8f3227f52756 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 8f3227f527567aef53da845ab78da8e16d9051c1 Auto-Submitted: auto-generated Date: Fri, 27 Mar 2026 00:30:54 +0000 Message-Id: <69c5cfbe.45a44.22e5dbbb@gitrepo.freebsd.org> The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8f3227f527567aef53da845ab78da8e16d9051c1 commit 8f3227f527567aef53da845ab78da8e16d9051c1 Author: Mark Johnston AuthorDate: 2026-03-27 00:24:18 +0000 Commit: Mark Johnston CommitDate: 2026-03-27 00:24:18 +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 --- 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 e8e670d39d09..f984161bfcd6 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" @@ -1803,6 +1802,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 4817855443af..c6499a18b884 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -212,6 +212,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)