linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
@ 2021-04-14 19:57 Rob Herring
  2021-04-14 20:25 ` Namhyung Kim
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Herring @ 2021-04-14 19:57 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: linux-kernel, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Namhyung Kim

xyarray__entry() is missing any bounds checking yet often the x and y
parameters come from external callers. Add bounds checks and an
unchecked __xyarray__entry().

Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Rob Herring <robh@kernel.org>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
index 51e35d6c8ec4..f0896c00b494 100644
--- a/tools/lib/perf/include/internal/xyarray.h
+++ b/tools/lib/perf/include/internal/xyarray.h
@@ -18,11 +18,18 @@ struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
 void xyarray__delete(struct xyarray *xy);
 void xyarray__reset(struct xyarray *xy);
 
-static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
+static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
 {
 	return &xy->contents[x * xy->row_size + y * xy->entry_size];
 }
 
+static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
+{
+	if (x >= xy->max_x || y >= xy->max_y)
+		return NULL;
+	return __xyarray__entry(xy, x, y);
+}
+
 static inline int xyarray__max_y(struct xyarray *xy)
 {
 	return xy->max_y;
-- 
2.27.0


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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-14 19:57 [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry() Rob Herring
@ 2021-04-14 20:25 ` Namhyung Kim
  2021-04-14 20:53   ` Rob Herring
  0 siblings, 1 reply; 7+ messages in thread
From: Namhyung Kim @ 2021-04-14 20:25 UTC (permalink / raw)
  To: Rob Herring
  Cc: Arnaldo Carvalho de Melo, linux-kernel, Peter Zijlstra,
	Ingo Molnar, Mark Rutland, Alexander Shishkin, Jiri Olsa

On Thu, Apr 15, 2021 at 4:58 AM Rob Herring <robh@kernel.org> wrote:
>
> xyarray__entry() is missing any bounds checking yet often the x and y
> parameters come from external callers. Add bounds checks and an
> unchecked __xyarray__entry().
>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> Cc: Jiri Olsa <jolsa@redhat.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Rob Herring <robh@kernel.org>
> ---
>  tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
> index 51e35d6c8ec4..f0896c00b494 100644
> --- a/tools/lib/perf/include/internal/xyarray.h
> +++ b/tools/lib/perf/include/internal/xyarray.h
> @@ -18,11 +18,18 @@ struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
>  void xyarray__delete(struct xyarray *xy);
>  void xyarray__reset(struct xyarray *xy);
>
> -static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> +static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
>  {
>         return &xy->contents[x * xy->row_size + y * xy->entry_size];
>  }
>
> +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> +{
> +       if (x >= xy->max_x || y >= xy->max_y)
> +               return NULL;

Maybe better to check negatives as well.

Thanks,
Namhyung


> +       return __xyarray__entry(xy, x, y);
> +}
> +
>  static inline int xyarray__max_y(struct xyarray *xy)
>  {
>         return xy->max_y;
> --
> 2.27.0
>

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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-14 20:25 ` Namhyung Kim
@ 2021-04-14 20:53   ` Rob Herring
  2021-04-15 19:46     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Herring @ 2021-04-14 20:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, linux-kernel, Peter Zijlstra,
	Ingo Molnar, Mark Rutland, Alexander Shishkin, Jiri Olsa

On Wed, Apr 14, 2021 at 3:25 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Thu, Apr 15, 2021 at 4:58 AM Rob Herring <robh@kernel.org> wrote:
> >
> > xyarray__entry() is missing any bounds checking yet often the x and y
> > parameters come from external callers. Add bounds checks and an
> > unchecked __xyarray__entry().
> >
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Cc: Ingo Molnar <mingo@redhat.com>
> > Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
> > Cc: Mark Rutland <mark.rutland@arm.com>
> > Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> > Cc: Jiri Olsa <jolsa@redhat.com>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Signed-off-by: Rob Herring <robh@kernel.org>
> > ---
> >  tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
> > index 51e35d6c8ec4..f0896c00b494 100644
> > --- a/tools/lib/perf/include/internal/xyarray.h
> > +++ b/tools/lib/perf/include/internal/xyarray.h
> > @@ -18,11 +18,18 @@ struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
> >  void xyarray__delete(struct xyarray *xy);
> >  void xyarray__reset(struct xyarray *xy);
> >
> > -static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > +static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
> >  {
> >         return &xy->contents[x * xy->row_size + y * xy->entry_size];
> >  }
> >
> > +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > +{
> > +       if (x >= xy->max_x || y >= xy->max_y)
> > +               return NULL;
>
> Maybe better to check negatives as well.

max_x and max_y are size_t and unsigned, so x and y will be promoted
to unsigned and the check will still work.

It's probably better to change the args to size_t though. And perhaps
on xyarray__new(), xyarray__max_y(), and xyarray__max_x() as well.

Rob

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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-14 20:53   ` Rob Herring
@ 2021-04-15 19:46     ` Arnaldo Carvalho de Melo
  2021-04-15 19:48       ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-04-15 19:46 UTC (permalink / raw)
  To: Rob Herring
  Cc: Namhyung Kim, linux-kernel, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa

Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:
> On Wed, Apr 14, 2021 at 3:25 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > On Thu, Apr 15, 2021 at 4:58 AM Rob Herring <robh@kernel.org> wrote:
> > >
> > > xyarray__entry() is missing any bounds checking yet often the x and y
> > > parameters come from external callers. Add bounds checks and an
> > > unchecked __xyarray__entry().
> > >
> > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > Cc: Ingo Molnar <mingo@redhat.com>
> > > Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
> > > Cc: Mark Rutland <mark.rutland@arm.com>
> > > Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> > > Cc: Jiri Olsa <jolsa@redhat.com>
> > > Cc: Namhyung Kim <namhyung@kernel.org>
> > > Signed-off-by: Rob Herring <robh@kernel.org>
> > > ---
> > >  tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
> > >  1 file changed, 8 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
> > > index 51e35d6c8ec4..f0896c00b494 100644
> > > --- a/tools/lib/perf/include/internal/xyarray.h
> > > +++ b/tools/lib/perf/include/internal/xyarray.h
> > > @@ -18,11 +18,18 @@ struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
> > >  void xyarray__delete(struct xyarray *xy);
> > >  void xyarray__reset(struct xyarray *xy);
> > >
> > > -static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > +static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
> > >  {
> > >         return &xy->contents[x * xy->row_size + y * xy->entry_size];
> > >  }
> > >
> > > +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > +{
> > > +       if (x >= xy->max_x || y >= xy->max_y)
> > > +               return NULL;
> >
> > Maybe better to check negatives as well.
> 
> max_x and max_y are size_t and unsigned, so x and y will be promoted
> to unsigned and the check will still work.

Fair enough, applied.

- Arnaldo
 
> It's probably better to change the args to size_t though. And perhaps
> on xyarray__new(), xyarray__max_y(), and xyarray__max_x() as well.

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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-15 19:46     ` Arnaldo Carvalho de Melo
@ 2021-04-15 19:48       ` Arnaldo Carvalho de Melo
  2021-04-15 19:51         ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-04-15 19:48 UTC (permalink / raw)
  To: Rob Herring
  Cc: Namhyung Kim, linux-kernel, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa

Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:
> > On Wed, Apr 14, 2021 at 3:25 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > >
> > > On Thu, Apr 15, 2021 at 4:58 AM Rob Herring <robh@kernel.org> wrote:
> > > >
> > > > xyarray__entry() is missing any bounds checking yet often the x and y
> > > > parameters come from external callers. Add bounds checks and an
> > > > unchecked __xyarray__entry().
> > > >
> > > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > > Cc: Ingo Molnar <mingo@redhat.com>
> > > > Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
> > > > Cc: Mark Rutland <mark.rutland@arm.com>
> > > > Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> > > > Cc: Jiri Olsa <jolsa@redhat.com>
> > > > Cc: Namhyung Kim <namhyung@kernel.org>
> > > > Signed-off-by: Rob Herring <robh@kernel.org>
> > > > ---
> > > >  tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
> > > >  1 file changed, 8 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
> > > > index 51e35d6c8ec4..f0896c00b494 100644
> > > > --- a/tools/lib/perf/include/internal/xyarray.h
> > > > +++ b/tools/lib/perf/include/internal/xyarray.h
> > > > @@ -18,11 +18,18 @@ struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
> > > >  void xyarray__delete(struct xyarray *xy);
> > > >  void xyarray__reset(struct xyarray *xy);
> > > >
> > > > -static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > > +static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
> > > >  {
> > > >         return &xy->contents[x * xy->row_size + y * xy->entry_size];
> > > >  }
> > > >
> > > > +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > > +{
> > > > +       if (x >= xy->max_x || y >= xy->max_y)
> > > > +               return NULL;
> > >
> > > Maybe better to check negatives as well.
> > 
> > max_x and max_y are size_t and unsigned, so x and y will be promoted
> > to unsigned and the check will still work.
> 
> Fair enough, applied.

So...:

  CC       /tmp/build/perf/builtin-script.o
In file included from xyarray.c:2:
/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h: In function ‘xyarray__entry’:
/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:8: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
   28 |  if (x >= xy->max_x || y >= xy->max_y)
      |        ^~
/home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:26: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
   28 |  if (x >= xy->max_x || y >= xy->max_y)
      |                          ^~
cc1: all warnings being treated as errors


Fedora 33's gcc complains, so I'll cast it to size_t.

- Arnaldo
 
>  
> > It's probably better to change the args to size_t though. And perhaps
> > on xyarray__new(), xyarray__max_y(), and xyarray__max_x() as well.

-- 

- Arnaldo

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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-15 19:48       ` Arnaldo Carvalho de Melo
@ 2021-04-15 19:51         ` Arnaldo Carvalho de Melo
  2021-04-15 20:02           ` Rob Herring
  0 siblings, 1 reply; 7+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-04-15 19:51 UTC (permalink / raw)
  To: Rob Herring
  Cc: Namhyung Kim, linux-kernel, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa

Em Thu, Apr 15, 2021 at 04:48:34PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:
> > > On Wed, Apr 14, 2021 at 3:25 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > > > > +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > > > +{
> > > > > +       if (x >= xy->max_x || y >= xy->max_y)
> > > > > +               return NULL;
> > > >
> > > > Maybe better to check negatives as well.
> > > 
> > > max_x and max_y are size_t and unsigned, so x and y will be promoted
> > > to unsigned and the check will still work.
> > 
> > Fair enough, applied.
> 
> So...:
> 
>   CC       /tmp/build/perf/builtin-script.o
> In file included from xyarray.c:2:
> /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h: In function ‘xyarray__entry’:
> /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:8: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
>    28 |  if (x >= xy->max_x || y >= xy->max_y)
>       |        ^~
> /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:26: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
>    28 |  if (x >= xy->max_x || y >= xy->max_y)
>       |                          ^~
> cc1: all warnings being treated as errors
> 
> 
> Fedora 33's gcc complains, so I'll cast it to size_t.

> > > It's probably better to change the args to size_t though. And perhaps
> > > on xyarray__new(), xyarray__max_y(), and xyarray__max_x() as well.

So I did this, should be enough:

diff --git a/tools/lib/perf/include/internal/xyarray.h b/tools/lib/perf/include/internal/xyarray.h
index f0896c00b4940016..f10af3da7b21cc15 100644
--- a/tools/lib/perf/include/internal/xyarray.h
+++ b/tools/lib/perf/include/internal/xyarray.h
@@ -23,7 +23,7 @@ static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
 	return &xy->contents[x * xy->row_size + y * xy->entry_size];
 }
 
-static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
+static inline void *xyarray__entry(struct xyarray *xy, size_t x, size_t y)
 {
 	if (x >= xy->max_x || y >= xy->max_y)
 		return NULL;

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

* Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
  2021-04-15 19:51         ` Arnaldo Carvalho de Melo
@ 2021-04-15 20:02           ` Rob Herring
  0 siblings, 0 replies; 7+ messages in thread
From: Rob Herring @ 2021-04-15 20:02 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, linux-kernel, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa

On Thu, Apr 15, 2021 at 2:51 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Thu, Apr 15, 2021 at 04:48:34PM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:
> > > > On Wed, Apr 14, 2021 at 3:25 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > > > > > +static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
> > > > > > +{
> > > > > > +       if (x >= xy->max_x || y >= xy->max_y)
> > > > > > +               return NULL;
> > > > >
> > > > > Maybe better to check negatives as well.
> > > >
> > > > max_x and max_y are size_t and unsigned, so x and y will be promoted
> > > > to unsigned and the check will still work.
> > >
> > > Fair enough, applied.
> >
> > So...:
> >
> >   CC       /tmp/build/perf/builtin-script.o
> > In file included from xyarray.c:2:
> > /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h: In function ‘xyarray__entry’:
> > /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:8: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
> >    28 |  if (x >= xy->max_x || y >= xy->max_y)
> >       |        ^~
> > /home/acme/git/perf/tools/lib/perf/include/internal/xyarray.h:28:26: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
> >    28 |  if (x >= xy->max_x || y >= xy->max_y)
> >       |                          ^~
> > cc1: all warnings being treated as errors
> >
> >
> > Fedora 33's gcc complains, so I'll cast it to size_t.

I guess gcc 10.2 on ubuntu doesn't yet. I was somewhat expecting to
see something and figured it was fine with -Wall...

Rob

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

end of thread, other threads:[~2021-04-15 20:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14 19:57 [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry() Rob Herring
2021-04-14 20:25 ` Namhyung Kim
2021-04-14 20:53   ` Rob Herring
2021-04-15 19:46     ` Arnaldo Carvalho de Melo
2021-04-15 19:48       ` Arnaldo Carvalho de Melo
2021-04-15 19:51         ` Arnaldo Carvalho de Melo
2021-04-15 20:02           ` Rob Herring

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