From nobody Mon Apr 13 02:54:58 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 4fvBpW1WVFz6ZMsT for ; Mon, 13 Apr 2026 02:54:59 +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 4fvBpV672qz3n6M for ; Mon, 13 Apr 2026 02:54:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776048898; 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=dP7vpKhAocgvUzfh7KNsCCph+TyGVJkJ+nqqKtInQ3c=; b=D1z3rPQaAInsAIHWy40L/NxfFM1jpqur9N4cEQgY/thPI+alE31kLKlzkb9jbdcY+/Uf23 +mhdTADzeOgs91zq/6tYS7pjAhtoMI8LcW0lFKDs85naFsQTdn31XJXAsUl3/TecajBIg/ EDqr0JN4Eh03/vvjGV0J3WkR04gczwzy9ln9E9m8jv6fDdI82kBkmhc8a9/Zjm8cGA1T4c 9JC5koO2T3GrZgkP/SxwHetB358EpFhuStvX5vpVr8JcVV6pUxWJz8WsVEgN0cu3yuo9QB VSrRXlXaif5VvGPeNAYM1G17Ux7LdNcvz1EMBdiEry/JsyLV/eUlVystjZSDRQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1776048898; a=rsa-sha256; cv=none; b=sYoI1vbHKelZ+oksoTqhcHK5J780lG4jUpIOBrq/2uEVIayh/OI3Wf1WHfpXja5kEdu1y9 qkYghglagjs7M+YWCvzeAKUMgPXXVBAEpAGO1bTiVuWVezQ6W/rN/TQEvx+P5K5D8OL3oJ CtvQHAHKXaoBohIhCZf/6fOKwJLD0IVjaEvD4H387z4pN8xsQxtKXsulU2Wir0PbCy8zVs sp1zR4g4iWreRfNVO2hARcUPIu14ohnG+Q/isDnnWlsmtVmsWArNkGBiVCcbKbYnu0Gvg0 pOwhsUhjvVPquvHUmtaIuduykXCt8TGU3y/Yhy/FdsT6u8BGqAsruNtH08+mPg== 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=1776048898; 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=dP7vpKhAocgvUzfh7KNsCCph+TyGVJkJ+nqqKtInQ3c=; b=dH5s+qYwehv0amCbe5FphN10AAvgpQ4R/lNoHI2mz8vXbMDX+Z4WFn60Jx/eVsp5qHN5ik /2Y0MOphH3eavYjlaitfeizt9L8b1g0rVcZBY1QbZ48XTsXyTXM8xLEZNANLY4WsewKUaX b1oP5nawWD+ruvjMtCVfSK6lLBZtsfFXxoL3SrWJZxJ3ebYqhjZKs5kcabgZu+pJU+y+Gl vMlD6p1SRZeUfFBThGVBxUC/pBcruqTyyw7zbEGSQ5bTWFaH6zEjUGCb0xd1jiNohzbhr7 Yd9rHYBEOt5JDD+YjxigvrNqMrBrf5xrpPse6xQDkuX162nGZfdy+NUjcfiBEg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fvBpV5PqFzm82 for ; Mon, 13 Apr 2026 02:54:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 46740 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 13 Apr 2026 02:54:58 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 84b871567ebe - stable/15 - 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/15 X-Git-Reftype: branch X-Git-Commit: 84b871567ebe264222d96655e4f6f92c73b0996e Auto-Submitted: auto-generated Date: Mon, 13 Apr 2026 02:54:58 +0000 Message-Id: <69dc5b02.46740.65dfb70f@gitrepo.freebsd.org> The branch stable/15 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=84b871567ebe264222d96655e4f6f92c73b0996e commit 84b871567ebe264222d96655e4f6f92c73b0996e Author: Mark Johnston AuthorDate: 2026-03-27 00:24:18 +0000 Commit: Mark Johnston CommitDate: 2026-04-13 02:53:06 +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 2bb965f3e5e4..3872fb5862c5 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" @@ -1807,6 +1806,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)