Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ad4851: fix ad4858 chan pointer handling
@ 2025-05-09 10:16 Antoniu Miclaus
  2025-05-09 12:47 ` Nuno Sá
  0 siblings, 1 reply; 3+ messages in thread
From: Antoniu Miclaus @ 2025-05-09 10:16 UTC (permalink / raw)
  To: jic23, linux-iio, linux-kernel; +Cc: Antoniu Miclaus

The pointer returned from ad4851_parse_channels_common() is incremented
internally as each channel is populated. In ad4858_parse_channels(),
the same pointer was further incremented while setting ext_scan_type
fields for each channel. This resulted in indio_dev->channels being set
to a pointer past the end of the allocated array, potentially causing
memory corruption or undefined behavior.

Fix this by iterating over the channels using an explicit index instead
of incrementing the pointer. This preserves the original base pointer
and ensures all channel metadata is set correctly.

Fixes: 6250803fe2ec ("iio: adc: ad4851: add ad485x driver")
Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
---
 drivers/iio/adc/ad4851.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/adc/ad4851.c b/drivers/iio/adc/ad4851.c
index 98ebc853db79..f1d2e2896f2a 100644
--- a/drivers/iio/adc/ad4851.c
+++ b/drivers/iio/adc/ad4851.c
@@ -1034,7 +1034,7 @@ static int ad4858_parse_channels(struct iio_dev *indio_dev)
 	struct device *dev = &st->spi->dev;
 	struct iio_chan_spec *ad4851_channels;
 	const struct iio_chan_spec ad4851_chan = AD4858_IIO_CHANNEL;
-	int ret;
+	int ret, i = 0;
 
 	ret = ad4851_parse_channels_common(indio_dev, &ad4851_channels,
 					   ad4851_chan);
@@ -1042,15 +1042,15 @@ static int ad4858_parse_channels(struct iio_dev *indio_dev)
 		return ret;
 
 	device_for_each_child_node_scoped(dev, child) {
-		ad4851_channels->has_ext_scan_type = 1;
+		ad4851_channels[i].has_ext_scan_type = 1;
 		if (fwnode_property_read_bool(child, "bipolar")) {
-			ad4851_channels->ext_scan_type = ad4851_scan_type_20_b;
-			ad4851_channels->num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_b);
+			ad4851_channels[i].ext_scan_type = ad4851_scan_type_20_b;
+			ad4851_channels[i].num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_b);
 		} else {
-			ad4851_channels->ext_scan_type = ad4851_scan_type_20_u;
-			ad4851_channels->num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_u);
+			ad4851_channels[i].ext_scan_type = ad4851_scan_type_20_u;
+			ad4851_channels[i].num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_u);
 		}
-		ad4851_channels++;
+		i++;
 	}
 
 	indio_dev->channels = ad4851_channels;
-- 
2.49.0


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

* Re: [PATCH] iio: adc: ad4851: fix ad4858 chan pointer handling
  2025-05-09 10:16 [PATCH] iio: adc: ad4851: fix ad4858 chan pointer handling Antoniu Miclaus
@ 2025-05-09 12:47 ` Nuno Sá
  2025-05-11 13:13   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Nuno Sá @ 2025-05-09 12:47 UTC (permalink / raw)
  To: Antoniu Miclaus, jic23, linux-iio, linux-kernel

On Fri, 2025-05-09 at 13:16 +0300, Antoniu Miclaus wrote:
> The pointer returned from ad4851_parse_channels_common() is incremented
> internally as each channel is populated. In ad4858_parse_channels(),
> the same pointer was further incremented while setting ext_scan_type
> fields for each channel. This resulted in indio_dev->channels being set
> to a pointer past the end of the allocated array, potentially causing
> memory corruption or undefined behavior.
> 
> Fix this by iterating over the channels using an explicit index instead
> of incrementing the pointer. This preserves the original base pointer
> and ensures all channel metadata is set correctly.
> 
> Fixes: 6250803fe2ec ("iio: adc: ad4851: add ad485x driver")
> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
> ---

LGTM

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

>  drivers/iio/adc/ad4851.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad4851.c b/drivers/iio/adc/ad4851.c
> index 98ebc853db79..f1d2e2896f2a 100644
> --- a/drivers/iio/adc/ad4851.c
> +++ b/drivers/iio/adc/ad4851.c
> @@ -1034,7 +1034,7 @@ static int ad4858_parse_channels(struct iio_dev
> *indio_dev)
>  	struct device *dev = &st->spi->dev;
>  	struct iio_chan_spec *ad4851_channels;
>  	const struct iio_chan_spec ad4851_chan = AD4858_IIO_CHANNEL;
> -	int ret;
> +	int ret, i = 0;
>  
>  	ret = ad4851_parse_channels_common(indio_dev, &ad4851_channels,
>  					   ad4851_chan);
> @@ -1042,15 +1042,15 @@ static int ad4858_parse_channels(struct iio_dev
> *indio_dev)
>  		return ret;
>  
>  	device_for_each_child_node_scoped(dev, child) {
> -		ad4851_channels->has_ext_scan_type = 1;
> +		ad4851_channels[i].has_ext_scan_type = 1;
>  		if (fwnode_property_read_bool(child, "bipolar")) {
> -			ad4851_channels->ext_scan_type =
> ad4851_scan_type_20_b;
> -			ad4851_channels->num_ext_scan_type =
> ARRAY_SIZE(ad4851_scan_type_20_b);
> +			ad4851_channels[i].ext_scan_type =
> ad4851_scan_type_20_b;
> +			ad4851_channels[i].num_ext_scan_type =
> ARRAY_SIZE(ad4851_scan_type_20_b);
>  		} else {
> -			ad4851_channels->ext_scan_type =
> ad4851_scan_type_20_u;
> -			ad4851_channels->num_ext_scan_type =
> ARRAY_SIZE(ad4851_scan_type_20_u);
> +			ad4851_channels[i].ext_scan_type =
> ad4851_scan_type_20_u;
> +			ad4851_channels[i].num_ext_scan_type =
> ARRAY_SIZE(ad4851_scan_type_20_u);
>  		}
> -		ad4851_channels++;
> +		i++;
>  	}
>  
>  	indio_dev->channels = ad4851_channels;

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

* Re: [PATCH] iio: adc: ad4851: fix ad4858 chan pointer handling
  2025-05-09 12:47 ` Nuno Sá
@ 2025-05-11 13:13   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2025-05-11 13:13 UTC (permalink / raw)
  To: Nuno Sá; +Cc: Antoniu Miclaus, linux-iio, linux-kernel

On Fri, 09 May 2025 13:47:41 +0100
Nuno Sá <noname.nuno@gmail.com> wrote:

> On Fri, 2025-05-09 at 13:16 +0300, Antoniu Miclaus wrote:
> > The pointer returned from ad4851_parse_channels_common() is incremented
> > internally as each channel is populated. In ad4858_parse_channels(),
> > the same pointer was further incremented while setting ext_scan_type
> > fields for each channel. This resulted in indio_dev->channels being set
> > to a pointer past the end of the allocated array, potentially causing
> > memory corruption or undefined behavior.
> > 
> > Fix this by iterating over the channels using an explicit index instead
> > of incrementing the pointer. This preserves the original base pointer
> > and ensures all channel metadata is set correctly.
> > 
> > Fixes: 6250803fe2ec ("iio: adc: ad4851: add ad485x driver")
> > Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
> > ---  
> 
> LGTM
Applied to the fixes-togreg branch of iio.git.

Thanks

Jonathan

> 
> Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> 
> >  drivers/iio/adc/ad4851.c | 14 +++++++-------
> >  1 file changed, 7 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/ad4851.c b/drivers/iio/adc/ad4851.c
> > index 98ebc853db79..f1d2e2896f2a 100644
> > --- a/drivers/iio/adc/ad4851.c
> > +++ b/drivers/iio/adc/ad4851.c
> > @@ -1034,7 +1034,7 @@ static int ad4858_parse_channels(struct iio_dev
> > *indio_dev)
> >  	struct device *dev = &st->spi->dev;
> >  	struct iio_chan_spec *ad4851_channels;
> >  	const struct iio_chan_spec ad4851_chan = AD4858_IIO_CHANNEL;
> > -	int ret;
> > +	int ret, i = 0;
> >  
> >  	ret = ad4851_parse_channels_common(indio_dev, &ad4851_channels,
> >  					   ad4851_chan);
> > @@ -1042,15 +1042,15 @@ static int ad4858_parse_channels(struct iio_dev
> > *indio_dev)
> >  		return ret;
> >  
> >  	device_for_each_child_node_scoped(dev, child) {
> > -		ad4851_channels->has_ext_scan_type = 1;
> > +		ad4851_channels[i].has_ext_scan_type = 1;
> >  		if (fwnode_property_read_bool(child, "bipolar")) {
> > -			ad4851_channels->ext_scan_type =
> > ad4851_scan_type_20_b;
> > -			ad4851_channels->num_ext_scan_type =
> > ARRAY_SIZE(ad4851_scan_type_20_b);
> > +			ad4851_channels[i].ext_scan_type =
> > ad4851_scan_type_20_b;
> > +			ad4851_channels[i].num_ext_scan_type =
> > ARRAY_SIZE(ad4851_scan_type_20_b);
> >  		} else {
> > -			ad4851_channels->ext_scan_type =
> > ad4851_scan_type_20_u;
> > -			ad4851_channels->num_ext_scan_type =
> > ARRAY_SIZE(ad4851_scan_type_20_u);
> > +			ad4851_channels[i].ext_scan_type =
> > ad4851_scan_type_20_u;
> > +			ad4851_channels[i].num_ext_scan_type =
> > ARRAY_SIZE(ad4851_scan_type_20_u);
> >  		}
> > -		ad4851_channels++;
> > +		i++;
> >  	}
> >  
> >  	indio_dev->channels = ad4851_channels;  
> 


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

end of thread, other threads:[~2025-05-11 13:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-09 10:16 [PATCH] iio: adc: ad4851: fix ad4858 chan pointer handling Antoniu Miclaus
2025-05-09 12:47 ` Nuno Sá
2025-05-11 13:13   ` Jonathan Cameron

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