[PATCH] configfs: lock the group inode in configfs_unregister_group()
From: Breno Leitao
Date: Tue Nov 10 2026 - 09:55:15 EST
configfs_unregister_group() detaches a group and marks its directory
S_DEAD while holding only the parent directory's inode lock. rmdir()
and configfs_unregister_subsystem() also lock the directory itself,
and configfs_detach_group() documents that its caller does.
Operations inside the group depend on that lock. symlink(2) holds it
once the VFS has checked that the directory is not dead, so
configfs_symlink() expects configfs_get_config_item() to find the item.
Racing it with configfs_unregister_group() breaks that:
CPU 1 (symlink into G) CPU 2 (unregister G)
inode_lock(G)
may_create_dentry(G)
!IS_DEADDIR(G)
inode_lock(parent)
configfs_detach_group(G)
d_drop(G)
S_DEAD set on G
configfs_symlink(G)
configfs_get_config_item(G)
returns NULL
parent_item->ci_type
[ 9.725911] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000008: 0000 [#1] SMP KASAN NOPTI
[ 9.726694] KASAN: null-ptr-deref in range [0x0000000000000040-0x0000000000000047]
[ 9.728710] RIP: 0010:configfs_symlink+0x132/0x820
[ 9.734253] Call Trace:
[ 9.735132] vfs_symlink+0x25b/0x4b0
[ 9.735379] filename_symlinkat+0x11f/0x300
[ 9.735637] __se_sys_symlink+0x16e/0x410
[ 9.735889] do_syscall_64+0x141/0x440
A NULL check there would not be enough: the IS_DEADDIR() re-check that
configfs_symlink() makes after its unlocked window is stale for the
same reason, so ->allow_link() can run while the group is unlinked and
freed.
pci-ep-cfs registers its controllers/<epc> groups with
configfs_register_group(), gives them ->allow_link(), and frees them
right after configfs_unregister_group() when the controller goes away.
Take the group's inode lock around the detach, as
configfs_unregister_subsystem() does for the subsystem directory.
mkdir(2), unlink(2) and rmdir(2) inside the group rely on the same lock
and VFS checks, but only symlink(2) was tested.
A test module that registers and unregisters a group with
->allow_link() in a loop, raced with symlink(2) into the group, faults
within a second without this change and ran clean for 191k cycles
with it.
Fixes: 5cf6a51e6062 ("configfs: allow dynamic group creation")
Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
---
fs/configfs/dir.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
index 7f4f448a6c66..c4f570686bd2 100644
--- a/fs/configfs/dir.c
+++ b/fs/configfs/dir.c
@@ -1822,6 +1822,7 @@ void configfs_unregister_group(struct config_group *group)
up_write(&frag->frag_sem);
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
+ inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
spin_lock(&configfs_dirent_lock);
configfs_detach_prep(sd, NULL);
spin_unlock(&configfs_dirent_lock);
@@ -1829,6 +1830,7 @@ void configfs_unregister_group(struct config_group *group)
configfs_detach_group(dentry);
d_inode(dentry)->i_flags |= S_DEAD;
dont_mount(dentry);
+ inode_unlock(d_inode(dentry));
d_drop(dentry);
fsnotify_rmdir(d_inode(parent), dentry);
inode_unlock(d_inode(parent));
---
base-commit: b8dd3e40fe051c328e71632e3f88b4df5d03ffbe
change-id: 20261110-b4-configfs_unregister_lock-6fea7fcd8b23
Best regards,
--
Breno Leitao <leitao@xxxxxxxxxx>