mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] kernfs: fix two races in __kernfs_new_node()
@ 2026-12-02 16:45 Josef Bacik
  2026-12-02 16:45 ` [PATCH 1/2] kernfs: deactivate a new node before publishing its id Josef Bacik
  2026-12-02 16:45 ` [PATCH 2/2] kernfs: RCU free a new node that was already in the idr Josef Bacik
  0 siblings, 2 replies; 3+ messages in thread
From: Josef Bacik @ 2026-12-02 16:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Tejun Heo
  Cc: Christian Brauner, driver-core, linux-fsdevel, linux-kernel

Hello,

kernfs_find_and_get_node_by_id() can find a node in the idr while
__kernfs_new_node() is still setting it up, and after its error path has
freed it.  Patch 1 sets the deactivated bias before the node goes into
the idr, and patch 2 frees the node with kernfs_free_rcu() on that error
path.

Each bug reproduced in QEMU with KASAN, lockdep and kmemleak only with
added delays, plus a forced failure for patch 2, and the same runs are
clean with both patches.  kernfs_test and the cgroup test_core,
test_kill, test_pids and test_freezer selftests pass.

This is against vfs.fixes, since the recent kernfs fixes went through
Christian's tree, and it applies to mainline as well.  Thanks,

Josef

---
Josef Bacik (2):
      kernfs: deactivate a new node before publishing its id
      kernfs: RCU free a new node that was already in the idr

 fs/kernfs/dir.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
---
base-commit: b8e026b973764a52b92c0cdc1d3cf96f2abc1058
change-id: 20261202-b4-kernfs-new-node-cfcf607114b4

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

* [PATCH 1/2] kernfs: deactivate a new node before publishing its id
  2026-12-02 16:45 [PATCH 0/2] kernfs: fix two races in __kernfs_new_node() Josef Bacik
@ 2026-12-02 16:45 ` Josef Bacik
  2026-12-02 16:45 ` [PATCH 2/2] kernfs: RCU free a new node that was already in the idr Josef Bacik
  1 sibling, 0 replies; 3+ messages in thread
From: Josef Bacik @ 2026-12-02 16:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Tejun Heo
  Cc: Christian Brauner, driver-core, linux-fsdevel, linux-kernel

kernfs_find_and_get_node_by_id() can return a node that is still being
initialized, although it must fail for one that was never activated.

Commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags") made it test
kn->active instead of KERNFS_ACTIVATED. The flag is clear in a zeroed
node, but an active count of zero reads as active, and
__kernfs_new_node() only sets KN_DEACTIVATED_BIAS after the node is in
the idr and has its reference count.

Fix by setting kn->active before the node is added to the idr.

Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
Signed-off-by: Josef Bacik <josef@bacik.io>
---
 fs/kernfs/dir.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 800af10d9511e..9569588aac751 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -668,6 +668,9 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	if (!kn)
 		goto err_out1;
 
+	/* must be set before the idr publishes @kn to lookups by id */
+	atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
+
 	idr_preload(GFP_KERNEL);
 	spin_lock(&root->kernfs_idr_lock);
 	ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
@@ -683,7 +686,6 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	kn->id = (u64)id_highbits << 32 | ret;
 
 	atomic_set(&kn->count, 1);
-	atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
 	RB_CLEAR_NODE(&kn->rb);
 
 	rcu_assign_pointer(kn->name, name);


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

* [PATCH 2/2] kernfs: RCU free a new node that was already in the idr
  2026-12-02 16:45 [PATCH 0/2] kernfs: fix two races in __kernfs_new_node() Josef Bacik
  2026-12-02 16:45 ` [PATCH 1/2] kernfs: deactivate a new node before publishing its id Josef Bacik
@ 2026-12-02 16:45 ` Josef Bacik
  1 sibling, 0 replies; 3+ messages in thread
From: Josef Bacik @ 2026-12-02 16:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Tejun Heo
  Cc: Christian Brauner, driver-core, linux-fsdevel, linux-kernel

kernfs_find_and_get_node_by_id() can read a node after the error path of
__kernfs_new_node() has freed it.

Commit 4207b556e62f ("kernfs: RCU protect kernfs_nodes and avoid
kernfs_idr_lock in kernfs_find_and_get_node_by_id()") moved the lookup
from kernfs_idr_lock to RCU and made kernfs_put() wait for a grace
period. The error path still frees the node right after idr_remove().

Fix by freeing the node with kernfs_free_rcu() there as well.

Fixes: 4207b556e62f ("kernfs: RCU protect kernfs_nodes and avoid kernfs_idr_lock in kernfs_find_and_get_node_by_id()")
Signed-off-by: Josef Bacik <josef@bacik.io>
---
 fs/kernfs/dir.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 9569588aac751..257c6082f1a82 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -718,14 +718,16 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
  err_out4:
 	RCU_INIT_POINTER(kn->__parent, NULL);
 	kernfs_put(parent);
-	if (kn->iattr) {
+	if (kn->iattr)
 		simple_xattrs_free(&root->xa_cache, &kn->iattr->xattrs, NULL);
-		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
-	}
  err_out3:
 	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
 	spin_unlock(&root->kernfs_idr_lock);
+	/* a lookup by id may still see @kn, this also frees @name and iattr */
+	call_rcu(&kn->rcu, kernfs_free_rcu);
+	return NULL;
+
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:

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

end of thread, other threads:[~2026-12-02 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 16:45 [PATCH 0/2] kernfs: fix two races in __kernfs_new_node() Josef Bacik
2026-12-02 16:45 ` [PATCH 1/2] kernfs: deactivate a new node before publishing its id Josef Bacik
2026-12-02 16:45 ` [PATCH 2/2] kernfs: RCU free a new node that was already in the idr Josef Bacik

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®