From nobody Mon Jun 8 11:01:53 2026 Received: from mail-dy1-f194.google.com (mail-dy1-f194.google.com [74.125.82.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6084C33BBCF for ; Fri, 29 May 2026 14:25:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780064753; cv=none; b=IWoY/6mWdyPfyEufQz9XB0FxgTWWNdCutdI5nHgT40uTBm6PTwy6ME5fy7oNX1/ChUjrMguMdXCm8/BNtkX3SbH9/KZ76roPLimcoPUJkBvsH9IJ4qi2pSTmr2/ikL/Gw3BCwbmVISNrjGJu31qQtcbjnktTuat+gtHjOclPOLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780064753; c=relaxed/simple; bh=LQB2kaz9QSTegWvTSJZra84FuHw/6aS/0tC7TZJBexg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=u+7lnGfsTyZgY/k7NalZAJBaPZKAdnQGZH5NsMm1R2Pwgj4MLqFNdoglCIOWx5HnC9nbZnq2sLfTUQ6myL1gJHTq8Vb1FsyWYzWEsktkHHqQu++Qr+aCG0Yj5Td6OZXRtk3Oj3UuM51IdsdYk01uYVT8RmDJzUBYTEUWlNuVpKQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=tI83X0Wc; arc=none smtp.client-ip=74.125.82.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tI83X0Wc" Received: by mail-dy1-f194.google.com with SMTP id 5a478bee46e88-304f0039c02so954213eec.1 for ; Fri, 29 May 2026 07:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780064749; x=1780669549; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=cSyq379iMRs5CLWz1GrbdWaHEBM9DGZinDVUSvey/oI=; b=tI83X0WchL7gjQ2oluUFBAo/ZebSlRKORALLuCgrXZQGyjwQzS/eYBi110hPoL1BqB gsu/Ww03ymPS48ic4QT3uXGkRqq2dpyZkAd58fWsEZ9++I5DfYKL55Gx2nl7ANPPQtVW vOsyDpAI+9FWf4N5hUSOpqbMEVWeTNFyZZ2hIdCJnn6W13x/+l4WFLSWDITGkL1UlhtK 0Fh+T6jDLd7aqqH7Vy65wuOahfe+2HDRpSP7PJKZNUWRhjMDE4G3epPslso00135C69q X4ERhsKlBi01O1l2mZU304EpNM3a3mv15wYrT2vx9vL1E4tYmFIiRwdhmuNsT9OxLKxs syKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780064749; x=1780669549; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=cSyq379iMRs5CLWz1GrbdWaHEBM9DGZinDVUSvey/oI=; b=Ub1vEAnxg5Ual7zCCLrta9jOZ9S7LvkkdXHtqp8TbOi8VQVgKh+D/W8V+LNXFCoMDm 51HJ9aIwTkx7FHdnbMJdBdCVCbNbbOcHn7JRm4ShOgGsmVDHmDQQRVhbcixpT7bc8mHm Jlfst2yjjhJpha/NnWU+YBvax/kyDsub2zls3jPZthqR3y0LL/G9BEpRKQLszjqrCO0z PsMjUo8M+uQ7twe544bf4nJ9nlwHcDO9p4r84Voj7RaVPP5XLURmIJsMNPZaWfbumYlO tv6HSUJ4YpzL/ms6soHv215LQis95ZMQbZEQdQMce72qfSSpe0nbDhz80ZFtUAMwQWbS sipg== X-Forwarded-Encrypted: i=1; AFNElJ+tR6UufF+qD4w9SZYUXkjWyr3tM2QvJALK/mK4c2wXTE//JTikz05QtAN5dTFTr6wbOtgXkDj6dm+y4PY=@vger.kernel.org X-Gm-Message-State: AOJu0YwSuUgsX/aHJf6Ic9+75UlsMeHKpGlIc6Uz5qKDTY1UbJF1gbqw 40PmM0tcQyeUi685IoLkJnJwPOzpmVHk8z7gI5dIwCCqjkLeOLh29qK+ X-Gm-Gg: Acq92OGo2JFXAsdBBQ5qsW9RPA6lCsIrO+mVEiQCRmImhSa56wRZKk8sT81EvcSy+QT JQGSwITFSH3H7PJbAK0NzFK193S8fySTfdehhy8eU7BE/e/ATHmZwi9njg1RsYwwHzTDtUEOzl5 FGawMBw5+HcMTMYdfeEWxZTxydZxXXJCEwXfcn/favvNSzIlUzSpVlzZ00YgVNYwBJ28w8HiTIS wX5Xx08GjA40BDDS6dJ5QrkoMwqAEp9ElBbcF+x0JbIMZdj4un7D5PqxfzXBfbFf6Sa6CK0WfC4 vAvTm0/6S9QdsgBYdAWaHARyeCJpnplmSftBQZi6a4CItZftRu+PTJYYhoj8yrtzA5KDtDJRt1F LbOy6VDKe+Dk0rWYLTss9rioVkANCxAgf5oLXvqhjWNQFGPz9gnZhX2xjRiWW/xvXKzjBFQLcsx Juj6NaWXa+h34sFFfxlpK8Y+T1w8d6d33A X-Received: by 2002:a05:7300:5714:b0:304:1fb6:88fd with SMTP id 5a478bee46e88-304fa693196mr4791eec.29.1780064749422; Fri, 29 May 2026 07:25:49 -0700 (PDT) Received: from zhanwei.. ([192.227.223.214]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-304ed2efb4esm1534916eec.8.2026.05.29.07.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 07:25:47 -0700 (PDT) From: Zhan Wei To: Christian Brauner , Alexander Viro Cc: Jan Kara , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+e70e1b6cba8714543f7c@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Zhan Wei Subject: [PATCH] eventpoll: restore EP_UNACTIVE_PTR sentinel for ctx->tfile_check_list Date: Fri, 29 May 2026 22:25:33 +0800 Message-ID: <20260529142533.23696-1-zhanwei919@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit e09c77d94003 ("eventpoll: hoist CTL_ADD scratch state into struct ep_ctl_ctx") moved tfile_check_list from a file-scope global into the stack-allocated struct ep_ctl_ctx, and in doing so replaced the EP_UNACTIVE_PTR sentinel with NULL on the grounds that "NULL is the obvious 'empty' value and zero-init handles it for free", describing the change as "No functional change". It is not. epitems_head->next is overloaded with two roles: 1. the "next" pointer that threads a head onto ctx->tfile_check_list; 2. a membership flag: ep_remove_file() uses !smp_load_acquire(&v->next) to mean "this head is not on any pending ctx->tfile_check_list and is therefore safe to free". Before that change the EP_UNACTIVE_PTR sentinel kept the two roles disjoint: a head on the list always had a non-NULL ->next (another head, or the sentinel at the tail), so ->next =3D=3D NULL was equivalent to "never listed". With the sentinel gone the list is NULL-terminated, so the tail head's ->next is NULL as well. ep_remove_file()'s gate can no longer distinguish "never listed" from "listed at the tail", and misfires on the tail head. The reader (reverse_path_check_proc) holds epnested_mutex + rcu_read_lock; the freer (ep_remove_file) holds ep->mtx + file->f_lock. The two sides share no mutex -- the sentinel was the invariant the gate relied on to know it could skip the read side. With it gone, ep_remove_file() frees the tail head while reverse_path_check_proc() is still walking it, producing the slab-use-after-free read. The syzbot reproducer hits this within seconds on a multi-CPU VM. Restore the sentinel: initialize ctx.tfile_check_list to EP_UNACTIVE_PTR in do_epoll_ctl_file(), and terminate the walk on "!=3D EP_UNACTIVE_PTR" in reverse_path_check() and clear_tfile_check_list(). The tail head's ->next becomes the sentinel again rather than NULL, so ep_remove_file()'s gate regains its exclusivity and stops misfiring on the tail. ep_remove_file() itself is unchanged. This restores the invariant the file-scope tfile_check_list relied on before that change while preserving the ctx packaging it introduced. Reported-by: syzbot+e70e1b6cba8714543f7c@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3De70e1b6cba8714543f7c Fixes: e09c77d94003 ("eventpoll: hoist CTL_ADD scratch state into struct ep= _ctl_ctx") Suggested-by: Christian Brauner Link: https://lore.kernel.org/all/20260528-rotwild-summt-kuhhandel-7276ef4c= 33b7@brauner.io/ Signed-off-by: Zhan Wei --- fs/eventpoll.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index a569e98d4a99..abef3bc48cc4 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -429,7 +429,11 @@ struct ep_ctl_ctx { /* * Singly-linked list of epitems_head objects collected during * ep_loop_check_proc(), then walked by reverse_path_check(). - * NULL means empty. + * Terminated by EP_UNACTIVE_PTR, not NULL: epitems_head->next + * doubles as a membership flag (a NULL ->next means "not on this + * list", see ep_remove_file()), so the list uses a non-NULL + * sentinel to keep the tail head distinguishable from an unlisted + * one. */ struct epitems_head *tfile_check_list; =20 @@ -1685,7 +1689,7 @@ static int reverse_path_check(struct ep_ctl_ctx *ctx) { struct epitems_head *p; =20 - for (p =3D ctx->tfile_check_list; p; p =3D p->next) { + for (p =3D ctx->tfile_check_list; p !=3D EP_UNACTIVE_PTR; p =3D p->next) { int error; path_count_init(ctx); rcu_read_lock(); @@ -2438,7 +2442,7 @@ static int ep_loop_check(struct ep_ctl_ctx *ctx, stru= ct eventpoll *ep, static void clear_tfile_check_list(struct ep_ctl_ctx *ctx) { rcu_read_lock(); - while (ctx->tfile_check_list) { + while (ctx->tfile_check_list !=3D EP_UNACTIVE_PTR) { struct epitems_head *head =3D ctx->tfile_check_list; ctx->tfile_check_list =3D head->next; unlist_file(head); @@ -2601,7 +2605,9 @@ int do_epoll_ctl_file(struct file *f, int op, struct = epoll_key *tf, int full_check; struct eventpoll *ep; struct epitem *epi; - struct ep_ctl_ctx ctx =3D { }; + struct ep_ctl_ctx ctx =3D { + .tfile_check_list =3D EP_UNACTIVE_PTR, + }; =20 /* The target file descriptor must support poll */ if (!file_can_poll(tf->file)) --=20 2.43.0