From nobody Sun May 24 19:33:42 2026 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 78FE0383981 for ; Sat, 23 May 2026 09:11:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779527478; cv=none; b=Xz7y6+1dfbVLEDKARd1cKTZQIpVWW9aRIE5amBF1xNNcmptIfXD9xKJDbWawiyqi/JTHE3wzb6jMiSfrH3zbaUT5G8E53fSu+KiQqV4fskd63Aig/e7ULlW3vkLS2FtVwNMYEfjrKymSPzNrT2Kcma6s3OzeExhtrIzOFfvBjOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779527478; c=relaxed/simple; bh=+6XWZLSwAFoEUg5RV++uOlpGIqHwmN4U8a5uZuhpo9A=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=vBg0l9SYan9clp2eQXRtZF2yE0PDuPWEGHZSGScaVtiiQxk1dw+kWl78TRXp5rP6nlAwO2HmkVH6x5SO4Q6VpJvp+Hyh3x9M7v+CQxdl46ZV3SeOqNairNqduA+A/cGqVatAOYh9oNpyc4SEwrNI5uUGNBJtsDZpBhCV0XU5xmE= 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=dvOqF53I; arc=none smtp.client-ip=209.85.216.46 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="dvOqF53I" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-367cbac9cb1so7759330a91.3 for ; Sat, 23 May 2026 02:11:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779527475; x=1780132275; 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=klr/LoAsJ5dhO7nmwcabFkbvj22O90F2obfI9mlsW8M=; b=dvOqF53I/RkI/m7kPAsrVEfMDNcvQpVSNFxJMh9LNH9u3yuc09S7y5lRcJCwsp81dr Ew/ekHSf2+Xfur+zRgeDE8FEhRNOxk9roiYnkEUl8wwUVNpqFAvcGW0oJ6WlyTpsMkvS zbsB/9n0icAjSunBes4RJUFPi03Nlf/JO6uJAHpHZvpXGnvBQvMBWqDTy+YrqtIKdj6r 4pBfcNsgBfBexOcS4vMAP+mLSNi4EFNMbzuheB7FLnT10tW6UfsOy24dIZuGhiL8D3BT ZWKD1Oz7PF4HTdmbZKbJokKHooMwyHZnL0zfmH37uKZXq5LO/0uPsUWGlIWmAOrICSbQ esqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779527475; x=1780132275; 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=klr/LoAsJ5dhO7nmwcabFkbvj22O90F2obfI9mlsW8M=; b=W88wMQtctE71N6N8rL8KNIr4/ApjuPlhrN3s+DmyydBfnzEJ2+Bz0EYrx/0gSSwmOP YWM3IbEWASLeNiUYQfF7jiuZB8gtlEJMH8XEkAHTqU2FYT25iWS4SEL2deL8rvd604r8 ZU9wrBKf4vCuN94r5nQdo2V9ZQlOlTbZ0jjvyB70eIjoNZXgeJQmZNWw8plTIAeNFD9Y XpYD7wMiPhBe+1Da16m8/h4GYyvHQ9/YAJF7ovDApQdpFN1DZh181R2fbLS6EreSlzpX OvY1IdpuGZthEWLuiUNOc7T1V+NekSOdL1WXFlku27Qksinf1hLLiyFMeynYYc1w3VVk xGOw== X-Forwarded-Encrypted: i=1; AFNElJ/RHAqqKeJL1iIhOiLBaRmARCqQ3WS/b5uVxsQiOoSw7sSWecoiFzjuFh//YlKBtSEC50Rt/RCQvQToysg=@vger.kernel.org X-Gm-Message-State: AOJu0YyphGUY0i8e06rsqolo0N5LOzHxwmpn8PBGAYfcqR8tJaP6KTRZ +rsXNowYTjRFTMsRbdVJ/X4UeGvb0tqVOJYE7Cf+L7SU69HzR306A5S9 X-Gm-Gg: Acq92OFjd98Yu+y8MFLmt2aGXaospPDrs/j+2WO/JRTmB0trhBTFRlOoSaYb2EbB/IU sEY7k7XIYDfsGvl3bWID0h7w01kKtWiH4oOT2OqwuTSg/d1KkNILUu3pkq27o16dz6B4YvOYY/R vGzls2tKqFp4/RirZA3dVbyh13w59qv1knYeL8zbFEyEkPdvpuQIKZ4KOPO6zIVEAa5YpCHlaUQ ghdCRl65V2QjKnIjzxbhqDTSF3XIaTC3acqxNbPpng8vwGJbf8BwIX2LrrVCYjd9KE7d3QpiIwM yMqVyRAGT1BBeaKde3INApXfl2RExO+H/BgC3Z8GQxOpohwvZVXdLadaSr7hFnEAL9gMZHMk7kN u90aGOLOBGutHC2TZuV89yd/hJ62Qbxx36nzJlAi8b2Nsdee8VUOsNtT8w8avZeYPBlTMfq9oBY 9bUywlNPr8m24FoGxJ5BpMFbFGg0OO2iSxgzj/5LR27gEHzNqlcZj8s80J2JB+tZVWDvsf5Nqkx nGFi/8= X-Received: by 2002:a17:90b:5202:b0:368:c2b1:ec9e with SMTP id 98e67ed59e1d1-36a6781901fmr7213219a91.22.1779527475288; Sat, 23 May 2026 02:11:15 -0700 (PDT) Received: from deepanshu-kernel-hacker.. ([2405:201:682f:383f:5219:64d7:4176:3a42]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36a6772f486sm2374543a91.2.2026.05.23.02.11.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 May 2026 02:11:13 -0700 (PDT) From: Deepanshu Kartikey To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Deepanshu Kartikey , syzbot+69a3d7738ad3aa175caf@syzkaller.appspotmail.com Subject: [PATCH] eventpoll: fix use-after-free in clear_tfile_check_list() Date: Sat, 23 May 2026 14:41:07 +0530 Message-ID: <20260523091107.61880-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 a non-NULL sentinel (EP_TFILE_LIST_END) 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 Signed-off-by: Deepanshu Kartikey --- fs/eventpoll.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index a569e98d4a99..349d79e8ddc8 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -222,6 +222,15 @@ =20 #define EP_UNACTIVE_PTR ((void *) -1L) =20 +/* Non-NULL sentinel terminating ctx->tfile_check_list, so that + * "head->next =3D=3D NULL" unambiguously means "this head is not on any + * check list" -- the invariant ep_remove_file() and list_file() rely + * on. Without this, the tail of the check list aliases the + * "not linked" state and ep_remove_file() may free a head that + * clear_tfile_check_list() still references. + */ +#define EP_TFILE_LIST_END ((struct epitems_head *)EP_UNACTIVE_PTR) + #define EP_ITEM_COST (sizeof(struct epitem) + sizeof(struct eppoll_entry)) =20 /* Wait structure used by the poll hooks */ @@ -472,7 +481,7 @@ 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; + head->next =3D ctx->tfile_check_list ? : EP_TFILE_LIST_END; ctx->tfile_check_list =3D head; } } @@ -1685,7 +1694,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_TFILE_LIST_END; p =3D p->next= ) { int error; path_count_init(ctx); rcu_read_lock(); @@ -2438,11 +2447,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 EP_TFILE_LIST_END) { struct epitems_head *head =3D ctx->tfile_check_list; ctx->tfile_check_list =3D head->next; unlist_file(head); } + ctx->tfile_check_list =3D NULL; rcu_read_unlock(); } =20 @@ -2601,7 +2611,7 @@ 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_TFILE_LIST_END }; =20 /* The target file descriptor must support poll */ if (!file_can_poll(tf->file)) --=20 2.43.0