Skip to contentSkip to conversation

iio: imu: bmi323: Fix potential out-of-bounds access of bmi323_hw[]

PATCH Superseded [email protected] 3 participants 3 messages Mar 27 – Apr 12

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: bmi323: Fix potential out-of-bounds access of bmi323_hw[]

From: Denis Rastyogin <[email protected]>

The bmi323_channels[] array defines a channel with chan->type = IIO_TEMP and enables the IIO_CHAN_INFO_SCALE mask. As a result, bmi323_write_raw() may be called for this channel. However, bmi323_iio_to_sensor() returns -EINVAL for IIO_TEMP, and if this value is not validated, it can lead to an out-of-bounds access when used as an array index.

A similar case is properly handled in bmi323_read_raw() and does not result in an error.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")
---
 drivers/iio/imu/bmi323/bmi323_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/imu/bmi323/bmi323_core.c +2 -0
index 6bcb9a436581..64ead4f667e0 100644
@@ -1713,6 +1713,8 @@ static int bmi323_write_raw(struct iio_dev *indio_dev,
 		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
+		if (chan->type == IIO_TEMP)
+			return -EINVAL;
 		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 		ret = bmi323_set_scale(data, bmi323_iio_to_sensor(chan->type),
Signature · 1 line
2.42.2
reviewer
#1 lore raw
Quoted 25 lines · On 3/27/26 5:32 AM, [email protected] wrote:

From: Denis Rastyogin <[email protected]>

The bmi323_channels[] array defines a channel with chan->type = IIO_TEMP and enables the IIO_CHAN_INFO_SCALE mask. As a result, bmi323_write_raw() may be called for this channel. However, bmi323_iio_to_sensor() returns -EINVAL for IIO_TEMP, and if this value is not validated, it can lead to an out-of-bounds access when used as an array index.

A similar case is properly handled in bmi323_read_raw() and does not result in an error.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")
---
 drivers/iio/imu/bmi323/bmi323_core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/imu/bmi323/bmi323_core.c +0 -0
index 6bcb9a436581..64ead4f667e0 100644

drivers/iio/imu/bmi323/bmi323_core.c static int bmi323_write_raw(struct iio_dev *indio_dev, · line 1720

Quoted 8 lines
drivers/iio/imu/bmi323/bmi323_core.c +2 -0
@@ -1713,6 +1713,8 @@ static int bmi323_write_raw(struct iio_dev *indio_dev,
 		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
+		if (chan->type == IIO_TEMP)
+			return -EINVAL;
 		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 		ret = bmi323_set_scale(data, bmi323_iio_to_sensor(chan->type),

This is OK, but why not check and propagate the error return?

case IIO_CHAN_INFO_SCALE:

		ret = bmi323_iio_to_sensor(chan->type);
		if (ret < 0)
			return ret;

if (!iio_device_claim_direct(indio_dev))
return -EBUSY;

  		ret = bmi323_set_scale(data, ret, val, val2);
		...

And even if we shouldn't hit the error in other case statements, it seems like it would be good practice to still check for error there too.

maintainer
#2 lore raw
Quoted 10 lines · On Sat, 11 Apr 2026 14:28:07 -0500 David Lechner <[email protected]> wrote:
Quoted 9 lines · On 3/27/26 5:32 AM, [email protected] wrote:

From: Denis Rastyogin <[email protected]>

The bmi323_channels[] array defines a channel with chan->type = IIO_TEMP and enables the IIO_CHAN_INFO_SCALE mask. As a result, bmi323_write_raw() may be called for this channel. However, bmi323_iio_to_sensor() returns -EINVAL for IIO_TEMP, and if this value is not validated, it can lead to an out-of-bounds access when used as an array index.

Quoted 4 lines
Quoted 4 lines

A similar case is properly handled in bmi323_read_raw() and does not result in an error.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Good catch.

Quoted 7 lines
Quoted 7 lines
Fixes 8a636db3aa57 ("iio: imu: Add driver for BMI323 IMU")
---
 drivers/iio/imu/bmi323/bmi323_core.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 10 lines

drivers/iio/imu/bmi323/bmi323_core.c static int bmi323_write_raw(struct iio_dev *indio_dev, · line 1717

Quoted 10 lines
drivers/iio/imu/bmi323/bmi323_core.c +2 -0
index 6bcb9a436581..64ead4f667e0 100644
@@ -1713,6 +1713,8 @@ static int bmi323_write_raw(struct iio_dev *indio_dev,
 		iio_device_release_direct(indio_dev);
 		return ret;
 	case IIO_CHAN_INFO_SCALE:
+		if (chan->type == IIO_TEMP)
+			return -EINVAL;

Please flip this around as we want support for this write to be opt in rather than opt out.

		if (chan->type != IIO_ACCEL &&
		    chan->type != IIO_ANGL_VEL)

Alternative below is better though as that is opt in anyway.

Quoted 21 lines

drivers/iio/imu/bmi323/bmi323_core.c static int bmi323_write_raw(struct iio_dev *indio_dev, · line 1720

Quoted 3 lines
drivers/iio/imu/bmi323/bmi323_core.c +0 -0
 		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 		ret = bmi323_set_scale(data, bmi323_iio_to_sensor(chan->type),  

This is OK, but why not check and propagate the error return?

case IIO_CHAN_INFO_SCALE:

		ret = bmi323_iio_to_sensor(chan->type);
		if (ret < 0)
			return ret;

if (!iio_device_claim_direct(indio_dev))
return -EBUSY;

  		ret = bmi323_set_scale(data, ret, val, val2);
		...

And even if we shouldn't hit the error in other case statements, it seems like it would be good practice to still check for error there too.