Skip to content

Commit d37ff4e

Browse files
Haoxiang Ligregkh
authored andcommitted
HID: sensor: custom: Fix use-after-free in enable_sensor
commit ad8fb82 upstream. enable_sensor_store() can call set_power_report_state(), which dereferences sensor_inst->power_state and sensor_inst->report_state. These pointers refer to entries in sensor_inst->fields. Create the field attributes before exposing the enable_sensor sysfs attribute, so enable_sensor cannot be accessed before the state it depends on has been initialized. On remove, delete enable_sensor before freeing the field attributes, so a concurrent sysfs write cannot dereference freed memory through power_state or report_state. Reported-by: Sashiko AI Review <sashiko-bot@kernel.org> Link: https://sashiko.dev/#/patchset/20260623021950.1736413-1-haoxiang_li2024@163.com?part=1 Fixes: 4a7de05 ("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>
1 parent 634f498 commit d37ff4e

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

drivers/hid/hid-sensor-custom.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -910,26 +910,26 @@ static int hid_sensor_custom_probe(struct platform_device *pdev)
910910
return ret;
911911
}
912912

913-
ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
914-
&enable_sensor_attr_group);
913+
ret = hid_sensor_custom_add_attributes(sensor_inst);
915914
if (ret)
916915
goto err_remove_callback;
917916

918-
ret = hid_sensor_custom_add_attributes(sensor_inst);
917+
ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
918+
&enable_sensor_attr_group);
919919
if (ret)
920-
goto err_remove_group;
920+
goto err_remove_attributes;
921921

922922
ret = hid_sensor_custom_dev_if_add(sensor_inst);
923923
if (ret)
924-
goto err_remove_attributes;
924+
goto err_remove_group;
925925

926926
return 0;
927927

928-
err_remove_attributes:
929-
hid_sensor_custom_remove_attributes(sensor_inst);
930928
err_remove_group:
931929
sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
932930
&enable_sensor_attr_group);
931+
err_remove_attributes:
932+
hid_sensor_custom_remove_attributes(sensor_inst);
933933
err_remove_callback:
934934
sensor_hub_remove_callback(hsdev, hsdev->usage);
935935

@@ -947,9 +947,10 @@ static int hid_sensor_custom_remove(struct platform_device *pdev)
947947
}
948948

949949
hid_sensor_custom_dev_if_remove(sensor_inst);
950-
hid_sensor_custom_remove_attributes(sensor_inst);
950+
/* Remove enable_sensor first as it uses fields via power_state/report_state. */
951951
sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
952952
&enable_sensor_attr_group);
953+
hid_sensor_custom_remove_attributes(sensor_inst);
953954
sensor_hub_remove_callback(hsdev, hsdev->usage);
954955

955956
return 0;

0 commit comments

Comments
 (0)