From nobody Mon Jun 8 13:29:44 2026 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 DA5AB371867 for ; Fri, 29 May 2026 04:25:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780028722; cv=none; b=M8NQF1/N9kKb2UoGvgy/U0JaSSdK1OK2nR5S/3NcaRea3ojKCmmpzIu7q705Grie3U+GrYLwW89gCF0wSbghRbtZDntQePhYV6zfT5Q9RvjiO2G+5gsV41eb/kUWHKwXkXNlrznPrbgRaI0FXs8CMr6uYApw6BVDNAP9E/Q3vFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780028722; c=relaxed/simple; bh=SKDZ+JkQB9hiitwCWJPWrobyYgdb2uq3qrf0kYLNKLI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=KHIx6hsykWBY/JtH3O4oieaGerXA0APwl7oJcIC5cdbugEExD56K0q6dTGeb5s1dC7Pw26pqbrR3L/bwsdFD4Dgqd8Whze1hG9/X5VjXLEyA5y4hcjrxMKJi9CgnS3PNAx66Mtm7bVGM7ukgs1im1WPmKlwKejZjg7n22CYApl4= 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=FViA4jfs; arc=none smtp.client-ip=209.85.215.180 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="FViA4jfs" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-c736261ee8dso6125000a12.1 for ; Thu, 28 May 2026 21:25:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780028720; x=1780633520; 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=fyWtLsIqYZuNoksRZwNaNaPrzBtwQ/7aQZjTna3IWMk=; b=FViA4jfsLwmSUTLEigu+cDNWjbxMmsFPk0sv1aZkX0Ae9fH6kYGNEanz4UgFb7vmzt Ps8R4JyGf429p6MxIiD4EE2AEn2LQkPC9Aix1A6XKhO31M0LfEDX28U10GmfhItgo1SD N4zZK5ZGh34n82dNcN/rJ9xbYw9aFgwgIV2/y3bMi7stkk73PdRmP+q/xXeJ2loGpiv5 H7EhFCiinSYOKX6rJo8+9JmRsWM2vWi53R0Iyt9C2tCiHAKgGwViNAJifguLfb5sk240 cLAz4IMdpChmRQHFl2QVmGUgqoYu9q9gHGO88Csy08Vb4m0zV3L84gPlZRGzIiNJAH5A 8s5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780028720; x=1780633520; 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=fyWtLsIqYZuNoksRZwNaNaPrzBtwQ/7aQZjTna3IWMk=; b=A5Tfipgr3YTi1Lo6rf7A3ydtfFEjGe3kG5/fqwJM70WvxDysnphMy5xmvD5UOSBoJB YNpE/XWESaR/4PdLlNhxLtghVvvn4ITb1rVs/onlMB++/1i9GsKTyNUdRxb5v8C1rYbg JypixYwc2SSF4ejZREQHlolXm+q2mMNa83HHDgDYDMYongGRxaBm6OOLEj8WIqD9aOpT mNJkSeSXrXoEW2TS32Gmez6nr+JTuhLy6ykekbq86YpCle6hp6aWqiLvAFcy3xw5q+EP v6r/0VSjpl24ddY6Pri3E4xTzRWY1fuZPwHXkJu9fWla0XdYBFYTgM/9D8p9/tXA2UtO P+WA== X-Forwarded-Encrypted: i=1; AFNElJ/ZHXIb01+myXlOjX5c5dsze+dte3mvSZFplDkNZ1PQO6ILotT8oK4MfZrzH1MTtF01aylUx18hPPtIRKA=@vger.kernel.org X-Gm-Message-State: AOJu0YwC46R+qsB4HwTunJXxiQSvDLw2aRzzFgxYkulp4T65WbWD77pW 2tidbZ28XI2/ccHVcUqSdEZ+xES/zCgj8kPUSWLKOj3IAo1KGLi2+hf0 X-Gm-Gg: Acq92OEaVndyU6qr+3jWhQxt/z3GZR9McXP1AhvfSnlr9+QhQu9wsO8XIN+5sLg/aRN 7g9aNSW7NVs2J1W9Eeen6NMNqzTwwp0Fy0VLF2/FXAfpdhpAdPZdGM3mAv703q8SHyQngG7XR7d vY71JvwhjvvngFRKChHDClBiRZoa8M8UKwxN4LyzaIRXoI2gf89b+irYPH+kydJ5hUeW2CO04lD KbZcm9jJdFzd9rSLEIL3CNNsLWpsxPrCrW3jWrWE9Ukl5lWArw3nbk0k/H31N7y4U3dItrPL9Ue OK8dGNqP0DRT/e6GyIVkOwzg0xbJcAs82zqYo6C2yD5VnbYYKXi2GtQrmqXjg1JHgeGrLfYvtbY rNgXq+4rgFy4el0n3r+lWqwuJID4/7r99gaN6SO5YZi/EZcMMCStCKaJhJ3jCf9gDoTxs/nrIIW zVNQfD204PqlhlCIr7KS/kXZsZGKisAAn1LAPlCeqXuIzU104n+PeXGw+0SSD9YZC6JcmKYfv+k +RlDu08yuz1BU0BSVANtHo6OxcZ X-Received: by 2002:a05:6a00:4b06:b0:841:6414:44ce with SMTP id d2e1a72fcca58-84212b2683emr1342680b3a.22.1780028719994; Thu, 28 May 2026 21:25:19 -0700 (PDT) Received: from deepanshu-kernel-hacker.. ([2405:201:682f:383f:357e:4de6:c770:8532]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84214cc0d1csm355132b3a.42.2026.05.28.21.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2026 21:25:19 -0700 (PDT) From: Deepanshu Kartikey To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Deepanshu Kartikey , syzbot+69a3d7738ad3aa175caf@syzkaller.appspotmail.com Subject: [PATCH v2] eventpoll: fix use-after-free in clear_tfile_check_list() Date: Fri, 29 May 2026 09:55:07 +0530 Message-ID: <20260529042507.87593-1-kartikey406@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" syzbot reports a slab-use-after-free read in clear_tfile_check_list() during EPOLL_CTL_ADD when ep_insert() takes an error path that calls ep_remove() before do_epoll_ctl_file() drains ctx->tfile_check_list. ep_remove_file() decides whether to kmem_cache_free() a struct epitems_head by testing v->next =3D=3D NULL, on the convention that NULL means "this head is not linked on any check list". list_file() pushes a head onto ctx->tfile_check_list by storing the previous list head into head->next; when the list is empty that store is NULL, so the head that ends up at the tail of the check list also has next =3D=3D NULL. ep_remove_file() then misreads the tail as "not linked" and frees it. clear_tfile_check_list() later walks the list and dereferences head->next on the freed object: BUG: KASAN: slab-use-after-free in clear_tfile_check_list+0x114/0x380 fs/eventpoll.c:2443 Allocated by task 5985: ep_attach_file fs/eventpoll.c:1751 [inline] ep_register_epitem fs/eventpoll.c:1833 [inline] ep_insert+0x512/0x1820 fs/eventpoll.c:1876 Freed by task 5985: ep_remove+0x155/0x2a0 fs/eventpoll.c:1135 ep_insert+0x1372/0x1820 Terminate ctx->tfile_check_list with EP_UNACTIVE_PTR (the existing non-NULL sentinel used elsewhere in this file for inactive list slots) so that next =3D=3D NULL unambiguously means "not on any check list". list_file() stores the sentinel when the list is empty, reverse_path_check() and clear_tfile_check_list() stop the walk on the sentinel, and do_epoll_ctl_file() seeds ctx->tfile_check_list with it. The guard in ep_remove_file() then correctly refuses to free a head while it remains on the check list. Reported-by: syzbot+69a3d7738ad3aa175caf@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3D69a3d7738ad3aa175caf Link: https://lore.kernel.org/all/20260523091107.61880-1-kartikey406@gmail.= com/T/ [v1] Signed-off-by: Deepanshu Kartikey --- v2: Reuse the existing EP_UNACTIVE_PTR sentinel instead of adding a new EP_TFILE_LIST_END define, per review feedback. --- fs/eventpoll.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index a569e98d4a99..2fc3b14d7ab2 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -472,7 +472,13 @@ static void list_file(struct file *file, struct ep_ctl= _ctx *ctx) =20 head =3D container_of(file->f_ep, struct epitems_head, epitems); if (!head->next) { - head->next =3D ctx->tfile_check_list; + /* + * Terminate the check list with EP_UNACTIVE_PTR (non-NULL) + * so that head->next =3D=3D NULL unambiguously means "not on + * any check list". ep_remove_file() relies on that + * invariant to decide whether the head is safe to free. + */ + head->next =3D ctx->tfile_check_list ? : (struct epitems_head *)EP_UNACT= IVE_PTR; ctx->tfile_check_list =3D head; } } @@ -1685,7 +1691,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 (struct epitems_head *)EP_UNACTI= VE_PTR; p =3D p->next) { int error; path_count_init(ctx); rcu_read_lock(); @@ -2438,11 +2444,12 @@ static int ep_loop_check(struct ep_ctl_ctx *ctx, st= ruct 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 (struct epitems_head *)EP_UNACTIVE_PTR)= { struct epitems_head *head =3D ctx->tfile_check_list; ctx->tfile_check_list =3D head->next; unlist_file(head); } + ctx->tfile_check_list =3D (struct epitems_head *)EP_UNACTIVE_PTR; rcu_read_unlock(); } =20 @@ -2601,7 +2608,8 @@ 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 + (struct epitems_head *)EP_UNACTIVE_PTR }; =20 /* The target file descriptor must support poll */ if (!file_can_poll(tf->file)) --=20 2.43.0