Skip to contentSkip to conversation

perf parse-events: Avoid erange from hex numbers

PATCH Applied Ian Rogers 2 participants 4 messages Sep 7, 2023 – Oct 12, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf parse-events: Avoid erange from hex numbers

We specify that a "num_hex" comprises 1 or more digits, however, that allows strtoull to fail with ERANGE. Limit the number of hex digits to being between 1 and 16.

Before:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
perf: util/parse-events.c:215: fix_raw: Assertion `errno == 0' failed.
Aborted (core dumped)
```

After:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ Bad event or PMU

Unable to find PMU or event on a PMU of 'cpu'

Initial error:
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ unknown term 'rE7574c47490475745' for pmu 'cpu'

valid terms: event,pc,edge,offcore_rsp,ldlat,inv,umask,frontend,cmask,config,config1,config2,config3,name,period,percore,metric-id
Run 'perf list' for a list of valid events

 Usage: perf stat [<options>] [<command>]

    -e, --event <event>   event selector. use 'perf list' to list available events
```

Issue found through fuzz testing.

---
 tools/perf/util/parse-events.l | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/parse-events.l +2 -2
index 4ef4b6f171a0..f7df52b22c39 100644
@@ -156,8 +156,8 @@ event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
 event		[^,{}/]+
 
 num_dec		[0-9]+
-num_hex		0x[a-fA-F0-9]+
-num_raw_hex	[a-fA-F0-9]+
+num_hex		0x[a-fA-F0-9]{1,16}
+num_raw_hex	[a-fA-F0-9]{1,16}
 name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!\-]*
 name_tag	[\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
author reviewer
#1 lore raw
Quoted 32 lines · Ian Rogers

We specify that a "num_hex" comprises 1 or more digits, however, that allows strtoull to fail with ERANGE. Limit the number of hex digits to being between 1 and 16.

Before:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
perf: util/parse-events.c:215: fix_raw: Assertion `errno == 0' failed.
Aborted (core dumped)
```

After:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ Bad event or PMU

Unable to find PMU or event on a PMU of 'cpu'

Initial error:
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ unknown term 'rE7574c47490475745' for pmu 'cpu'

valid terms: event,pc,edge,offcore_rsp,ldlat,inv,umask,frontend,cmask,config,config1,config2,config3,name,period,percore,metric-id
Run 'perf list' for a list of valid events

 Usage: perf stat [<options>] [<command>]

    -e, --event <event>   event selector. use 'perf list' to list available events
```
Quoted 3 lines

Issue found through fuzz testing.

Hi, would be nice to land this.

Thanks!
Ian

Quoted 22 lines
---
 tools/perf/util/parse-events.l | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/parse-events.l +2 -3
index 4ef4b6f171a0..f7df52b22c39 100644
@@ -156,8 +156,8 @@ event_pmu   [^,{}/]+[/][^/]*[/][^,{}/]*
 event          [^,{}/]+
 
 num_dec                [0-9]+
-num_hex                0x[a-fA-F0-9]+
-num_raw_hex    [a-fA-F0-9]+
+num_hex                0x[a-fA-F0-9]{1,16}
+num_raw_hex    [a-fA-F0-9]{1,16}
 name           [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!\-]*
 name_tag       [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
 name_minus     [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
--

2.42.0.283.g2d96d420d3-goog

maintainer Acked-by
#2 lore raw

Hi Ian,

Quoted 29 lines · Ian Rogers
Quoted 27 lines · Ian Rogers

We specify that a "num_hex" comprises 1 or more digits, however, that allows strtoull to fail with ERANGE. Limit the number of hex digits to being between 1 and 16.

Before:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
perf: util/parse-events.c:215: fix_raw: Assertion `errno == 0' failed.
Aborted (core dumped)
```

After:
```

$ perf stat -e 'cpu/rE7574c47490475745/' true
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ Bad event or PMU

Unable to find PMU or event on a PMU of 'cpu'

Initial error:
event syntax error: 'cpu/rE7574c47490475745/'

                         \___ unknown term 'rE7574c47490475745' for pmu 'cpu'

valid terms: event,pc,edge,offcore_rsp,ldlat,inv,umask,frontend,cmask,config,config1,config2,config3,name,period,percore,metric-id Run 'perf list' for a list of valid events

Quoted 8 lines
Quoted 8 lines

Usage: perf stat [<options>] [<command>]

-e, --event <event> event selector. use 'perf list' to list available events
```

Issue found through fuzz testing.

Thanks,
Namhyung

maintainer Applied
#3 lore raw
Quoted 35 lines · Namhyung Kim

Hi Ian,

Quoted 31 lines · Ian Rogers
Quoted 29 lines · Ian Rogers

We specify that a "num_hex" comprises 1 or more digits, however, that allows strtoull to fail with ERANGE. Limit the number of hex digits to being between 1 and 16.

Before:

```
$ perf stat -e 'cpu/rE7574c47490475745/' true
perf: util/parse-events.c:215: fix_raw: Assertion `errno == 0' failed.
Aborted (core dumped)
```

After:
```

$ perf stat -e 'cpu/rE7574c47490475745/' true
event syntax error: 'cpu/rE7574c47490475745/'
                         \___ Bad event or PMU

Unable to find PMU or event on a PMU of 'cpu'

Initial error:
event syntax error: 'cpu/rE7574c47490475745/'

                         \___ unknown term 'rE7574c47490475745' for pmu 'cpu'

valid terms: event,pc,edge,offcore_rsp,ldlat,inv,umask,frontend,cmask,config,config1,config2,config3,name,period,percore,metric-id Run 'perf list' for a list of valid events

Usage: perf stat [<options>] [<command>]

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

-e, --event <event> event selector. use 'perf list' to list available events
```

Issue found through fuzz testing.

Applied to perf-tools-next, thanks!