linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fix comparison of unsigned expression < 0
@ 2023-11-28  7:55 Haibo Li
  2023-11-29  1:22 ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Haibo Li @ 2023-11-28  7:55 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Ryabinin, Alexander Potapenko, Andrey Konovalov,
	Dmitry Vyukov, Vincenzo Frascino, Andrew Morton,
	Matthias Brugger, AngeloGioacchino Del Regno, kasan-dev,
	linux-mm, linux-arm-kernel, linux-mediatek, xiaoming.yu,
	Haibo Li, kernel test robot

Kernel test robot reported:

'''
mm/kasan/report.c:637 kasan_non_canonical_hook() warn:
unsigned 'addr' is never less than zero.
'''
The KASAN_SHADOW_OFFSET is 0 on loongarch64.

To fix it,check the KASAN_SHADOW_OFFSET before do comparison.

Signed-off-by: Haibo Li <haibo.li@mediatek.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/
  202311270743.3oTCwYPd-lkp@intel.com/
---
 mm/kasan/report.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index e77facb62900..dafec2df0268 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -634,10 +634,10 @@ void kasan_non_canonical_hook(unsigned long addr)
 {
 	unsigned long orig_addr;
 	const char *bug_type;
-
+#if KASAN_SHADOW_OFFSET > 0
 	if (addr < KASAN_SHADOW_OFFSET)
 		return;
-
+#endif
 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
 	/*
 	 * For faults near the shadow address for NULL, we can be fairly certain
-- 
2.18.0



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

* Re: [PATCH] fix comparison of unsigned expression < 0
  2023-11-28  7:55 [PATCH] fix comparison of unsigned expression < 0 Haibo Li
@ 2023-11-29  1:22 ` Andrew Morton
  2023-11-29  3:01   ` Andrey Konovalov
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2023-11-29  1:22 UTC (permalink / raw)
  To: Haibo Li
  Cc: linux-kernel, Andrey Ryabinin, Alexander Potapenko,
	Andrey Konovalov, Dmitry Vyukov, Vincenzo Frascino,
	Matthias Brugger, AngeloGioacchino Del Regno, kasan-dev,
	linux-mm, linux-arm-kernel, linux-mediatek, xiaoming.yu,
	kernel test robot

On Tue, 28 Nov 2023 15:55:32 +0800 Haibo Li <haibo.li@mediatek.com> wrote:

> Kernel test robot reported:
> 
> '''
> mm/kasan/report.c:637 kasan_non_canonical_hook() warn:
> unsigned 'addr' is never less than zero.
> '''
> The KASAN_SHADOW_OFFSET is 0 on loongarch64.
> 
> To fix it,check the KASAN_SHADOW_OFFSET before do comparison.
> 
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -634,10 +634,10 @@ void kasan_non_canonical_hook(unsigned long addr)
>  {
>  	unsigned long orig_addr;
>  	const char *bug_type;
> -
> +#if KASAN_SHADOW_OFFSET > 0
>  	if (addr < KASAN_SHADOW_OFFSET)
>  		return;
> -
> +#endif

We'd rather not add ugly ifdefs for a simple test like this.  If we
replace "<" with "<=", does it fix?  I suspect that's wrong.

But really, some hardwired comparison with an absolute address seems
lazy.  If KASAN_SHADOW_OFFSET is variable on a per-architecture basis
then the expression which checks the validity of an arbitrary address
should also be per-architecture.



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

* Re: [PATCH] fix comparison of unsigned expression < 0
  2023-11-29  1:22 ` Andrew Morton
@ 2023-11-29  3:01   ` Andrey Konovalov
  2023-11-29  3:42     ` Haibo Li
  2023-12-04  4:12     ` Dan Carpenter
  0 siblings, 2 replies; 6+ messages in thread
From: Andrey Konovalov @ 2023-11-29  3:01 UTC (permalink / raw)
  To: Andrew Morton, kernel test robot, Haibo Li
  Cc: linux-kernel, Andrey Ryabinin, Alexander Potapenko,
	Dmitry Vyukov, Vincenzo Frascino, Matthias Brugger,
	AngeloGioacchino Del Regno, kasan-dev, linux-mm,
	linux-arm-kernel, linux-mediatek, xiaoming.yu

On Wed, Nov 29, 2023 at 2:22 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Tue, 28 Nov 2023 15:55:32 +0800 Haibo Li <haibo.li@mediatek.com> wrote:
>
> > Kernel test robot reported:
> >
> > '''
> > mm/kasan/report.c:637 kasan_non_canonical_hook() warn:
> > unsigned 'addr' is never less than zero.
> > '''
> > The KASAN_SHADOW_OFFSET is 0 on loongarch64.
> >
> > To fix it,check the KASAN_SHADOW_OFFSET before do comparison.
> >
> > --- a/mm/kasan/report.c
> > +++ b/mm/kasan/report.c
> > @@ -634,10 +634,10 @@ void kasan_non_canonical_hook(unsigned long addr)
> >  {
> >       unsigned long orig_addr;
> >       const char *bug_type;
> > -
> > +#if KASAN_SHADOW_OFFSET > 0
> >       if (addr < KASAN_SHADOW_OFFSET)
> >               return;
> > -
> > +#endif
>
> We'd rather not add ugly ifdefs for a simple test like this.  If we
> replace "<" with "<=", does it fix?  I suspect that's wrong.

Changing the comparison into "<=" would be wrong.

But I actually don't think we need to fix anything here.

This issue looks quite close to a similar comparison with 0 issue
Linus shared his opinion on here:

https://lore.kernel.org/all/Pine.LNX.4.58.0411230958260.20993@ppc970.osdl.org/

I don't know if the common consensus with the regard to issues like
that changed since then. But if not, perhaps we can treat this kernel
test robot report as a false positive.

Thanks!


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

* Re: [PATCH] fix comparison of unsigned expression < 0
  2023-11-29  3:01   ` Andrey Konovalov
@ 2023-11-29  3:42     ` Haibo Li
  2023-12-04  4:12     ` Dan Carpenter
  1 sibling, 0 replies; 6+ messages in thread
From: Haibo Li @ 2023-11-29  3:42 UTC (permalink / raw)
  To: andreyknvl
  Cc: akpm, angelogioacchino.delregno, dvyukov, glider, haibo.li,
	kasan-dev, linux-arm-kernel, linux-kernel, linux-mediatek,
	linux-mm, lkp, matthias.bgg, ryabinin.a.a, vincenzo.frascino,
	xiaoming.yu

> On Wed, Nov 29, 2023 at 2:22 AM Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > On Tue, 28 Nov 2023 15:55:32 +0800 Haibo Li <haibo.li@mediatek.com> wrote:
> >
> > > Kernel test robot reported:
> > >
> > > '''
> > > mm/kasan/report.c:637 kasan_non_canonical_hook() warn:
> > > unsigned 'addr' is never less than zero.
> > > '''
> > > The KASAN_SHADOW_OFFSET is 0 on loongarch64.
> > >
> > > To fix it,check the KASAN_SHADOW_OFFSET before do comparison.
> > >
> > > --- a/mm/kasan/report.c
> > > +++ b/mm/kasan/report.c
> > > @@ -634,10 +634,10 @@ void kasan_non_canonical_hook(unsigned long addr)
> > >  {
> > >       unsigned long orig_addr;
> > >       const char *bug_type;
> > > -
> > > +#if KASAN_SHADOW_OFFSET > 0
> > >       if (addr < KASAN_SHADOW_OFFSET)
> > >               return;
> > > -
> > > +#endif
> >
> > We'd rather not add ugly ifdefs for a simple test like this.  If we
> > replace "<" with "<=", does it fix?  I suspect that's wrong.
>
> Changing the comparison into "<=" would be wrong.
>
> But I actually don't think we need to fix anything here.
>
> This issue looks quite close to a similar comparison with 0 issue
> Linus shared his opinion on here:
>
> https://lore.kernel.org/all/Pine.LNX.4.58.0411230958260.20993@ppc970.osdl.org/
>
> I don't know if the common consensus with the regard to issues like
> that changed since then. But if not, perhaps we can treat this kernel
> test robot report as a false positive.
>
> Thanks!

Thanks for the information.Let's keep it as unchanged.



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

* Re: [PATCH] fix comparison of unsigned expression < 0
  2023-11-29  3:01   ` Andrey Konovalov
  2023-11-29  3:42     ` Haibo Li
@ 2023-12-04  4:12     ` Dan Carpenter
  2023-12-13 15:31       ` Andrey Konovalov
  1 sibling, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2023-12-04  4:12 UTC (permalink / raw)
  To: Andrey Konovalov, Liu, Yujie
  Cc: Andrew Morton, kernel test robot, Haibo Li, linux-kernel,
	Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov,
	Vincenzo Frascino, Matthias Brugger, AngeloGioacchino Del Regno,
	kasan-dev, linux-mm, linux-arm-kernel, linux-mediatek,
	xiaoming.yu

On Wed, Nov 29, 2023 at 04:01:47AM +0100, Andrey Konovalov wrote:
> On Wed, Nov 29, 2023 at 2:22 AM Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > On Tue, 28 Nov 2023 15:55:32 +0800 Haibo Li <haibo.li@mediatek.com> wrote:
> >
> > > Kernel test robot reported:
> > >
> > > '''
> > > mm/kasan/report.c:637 kasan_non_canonical_hook() warn:
> > > unsigned 'addr' is never less than zero.
> > > '''
> > > The KASAN_SHADOW_OFFSET is 0 on loongarch64.
> > >
> > > To fix it,check the KASAN_SHADOW_OFFSET before do comparison.
> > >
> > > --- a/mm/kasan/report.c
> > > +++ b/mm/kasan/report.c
> > > @@ -634,10 +634,10 @@ void kasan_non_canonical_hook(unsigned long addr)
> > >  {
> > >       unsigned long orig_addr;
> > >       const char *bug_type;
> > > -
> > > +#if KASAN_SHADOW_OFFSET > 0
> > >       if (addr < KASAN_SHADOW_OFFSET)
> > >               return;
> > > -
> > > +#endif
> >
> > We'd rather not add ugly ifdefs for a simple test like this.  If we
> > replace "<" with "<=", does it fix?  I suspect that's wrong.
> 
> Changing the comparison into "<=" would be wrong.
> 

I would say that changing it to <= is seldom the correct thing.  I've
wanted to make that trigger a warning as well.

> But I actually don't think we need to fix anything here.
> 
> This issue looks quite close to a similar comparison with 0 issue
> Linus shared his opinion on here:
> 
> https://lore.kernel.org/all/Pine.LNX.4.58.0411230958260.20993@ppc970.osdl.org/
> 
> I don't know if the common consensus with the regard to issues like
> that changed since then. But if not, perhaps we can treat this kernel
> test robot report as a false positive.

I would say that the consensus has changed somewhere around 2015 or
so.  Unsigned comparisons to zero used to be one of the most common
types of bugs in new code but now almost all subsystems have turned on
the GCC warning for this.

However, this is a Smatch warning and I agree with Linus on this.  For
example, Smatch doesn't complain about the example code the Linus
mentioned.

	if (a < 0 || a > X)

And in this case, it's a one liner fix for me to add KASAN_SHADOW_OFFSET
as an allowed macro and silence the warning.

regards,
dan carpenter


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

* Re: [PATCH] fix comparison of unsigned expression < 0
  2023-12-04  4:12     ` Dan Carpenter
@ 2023-12-13 15:31       ` Andrey Konovalov
  0 siblings, 0 replies; 6+ messages in thread
From: Andrey Konovalov @ 2023-12-13 15:31 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Liu, Yujie, Andrew Morton, kernel test robot, Haibo Li,
	linux-kernel, Andrey Ryabinin, Alexander Potapenko,
	Dmitry Vyukov, Vincenzo Frascino, Matthias Brugger,
	AngeloGioacchino Del Regno, kasan-dev, linux-mm,
	linux-arm-kernel, linux-mediatek, xiaoming.yu

On Mon, Dec 4, 2023 at 5:12 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> > But I actually don't think we need to fix anything here.
> >
> > This issue looks quite close to a similar comparison with 0 issue
> > Linus shared his opinion on here:
> >
> > https://lore.kernel.org/all/Pine.LNX.4.58.0411230958260.20993@ppc970.osdl.org/
> >
> > I don't know if the common consensus with the regard to issues like
> > that changed since then. But if not, perhaps we can treat this kernel
> > test robot report as a false positive.
>
> I would say that the consensus has changed somewhere around 2015 or
> so.  Unsigned comparisons to zero used to be one of the most common
> types of bugs in new code but now almost all subsystems have turned on
> the GCC warning for this.
>
> However, this is a Smatch warning and I agree with Linus on this.  For
> example, Smatch doesn't complain about the example code the Linus
> mentioned.
>
>         if (a < 0 || a > X)
>
> And in this case, it's a one liner fix for me to add KASAN_SHADOW_OFFSET
> as an allowed macro and silence the warning.

Hi Dan,

If this sounds like a good idea to you, please add an exception.

From the KASAN side, I think adding an exception for this case makes sense.

Thank you!


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

end of thread, other threads:[~2023-12-13 15:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-28  7:55 [PATCH] fix comparison of unsigned expression < 0 Haibo Li
2023-11-29  1:22 ` Andrew Morton
2023-11-29  3:01   ` Andrey Konovalov
2023-11-29  3:42     ` Haibo Li
2023-12-04  4:12     ` Dan Carpenter
2023-12-13 15:31       ` Andrey Konovalov

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