Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

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
Loading
Loading
Loading
Loading
+0 −1
Original line number Original line Diff line number Diff line
@@ -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;
+16 −9
Original line number Original line Diff line number Diff line
@@ -292,17 +292,24 @@ 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)
		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);
	perf_event_release_kernel(event);
	return ERR_PTR(-EINVAL);
	return ERR_PTR(-EINVAL);
}
}
	return event;
}


static void perf_event_fd_array_put_ptr(void *ptr)
static void perf_event_fd_array_put_ptr(void *ptr)
{
{
+6 −1
Original line number Original line Diff line number Diff line
@@ -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)
	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,