Skip to contentSkip to conversation

libperf: xyarray: Add bounds checks to xyarray__entry()

PATCH In review Rob Herring 3 participants 7 messages Apr 14, 2021 – Apr 15, 2021 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libperf: xyarray: Add bounds checks to xyarray__entry()

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 <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/xyarray.h +8 -1
index 51e35d6c8ec4..f0896c00b494 100644
@@ -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;
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 25 lines · Rob Herring

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 <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/xyarray.h +0 -0
index 51e35d6c8ec4..f0896c00b494 100644
@@ -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);
 

tools/lib/perf/include/internal/xyarray.h struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size); · line 29

Quoted 10 lines
tools/lib/perf/include/internal/xyarray.h +5 -1
-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

Quoted 9 lines
tools/lib/perf/include/internal/xyarray.h +3 -1
+       return __xyarray__entry(xy, x, y);
+}
+
 static inline int xyarray__max_y(struct xyarray *xy)
 {
        return xy->max_y;
--

2.27.0

#2 lore raw
Quoted 33 lines · Namhyung Kim
Quoted 31 lines · Rob Herring

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 <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/xyarray.h +1 -1
index 51e35d6c8ec4..f0896c00b494 100644
@@ -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];
 }
 
Quoted 6 lines

tools/lib/perf/include/internal/xyarray.h struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size); · line 29

Quoted 4 lines
tools/lib/perf/include/internal/xyarray.h +4 -0
+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

#3 lore raw

Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:

Quoted 41 lines · Namhyung Kim
Quoted 33 lines · Rob Herring
Quoted 31 lines

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 <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/xyarray.h +1 -1
index 51e35d6c8ec4..f0896c00b494 100644
@@ -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];
 }
 
Quoted 6 lines

tools/lib/perf/include/internal/xyarray.h struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size); · line 29

Quoted 4 lines
tools/lib/perf/include/internal/xyarray.h +4 -0
+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.

Quoted 2 lines

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

Quoted 2 lines

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.

#4 lore raw

Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 42 lines

Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:

Quoted 41 lines · Namhyung Kim
Quoted 39 lines · Rob Herring
Quoted 25 lines

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 <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/lib/perf/include/internal/xyarray.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/perf/include/internal/xyarray.h +0 -0
index 51e35d6c8ec4..f0896c00b494 100644
@@ -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);
 

tools/lib/perf/include/internal/xyarray.h struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size); · line 29

Quoted 10 lines
tools/lib/perf/include/internal/xyarray.h +5 -1
-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.

Quoted 4 lines
Quoted 2 lines

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

Quoted 3 lines
Quoted 3 lines

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.

Signature · 1 line
- Arnaldo
#5 lore raw

Em Thu, Apr 15, 2021 at 04:48:34PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 22 lines

Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 9 lines

Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:

Quoted 8 lines · Namhyung Kim
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+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.

Quoted 4 lines
Quoted 2 lines

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)

Quoted 11 lines
      |        ^~

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

Quoted 2 lines
Quoted 2 lines

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:

tools/lib/perf/include/internal/xyarray.h +1 -1
index f0896c00b4940016..f10af3da7b21cc15 100644
@@ -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;
#6 lore raw
Quoted 32 lines · Arnaldo Carvalho de Melo

Em Thu, Apr 15, 2021 at 04:48:34PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 30 lines

Em Thu, Apr 15, 2021 at 04:46:46PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 9 lines

Em Wed, Apr 14, 2021 at 03:53:36PM -0500, Rob Herring escreveu:

Quoted 8 lines · Namhyung Kim
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+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.

Quoted 4 lines
Quoted 2 lines

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