Skip to content

Commit b7d153b

Browse files
captain5050gregkh
authored andcommitted
libperf evlist: Avoid out-of-bounds access
[ Upstream commit 1947b92 ] 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 <irogers@google.com> Cc: Yang Jihong <yangjihong1@huawei.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/r/20240229070757.796244-1-irogers@google.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent aaefa79 commit b7d153b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

tools/lib/perf/evlist.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist)
233233

234234
static void perf_evlist__id_hash(struct perf_evlist *evlist,
235235
struct perf_evsel *evsel,
236-
int cpu, int thread, u64 id)
236+
int cpu_map_idx, int thread, u64 id)
237237
{
238238
int hash;
239-
struct perf_sample_id *sid = SID(evsel, cpu, thread);
239+
struct perf_sample_id *sid = SID(evsel, cpu_map_idx, thread);
240240

241241
sid->id = id;
242242
sid->evsel = evsel;
@@ -254,21 +254,27 @@ void perf_evlist__reset_id_hash(struct perf_evlist *evlist)
254254

255255
void perf_evlist__id_add(struct perf_evlist *evlist,
256256
struct perf_evsel *evsel,
257-
int cpu, int thread, u64 id)
257+
int cpu_map_idx, int thread, u64 id)
258258
{
259-
perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
259+
if (!SID(evsel, cpu_map_idx, thread))
260+
return;
261+
262+
perf_evlist__id_hash(evlist, evsel, cpu_map_idx, thread, id);
260263
evsel->id[evsel->ids++] = id;
261264
}
262265

263266
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
264267
struct perf_evsel *evsel,
265-
int cpu, int thread, int fd)
268+
int cpu_map_idx, int thread, int fd)
266269
{
267270
u64 read_data[4] = { 0, };
268271
int id_idx = 1; /* The first entry is the counter value */
269272
u64 id;
270273
int ret;
271274

275+
if (!SID(evsel, cpu_map_idx, thread))
276+
return -1;
277+
272278
ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
273279
if (!ret)
274280
goto add;
@@ -297,7 +303,7 @@ int perf_evlist__id_add_fd(struct perf_evlist *evlist,
297303
id = read_data[id_idx];
298304

299305
add:
300-
perf_evlist__id_add(evlist, evsel, cpu, thread, id);
306+
perf_evlist__id_add(evlist, evsel, cpu_map_idx, thread, id);
301307
return 0;
302308
}
303309

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist);
127127

128128
void perf_evlist__id_add(struct perf_evlist *evlist,
129129
struct perf_evsel *evsel,
130-
int cpu, int thread, u64 id);
130+
int cpu_map_idx, int thread, u64 id);
131131

132132
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
133133
struct perf_evsel *evsel,
134-
int cpu, int thread, int fd);
134+
int cpu_map_idx, int thread, int fd);
135135

136136
void perf_evlist__reset_id_hash(struct perf_evlist *evlist);
137137

0 commit comments

Comments
 (0)