Skip to content

Commit

Permalink
bpf: fix bpf_perf_event_read() helper
Browse files Browse the repository at this point in the history
Fix safety checks for bpf_perf_event_read():
- only non-inherited events can be added to perf_event_array map
  (do this check statically at map insertion time)
- dynamically check that event is local and !pmu->count
Otherwise buggy bpf program can cause kernel splat.

Also fix error path after perf_event_attrs()
and remove redundant 'extern'.

Fixes: 35578d7 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Alexei Starovoitov authored and David S. Miller committed Oct 27, 2015
1 parent 8b7c94e commit 62544ce
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 0 additions & 1 deletion include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
extern const struct bpf_func_proto bpf_map_update_elem_proto;
extern const struct bpf_func_proto bpf_map_delete_elem_proto;

extern const struct bpf_func_proto bpf_perf_event_read_proto;
extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
extern const struct bpf_func_proto bpf_tail_call_proto;
Expand Down
25 changes: 16 additions & 9 deletions kernel/bpf/arraymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,23 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)

attr = perf_event_attrs(event);
if (IS_ERR(attr))
return (void *)attr;
goto err;

if (attr->type != PERF_TYPE_RAW &&
!(attr->type == PERF_TYPE_SOFTWARE &&
attr->config == PERF_COUNT_SW_BPF_OUTPUT) &&
attr->type != PERF_TYPE_HARDWARE) {
perf_event_release_kernel(event);
return ERR_PTR(-EINVAL);
}
return event;
if (attr->inherit)
goto err;

if (attr->type == PERF_TYPE_RAW)
return event;

if (attr->type == PERF_TYPE_HARDWARE)
return event;

if (attr->type == PERF_TYPE_SOFTWARE &&
attr->config == PERF_COUNT_SW_BPF_OUTPUT)
return event;
err:
perf_event_release_kernel(event);
return ERR_PTR(-EINVAL);
}

static void perf_event_fd_array_put_ptr(void *ptr)
Expand Down
7 changes: 6 additions & 1 deletion kernel/trace/bpf_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
if (!event)
return -ENOENT;

/* make sure event is local and doesn't have pmu::count */
if (event->oncpu != smp_processor_id() ||
event->pmu->count)
return -EINVAL;

/*
* we don't know if the function is run successfully by the
* return value. It can be judged in other places, such as
Expand All @@ -207,7 +212,7 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
return perf_event_read_local(event);
}

const struct bpf_func_proto bpf_perf_event_read_proto = {
static const struct bpf_func_proto bpf_perf_event_read_proto = {
.func = bpf_perf_event_read,
.gpl_only = false,
.ret_type = RET_INTEGER,
Expand Down

0 comments on commit 62544ce

Please sign in to comment.