Skip to content

Commit a285412

Browse files
committed
perf tools: Pre-check sample size before parsing
Check that the total size of the sample fields having a fixed size do not exceed the one of the whole event. This robustifies the sample parsing. Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Stephane Eranian <eranian@google.com>
1 parent 7442996 commit a285412

7 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎tools/perf/builtin-test.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ static int test__basic_mmap(void)
474474
unsigned int nr_events[nsyscalls],
475475
expected_nr_events[nsyscalls], i, j;
476476
struct perf_evsel *evsels[nsyscalls], *evsel;
477+
int sample_size = perf_sample_size(attr.sample_type);
477478

478479
for (i = 0; i < nsyscalls; ++i) {
479480
char name[64];
@@ -558,7 +559,8 @@ static int test__basic_mmap(void)
558559
goto out_munmap;
559560
}
560561

561-
perf_event__parse_sample(event, attr.sample_type, false, &sample);
562+
perf_event__parse_sample(event, attr.sample_type, sample_size,
563+
false, &sample);
562564
evsel = perf_evlist__id2evsel(evlist, sample.id);
563565
if (evsel == NULL) {
564566
pr_debug("event with id %" PRIu64

‎tools/perf/util/event.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ const char *perf_event__name(unsigned int id)
3535
return perf_event__names[id];
3636
}
3737

38+
int perf_sample_size(u64 sample_type)
39+
{
40+
u64 mask = sample_type & PERF_SAMPLE_MASK;
41+
int size = 0;
42+
int i;
43+
44+
for (i = 0; i < 64; i++) {
45+
if ((mask << i) & 1)
46+
size++;
47+
}
48+
49+
size *= sizeof(u64);
50+
51+
return size;
52+
}
53+
3854
static struct perf_sample synth_sample = {
3955
.pid = -1,
4056
.tid = -1,

‎tools/perf/util/event.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ struct read_event {
5656
u64 id;
5757
};
5858

59+
60+
#define PERF_SAMPLE_MASK \
61+
(PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
62+
PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
63+
PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
64+
PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
65+
5966
struct sample_event {
6067
struct perf_event_header header;
6168
u64 array[];
@@ -75,6 +82,8 @@ struct perf_sample {
7582
struct ip_callchain *callchain;
7683
};
7784

85+
int perf_sample_size(u64 sample_type);
86+
7887
#define BUILD_ID_SIZE 20
7988

8089
struct build_id_event {
@@ -178,6 +187,7 @@ int perf_event__preprocess_sample(const union perf_event *self,
178187
const char *perf_event__name(unsigned int id);
179188

180189
int perf_event__parse_sample(const union perf_event *event, u64 type,
181-
bool sample_id_all, struct perf_sample *sample);
190+
int sample_size, bool sample_id_all,
191+
struct perf_sample *sample);
182192

183193
#endif /* __PERF_RECORD_H */

‎tools/perf/util/evsel.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
304304
}
305305

306306
int perf_event__parse_sample(const union perf_event *event, u64 type,
307-
bool sample_id_all, struct perf_sample *data)
307+
int sample_size, bool sample_id_all,
308+
struct perf_sample *data)
308309
{
309310
const u64 *array;
310311

@@ -319,6 +320,9 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,
319320

320321
array = event->sample.array;
321322

323+
if (sample_size + sizeof(event->header) > event->header.size)
324+
return -EFAULT;
325+
322326
if (type & PERF_SAMPLE_IP) {
323327
data->ip = event->ip.ip;
324328
array++;

‎tools/perf/util/python.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,9 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
690690
return PyErr_NoMemory();
691691

692692
first = list_entry(evlist->entries.next, struct perf_evsel, node);
693-
perf_event__parse_sample(event, first->attr.sample_type, sample_id_all,
694-
&pevent->sample);
693+
perf_event__parse_sample(event, first->attr.sample_type,
694+
perf_sample_size(first->attr.sample_type),
695+
sample_id_all, &pevent->sample);
695696
return pyevent;
696697
}
697698

‎tools/perf/util/session.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ static void perf_session__id_header_size(struct perf_session *session)
9797
void perf_session__update_sample_type(struct perf_session *self)
9898
{
9999
self->sample_type = perf_evlist__sample_type(self->evlist);
100+
self->sample_size = perf_sample_size(self->sample_type);
100101
self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
101102
perf_session__id_header_size(self);
102103
}

‎tools/perf/util/session.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct perf_session {
4343
*/
4444
struct hists hists;
4545
u64 sample_type;
46+
int sample_size;
4647
int fd;
4748
bool fd_pipe;
4849
bool repipe;
@@ -159,6 +160,7 @@ static inline int perf_session__parse_sample(struct perf_session *session,
159160
struct perf_sample *sample)
160161
{
161162
return perf_event__parse_sample(event, session->sample_type,
163+
session->sample_size,
162164
session->sample_id_all, sample);
163165
}
164166

0 commit comments

Comments
 (0)