| From 3789d0802ddb4b3be04062caf4bfadd23496e9a7 Mon Sep 17 00:00:00 2001 |
| From: Haoxiang Li <haoxiang_li2024@163.com> |
| Date: Tue, 7 Jul 2026 15:15:45 +0800 |
| Subject: HID: sensor: custom: Fix field sysfs group cleanup on failure |
| |
| From: Haoxiang Li <haoxiang_li2024@163.com> |
| |
| commit 3789d0802ddb4b3be04062caf4bfadd23496e9a7 upstream. |
| |
| hid_sensor_custom_add_attributes() creates one sysfs group for each |
| custom sensor field. If sysfs_create_group() fails after some groups |
| have already been created, the function returns the error without |
| removing the previously created groups. |
| |
| Add a local unwind path to remove the groups that were already created. |
| With enable_sensor exposed only after the field attributes are ready, |
| this path can free sensor_inst->fields without leaving enable_sensor |
| able to access pointers into that array. |
| |
| Fixes: 4a7de0519df5 ("HID: sensor: Custom and Generic sensor support") |
| Cc: stable@vger.kernel.org |
| Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com> |
| Acked-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> |
| Signed-off-by: Jiri Kosina <jkosina@suse.com> |
| Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
| --- |
| drivers/hid/hid-sensor-custom.c | 9 ++++++++- |
| 1 file changed, 8 insertions(+), 1 deletion(-) |
| |
| --- a/drivers/hid/hid-sensor-custom.c |
| +++ b/drivers/hid/hid-sensor-custom.c |
| @@ -609,7 +609,7 @@ static int hid_sensor_custom_add_attribu |
| &sensor_inst->fields[i]. |
| hid_custom_attribute_group); |
| if (ret) |
| - break; |
| + goto err_remove_groups; |
| |
| /* For power or report field store indexes */ |
| if (sensor_inst->fields[i].attribute.attrib_id == |
| @@ -621,6 +621,13 @@ static int hid_sensor_custom_add_attribu |
| } |
| |
| return ret; |
| + |
| +err_remove_groups: |
| + while (--i >= 0) |
| + sysfs_remove_group(&sensor_inst->pdev->dev.kobj, |
| + &sensor_inst->fields[i].hid_custom_attribute_group); |
| + kfree(sensor_inst->fields); |
| + return ret; |
| } |
| |
| static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom * |