Skip to contentSkip to conversation

fs: sysfs: do not remove files if group is null

PATCH In review Daniel Steger 2 participants 2 messages Aug 19, 2021 – Aug 21, 2021 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: sysfs: do not remove files if group is null

The current implementation allows the remove_files() API to be called without checking if the grp->name is null. Ensure that the group name is valid prior to removing files.

This patch fixes a race condition where device_del() will cleanup sysfs entries prior to device managed sysfs entries. This results in a NULL group->name and a system error during device cleanup.

To reproduce the issue, simply create a new child device in a platform driver of your choice. Add a sysfs file group using devm API. On driver exist ensure to unregister your child device. Do not call devm_device_remove_group() and leave it up to the implementation to automatically clean up the files. Here is where you will see a kernel error complaining that the files have already been removed.

---
 fs/sysfs/group.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/sysfs/group.c +3 -2
index 64e6a6698935..023b40840f36 100644
@@ -286,9 +286,10 @@ void sysfs_remove_group(struct kobject *kobj,
 		kernfs_get(kn);
 	}
 
-	remove_files(kn, grp);
-	if (grp->name)
+	if (grp->name) {
+		remove_files(kn, grp);
 		kernfs_remove(kn);
+	}
 
 	kernfs_put(kn);
 }
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 11 lines · Daniel Steger

The current implementation allows the remove_files() API to be called without checking if the grp->name is null. Ensure that the group name is valid prior to removing files.

This patch fixes a race condition where device_del() will cleanup sysfs entries prior to device managed sysfs entries. This results in a NULL group->name and a system error during device cleanup.

To reproduce the issue, simply create a new child device in a platform driver of your choice. Add a sysfs file group using devm API.

What driver is doing this today?

Quoted 1 line

On driver exist ensure to unregister your child device.

What child device? Why is a platform driver creating a child device at all?

Quoted 15 lines

Do not call devm_device_remove_group() and leave it up to the implementation to automatically clean up the files. Here is where you will see a kernel error complaining that the files have already been removed.

---
 fs/sysfs/group.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/sysfs/group.c +0 -0
index 64e6a6698935..023b40840f36 100644

fs/sysfs/group.c void sysfs_remove_group(struct kobject *kobj, · line 290

Quoted 7 lines
fs/sysfs/group.c +2 -2
@@ -286,9 +286,10 @@ void sysfs_remove_group(struct kobject *kobj,
 		kernfs_get(kn);
 	}
 
-	remove_files(kn, grp);
-	if (grp->name)
+	if (grp->name) {
+		remove_files(kn, grp);

What about groups without names? Will then now not be removed properly? Why does the name matter here?

thanks,

greg k-h