Skip to content

Commit 8aa6e85

Browse files
captain5050smb49
authored andcommitted
libperf evlist: Avoid out-of-bounds access
BugLink: https://bugs.launchpad.net/bugs/2065805 [ Upstream commit 1947b92464c3268381604bbe2ac977a3fd78192f ] Parallel testing appears to show a race between allocating and setting evsel ids. As there is a bounds check on the xyarray it yields a segv like: ``` AddressSanitizer:DEADLYSIGNAL ================================================================= ==484408==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000010 ==484408==The signal is caused by a WRITE memory access. ==484408==Hint: address points to the zero page. #0 0x55cef5d4eff4 in perf_evlist__id_hash tools/lib/perf/evlist.c:256 #1 0x55cef5d4f132 in perf_evlist__id_add tools/lib/perf/evlist.c:274 #2 0x55cef5d4f545 in perf_evlist__id_add_fd tools/lib/perf/evlist.c:315 #3 0x55cef5a1923f in store_evsel_ids util/evsel.c:3130 #4 0x55cef5a19400 in evsel__store_ids util/evsel.c:3147 #5 0x55cef5888204 in __run_perf_stat tools/perf/builtin-stat.c:832 #6 0x55cef5888c06 in run_perf_stat tools/perf/builtin-stat.c:960 #7 0x55cef58932db in cmd_stat tools/perf/builtin-stat.c:2878 ... ``` Avoid this crash by early exiting the perf_evlist__id_add_fd and perf_evlist__id_add is the access is out-of-bounds. Signed-off-by: Ian Rogers <[email protected]> Cc: Yang Jihong <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Manuel Diewald <[email protected]> Signed-off-by: Roxana Nicolescu <[email protected]>
1 parent 956192a commit 8aa6e85

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

tools/lib/perf/evlist.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist)
224224

225225
static void perf_evlist__id_hash(struct perf_evlist *evlist,
226226
struct perf_evsel *evsel,
227-
int cpu, int thread, u64 id)
227+
int cpu_map_idx, int thread, u64 id)
228228
{
229229
int hash;
230-
struct perf_sample_id *sid = SID(evsel, cpu, thread);
230+
struct perf_sample_id *sid = SID(evsel, cpu_map_idx, thread);
231231

232232
sid->id = id;
233233
sid->evsel = evsel;
@@ -245,21 +245,27 @@ void perf_evlist__reset_id_hash(struct perf_evlist *evlist)
245245

246246
void perf_evlist__id_add(struct perf_evlist *evlist,
247247
struct perf_evsel *evsel,
248-
int cpu, int thread, u64 id)
248+
int cpu_map_idx, int thread, u64 id)
249249
{
250-
perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
250+
if (!SID(evsel, cpu_map_idx, thread))
251+
return;
252+
253+
perf_evlist__id_hash(evlist, evsel, cpu_map_idx, thread, id);
251254
evsel->id[evsel->ids++] = id;
252255
}
253256

254257
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
255258
struct perf_evsel *evsel,
256-
int cpu, int thread, int fd)
259+
int cpu_map_idx, int thread, int fd)
257260
{
258261
u64 read_data[4] = { 0, };
259262
int id_idx = 1; /* The first entry is the counter value */
260263
u64 id;
261264
int ret;
262265

266+
if (!SID(evsel, cpu_map_idx, thread))
267+
return -1;
268+
263269
ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
264270
if (!ret)
265271
goto add;
@@ -288,7 +294,7 @@ int perf_evlist__id_add_fd(struct perf_evlist *evlist,
288294
id = read_data[id_idx];
289295

290296
add:
291-
perf_evlist__id_add(evlist, evsel, cpu, thread, id);
297+
perf_evlist__id_add(evlist, evsel, cpu_map_idx, thread, id);
292298
return 0;
293299
}
294300

tools/lib/perf/include/internal/evlist.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist);
119119

120120
void perf_evlist__id_add(struct perf_evlist *evlist,
121121
struct perf_evsel *evsel,
122-
int cpu, int thread, u64 id);
122+
int cpu_map_idx, int thread, u64 id);
123123

124124
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
125125
struct perf_evsel *evsel,
126-
int cpu, int thread, int fd);
126+
int cpu_map_idx, int thread, int fd);
127127

128128
void perf_evlist__reset_id_hash(struct perf_evlist *evlist);
129129

0 commit comments

Comments
 (0)