Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] iio: light: hid-sensor-als: Don't read past the end of short fields
@ 2026-10-27 10:58 Daniel Schaefer via B4 Relay
  2026-11-01  3:37 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Schaefer via B4 Relay @ 2026-10-27 10:58 UTC (permalink / raw)
  To: Jiri Kosina, Jonathan Cameron, Srinivas Pandruvada, David Lechner,
	Nuno Sá, Andy Shevchenko
  Cc: srinivas pandruvada, linux-input, linux-iio, linux-kernel,
	Daniel Schaefer, Sven Vainküla

From: Daniel Schaefer <dhs@frame.work>

Reports are not guaranteed to be 4 bytes.
On color ALS sensors there are fields that can be shorter than 4 bytes:
16-bit illuminance, color temperature and chromaticity x/y.

I don't have such a sensor right now, but I have emulated one in
firmware. With this patch, no garbage data is read from the next report:

  before: 05dc02bc 05dc02bc 190005dc 0ce41900 00000ce4
  after:  000002bc 000002bc 000005dc 00001900 00000ce4

Same fix as commit ad02ca57e44e ("iio: hid-sensor-prox: Add support for
16-bit report size").

Fixes: ed5514c925a0 ("iio: hid-sensors: Added ALS")
Signed-off-by: Daniel Schaefer <dhs@frame.work>
---
Tested on an emulated sensor with 2 byte reports that is correctly recognized
by Windows.

Cc: Sven Vainküla <sven@xn--vainkla-r2a.ee>
---
 drivers/iio/light/hid-sensor-als.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c
index 929343e42d83..00dfc2037c19 100644
--- a/drivers/iio/light/hid-sensor-als.c
+++ b/drivers/iio/light/hid-sensor-als.c
@@ -267,7 +267,21 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
 	struct als_state *als_state = iio_priv(indio_dev);
 	int ret = -EINVAL;
-	u32 sample_data = *(u32 *)raw_data;
+	u32 sample_data;
+
+	switch (raw_len) {
+	case 1:
+		sample_data = *(u8 *)raw_data;
+		break;
+	case 2:
+		sample_data = *(u16 *)raw_data;
+		break;
+	default:
+		if (raw_len < sizeof(u32))
+			return -EINVAL;
+		sample_data = *(u32 *)raw_data;
+		break;
+	}
 
 	switch (usage_id) {
 	case HID_USAGE_SENSOR_LIGHT_ILLUM:

---
base-commit: 09f742b49cb2728086d78be2b0b35e4d1d71fc85
change-id: 20261027-iio-als-overread-c9e538fa61fa

Best regards,
-- 
Daniel Schaefer <dhs@frame.work>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] iio: light: hid-sensor-als: Don't read past the end of short fields
  2026-10-27 10:58 [PATCH] iio: light: hid-sensor-als: Don't read past the end of short fields Daniel Schaefer via B4 Relay
@ 2026-11-01  3:37 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-11-01  3:37 UTC (permalink / raw)
  To: Daniel Schaefer via B4 Relay
  Cc: dhs, Jiri Kosina, Srinivas Pandruvada, David Lechner,
	Nuno Sá, Andy Shevchenko, srinivas pandruvada, linux-input,
	linux-iio, linux-kernel, Sven Vainküla

On Tue, 27 Oct 2026 18:58:39 +0800
Daniel Schaefer via B4 Relay <devnull+dhs.frame.work@kernel.org> wrote:

> From: Daniel Schaefer <dhs@frame.work>
> 
> Reports are not guaranteed to be 4 bytes.
> On color ALS sensors there are fields that can be shorter than 4 bytes:
> 16-bit illuminance, color temperature and chromaticity x/y.
> 
> I don't have such a sensor right now, but I have emulated one in
> firmware. With this patch, no garbage data is read from the next report:
> 
>   before: 05dc02bc 05dc02bc 190005dc 0ce41900 00000ce4
>   after:  000002bc 000002bc 000005dc 00001900 00000ce4
> 
> Same fix as commit ad02ca57e44e ("iio: hid-sensor-prox: Add support for
> 16-bit report size").
> 
> Fixes: ed5514c925a0 ("iio: hid-sensors: Added ALS")
> Signed-off-by: Daniel Schaefer <dhs@frame.work>
Hi Daniel,

Seems in line with the referenced commit and to make sense to me.
So I'll pick this up now - however still time for more reviews as
I'll need to rebase on rc1 anyway in the next few days.

Applied to the fixes-togreg branch of iio.git

Thanks,

Jonathan

> ---
> Tested on an emulated sensor with 2 byte reports that is correctly recognized
> by Windows.
> 
> Cc: Sven Vainküla <sven@xn--vainkla-r2a.ee>
> ---
>  drivers/iio/light/hid-sensor-als.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c
> index 929343e42d83..00dfc2037c19 100644
> --- a/drivers/iio/light/hid-sensor-als.c
> +++ b/drivers/iio/light/hid-sensor-als.c
> @@ -267,7 +267,21 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
>  	struct iio_dev *indio_dev = platform_get_drvdata(priv);
>  	struct als_state *als_state = iio_priv(indio_dev);
>  	int ret = -EINVAL;
> -	u32 sample_data = *(u32 *)raw_data;
> +	u32 sample_data;
> +
> +	switch (raw_len) {
> +	case 1:
> +		sample_data = *(u8 *)raw_data;
> +		break;
> +	case 2:
> +		sample_data = *(u16 *)raw_data;
> +		break;
> +	default:
> +		if (raw_len < sizeof(u32))
> +			return -EINVAL;
> +		sample_data = *(u32 *)raw_data;
> +		break;
> +	}
>  
>  	switch (usage_id) {
>  	case HID_USAGE_SENSOR_LIGHT_ILLUM:
> 
> ---
> base-commit: 09f742b49cb2728086d78be2b0b35e4d1d71fc85
> change-id: 20261027-iio-als-overread-c9e538fa61fa
> 
> Best regards,


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-01  3:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-27 10:58 [PATCH] iio: light: hid-sensor-als: Don't read past the end of short fields Daniel Schaefer via B4 Relay
2026-11-01  3:37 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox