[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YHiYkhBKWRYA6WRC@kernel.org>
Date: Thu, 15 Apr 2021 16:48:34 -0300
From: Arnaldo Carvalho de Melo <acme@...nel.org>
To: Rob Herring <robh@...nel.org>
Cc: Namhyung Kim <namhyung@...nel.org>,
linux-kernel <linux-kernel@...r.kernel.org>,
Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...hat.com>
Subject: Re: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
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@...nel.org> wrote:
> > >
> > > On Thu, Apr 15, 2021 at 4:58 AM Rob Herring <robh@...nel.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@...radead.org>
> > > > Cc: Ingo Molnar <mingo@...hat.com>
> > > > Cc: Arnaldo Carvalho de Melo <acme@...nel.org>
> > > > Cc: Mark Rutland <mark.rutland@....com>
> > > > Cc: Alexander Shishkin <alexander.shishkin@...ux.intel.com>
> > > > Cc: Jiri Olsa <jolsa@...hat.com>
> > > > Cc: Namhyung Kim <namhyung@...nel.org>
> > > > Signed-off-by: Rob Herring <robh@...nel.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
Powered by blists - more mailing lists