lkml.org 
[lkml]   [2021]   [Apr]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH] libperf: xyarray: Add bounds checks to xyarray__entry()
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
>

\
 
 \ /
  Last update: 2021-04-14 22:26    [W:0.049 / U:0.168 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site