Commit 62544ce8 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by David S. Miller

bpf: fix bpf_perf_event_read() helper

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: 35578d79 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Tested-by: default avatarWang Nan <wangnan0@huawei.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8b7c94e3
...@@ -194,7 +194,6 @@ extern const struct bpf_func_proto bpf_map_lookup_elem_proto; ...@@ -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_update_elem_proto;
extern const struct bpf_func_proto bpf_map_delete_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_prandom_u32_proto;
extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
extern const struct bpf_func_proto bpf_tail_call_proto; extern const struct bpf_func_proto bpf_tail_call_proto;
......
...@@ -292,16 +292,23 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd) ...@@ -292,16 +292,23 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
attr = perf_event_attrs(event); attr = perf_event_attrs(event);
if (IS_ERR(attr)) if (IS_ERR(attr))
return (void *)attr; goto err;
if (attr->type != PERF_TYPE_RAW && if (attr->inherit)
!(attr->type == PERF_TYPE_SOFTWARE && goto err;
attr->config == PERF_COUNT_SW_BPF_OUTPUT) &&
attr->type != PERF_TYPE_HARDWARE) { if (attr->type == PERF_TYPE_RAW)
perf_event_release_kernel(event); return event;
return ERR_PTR(-EINVAL);
} if (attr->type == PERF_TYPE_HARDWARE)
return event; 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) static void perf_event_fd_array_put_ptr(void *ptr)
......
...@@ -199,6 +199,11 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5) ...@@ -199,6 +199,11 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
if (!event) if (!event)
return -ENOENT; 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 * we don't know if the function is run successfully by the
* return value. It can be judged in other places, such as * return value. It can be judged in other places, such as
...@@ -207,7 +212,7 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5) ...@@ -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); 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, .func = bpf_perf_event_read,
.gpl_only = false, .gpl_only = false,
.ret_type = RET_INTEGER, .ret_type = RET_INTEGER,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment