sysfs:Addresses null pointer dereference in sysfs_merge_group and sysfs_unmerge_group.
PATCH 1/1 sysfs:Addresses null pointer dereference in sysfs_merge_group and sysfs_unmerge_group.
From: Rohan Kollambalath <[email protected]>
These functions take a struct attribute_group as an input which has an optional .name field. These functions rely on the .name field being populated and do not check if its null. They pass this name into other functions, eventually leading to a null pointer dereference.
This change adds a simple check that returns an error if the .name field is null and clarifies this requirement in the comments.
--- fs/sysfs/group.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
fs/sysfs/group.c +10 -4
@@ -318,12 +318,12 @@ void sysfs_remove_groups(struct kobject *kobj,EXPORT_SYMBOL_GPL (sysfs_remove_groups);/** -* sysfs_merge_group - merge files into a pre-existing attribute group. +* sysfs_merge_group - merge files into a pre-existing named attribute group. * @kobj: The kobject containing the group. * @grp: The files to create and the attribute group they belong to. * -* This function returns an error if the group doesn't exist or any of the -* files already exist in that group, in which case none of the new files +* This function returns an error if the group doesn't exist, the .name field is NULL or +* any of the files already exist in that group, in which case none of the new files * are created. */ int sysfs_merge_group(struct kobject *kobj,
@@ -336,6 +336,9 @@ int sysfs_merge_group(struct kobject *kobj,struct attribute *const *attr;int i; +if (!grp->name) +return -ENOENT ; + parent =kernfs_find_and_get (kobj->sd, grp->name);if (!parent)return -ENOENT ;
@@ -356,7 +359,7 @@ int sysfs_merge_group(struct kobject *kobj,EXPORT_SYMBOL_GPL (sysfs_merge_group);/** -* sysfs_unmerge_group - remove files from a pre-existing attribute group. +* sysfs_unmerge_group - remove files from a pre-existing named attribute group. * @kobj: The kobject containing the group. * @grp: The files to remove and the attribute group they belong to. */
@@ -366,6 +369,9 @@ void sysfs_unmerge_group(struct kobject *kobj,struct kernfs_node *parent;struct attribute *const *attr; +if (!grp->name) +return -ENOENT ; + parent =kernfs_find_and_get (kobj->sd, grp->name);if (parent) {for (attr = grp->attrs; *attr; ++attr)
Signature · 1 line
2.25.1
Greg KH
maintainerQuoted 6 lines · Rohan Kollambalath
From: Rohan Kollambalath <[email protected]>
These functions take a struct
attribute_groupas an input which has an optional .name field. These functions rely on the .name field being populated and do not check if its null. They pass this name into other functions, eventually leading to a null pointer dereference.
What in-kernel drivers cause this to trigger? Why not fix them up instead?
Quoted 15 lines
This change adds a simple check that returns an error if the .name field is null and clarifies this requirement in the comments.
Signed-off-by Rohan Kollambalath <[email protected]>--- fs/sysfs/group.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)fs/sysfs/group.c +0
-0@@ -318,12 +318,12 @@ void sysfs_remove_groups(struct kobject *kobj,EXPORT_SYMBOL_GPL (sysfs_remove_groups);
fs/sysfs/group.c void sysfs_remove_groups(struct kobject *kobj, · line 326
Quoted 10 lines
fs/sysfs/group.c +3
-3/** -* sysfs_merge_group - merge files into a pre-existing attribute group. +* sysfs_merge_group - merge files into a pre-existing named attribute group. * @kobj: The kobject containing the group. * @grp: The files to create and the attribute group they belong to. * -* This function returns an error if the group doesn't exist or any of the -* files already exist in that group, in which case none of the new files +* This function returns an error if the group doesn't exist, the .name field is NULL or +* any of the files already exist in that group, in which case none of the new files
Please properly wrap comments at the correct column.
fs/sysfs/group.c int sysfs_merge_group(struct kobject *kobj, · line 340
Quoted 9 lines
fs/sysfs/group.c +2
-0* are created. */ int sysfs_merge_group(struct kobject *kobj,@@ -336,6 +336,9 @@ int sysfs_merge_group(struct kobject *kobj,struct attribute *const *attr;int i; +if (!grp->name) +return -ENOENT ;
Why that error value?
Quoted 14 lines
fs/sysfs/group.c +2
-1+ parent =kernfs_find_and_get (kobj->sd, grp->name);if (!parent)return -ENOENT ;@@ -356,7 +359,7 @@ int sysfs_merge_group(struct kobject *kobj,EXPORT_SYMBOL_GPL (sysfs_merge_group);/** -* sysfs_unmerge_group - remove files from a pre-existing attribute group. +* sysfs_unmerge_group - remove files from a pre-existing named attribute group. * @kobj: The kobject containing the group. * @grp: The files to remove and the attribute group they belong to. */
fs/sysfs/group.c void sysfs_unmerge_group(struct kobject *kobj, · line 373
Quoted 5 lines
fs/sysfs/group.c +2
-0@@ -366,6 +369,9 @@ void sysfs_unmerge_group(struct kobject *kobj,struct kernfs_node *parent;struct attribute *const *attr; +if (!grp->name) +return -ENOENT ;
Again, why that value?
And again, why not fix up the callers?
thanks,
greg k-h
A message that is not in the archive (sent off-list or not yet received): CA+a0dEiZzZgsjhVRDMTyYgFWk9TwjAHeTjFphZQwvFctE9Zosg@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): 2024021007-casually-supernova-329f@gregkh
A message that is not in the archive (sent off-list or not yet received): CA+a0dEjp2ExRTDZjN9z_Z12pNqHiiGkqsuTjh40TKN=W81+FWw@mail.gmail.com
Greg KH
maintainerQuoted 1 line · Rohan
Hey,
Please don't top-post, or send html email, as they get dropped from the mailing lists.
Quoted 5 lines
I found this bug when writing my own kernel driver. I believed the function wasn't explicit in stating its the requirement for the name field and made it easy for developers to mistake it with
sysfs_create_groupand cause a crash. I wanted to change the code for robustness purposes. Other functions in the same file already do this check such assysfs_group_change_owner.
Adding documentation is great, but don't add checks that are not needed, we require callers to set up things properly, otherwise the kernel would be nothing but a constant set of checks for things that can never happen.
Quoted 3 lines
As for why I chose
-ENOENT, I did this because it was the same error code returned if the group cannot be found bykernfs_find_and_getbut I understand if this should be changed.
Yes, that is a different type of error entirely. Just do the documentation update, I'll gladly take that.
thanks,
greg k-h