linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr()
@ 2021-10-11 11:40 Teng Qi
  2021-10-11 12:22 ` Lorenzo Bianconi
  0 siblings, 1 reply; 3+ messages in thread
From: Teng Qi @ 2021-10-11 11:40 UTC (permalink / raw)
  To: lorenzo.bianconi83, jic23, lars
  Cc: linux-iio, linux-kernel, islituo, baijiaju1990, Teng Qi, TOTE Robot

The length of hw->settings->odr_table is 2 and ref_sensor->id is an enum
variable whose value is between 0 and 5.
However, the value ST_LSM6DSX_ID_MAX (i.e. 5) is not catched properly in
 switch (sensor->id) {

If ref_sensor->id is ST_LSM6DSX_ID_MAX, an array overflow will ocurrs in
function st_lsm6dsx_check_odr():
  odr_table = &sensor->hw->settings->odr_table[sensor->id];

and in function st_lsm6dsx_set_odr():
  reg = &hw->settings->odr_table[ref_sensor->id].reg;

To fix this array overflow, handle ST_LSM6DSX_ID_GYRO explicitly and 
return -EINVAL for the default case.

Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>

---
v2:
* explicitly handle ST_LSM6DSX_ID_GYRO and return -EINVAL for the default
case instead of adding an if statement behind the switch statement.
  Thank Lars-Peter Clausen for helpful and friendly advice.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
index db45f1fc0b81..8dbf744c5651 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
@@ -1279,6 +1279,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
 	int err;
 
 	switch (sensor->id) {
+	case ST_LSM6DSX_ID_GYRO:
+		break;
 	case ST_LSM6DSX_ID_EXT0:
 	case ST_LSM6DSX_ID_EXT1:
 	case ST_LSM6DSX_ID_EXT2:
@@ -1304,8 +1306,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
 		}
 		break;
 	}
-	default:
-		break;
+	default: /* should never occur */
+		return -EINVAL;
 	}
 
 	if (req_odr > 0) {
-- 
2.25.1


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

* Re: [PATCH v2] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr()
  2021-10-11 11:40 [PATCH v2] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr() Teng Qi
@ 2021-10-11 12:22 ` Lorenzo Bianconi
  2021-10-17 13:28   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Lorenzo Bianconi @ 2021-10-11 12:22 UTC (permalink / raw)
  To: Teng Qi
  Cc: lorenzo.bianconi83, jic23, lars, linux-iio, linux-kernel,
	islituo, baijiaju1990, TOTE Robot

[-- Attachment #1: Type: text/plain, Size: 1932 bytes --]

> The length of hw->settings->odr_table is 2 and ref_sensor->id is an enum
> variable whose value is between 0 and 5.
> However, the value ST_LSM6DSX_ID_MAX (i.e. 5) is not catched properly in
>  switch (sensor->id) {
> 
> If ref_sensor->id is ST_LSM6DSX_ID_MAX, an array overflow will ocurrs in
> function st_lsm6dsx_check_odr():
>   odr_table = &sensor->hw->settings->odr_table[sensor->id];
> 
> and in function st_lsm6dsx_set_odr():
>   reg = &hw->settings->odr_table[ref_sensor->id].reg;
> 
> To fix this array overflow, handle ST_LSM6DSX_ID_GYRO explicitly and 
> return -EINVAL for the default case.
> 
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>

Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>
> 
> ---
> v2:
> * explicitly handle ST_LSM6DSX_ID_GYRO and return -EINVAL for the default
> case instead of adding an if statement behind the switch statement.
>   Thank Lars-Peter Clausen for helpful and friendly advice.
> 
> ---
>  drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> index db45f1fc0b81..8dbf744c5651 100644
> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> @@ -1279,6 +1279,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
>  	int err;
>  
>  	switch (sensor->id) {
> +	case ST_LSM6DSX_ID_GYRO:
> +		break;
>  	case ST_LSM6DSX_ID_EXT0:
>  	case ST_LSM6DSX_ID_EXT1:
>  	case ST_LSM6DSX_ID_EXT2:
> @@ -1304,8 +1306,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
>  		}
>  		break;
>  	}
> -	default:
> -		break;
> +	default: /* should never occur */
> +		return -EINVAL;
>  	}
>  
>  	if (req_odr > 0) {
> -- 
> 2.25.1
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

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

* Re: [PATCH v2] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr()
  2021-10-11 12:22 ` Lorenzo Bianconi
@ 2021-10-17 13:28   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2021-10-17 13:28 UTC (permalink / raw)
  To: Lorenzo Bianconi
  Cc: Teng Qi, lorenzo.bianconi83, lars, linux-iio, linux-kernel,
	islituo, baijiaju1990, TOTE Robot

On Mon, 11 Oct 2021 14:22:19 +0200
Lorenzo Bianconi <lorenzo@kernel.org> wrote:

> > The length of hw->settings->odr_table is 2 and ref_sensor->id is an enum
> > variable whose value is between 0 and 5.
> > However, the value ST_LSM6DSX_ID_MAX (i.e. 5) is not catched properly in
> >  switch (sensor->id) {
> > 
> > If ref_sensor->id is ST_LSM6DSX_ID_MAX, an array overflow will ocurrs in
> > function st_lsm6dsx_check_odr():
> >   odr_table = &sensor->hw->settings->odr_table[sensor->id];
> > 
> > and in function st_lsm6dsx_set_odr():
> >   reg = &hw->settings->odr_table[ref_sensor->id].reg;
> > 
> > To fix this array overflow, handle ST_LSM6DSX_ID_GYRO explicitly and 
> > return -EINVAL for the default case.
> > 
> > Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> > Signed-off-by: Teng Qi <starmiku1207184332@gmail.com>  
> 
> Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>

Hi Teng Qi,

As discussed previously this isn't technically a 'fix' as the default case can
never actually occur, so I've modified the patch description a tiny bit make
that clear.  It's a good change, but we probably don't want to back port it to
older kernels and most things that claim to be 'fixes' are back ported.

Applied to the iio-togreg branch of iio.git and pushed out as testing for 0-day
to see if it can find anything we missed.

Always good to see a new robot finding suspicious bits of code like this :)

Thanks,

Jonathan
 
> > 
> > ---
> > v2:
> > * explicitly handle ST_LSM6DSX_ID_GYRO and return -EINVAL for the default
> > case instead of adding an if statement behind the switch statement.
> >   Thank Lars-Peter Clausen for helpful and friendly advice.
> > 
> > ---
> >  drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > index db45f1fc0b81..8dbf744c5651 100644
> > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
> > @@ -1279,6 +1279,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
> >  	int err;
> >  
> >  	switch (sensor->id) {
> > +	case ST_LSM6DSX_ID_GYRO:
> > +		break;
> >  	case ST_LSM6DSX_ID_EXT0:
> >  	case ST_LSM6DSX_ID_EXT1:
> >  	case ST_LSM6DSX_ID_EXT2:
> > @@ -1304,8 +1306,8 @@ st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
> >  		}
> >  		break;
> >  	}
> > -	default:
> > -		break;
> > +	default: /* should never occur */
> > +		return -EINVAL;
> >  	}
> >  
> >  	if (req_odr > 0) {
> > -- 
> > 2.25.1
> >   


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

end of thread, other threads:[~2021-10-17 13:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-11 11:40 [PATCH v2] iio: imu: st_lsm6dsx: Fix an array overflow in st_lsm6dsx_set_odr() Teng Qi
2021-10-11 12:22 ` Lorenzo Bianconi
2021-10-17 13:28   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).