iio: temperature: hid-sensor-temperature: switch to non-devm iio_device_register()
Subsystems (3)
PATCH 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.
Mixing devm_* APIs with goto-based error unwinding breaks the expected LIFO resource release model and can introduce race windows during device removal. In particular, the IIO device may remain visible to userspace while dependent resources are already being freed, potentially leading to use-after-free issues.
Add explicit iio_device_unregister() call in the teardown path to ensure deterministic cleanup and follow kernel resource management conventions.
--- 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:
- [Critical] Concurrent execution of HID sensor hub callbacks and device unbind leads to a Use-After-Free of indio_dev.
- [High] The static global temperature_callbacks struct is mutated during probe, causing data corruption and cross-device routing bugs when multiple sensors are present. --
commit 829c19b8a370c5d8b09345d4e07b865deb47a88f
Author: Sanjay Chitroda <[email protected]>
iio: temperature: hid-sensor-temperature: switch to non-devm iio_device_register()
This commit replaces devm_iio_device_register() with iio_device_register() to ensure deterministic cleanup. It adds an explicit iio_device_unregister() call in the remove path to fix potential use-after-free race conditions during device removal.
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 isn't a bug introduced by this patch, but looking at the probe function just prior to this registration, is there a risk of corrupting device routing when multiple sensors are present?
The driver defines temperature_callbacks as a static global struct:
static struct hid_sensor_hub_callbacks temperature_callbacks = {
.send_event = &temperature_proc_event, .capture_sample = &temperature_capture_sample,
};
During hid_temperature_probe(), the current device's pdev is directly assigned to temperature_callbacks.pdev.
If a system has multiple HID temperature sensors, does the last probed sensor overwrite this global pointer? It seems like when sensor_hub_raw_event() processes hardware events, it might fetch the shared struct and pass the single overwritten pdev to temperature_capture_sample(). Would this cause the data from all temperature sensors to be written exclusively into the IIO state of the last probed sensor?
[ ... ]
drivers/iio/temperature/hid-sensor-temperature.c static void hid_temperature_remove(struct platform_device *pdev) · line 268
Quoted 6 lines
drivers/iio/temperature/hid-sensor-temperature.c +1
-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); +iio_device_unregister (indio_dev);sensor_hub_remove_callback (hsdev,HID_USAGE_SENSOR_TEMPERATURE );
[Severity: Critical]
This is a pre-existing issue, but I wanted to ask about the teardown order here.
In hid_temperature_remove(), we call sensor_hub_remove_callback(), which removes the callback from the sensor hub's list under dyn_callback_lock. However, it doesn't appear to synchronize with or wait for concurrently executing callback handlers like sensor_hub_raw_event() to finish before returning.
Since indio_dev is allocated with devm_iio_device_alloc() during probe, it will be freed by devres immediately after hid_temperature_remove() returns.
If sensor_hub_raw_event() is executing concurrently and has already retrieved the callback pointer, could it pass the pdev to temperature_capture_sample() and perform a use-after-free write on the freed private state?
For example:temp_st->scan.temperature_data = *(s32 *)raw_data;
Should we be flushing or waiting for running callbacks to complete before letting the device context be freed?
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 11 lines · Sanjay Chitroda
Avoid using
devm_iio_device_register(), as this driver requires explicit error handling and teardown ordering.Mixing devm_* APIs with goto-based error unwinding breaks the expected LIFO resource release model and can introduce race windows during device removal. In particular, the IIO device may remain visible to userspace while dependent resources are already being freed, potentially leading to use-after-free issues.
Add explicit
iio_device_unregister()call in the teardown path to ensure deterministic cleanup and follow kernel resource management conventions.
Strictly speaking this needs Cc: stable@ as well as mentioned in the documentation.
LGTM,
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 12 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit error handling and teardown ordering.Mixing devm_* APIs with goto-based error unwinding breaks the expected LIFO resource release model and can introduce race windows during device removal. In particular, the IIO device may remain visible to userspace while dependent resources are already being freed, potentially leading to use-after-free issues.
Quoted 9 lines
Add explicit
iio_device_unregister()call in the teardown path to ensure deterministic cleanup and follow kernel resource management conventions.Fixes 59d0f2da3569 ("iio: hid: Add temperature sensor support")Signed-off-by Sanjay Chitroda <[email protected]>--- drivers/iio/temperature/hid-sensor-temperature.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
My rb will be a tad redundant but
and should Cc stable as Andy said but hopefully Jonathan can amend :)
Signature · 2 lines
best regards, max
srinivas pandruvada
maintainerQuoted 6 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.
Quoted 9 lines
Mixing devm_* APIs with goto-based error unwinding breaks the expected
LIFO resource release model and can introduce race windows during device
removal. In particular, the IIO device may remain visible to userspace
while dependent resources are already being freed, potentially leading
to use-after-free issues.
Please explain this use after free case here.
Thanks,
Srinivas
Quoted 39 lines
Add explicit
iio_device_unregister()call in the teardown path to ensure
deterministic cleanup and follow kernel resource management conventions.Fixes 59d0f2da3569 ("iio: hid: Add temperature sensor support")Signed-off-by Sanjay Chitroda <[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 18 lines · srinivas pandruvada
Quoted 6 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.Quoted 9 lines
Mixing devm_* APIs with goto-based error unwinding breaks the expected
LIFO resource release model and can introduce race windows during device
removal. In particular, the IIO device may remain visible to userspace
while dependent resources are already being freed, potentially leading
to use-after-free issues.
Quoted 4 lines
Please explain this use after free case here.
Thanks,
Srinivas
My guess is that because the device would still be registered but would actually be removed, sysfs still has "wild" pointers to read_raw() and write_raw() (which don't exist anymore), causing the UAF. If I'm wrong feel free to correct me though.
srinivas pandruvada
maintainerQuoted 22 lines · Maxwell Doose
Quoted 20 lines · srinivas pandruvada
Quoted 6 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.Quoted 9 lines
Mixing devm_* APIs with goto-based error unwinding breaks the expected
LIFO resource release model and can introduce race windows during device
removal. In particular, the IIO device may remain visible to userspace
while dependent resources are already being freed, potentially leading
to use-after-free issues.Please explain this use after free case here.
Quoted 7 lines
Quoted 2 lines
Thanks,
SrinivasMy guess is that because the device would still be registered but would actually be removed, sysfs still has "wild" pointers to
read_raw()andwrite_raw()(which don't exist anymore), causing the UAF. If I'm wrong feel free to correct me though.
iio_device_unregister() will be last one to be called after device removal from devm action handler. This will cleanup attributes. So, read_raw() or write_raw() can be called. The problem can be handlers for read_raw() and write_raw() if anything there which are dependent on clean done by hid_temperature_remove(). Here callbacks are cleaned up, so nothing to respond to read sensor_hub_input_attr_get_raw_value(), so it has to wait for 5 seconds to timeout, which is not great. So nothing against change done here.
But still not sure any use after free case, unless I am missing something.
Thanks,
Srinivas
Jonathan Cameron
maintainerQuoted 40 lines · On Mon, 22 Jun 2026 13:50:22 -0700 srinivas pandruvada…
Quoted 22 lines · Maxwell Doose
Quoted 20 lines · srinivas pandruvada
Quoted 6 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.Quoted 9 lines
Mixing devm_* APIs with goto-based error unwinding breaks the expected
LIFO resource release model and can introduce race windows during device
removal. In particular, the IIO device may remain visible to userspace
while dependent resources are already being freed, potentially leading
to use-after-free issues.Please explain this use after free case here.
Quoted 7 lines
Quoted 2 lines
Thanks,
SrinivasMy guess is that because the device would still be registered but would actually be removed, sysfs still has "wild" pointers to
read_raw()andwrite_raw()(which don't exist anymore), causing the UAF. If I'm wrong feel free to correct me though.
iio_device_unregister()will be last one to be called after device removal from devm action handler. This will cleanup attributes. So,read_raw()orwrite_raw()can be called. The problem can be handlers forread_raw()andwrite_raw()if anything there which are dependent on clean done byhid_temperature_remove(). Here callbacks are cleaned up, so nothing to respond to readsensor_hub_input_attr_get_raw_value(), so it has to wait for 5 seconds to timeout, which is not great. So nothing against change done here.
Quoted 3 lines
But still not sure any use after free case, unless I am missing something.
Agreed that to call UAF you need an explained path (and preferably testing that it happens). The timeout issue Srinivas calls out is sufficient for us to merge this as a fix, but the patch description should then talk about that.
Thanks,
Jonathan
Quoted 4 lines
Thanks,
Srinivas
Quoted 43 lines · Jonathan Cameron
Quoted 40 lines · On Mon, 22 Jun 2026 13:50:22 -0700 srinivas pandruvada…
Quoted 22 lines · Maxwell Doose
Quoted 20 lines · srinivas pandruvada
Quoted 6 lines · Sanjay Chitroda
From: Sanjay Chitroda <[email protected]>
Avoid using
devm_iio_device_register(), as this driver requires explicit
error handling and teardown ordering.Quoted 9 lines
Mixing devm_* APIs with goto-based error unwinding breaks the expected
LIFO resource release model and can introduce race windows during device
removal. In particular, the IIO device may remain visible to userspace
while dependent resources are already being freed, potentially leading
to use-after-free issues.Please explain this use after free case here.
Quoted 7 lines
Quoted 2 lines
Thanks,
SrinivasMy guess is that because the device would still be registered but would actually be removed, sysfs still has "wild" pointers to
read_raw()andwrite_raw()(which don't exist anymore), causing the UAF. If I'm wrong feel free to correct me though.
iio_device_unregister()will be last one to be called after device removal from devm action handler. This will cleanup attributes. So,read_raw()orwrite_raw()can be called. The problem can be handlers forread_raw()andwrite_raw()if anything there which are dependent on clean done byhid_temperature_remove(). Here callbacks are cleaned up, so nothing to respond to readsensor_hub_input_attr_get_raw_value(), so it has to wait for 5 seconds to timeout, which is not great. So nothing against change done here.
Quoted 8 lines
Quoted 3 lines
But still not sure any use after free case, unless I am missing something.
Agreed that to call UAF you need an explained path (and preferably testing that it happens). The timeout issue Srinivas calls out is sufficient for us to merge this as a fix, but the patch description should then talk about that.
Thank you all for the review.
I investigated the remove path in more detail. I agree that my original use-after-free explanation was not sufficiently justified.
The issue is actually the teardown ordering. With "devm_iio_device_register()", the IIO device remains registered until the devres cleanup phase, while "remove()" first removes the sensor hub callback. During this window, the IIO device is still visible to userspace and "read_raw()" requests may be issued. These requests eventually wait for a response from the sensor hub callback, but the callback has already been removed, resulting in the operation timing out.
I'll update the commit message in v2 to describe this observable behaviour instead of mentioning a potential use-after-free. The code change itself remains the same.
Thanks, Sanjay
Quoted 8 lines
Thanks,
Jonathan
Quoted 4 lines
Thanks,
Srinivas