Skip to content

Commit 7771c8c

Browse files
Tzung-Bi Shihjic23
authored andcommitted
iio: cros_ec: fix an use-after-free in cros_ec_sensors_push_data()
cros_ec_sensors_push_data() reads `indio_dev->active_scan_mask` and calls iio_push_to_buffers_with_timestamp() without making sure the `indio_dev` stays in buffer mode. There is a race if `indio_dev` exits buffer mode right before cros_ec_sensors_push_data() accesses them. An use-after-free on `indio_dev->active_scan_mask` was observed. The call trace: [...] _find_next_bit cros_ec_sensors_push_data cros_ec_sensorhub_event blocking_notifier_call_chain cros_ec_irq_thread It was caused by a race condition: one thread just freed `active_scan_mask` at [1]; while another thread tried to access the memory at [2]. Fix it by calling iio_device_claim_buffer_mode() to ensure the `indio_dev` can't exit buffer mode during cros_ec_sensors_push_data(). [1]: https://elixir.bootlin.com/linux/v6.5/source/drivers/iio/industrialio-buffer.c#L1189 [2]: https://elixir.bootlin.com/linux/v6.5/source/drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c#L198 Cc: stable@vger.kernel.org Fixes: aa984f1 ("iio: cros_ec: Register to cros_ec_sensorhub when EC supports FIFO") Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org> Reviewed-by: Guenter Roeck <groeck@chromium.org> Reviewed-by: Stephen Boyd <swboyd@chromium.org> Link: https://lore.kernel.org/r/20230829030622.1571852-1-tzungbi@kernel.org Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent ea191d0 commit 7771c8c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
190190
/*
191191
* Ignore samples if the buffer is not set: it is needed if the ODR is
192192
* set but the buffer is not enabled yet.
193+
*
194+
* Note: iio_device_claim_buffer_mode() returns -EBUSY if the buffer
195+
* is not enabled.
193196
*/
194-
if (!iio_buffer_enabled(indio_dev))
197+
if (iio_device_claim_buffer_mode(indio_dev) < 0)
195198
return 0;
196199

197200
out = (s16 *)st->samples;
@@ -210,6 +213,7 @@ int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
210213
iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
211214
timestamp + delta);
212215

216+
iio_device_release_buffer_mode(indio_dev);
213217
return 0;
214218
}
215219
EXPORT_SYMBOL_GPL(cros_ec_sensors_push_data);

0 commit comments

Comments
 (0)