mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values
@ 2026-10-24 21:44 David Carlier
  2026-10-25 10:17 ` Joshua Crofts
  2026-10-25 20:37 ` Andy Shevchenko
  0 siblings, 2 replies; 5+ messages in thread
From: David Carlier @ 2026-10-24 21:44 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Ariana Lazar, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, David Carlier, stable

mcp47feb02_write_raw() writes the user supplied raw value to the DAC
register without checking it against the chip resolution. The parts
are 8, 10 or 12 bit, and the bits above the resolution are not
implemented, so writing 300 to out_voltage0_raw on an 8-bit part sets
the output to code 44 instead of failing, and -1 is written as 0xffff.

The out-of-range value is also cached in dac_data, so it is written
back on resume and stored to EEPROM by store_eeprom.

Return -EINVAL for values outside [0, 2^resolution - 1].

Found by code inspection. Build tested with W=1 (=m and =y) only;
no hardware was available for runtime testing.

Fixes: bf394cc80369 ("iio: dac: adding support for Microchip MCP47FEB02")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: David Carlier <devnexen@gmail.com>
---
 drivers/iio/dac/mcp47feb02-core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iio/dac/mcp47feb02-core.c b/drivers/iio/dac/mcp47feb02-core.c
index 88bffbab0854..1a0e80b5db87 100644
--- a/drivers/iio/dac/mcp47feb02-core.c
+++ b/drivers/iio/dac/mcp47feb02-core.c
@@ -696,6 +696,9 @@ static int mcp47feb02_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
+		if (val < 0 || val >= BIT(data->chip_features->resolution))
+			return -EINVAL;
+
 		ret = regmap_write(data->regmap, REG_ADDR(ch->address), val);
 		if (ret)
 			return ret;
-- 
2.55.0


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

* Re: [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values
  2026-10-24 21:44 [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values David Carlier
@ 2026-10-25 10:17 ` Joshua Crofts
  2026-10-25 20:37 ` Andy Shevchenko
  1 sibling, 0 replies; 5+ messages in thread
From: Joshua Crofts @ 2026-10-25 10:17 UTC (permalink / raw)
  To: David Carlier
  Cc: Jonathan Cameron, Ariana Lazar, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, stable

On Sat, 24 Oct 2026 22:44:23 +0100
David Carlier <devnexen@gmail.com> wrote:

> mcp47feb02_write_raw() writes the user supplied raw value to the DAC
> register without checking it against the chip resolution. The parts
> are 8, 10 or 12 bit, and the bits above the resolution are not
> implemented, so writing 300 to out_voltage0_raw on an 8-bit part sets
> the output to code 44 instead of failing, and -1 is written as 0xffff.
> 
> The out-of-range value is also cached in dac_data, so it is written
> back on resume and stored to EEPROM by store_eeprom.
> 
> Return -EINVAL for values outside [0, 2^resolution - 1].
> 
> Found by code inspection. Build tested with W=1 (=m and =y) only;
> no hardware was available for runtime testing.

This should go under the ---.

> Fixes: bf394cc80369 ("iio: dac: adding support for Microchip MCP47FEB02")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM

This tag has been deprecated.

Otherwise LGTM.

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards,
Joshua Crofts

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

* Re: [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values
  2026-10-24 21:44 [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values David Carlier
  2026-10-25 10:17 ` Joshua Crofts
@ 2026-10-25 20:37 ` Andy Shevchenko
       [not found]   ` <CA+XhMqx5jusLqo8G2XCY9gy_P=3-jAwTacgzrR4XFyjpagi8dA@mail.gmail.com>
  1 sibling, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-10-25 20:37 UTC (permalink / raw)
  To: David Carlier
  Cc: Jonathan Cameron, Ariana Lazar, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, stable

On Sat, Oct 24, 2026 at 10:44:23PM +0100, David Carlier wrote:
> mcp47feb02_write_raw() writes the user supplied raw value to the DAC
> register without checking it against the chip resolution. The parts
> are 8, 10 or 12 bit, and the bits above the resolution are not
> implemented, so writing 300 to out_voltage0_raw on an 8-bit part sets
> the output to code 44 instead of failing, and -1 is written as 0xffff.
> 
> The out-of-range value is also cached in dac_data, so it is written
> back on resume and stored to EEPROM by store_eeprom.
> 
> Return -EINVAL for values outside [0, 2^resolution - 1].
> 
> Found by code inspection. Build tested with W=1 (=m and =y) only;
> no hardware was available for runtime testing.

...

>  	case IIO_CHAN_INFO_RAW:
> +		if (val < 0 || val >= BIT(data->chip_features->resolution))

If resolution is 32, this will suck. (Yes, this is a bit theoretical case.)

> +			return -EINVAL;

Not sure about this error code. Do other IIO drivers use same in the similar
situation?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values
       [not found]   ` <CA+XhMqx5jusLqo8G2XCY9gy_P=3-jAwTacgzrR4XFyjpagi8dA@mail.gmail.com>
@ 2026-10-25 21:07     ` Andy Shevchenko
       [not found]       ` <CA+XhMqzZZyN80nP2ER3jyTsM9UC2xMRLiqdCsAtp+K=hDbqtQg@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-10-25 21:07 UTC (permalink / raw)
  To: David CARLIER
  Cc: Jonathan Cameron, Ariana Lazar, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, stable

On Sun, Oct 25, 2026 at 08:50:16PM +0000, David CARLIER wrote:
> On Sun, 25 Oct 2026 at 20:37, Andy Shevchenko <andriy.shevchenko@intel.com>
> wrote:
> > On Sat, Oct 24, 2026 at 10:44:23PM +0100, David Carlier wrote:

...

> > >       case IIO_CHAN_INFO_RAW:
> > > +             if (val < 0 || val >= BIT(data->chip_features->resolution))
> >
> > If resolution is 32, this will suck. (Yes, this is a bit theoretical case.)
> >
> 
> Hmm true.
> 
> > > +                     return -EINVAL;
> >
> > Not sure about this error code. Do other IIO drivers use same in the
> > similar situation?
> 
> Yes other drivers for every out-of-range check IIO_CHAN_INFO_RAW.

No doubts. But I asked a different question. Can you answer it?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values
       [not found]       ` <CA+XhMqzZZyN80nP2ER3jyTsM9UC2xMRLiqdCsAtp+K=hDbqtQg@mail.gmail.com>
@ 2026-10-25 21:25         ` Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2026-10-25 21:25 UTC (permalink / raw)
  To: David CARLIER
  Cc: Jonathan Cameron, Ariana Lazar, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, stable

On Sun, Oct 25, 2026 at 09:11:16PM +0000, David CARLIER wrote:
> On Sun, 25 Oct 2026 at 21:07, Andy Shevchenko <andriy.shevchenko@intel.com>
> wrote:
> > On Sun, Oct 25, 2026 at 08:50:16PM +0000, David CARLIER wrote:
> > > On Sun, 25 Oct 2026 at 20:37, Andy Shevchenko <andriy.shevchenko@intel.com>
> > > wrote:
> > > > On Sat, Oct 24, 2026 at 10:44:23PM +0100, David Carlier wrote:

...

> > > > > +                     return -EINVAL;
> > > >
> > > > Not sure about this error code. Do other IIO drivers use same in the
> > > > similar situation?
> > >
> > > Yes other drivers for every out-of-range check IIO_CHAN_INFO_RAW.
> >
> > No doubts. But I asked a different question. Can you answer it?
> 
> I meant in this case the other drivers -EINVAL.

OK, thanks for confirming!

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2026-10-25 21:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-24 21:44 [PATCH] iio: dac: mcp47feb02: reject out-of-range raw values David Carlier
2026-10-25 10:17 ` Joshua Crofts
2026-10-25 20:37 ` Andy Shevchenko
     [not found]   ` <CA+XhMqx5jusLqo8G2XCY9gy_P=3-jAwTacgzrR4XFyjpagi8dA@mail.gmail.com>
2026-10-25 21:07     ` Andy Shevchenko
     [not found]       ` <CA+XhMqzZZyN80nP2ER3jyTsM9UC2xMRLiqdCsAtp+K=hDbqtQg@mail.gmail.com>
2026-10-25 21:25         ` Andy Shevchenko

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

all inboxes | Powered by JetHome®