From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796001614; b=w8egzP9+vl7W/H0usFHEjTxoH2vrDe9EaySy3zSQsHJU1m2lY4wIy/FcsCK+1cA+ZmXa jQYRJmHB2EvUICGMC3OrrJBRm/FMxMyl60sDBTA66rEu9scaY/oyjrKLMzelfua4mg3ev r1XtveE9Kcgf8qQYCbPBE31odZOpZHjchxrdxcBYJMrR7kbmsH/bcFiuQKyFjtSgSJq33 V5uyL5EBL8i2gz1pCfSNoQJhOE/xN+HruLolWHACKNI57RzdGCRQapDOlbFtAypkaV3Xh OVqEmsZ+1z+3rzF3W6QNCNxCKgzowh/rOXCIW54MBunaI/p8YeI0b2TqRvdaD+PAtbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796001614; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=13KTkoIMHaLvC94OZIk41TNExl3JFY7suboZWUCVrSg=; b=cZW0fde2uYZDDJMjZKiJTpXok2Q/8/W7+CubWnDsU6lCr3EDSsbDK6MuGQAkPuvYnB6n QQvz5tUODgMRrUY24CpElGmNH57Skq4OyjBdtCfZRqjYW/z8Hw1RbVb1wuiGWLFYWbKIj wrpkKPAJgDNR+NvJpIkuDD5Y9mOWGNfxKg2o9SSoKddzrd33X/uBNC7dqV4pEuzandDWi AWqOCytSr5AdUu2iNAMsNkxm0Y1knWl2PjEjLNi89Oiwr5xCwq+1JDjHeuyyZGCseVDhx +Uopqq97h+Yg9oUCiOQ908ceKhEB/U5B0NMOf3hKrWMtHZBmOdw6ehpNHrwAJ2JTLhw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=CY6W7WOw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14A9A606A31C; Mon, 30 Nov 2026 01:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=CY6W7WOw Received: by smtp.kernel.org (Postfix) with ESMTPSA id 051D91F00898; Mon, 30 Nov 2026 01:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796001613; bh=13KTkoIMHaLvC94OZIk41TNExl3JFY7suboZWUCVrSg=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=CY6W7WOwEP97BQAZ2TcT3HaSULbWRLz4/KF7P2i7bvWtfVlIT2gEgjdPdORIK5utC n88yeGyCwPRL3W8Xc15vWCBXQ64q4iEqYdFOpllyYWH3NlvkSOE7zAii3foQEYLPRa HjNDv8PeK9kYjJ2pZNoouY/YrZgWHhMDjrXbTMiU= Date: Sun, 29 Nov 2026 17:20:12 -0800 From: Andrew Morton To: Fan Wu Cc: gregkh@linuxfoundation.org, tj@kernel.org, chenridong@huawei.com, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Shakeel Butt , Christian Brauner , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v3] kernfs: recheck of->released after acquiring the active reference Message-Id: <20261129172012.97bd9e0688ff4cc256c54c90@linux-foundation.org> In-Reply-To: <20260828040348.1809278-1-fanwu01@zju.edu.cn> References: <20260828040348.1809278-1-fanwu01@zju.edu.cn> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 28 Aug 2026 04:03:48 +0000 Fan Wu wrote: > kernfs_get_active_of(), added by commit 3c9ba2777d6c ("kernfs: Fix UAF > in polling when open file is released"), tests @of->released before > acquiring the active reference on @of->kn. A hide/drain/show cycle can > run between the two steps: the drain path releases the open file, and > the reactivation lets kernfs_get_active() succeed again, so a file > operation can run on an already released open file. On the cgroup > pressure files, the poll callback dereferences of->priv while forming > &ctx->psi.trigger and can hit either stale, freed memory or NULL. > > Re-check @of->released after the successful acquisition and drop the > active reference again if it has been set. No lock is needed: > @of->released is only ever set to true, the drain which sets it > precedes the reactivation under kernfs_rwsem, and the fully-ordered > RMW on @kn->active in kernfs_get_active() then orders the read after > that reactivation. > > To allow the lockless reads to use READ_ONCE(), drop the :1 storage > from @released and store it with WRITE_ONCE() to match. > > This issue was found by an in-house static analysis tool. Thanks. I hit this at runtime, stress-testing kernfs on 64-vCPU QEMU. I repackaged your patch as below and shall send it on to some folks. From: Fan Wu Subject: kernfs: recheck of->released after acquiring the active reference Date: Fri, 28 Aug 2026 04:03:48 +0000 kernfs_get_active_of(), added by commit 3c9ba2777d6c ("kernfs: Fix UAF in polling when open file is released"), tests @of->released before acquiring the active reference on @of->kn. A hide/drain/show cycle can run between the two steps: the drain path releases the open file, and the reactivation lets kernfs_get_active() succeed again, so a file operation can run on an already released open file. On the cgroup pressure files, the poll callback dereferences of->priv while forming &ctx->psi.trigger and can hit either stale, freed memory or NULL. Re-check @of->released after the successful acquisition and drop the active reference again if it has been set. No lock is needed: @of->released is only ever set to true, the drain which sets it precedes the reactivation under kernfs_rwsem, and the fully-ordered RMW on @kn->active in kernfs_get_active() then orders the read after that reactivation. To allow the lockless reads to use READ_ONCE(), drop the :1 storage from @released and store it with WRITE_ONCE() to match. This issue was found by an in-house static analysis tool. akpm: Reproduced and tested on 64 vCPU qemu. Eight processes polled cgroup.pressure while another repeatedly wrote 0 and 1 to cgroup.pressure. Unfixed kernel hit an oops and hung during a 90-second run. With the fix it completed 122,291 0/1 cycles and 6,275,416 polls in 180 seconds. [ 23.550063] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 23.552369] #PF: supervisor read access in kernel mode [ 23.554094] #PF: error_code(0x0000) - not-present page [ 23.555794] PGD 207206067 P4D 207206067 PUD 20720e067 PMD 0 [ 23.557712] Oops: Oops: 0000 [#1] SMP NOPTI [ 23.559118] CPU: 3 UID: 0 PID: 349 Comm: ktpoll Not tainted 7.4.0-rc4-g53c2ad822f79 #1 PREEMPT(full) [ 23.562126] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 23.565135] RIP: 0010:psi_trigger_poll+0x16/0x90 [ 23.566689] Code: f0 fd ff ff 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 40 d6 0f 1f 44 00 00 eb 0a b8 4f 01 00 00 e9 5b da 44 01 53 <48> 8b 1f 48 85 db 74 36 48 8b 7b 78 48 85 ff 74 38 48 89 d6 e8 a1 [ 23.572790] RSP: 0018:ffffc900020f3aa0 EFLAGS: 00010202 [ 23.574530] RAX: ffffffff814b7190 RBX: ffff8882067da000 RCX: 000000003732965b [ 23.576787] RDX: ffffc900020f3c20 RSI: ffff88820731f240 RDI: 0000000000000008 [ 23.579038] RBP: ffff8882062b9878 R08: 0000000000000582 R09: 0000000000000000 [ 23.581299] R10: 0000000000000000 R11: 0000000000008000 R12: ffffc900020f3c20 [ 23.583588] R13: ffffc900020f3b2c R14: ffff88820731f240 R15: 0000000000000000 [ 23.585857] FS: 00007f6b6d9456c0(0000) GS:ffff8883f5973000(0000) knlGS:0000000000000000 [ 23.588446] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 23.590218] CR2: 0000000000000008 CR3: 000000017d813000 CR4: 0000000000350ef0 [ 23.592172] Call Trace: [ 23.592873] [ 23.593490] kernfs_fop_poll+0x65/0xe0 [ 23.594552] do_sys_poll+0x247/0x540 [ 23.595583] __x64_sys_poll+0xc2/0x150 [ 23.596651] do_syscall_64+0x10b/0x650 [ 23.597707] entry_SYSCALL_64_after_hwframe+0x77/0x7f [ 23.599085] RIP: 0033:0x7f6b6df6d72e [ 23.600107] Code: 08 0f 85 d5 4b ff ff 49 89 fb 48 89 f0 48 89 d7 48 89 ce 4c 89 c2 4d 89 ca 4c 8b 44 24 08 4c 8b 4c 24 10 4c 89 5c 24 08 0f 05 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 80 00 00 00 00 48 83 ec 08 [ 23.605142] RSP: 002b:00007f6b6d944b58 EFLAGS: 00000246 ORIG_RAX: 0000000000000007 [ 23.607203] RAX: ffffffffffffffda RBX: 00007f6b6d9456c0 RCX: 00007f6b6df6d72e [ 23.609225] RDX: 0000000000000000 RSI: 0000000000000001 RDI: 00007f6b6ddf07a0 [ 23.611030] RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000 [ 23.612815] R10: 0000000000000000 R11: 0000000000000246 R12: 00007f6b6dba87c0 [ 23.614602] R13: 000000001295a100 R14: 00007f6b6dc237e0 R15: 00007f6b6d945640 [ 23.616335] [ 23.616905] Modules linked in: [ 23.617680] CR2: 0000000000000008 [ 23.618485] ---[ end trace 0000000000000000 ]--- [ 23.619642] RIP: 0010:psi_trigger_poll+0x16/0x90 [ 23.619653] Code: f0 fd ff ff 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 40 d6 0f 1f 44 00 00 eb 0a b8 4f 01 00 00 e9 5b da 44 01 53 <48> 8b 1f 48 85 db 74 36 48 8b 7b 78 48 85 ff 74 38 48 89 d6 e8 a1 Link: https://patch.msgid.link/20260828040348.1809278-1-fanwu01@zju.edu.cn Fixes: 3c9ba2777d6c ("kernfs: Fix UAF in polling when open file is released") Signed-off-by: Fan Wu Signed-off-by: Andrew Morton Suggested-by: Tejun Heo Acked-by: Tejun Heo Tested-by: Andrew Morton Cc: Shakeel Butt Cc: Christian Brauner Cc: Greg Kroah-Hartman Cc: Ridong Chen Cc: fs/kernfs/file.c | 10 ++++++++-- include/linux/kernfs.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) --- a/fs/kernfs/file.c~kernfs-recheck-of-released-after-acquiring-the-active-reference +++ a/fs/kernfs/file.c @@ -67,12 +67,18 @@ static struct kernfs_open_node *of_on(st static struct kernfs_open_file *kernfs_get_active_of(struct kernfs_open_file *of) { /* Skip if file was already released */ - if (unlikely(of->released)) + if (unlikely(READ_ONCE(of->released))) return NULL; if (!kernfs_get_active(of->kn)) return NULL; + /* @of may have been drained in between: recheck. */ + if (unlikely(READ_ONCE(of->released))) { + kernfs_put_active(of->kn); + return NULL; + } + return of; } @@ -769,7 +775,7 @@ static void kernfs_release_file(struct k * and being drained. Don't use kernfs_ops(). */ kn->attr.ops->release(of); - of->released = true; + WRITE_ONCE(of->released, true); of_on(of)->nr_to_release--; } } --- a/include/linux/kernfs.h~kernfs-recheck-of-released-after-acquiring-the-active-reference +++ a/include/linux/kernfs.h @@ -273,7 +273,7 @@ struct kernfs_open_file { size_t atomic_write_len; bool mmapped:1; - bool released:1; + bool released; const struct vm_operations_struct *vm_ops; }; _