linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field
@ 2021-12-15 23:25 Kees Cook
  2021-12-16 12:36 ` Jonathan Cameron
  2022-02-04  4:29 ` Yury Norov
  0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2021-12-15 23:25 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Kees Cook, Lars-Peter Clausen, Maxime Coquelin, Alexandre Torgue,
	Yury Norov, linux-kernel, linux-iio, linux-stm32,
	linux-arm-kernel, linux-hardening

The find.h APIs are designed to be used only on unsigned long arguments.
This can technically result in a over-read, but it is harmless in this
case. Regardless, fix it to avoid the warning seen under -Warray-bounds,
which we'd like to enable globally:

In file included from ./include/linux/bitmap.h:9,
                 from ./include/linux/cpumask.h:12,
                 from ./arch/x86/include/asm/cpumask.h:5,
                 from ./arch/x86/include/asm/msr.h:11,
                 from ./arch/x86/include/asm/processor.h:22,
                 from ./arch/x86/include/asm/cpufeature.h:5,
                 from ./arch/x86/include/asm/thread_info.h:53,
                 from ./include/linux/thread_info.h:60,
                 from ./arch/x86/include/asm/preempt.h:7,
                 from ./include/linux/preempt.h:78,
                 from ./include/linux/spinlock.h:55,
                 from ./include/linux/swait.h:7,
                 from ./include/linux/completion.h:12,
                 from drivers/iio/adc/stmpe-adc.c:10:
drivers/iio/adc/stmpe-adc.c: In function 'stmpe_adc_probe':
./include/linux/find.h:98:23: warning: array subscript 'long unsigned int[0]' is partly outside array bounds of 'u32[1]' {aka 'unsigned int[1]'} [-Warray-bounds]
   98 |                 val = *addr | ~GENMASK(size - 1, offset);
      |                       ^~~~~
drivers/iio/adc/stmpe-adc.c:258:13: note: while referencing 'norequest_mask'
  258 |         u32 norequest_mask = 0;
      |             ^~~~~~~~~~~~~~

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/iio/adc/stmpe-adc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/adc/stmpe-adc.c b/drivers/iio/adc/stmpe-adc.c
index fba659bfdb40..d2d405388499 100644
--- a/drivers/iio/adc/stmpe-adc.c
+++ b/drivers/iio/adc/stmpe-adc.c
@@ -256,6 +256,7 @@ static int stmpe_adc_probe(struct platform_device *pdev)
 	struct stmpe_adc *info;
 	struct device_node *np;
 	u32 norequest_mask = 0;
+	unsigned long bits;
 	int irq_temp, irq_adc;
 	int num_chan = 0;
 	int i = 0;
@@ -309,8 +310,8 @@ static int stmpe_adc_probe(struct platform_device *pdev)
 
 	of_property_read_u32(np, "st,norequest-mask", &norequest_mask);
 
-	for_each_clear_bit(i, (unsigned long *) &norequest_mask,
-			   (STMPE_ADC_LAST_NR + 1)) {
+	bits = norequest_mask;
+	for_each_clear_bit(i, &bits, (STMPE_ADC_LAST_NR + 1)) {
 		stmpe_adc_voltage_chan(&info->stmpe_adc_iio_channels[num_chan], i);
 		num_chan++;
 	}
-- 
2.30.2


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

* Re: [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field
  2021-12-15 23:25 [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field Kees Cook
@ 2021-12-16 12:36 ` Jonathan Cameron
  2022-02-04  4:29 ` Yury Norov
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2021-12-16 12:36 UTC (permalink / raw)
  To: Kees Cook
  Cc: Lars-Peter Clausen, Maxime Coquelin, Alexandre Torgue,
	Yury Norov, linux-kernel, linux-iio, linux-stm32,
	linux-arm-kernel, linux-hardening

On Wed, 15 Dec 2021 15:25:13 -0800
Kees Cook <keescook@chromium.org> wrote:

> The find.h APIs are designed to be used only on unsigned long arguments.
> This can technically result in a over-read, but it is harmless in this
> case. Regardless, fix it to avoid the warning seen under -Warray-bounds,
> which we'd like to enable globally:
> 
> In file included from ./include/linux/bitmap.h:9,
>                  from ./include/linux/cpumask.h:12,
>                  from ./arch/x86/include/asm/cpumask.h:5,
>                  from ./arch/x86/include/asm/msr.h:11,
>                  from ./arch/x86/include/asm/processor.h:22,
>                  from ./arch/x86/include/asm/cpufeature.h:5,
>                  from ./arch/x86/include/asm/thread_info.h:53,
>                  from ./include/linux/thread_info.h:60,
>                  from ./arch/x86/include/asm/preempt.h:7,
>                  from ./include/linux/preempt.h:78,
>                  from ./include/linux/spinlock.h:55,
>                  from ./include/linux/swait.h:7,
>                  from ./include/linux/completion.h:12,
>                  from drivers/iio/adc/stmpe-adc.c:10:
> drivers/iio/adc/stmpe-adc.c: In function 'stmpe_adc_probe':
> ./include/linux/find.h:98:23: warning: array subscript 'long unsigned int[0]' is partly outside array bounds of 'u32[1]' {aka 'unsigned int[1]'} [-Warray-bounds]
>    98 |                 val = *addr | ~GENMASK(size - 1, offset);
>       |                       ^~~~~
> drivers/iio/adc/stmpe-adc.c:258:13: note: while referencing 'norequest_mask'
>   258 |         u32 norequest_mask = 0;
>       |             ^~~~~~~~~~~~~~
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>

Applied to the togreg branch of iio.git and pushed out as testing to let
0-day have a first poke at it.

I took the view this one was trivial, but if anyone else wants to add
tags there will be a few days before this goes out in a form I'm not happy
to rebase.

Thanks,

Jonathan

> ---
>  drivers/iio/adc/stmpe-adc.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/stmpe-adc.c b/drivers/iio/adc/stmpe-adc.c
> index fba659bfdb40..d2d405388499 100644
> --- a/drivers/iio/adc/stmpe-adc.c
> +++ b/drivers/iio/adc/stmpe-adc.c
> @@ -256,6 +256,7 @@ static int stmpe_adc_probe(struct platform_device *pdev)
>  	struct stmpe_adc *info;
>  	struct device_node *np;
>  	u32 norequest_mask = 0;
> +	unsigned long bits;
>  	int irq_temp, irq_adc;
>  	int num_chan = 0;
>  	int i = 0;
> @@ -309,8 +310,8 @@ static int stmpe_adc_probe(struct platform_device *pdev)
>  
>  	of_property_read_u32(np, "st,norequest-mask", &norequest_mask);
>  
> -	for_each_clear_bit(i, (unsigned long *) &norequest_mask,
> -			   (STMPE_ADC_LAST_NR + 1)) {
> +	bits = norequest_mask;
> +	for_each_clear_bit(i, &bits, (STMPE_ADC_LAST_NR + 1)) {
>  		stmpe_adc_voltage_chan(&info->stmpe_adc_iio_channels[num_chan], i);
>  		num_chan++;
>  	}


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

* Re: [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field
  2021-12-15 23:25 [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field Kees Cook
  2021-12-16 12:36 ` Jonathan Cameron
@ 2022-02-04  4:29 ` Yury Norov
  2022-02-05 13:38   ` David Laight
  1 sibling, 1 reply; 4+ messages in thread
From: Yury Norov @ 2022-02-04  4:29 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jonathan Cameron, Lars-Peter Clausen, Maxime Coquelin,
	Alexandre Torgue, linux-kernel, linux-iio, linux-stm32,
	linux-arm-kernel, linux-hardening

On Wed, Dec 15, 2021 at 03:25:13PM -0800, Kees Cook wrote:
> The find.h APIs are designed to be used only on unsigned long arguments.
> This can technically result in a over-read, but it is harmless in this
> case. Regardless, fix it to avoid the warning seen under -Warray-bounds,
> which we'd like to enable globally:
> 
> In file included from ./include/linux/bitmap.h:9,
>                  from ./include/linux/cpumask.h:12,
>                  from ./arch/x86/include/asm/cpumask.h:5,
>                  from ./arch/x86/include/asm/msr.h:11,
>                  from ./arch/x86/include/asm/processor.h:22,
>                  from ./arch/x86/include/asm/cpufeature.h:5,
>                  from ./arch/x86/include/asm/thread_info.h:53,
>                  from ./include/linux/thread_info.h:60,
>                  from ./arch/x86/include/asm/preempt.h:7,
>                  from ./include/linux/preempt.h:78,
>                  from ./include/linux/spinlock.h:55,
>                  from ./include/linux/swait.h:7,
>                  from ./include/linux/completion.h:12,
>                  from drivers/iio/adc/stmpe-adc.c:10:
> drivers/iio/adc/stmpe-adc.c: In function 'stmpe_adc_probe':
> ./include/linux/find.h:98:23: warning: array subscript 'long unsigned int[0]' is partly outside array bounds of 'u32[1]' {aka 'unsigned int[1]'} [-Warray-bounds]
>    98 |                 val = *addr | ~GENMASK(size - 1, offset);
>       |                       ^~~~~
> drivers/iio/adc/stmpe-adc.c:258:13: note: while referencing 'norequest_mask'
>   258 |         u32 norequest_mask = 0;
>       |             ^~~~~~~~~~~~~~
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  drivers/iio/adc/stmpe-adc.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/stmpe-adc.c b/drivers/iio/adc/stmpe-adc.c
> index fba659bfdb40..d2d405388499 100644
> --- a/drivers/iio/adc/stmpe-adc.c
> +++ b/drivers/iio/adc/stmpe-adc.c
> @@ -256,6 +256,7 @@ static int stmpe_adc_probe(struct platform_device *pdev)
>  	struct stmpe_adc *info;
>  	struct device_node *np;
>  	u32 norequest_mask = 0;
> +	unsigned long bits;
>  	int irq_temp, irq_adc;
>  	int num_chan = 0;
>  	int i = 0;
> @@ -309,8 +310,8 @@ static int stmpe_adc_probe(struct platform_device *pdev)
>  
>  	of_property_read_u32(np, "st,norequest-mask", &norequest_mask);
>  
> -	for_each_clear_bit(i, (unsigned long *) &norequest_mask,
> -			   (STMPE_ADC_LAST_NR + 1)) {
> +	bits = norequest_mask;

This would not work on 64-bit BE architecture. It should use bitmap_from_arr32()

> +	for_each_clear_bit(i, &bits, (STMPE_ADC_LAST_NR + 1)) {
>  		stmpe_adc_voltage_chan(&info->stmpe_adc_iio_channels[num_chan], i);
>  		num_chan++;
>  	}
> -- 
> 2.30.2

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

* RE: [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field
  2022-02-04  4:29 ` Yury Norov
@ 2022-02-05 13:38   ` David Laight
  0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2022-02-05 13:38 UTC (permalink / raw)
  To: 'Yury Norov', Kees Cook
  Cc: Jonathan Cameron, Lars-Peter Clausen, Maxime Coquelin,
	Alexandre Torgue, linux-kernel, linux-iio, linux-stm32,
	linux-arm-kernel, linux-hardening

From: Yury Norov
> Sent: 04 February 2022 04:30
> 
> On Wed, Dec 15, 2021 at 03:25:13PM -0800, Kees Cook wrote:
> > The find.h APIs are designed to be used only on unsigned long arguments.
> > This can technically result in a over-read, but it is harmless in this
> > case. Regardless, fix it to avoid the warning seen under -Warray-bounds,
> > which we'd like to enable globally:
> >
...
> >
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >  drivers/iio/adc/stmpe-adc.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/iio/adc/stmpe-adc.c b/drivers/iio/adc/stmpe-adc.c
> > index fba659bfdb40..d2d405388499 100644
> > --- a/drivers/iio/adc/stmpe-adc.c
> > +++ b/drivers/iio/adc/stmpe-adc.c
> > @@ -256,6 +256,7 @@ static int stmpe_adc_probe(struct platform_device *pdev)
> >  	struct stmpe_adc *info;
> >  	struct device_node *np;
> >  	u32 norequest_mask = 0;
> > +	unsigned long bits;
> >  	int irq_temp, irq_adc;
> >  	int num_chan = 0;
> >  	int i = 0;
> > @@ -309,8 +310,8 @@ static int stmpe_adc_probe(struct platform_device *pdev)
> >
> >  	of_property_read_u32(np, "st,norequest-mask", &norequest_mask);
> >
> > -	for_each_clear_bit(i, (unsigned long *) &norequest_mask,
> > -			   (STMPE_ADC_LAST_NR + 1)) {
> > +	bits = norequest_mask;
> 
> This would not work on 64-bit BE architecture. It should use bitmap_from_arr32()

The old version was wrong on 64 bit BE, Kees version is probably ok.

But changing norequest_mask to 'unsigned long' is probably neater.
OTOH using the bitmap functions on single words is just bloat.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

end of thread, other threads:[~2022-02-05 13:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15 23:25 [PATCH] iio: stmpe-adc: Use correctly sized arguments for bit field Kees Cook
2021-12-16 12:36 ` Jonathan Cameron
2022-02-04  4:29 ` Yury Norov
2022-02-05 13:38   ` David Laight

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).