mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] kernfs: recheck of->released after acquiring the active reference
@ 2026-08-28  4:03 Fan Wu
  2026-11-30  1:20 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Fan Wu @ 2026-08-28  4:03 UTC (permalink / raw)
  To: gregkh, tj; +Cc: chenridong, driver-core, linux-kernel, Fan Wu, stable

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.

Fixes: 3c9ba2777d6c ("kernfs: Fix UAF in polling when open file is released")
Cc: stable@vger.kernel.org
Suggested-by: Tejun Heo <tj@kernel.org>
Acked-by: Tejun Heo <tj@kernel.org>
Assisted-by: Codex:gpt-5.6
Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
---
Changes in v3:
- Drop the :1 bit-field storage from @released so that READ_ONCE() can
  be applied to it, use READ_ONCE() for both lockless reads of the
  field, and add the matching WRITE_ONCE() on the store side.
  READ_ONCE() on a bit-field does not build, as reported by the
  kernel test robot on v2. The logic is unchanged from v2, where the
  Acked-by was obtained; happy to drop it if you prefer a fresh one.

v2: <20260821050720.14848-1-fanwu01@zju.edu.cn>
lkp report on v2: <202608230046.Ixvo8Av6-lkp@intel.com>
---
 fs/kernfs/file.c       | 10 ++++++++--
 include/linux/kernfs.h |  2 +-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 9adf36e6364b..98adceddbcfa 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -74,12 +74,18 @@ static struct kernfs_open_node *of_on(struct kernfs_open_file *of)
 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;
 }
 
@@ -762,7 +768,7 @@ static void kernfs_release_file(struct kernfs_node *kn,
 		 * 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--;
 	}
 }
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index b5a5f32fdfd1..e95e333e24ea 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -269,7 +269,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;
 };
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] kernfs: recheck of->released after acquiring the active reference
  2026-08-28  4:03 [PATCH v3] kernfs: recheck of->released after acquiring the active reference Fan Wu
@ 2026-11-30  1:20 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-11-30  1:20 UTC (permalink / raw)
  To: Fan Wu
  Cc: gregkh, tj, chenridong, driver-core, linux-kernel, stable,
	Shakeel Butt, Christian Brauner, linux-fsdevel

On Fri, 28 Aug 2026 04:03:48 +0000 Fan Wu <fanwu01@zju.edu.cn> 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 <fanwu01@zju.edu.cn>
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]  <TASK>
[   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 <c3> 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]  </TASK>
[   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 <fanwu01@zju.edu.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Suggested-by: Tejun Heo <tj@kernel.org>
Acked-by: Tejun Heo <tj@kernel.org>
Tested-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Ridong Chen <ridong.chen@linux.dev>
Cc: <stable@vger.kernel.org>

 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;
 };
 
_


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  1:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-28  4:03 [PATCH v3] kernfs: recheck of->released after acquiring the active reference Fan Wu
2026-11-30  1:20 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®