From nobody Mon Apr 13 02:58:41 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 4fvBtp0ZvDz6ZMT8 for ; Mon, 13 Apr 2026 02:58:42 +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 4fvBtn6s8Dz3qXg for ; Mon, 13 Apr 2026 02:58:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776049122; 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=h8ydgQvVxXdc5UY8VUqw+GDSzENVP0FBZwKMY79Q+AM=; b=OLvDdbf5h7/o4mztlvQCZxiGlo2WRRY0E4Uuz/JmcTj1NmJ1xWpJm0T0B9PvtiVFNfe34L D2X9NVfaUl9HC0l1LtKFDLip9fbIWyqOdMOo1NPRuu/11jzm/GmkWUGCjS4OLjlT+Ffgl9 wyOKQY93+HfsKtLftq5KFunMqEL1D/t4+Rm8qFMGDqGDEy1UB2tJK3tXuzepM5HIM5f6Q8 i9SQ0XwfBLGu/xKqqwIVAG5/aWg4+Gr9eDXLqsKt1b26nodIFiFwgfTUOTbfL+RZiHRNZU PlT006TZH1ttv3CpWGLxcY4kUj2hjwRLNE7YuGUa9RtxbXBWyVdcqdbWHclY2g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1776049122; a=rsa-sha256; cv=none; b=C20uwe6J8DuV33OtSLTEsiRDXs7QohN+Onqv5nC+4Hqb31CwxB0ZIvZj4Hd9lnjhBpkNjI dNZ23opt+sg6utCizt6nPJQUuRU4/9LsRIsOWzEloRsN7zRvIn80oWj9s0JghgCRbKAL2H u576Z6HHo2Ks31dXpjEUKktmIzsUEW2FPIQwuTxDEyoVeDCtvhwEC8urq3mlPS1Dvdb9gR zIGcaSKcUZwZ+sfsRwKLgjT1JR9jyOPpicZUyREsf6gnp/6qH3VN98IThN0VRlPjbbImoQ wChmL//iSYeBRisNbvyXbMsObzfsND1x5/uAKNPKzv0VcOVKRZyQ5oZdCd0tLA== 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=1776049122; 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=h8ydgQvVxXdc5UY8VUqw+GDSzENVP0FBZwKMY79Q+AM=; b=qkulZdUHchXLwfy4oiLV4G9lMWcFF1i/zkZ7++8t28QHsa0CmCC9o2uRJ8oo6IJNP2RDBf do3U/1Bv+uCobwRl6g+fUGTM6aDkbKRCAyRMDAlMnb91s8r1MxJdgadDt1b3AZqOoCllio e4b/KH7tUE8YiX9O73eBOrGwG8lF7ZWcR39eqGUff2Eb9KCSKU/K85M5NYn6gpBAbRVp9i te6W1tWAi4gYfl3Z/ld2b27rJWZi1fq9hwIq25/LJ19pZuTKXdQEdcNACCHLts23PxzbOG 3jrUrefIQRpEV7xhfXohy+l0C5kjtZjQhmUvkwZKwCTmrQiKTqpRuD8u8UOyUw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fvBtn61YWzm5w for ; Mon, 13 Apr 2026 02:58:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 46633 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 13 Apr 2026 02:58:41 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: bb5347cc6f78 - stable/13 - 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/13 X-Git-Reftype: branch X-Git-Commit: bb5347cc6f78949dab1c631af546aa3fbd87e724 Auto-Submitted: auto-generated Date: Mon, 13 Apr 2026 02:58:41 +0000 Message-Id: <69dc5be1.46633.3225eb05@gitrepo.freebsd.org> The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=bb5347cc6f78949dab1c631af546aa3fbd87e724 commit bb5347cc6f78949dab1c631af546aa3fbd87e724 Author: Mark Johnston AuthorDate: 2026-03-27 00:24:18 +0000 Commit: Mark Johnston CommitDate: 2026-04-13 02:53:41 +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 3fb576648fee..850db2d97dfa 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" @@ -1632,6 +1631,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 a39716fdce0b..566b0a898307 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -198,6 +198,8 @@ struct filedesc_to_leader { #ifdef _KERNEL +#include + /* Operation types for kern_dup(). */ enum { FDDUP_NORMAL, /* dup() behavior. */ @@ -293,6 +295,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_locked(struct filedesc *fdp, int fd)