Skip to content

Commit 2ce90cf

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 abec577 commit 2ce90cf

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
@@ -1006,26 +1006,26 @@ static int hid_sensor_custom_probe(struct platform_device *pdev)
10061006
return ret;
10071007
}
10081008

1009-
ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
1010-
&enable_sensor_attr_group);
1009+
ret = hid_sensor_custom_add_attributes(sensor_inst);
10111010
if (ret)
10121011
goto err_remove_callback;
10131012

1014-
ret = hid_sensor_custom_add_attributes(sensor_inst);
1013+
ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
1014+
&enable_sensor_attr_group);
10151015
if (ret)
1016-
goto err_remove_group;
1016+
goto err_remove_attributes;
10171017

10181018
ret = hid_sensor_custom_dev_if_add(sensor_inst);
10191019
if (ret)
1020-
goto err_remove_attributes;
1020+
goto err_remove_group;
10211021

10221022
return 0;
10231023

1024-
err_remove_attributes:
1025-
hid_sensor_custom_remove_attributes(sensor_inst);
10261024
err_remove_group:
10271025
sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
10281026
&enable_sensor_attr_group);
1027+
err_remove_attributes:
1028+
hid_sensor_custom_remove_attributes(sensor_inst);
10291029
err_remove_callback:
10301030
sensor_hub_remove_callback(hsdev, hsdev->usage);
10311031

@@ -1043,9 +1043,10 @@ static int hid_sensor_custom_remove(struct platform_device *pdev)
10431043
}
10441044

10451045
hid_sensor_custom_dev_if_remove(sensor_inst);
1046-
hid_sensor_custom_remove_attributes(sensor_inst);
1046+
/* Remove enable_sensor first as it uses fields via power_state/report_state. */
10471047
sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
10481048
&enable_sensor_attr_group);
1049+
hid_sensor_custom_remove_attributes(sensor_inst);
10491050
sensor_hub_remove_callback(hsdev, hsdev->usage);
10501051

10511052
return 0;

0 commit comments

Comments
 (0)