iio: temperature: hid-sensor-temperature: switch to non-devm iio_device_register()
Subsystems (3)
PATCH v2 1/1 iio: temperature: hid-sensor-temperature: switch to non-devm iio_device_register()
From: Sanjay Chitroda <[email protected]>
Avoid using devm_iio_device_register(), as this driver requires explicit error handling and teardown ordering.
With devm_iio_device_register(), IIO device remains registered until the devres cleanup phase. However, driver's remove() callback removes the sensor hub callback and trigger support. This can create a race window where IIO device is still visible and read_raw() requests are issued. These requests might call sensor_hub_input_attr_get_raw_value(), which waits up to 5 seconds for a response from the sensor hub callback that has already been removed.
Add an explicit iio_device_unregister() call in the teardown path to ensure deterministic cleanup, so that userspace can no longer access the device once backend resources begin to be dismantled.
---
Changes in v2:
- Added review tags of Maxwell and Andy along with stable Cc
- Based on input from Srinivas and investigation use-after-free
explanation was not sufficiently justified so updated the commit
message with appropriate information and kept Fixes tag
- Link to v1: https://lore.kernel.org/all/[email protected]
--- drivers/iio/temperature/hid-sensor-temperature.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/hid-sensor-temperature.c +2 -1
@@ -244,7 +244,7 @@ static int hid_temperature_probe(struct platform_device *pdev)if (ret) goto error_remove_trigger; - ret =devm_iio_device_register (indio_dev->dev.parent, indio_dev); + ret =iio_device_register (indio_dev);if (ret) goto error_remove_callback;
@@ -264,6 +264,7 @@ static void hid_temperature_remove(struct platform_device *pdev)struct iio_dev *indio_dev =platform_get_drvdata (pdev);struct temperature_state *temp_st =iio_priv (indio_dev); +iio_device_unregister (indio_dev);sensor_hub_remove_callback (hsdev,HID_USAGE_SENSOR_TEMPERATURE );hid_sensor_remove_trigger (indio_dev, &temp_st->common_attributes); }
Signature · 1 line
--
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The driver incorrectly uses a static global structure temperature_callbacks to hold per-device state, causing cross-device data corruption and potential Use-After-Free.
- [High] A race condition during device unbind can lead to a Use-After-Free or NULL pointer dereference because sensor_hub_remove_callback does not wait for concurrently executing events. --
commit 0a06e7ba97cafc3e2c79740421bf8186cb0fc540
Author: Sanjay Chitroda <[email protected]>
iio: temperature: hid-sensor-temperature: switch to non-devm iio_device_register()
The patch switches from devm_iio_device_register() to iio_device_register() to fix a teardown ordering issue. It adds an explicit iio_device_unregister() in the remove function to ensure the device is not accessed after its backend resources begin to be dismantled.
drivers/iio/temperature/hid-sensor-temperature.c static int hid_temperature_probe(struct platform_device *pdev) · line 247
Quoted 10 lines
drivers/iio/temperature/hid-sensor-temperature.c +1
-1@@ -244,7 +244,7 @@ static int hid_temperature_probe(struct platform_device *pdev)if (ret) goto error_remove_trigger; - ret =devm_iio_device_register (indio_dev->dev.parent, indio_dev); + ret =iio_device_register (indio_dev);
[Severity: High]
This is a pre-existing issue, but does this driver incorrectly use a static global structure for per-device state?
Looking at hid_temperature_probe(), we overwrite the shared global pdev pointer in the statically allocated temperature_callbacks structure:
drivers/iio/temperature/hid-sensor-temperature.c:hid_temperature_probe() {
...
temperature_callbacks.pdev = pdev;
ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
&temperature_callbacks);
...
}If multiple temperature sensors are probed, the most recently probed device will overwrite this shared pointer. If the last probed device is later unbound, its driver data is cleared.
Subsequent events for other active devices will dereference the cleared pdev in temperature_proc_event(), leading to a null pointer dereference or use-after-free.
drivers/iio/temperature/hid-sensor-temperature.c static void hid_temperature_remove(struct platform_device *pdev) · line 268
Quoted 9 lines
drivers/iio/temperature/hid-sensor-temperature.c +1
-0if (ret) goto error_remove_callback;@@ -264,6 +264,7 @@ static void hid_temperature_remove(struct platform_device *pdev)struct iio_dev *indio_dev =platform_get_drvdata (pdev);struct temperature_state *temp_st =iio_priv (indio_dev); +iio_device_unregister (indio_dev);sensor_hub_remove_callback (hsdev,HID_USAGE_SENSOR_TEMPERATURE );
[Severity: High]
This isn't a bug introduced by this patch, but can a race condition during device unbind lead to a use-after-free or null pointer dereference?
The sensor_hub_remove_callback() removes the callback from the sensor hub's list, but does not synchronize with concurrently executing callback handlers like sensor_hub_raw_event().
If an event from the sensor hub arrives exactly as the driver is unbinding, the event handler might fetch the callback just before it is removed. When hid_temperature_remove() completes, devres frees indio_dev, and the driver core sets drvdata to NULL.
The concurrent event handler then executes temperature_capture_sample(), calling platform_get_drvdata(pdev) which either returns NULL or a dangling pointer to the freed indio_dev, causing a crash.
Quoted 2 lines
drivers/iio/temperature/hid-sensor-temperature.c +0
-0hid_sensor_remove_trigger (indio_dev, &temp_st->common_attributes); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 17 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit error handling and teardown ordering.With
devm_iio_device_register(), IIO device remains registered until the devres cleanup phase. However, driver'sremove()callback removes the sensor hub callback and trigger support. This can create a race window where IIO device is still visible andread_raw()requests are issued. These requests might callsensor_hub_input_attr_get_raw_value(), which waits up to 5 seconds for a response from the sensor hub callback that has already been removed.Add an explicit
iio_device_unregister()call in the teardown path to ensure deterministic cleanup, so that userspace can no longer access the device once backend resources begin to be dismantled.
Quoted 6 lines
Fixes 59d0f2da3569 ("iio: hid: Add temperature sensor support")Reviewed-by Maxwell Doose <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>Signed-off-by Sanjay Chitroda <[email protected]>
Hi Jonathan,
There are two pre-existing issue highlighted by Sashiko which can be analysed and fix in separate change if required.
Requesting your valuable input on this fix. also, this is pre-requisite for HID IIO devm conversation series.
Thanks, Sanjay
Quoted 32 lines
---
Changes in v2:
- Added review tags of Maxwell and Andy along with stable Cc
- Based on input from Srinivas and investigation use-after-free
explanation was not sufficiently justified so updated the commit
message with appropriate information and kept Fixes tag
- Link to v1: https://lore.kernel.org/all/[email protected]
---drivers/iio/temperature/hid-sensor-temperature.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)drivers/iio/temperature/hid-sensor-temperature.c +2
-1@@ -244,7 +244,7 @@ static int hid_temperature_probe(struct platform_device *pdev)if (ret) goto error_remove_trigger; - ret =devm_iio_device_register (indio_dev->dev.parent, indio_dev); + ret =iio_device_register (indio_dev);if (ret) goto error_remove_callback;@@ -264,6 +264,7 @@ static void hid_temperature_remove(struct platform_device *pdev)struct iio_dev *indio_dev =platform_get_drvdata (pdev);struct temperature_state *temp_st =iio_priv (indio_dev); +iio_device_unregister (indio_dev);sensor_hub_remove_callback (hsdev,HID_USAGE_SENSOR_TEMPERATURE );hid_sensor_remove_trigger (indio_dev, &temp_st->common_attributes); }
Quoted 23 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.With
devm_iio_device_register(), IIO device remains registered until the
devres cleanup phase. However, driver'sremove()callback removes the sensor hub callback and trigger support. This can create a race window
where IIO device is still visible andread_raw()requests are issued. These requests might callsensor_hub_input_attr_get_raw_value(), which
waits up to 5 seconds for a response from the sensor hub callback that
has already been removed.Add an explicit
iio_device_unregister()call in the teardown path to ensure deterministic cleanup, so that userspace can no longer access the
device once backend resources begin to be dismantled.
Quoted 6 lines
Fixes 59d0f2da3569 ("iio: hid: Add temperature sensor support")Reviewed-by Maxwell Doose <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>Signed-off-by Sanjay Chitroda <[email protected]>
Quoted 39 lines
---
Changes in v2:
- Added review tags of Maxwell and Andy along with stable Cc
- Based on input from Srinivas and investigation use-after-free explanation was not sufficiently justified so updated the commit message with appropriate information and kept Fixes tag
- Link to v1:
https://lore.kernel.org/all/[email protected]
---
drivers/iio/temperature/hid-sensor-temperature.c | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)drivers/iio/temperature/hid-sensor-temperature.c +0
-0@@ -244,7 +244,7 @@ static int hid_temperature_probe(struct platform_device *pdev) if (ret) goto error_remove_trigger;drivers/iio/temperature/hid-sensor-temperature.c +0
-1- ret = devm_iio_device_register(indio_dev->dev.parent,
indio_dev);drivers/iio/temperature/hid-sensor-temperature.c +1
-0+ ret =iio_device_register (indio_dev);if (ret)
gotoerror_remove_callback;drivers/iio/temperature/hid-sensor-temperature.c +0
-0@@ -264,6 +264,7 @@ static void hid_temperature_remove(struct platform_device *pdev) struct iio_dev *indio_dev = platform_get_drvdata(pdev); struct temperature_state *temp_st = iio_priv(indio_dev);drivers/iio/temperature/hid-sensor-temperature.c +1
-0+iio_device_unregister (indio_dev);
sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE); hid_sensor_remove_trigger(indio_dev, &temp_st-Quoted 1 line
common_attributes);}
Quoted 24 lines · On Wed, 08 Jul 2026 16:25:41 -0700 srinivas pandruvada…
Quoted 23 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.With
devm_iio_device_register(), IIO device remains registered until the
devres cleanup phase. However, driver'sremove()callback removes the sensor hub callback and trigger support. This can create a race window
where IIO device is still visible andread_raw()requests are issued. These requests might callsensor_hub_input_attr_get_raw_value(), which
waits up to 5 seconds for a response from the sensor hub callback that
has already been removed.Add an explicit
iio_device_unregister()call in the teardown path to ensure deterministic cleanup, so that userspace can no longer access the
device once backend resources begin to be dismantled.
Quoted 8 lines
Quoted 6 lines
Fixes 59d0f2da3569 ("iio: hid: Add temperature sensor support")Reviewed-by Maxwell Doose <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>Signed-off-by Sanjay Chitroda <[email protected]>
Applied to the fixes-togreg branch of iio.git.
Note that to take anything non fix related on top of this will require me waiting until this is upstream. I'm not sure how many fixes I'll have outstanding (and so if I will soon reach critical mass) but most likely that will be at least a few weeks at least.
Thanks
Jonathan
Quoted 40 lines
Quoted 40 lines
---
Changes in v2:
- Added review tags of Maxwell and Andy along with stable Cc
- Based on input from Srinivas and investigation use-after-free explanation was not sufficiently justified so updated the commit message with appropriate information and kept Fixes tag
- Link to v1:
https://lore.kernel.org/all/[email protected]
---
drivers/iio/temperature/hid-sensor-temperature.c | 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)drivers/iio/temperature/hid-sensor-temperature.c +0
-0@@ -244,7 +244,7 @@ static int hid_temperature_probe(struct platform_device *pdev) if (ret) goto error_remove_trigger;drivers/iio/temperature/hid-sensor-temperature.c +0
-1- ret = devm_iio_device_register(indio_dev->dev.parent,
indio_dev);drivers/iio/temperature/hid-sensor-temperature.c +1
-0+ ret =iio_device_register (indio_dev);if (ret)
gotoerror_remove_callback;drivers/iio/temperature/hid-sensor-temperature.c +0
-0@@ -264,6 +264,7 @@ static void hid_temperature_remove(struct platform_device *pdev) struct iio_dev *indio_dev = platform_get_drvdata(pdev); struct temperature_state *temp_st = iio_priv(indio_dev);drivers/iio/temperature/hid-sensor-temperature.c +1
-0+iio_device_unregister (indio_dev);
sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE); hid_sensor_remove_trigger(indio_dev, &temp_st-Quoted 1 line
common_attributes);}