perf parse-events: Avoid erange from hex numbers
authorIan Rogers <irogers@google.com>
Thu, 7 Sep 2023 21:05:33 +0000 (14:05 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 12 Oct 2023 17:01:55 +0000 (10:01 -0700)
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.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20230907210533.3712979-1-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/parse-events.l

index 7bdf0565a92c09b6db9f21a3fbb59593581a7fd6..e86c45675e1d50b7294508cb7e6b221f363fce85 100644 (file)
@@ -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\-_*?.:]*