Skip to contentSkip to conversation

iio: chemical: scd30: Prevent potential divide-by-zero error

PATCH Superseded Maxwell Doose 4 participants 9 messages May 9 – May 11

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: chemical: scd30: Prevent potential divide-by-zero error

In scd30_read_raw, the current value of tmp in the IIO_CHAN_INFO_SAMP_FREQ case is unchecked. Add checking to see if the value we got was 0 to prevent a divide-by-zero error.

Fixes 64b3d8b1b0f5 ("iio: chemical: scd30: add core driver")
---
 drivers/iio/chemical/scd30_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/chemical/scd30_core.c +7 -0
index be8c055be184..3e2bb6f4e42c 100644
@@ -237,6 +237,13 @@ static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const
 		if (ret)
 			return ret;
 
+		/* Likely only taken if something really strange happens */
+		if (!tmp) {
+			dev_err(&indio_dev->dev,
+				"Invalid measurement interval 0 received\n");
+			return -EIO;
+		}
+
 		*val = 0;
 		*val2 = 1000000000 / tmp;
 		return IIO_VAL_INT_PLUS_NANO;
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 15 lines · Maxwell Doose

In scd30_read_raw, the current value of tmp in the IIO_CHAN_INFO_SAMP_FREQ case is unchecked. Add checking to see if the value we got was 0 to prevent a divide-by-zero error.

Fixes 64b3d8b1b0f5 ("iio: chemical: scd30: add core driver")
---
 drivers/iio/chemical/scd30_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/chemical/scd30_core.c +0 -0
index be8c055be184..3e2bb6f4e42c 100644

drivers/iio/chemical/scd30_core.c static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const · line 240

Quoted 4 lines
drivers/iio/chemical/scd30_core.c +1 -0
@@ -237,6 +237,13 @@ static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const
 		if (ret)
 			return ret;
 
+		/* Likely only taken if something really strange happens */

Instead of saying "strange" I would mention a possible hardware failure to justify the EIO return. I'm assuming this is reading the value over SPI or I2C?

Quoted 9 lines
drivers/iio/chemical/scd30_core.c +6 -0
+		if (!tmp) {
+			dev_err(&indio_dev->dev,
+				"Invalid measurement interval 0 received\n");
+			return -EIO;
+		}
+
 		*val = 0;
 		*val2 = 1000000000 / tmp;
 		return IIO_VAL_INT_PLUS_NANO;
#2 lore raw
Quoted 20 lines · David Lechner
Quoted 18 lines · Maxwell Doose

In scd30_read_raw, the current value of tmp in the IIO_CHAN_INFO_SAMP_FREQ case is unchecked. Add checking to see if the value we got was 0 to prevent a divide-by-zero error.

Fixes 64b3d8b1b0f5 ("iio: chemical: scd30: add core driver")
---
 drivers/iio/chemical/scd30_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/chemical/scd30_core.c +0 -0
index be8c055be184..3e2bb6f4e42c 100644
@@ -237,6 +237,13 @@ static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const
              if (ret)
                      return ret;
 
Quoted 6 lines

drivers/iio/chemical/scd30_core.c static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const · line 240

Quoted 1 line
drivers/iio/chemical/scd30_core.c +1 -0
+             /* Likely only taken if something really strange happens */

Instead of saying "strange" I would mention a possible hardware failure to justify the EIO return. I'm assuming this is reading the value over SPI or I2C?

Yes, we're reading over either SPI or I2C, the sensor supports both. Hardware failure is going to be the most likely cause of this path being taken, but I could also see some sort of noise on the bus being the cause. Perhaps we change it to:

/* Likely taken if hardware is failing or noise on bus */

best regards,
max

reviewer
#3 lore raw
Quoted 33 lines · Maxwell Doose
Quoted 20 lines · David Lechner
Quoted 18 lines · Maxwell Doose

In scd30_read_raw, the current value of tmp in the IIO_CHAN_INFO_SAMP_FREQ case is unchecked. Add checking to see if the value we got was 0 to prevent a divide-by-zero error.

Fixes 64b3d8b1b0f5 ("iio: chemical: scd30: add core driver")
---
 drivers/iio/chemical/scd30_core.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/chemical/scd30_core.c +0 -0
index be8c055be184..3e2bb6f4e42c 100644
@@ -237,6 +237,13 @@ static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const
              if (ret)
                      return ret;
 
Quoted 6 lines

drivers/iio/chemical/scd30_core.c static int scd30_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const · line 240

Quoted 1 line
drivers/iio/chemical/scd30_core.c +1 -0
+             /* Likely only taken if something really strange happens */

Instead of saying "strange" I would mention a possible hardware failure to justify the EIO return. I'm assuming this is reading the value over SPI or I2C?

Yes, we're reading over either SPI or I2C, the sensor supports both. Hardware failure is going to be the most likely cause of this path being taken, but I could also see some sort of noise on the bus being the cause. Perhaps we change it to:

Quoted 4 lines

/* Likely taken if hardware is failing or noise on bus */

best regards,
max

Sounds OK.

A more verbose alternative:

/*
* Value of 0 is unexpected, but could happen, e.g. due to
* hardware failure.
*/

#4 lore raw
Quoted 11 lines · David Lechner
Quoted 9 lines · Maxwell Doose

Yes, we're reading over either SPI or I2C, the sensor supports both. Hardware failure is going to be the most likely cause of this path being taken, but I could also see some sort of noise on the bus being the cause. Perhaps we change it to:

/* Likely taken if hardware is failing or noise on bus */

best regards,
max

Quoted 8 lines

Sounds OK.

A more verbose alternative:

/*
* Value of 0 is unexpected, but could happen, e.g. due to
* hardware failure.
*/

Also works for me, I'm impartial to either. Perhaps we can see if Jonathan is willing to change it during merge or if I'll have to go back and change it in a v2.

best regards,
max

#5 lore raw
Quoted 22 lines · Maxwell Doose
Quoted 11 lines · David Lechner
Quoted 9 lines · Maxwell Doose

Yes, we're reading over either SPI or I2C, the sensor supports both. Hardware failure is going to be the most likely cause of this path being taken, but I could also see some sort of noise on the bus being the cause. Perhaps we change it to:

/* Likely taken if hardware is failing or noise on bus */

best regards,
max

Quoted 8 lines

Sounds OK.

A more verbose alternative:

/*
* Value of 0 is unexpected, but could happen, e.g. due to
* hardware failure.
*/

Quoted 6 lines

Also works for me, I'm impartial to either. Perhaps we can see if Jonathan is willing to change it during merge or if I'll have to go back and change it in a v2.

best regards,
max

Also maybe we update to use dev_err_ratelimited(), sashiko has a good point here:

https://sashiko.dev/#/patchset/20260509211921.84969-1-m32285159%40gmail.com

best regards,
max

maintainer
#6 lore raw
Quoted 27 lines · On Sat, 9 May 2026 19:47:53 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 26 lines · Maxwell Doose
Quoted 11 lines · David Lechner
Quoted 9 lines · Maxwell Doose

Yes, we're reading over either SPI or I2C, the sensor supports both. Hardware failure is going to be the most likely cause of this path being taken, but I could also see some sort of noise on the bus being the cause. Perhaps we change it to:

/* Likely taken if hardware is failing or noise on bus */

best regards,
max

Quoted 8 lines

Sounds OK.

A more verbose alternative:

/*
* Value of 0 is unexpected, but could happen, e.g. due to
* hardware failure.
*/

Also works for me, I'm impartial to either. Perhaps we can see if Jonathan is willing to change it during merge or if I'll have to go back and change it in a v2.

Quoted 8 lines
Quoted 2 lines

best regards,
max

Also maybe we update to use dev_err_ratelimited(), sashiko has a good point here:

https://sashiko.dev/#/patchset/20260509211921.84969-1-m32285159%40gmail.com

Do we care for something that is going to be a very rare hardware failure? That's one interesting attack vector. I suppose maybe they have another exploit that lets them trip the scd30 into an odd state.

Ah well, doesn't do much harm to rate limit it I guess.

Jonathan

Quoted 2 lines

best regards,
max

#7 lore raw
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Sat, 9 May 2026 19:47:53 -0500 Maxwell Doose <[email protected]> wrote:

On Sat, May 9, 2026 at 4:53 PM Maxwell Doose <[email protected]> wrote:

...

Quoted 6 lines
Quoted 5 lines

Also maybe we update to use dev_err_ratelimited(), sashiko has a good point here:

https://sashiko.dev/#/patchset/20260509211921.84969-1-m32285159%40gmail.com

Do we care for something that is going to be a very rare hardware failure?

If it happens rarely, ratelimit is no-op (no change from regular printing), the only difference if (and I think this is actually what may happen IRL) there is an intermittent failure of HW and it starts spamming logs like a hell.

Quoted 4 lines

That's one interesting attack vector. I suppose maybe they have another exploit that lets them trip the scd30 into an odd state.

Ah well, doesn't do much harm to rate limit it I guess.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Sat, 9 May 2026 19:47:53 -0500 Maxwell Doose <[email protected]> wrote:

On Sat, May 9, 2026 at 4:53 PM Maxwell Doose <[email protected]> wrote:

...

Quoted 3 lines
Quoted 3 lines

Also maybe we update to use dev_err_ratelimited(), sashiko has a good point here:

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

https://sashiko.dev/#/patchset/20260509211921.84969-1-m32285159%40gmail.com

Do we care for something that is going to be a very rare hardware failure?

If it happens rarely, ratelimit is no-op (no change from regular printing), the only difference if (and I think this is actually what may happen IRL) there is an intermittent failure of HW and it starts spamming logs like a hell.

Yeah, I believe that sashiko was pointing out that if we have a device spamming a bunch of 0s then dmesg would get flooded with errors.

best regards,
max

Quoted 10 lines
Quoted 4 lines

That's one interesting attack vector. I suppose maybe they have another exploit that lets them trip the scd30 into an odd state.

Ah well, doesn't do much harm to rate limit it I guess.

--
With Best Regards,
Andy Shevchenko