Skip to contentSkip to conversation

bpf: fix bpf_prog_array_copy_to_user warning from perf event prog query

PATCH Applied Daniel Borkmann 2 participants 2 messages Feb 14, 2018

Subsystems (7)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 bpf: fix bpf_prog_array_copy_to_user warning from perf event prog query

syzkaller tried to perform a prog query in perf_event_query_prog_array() where struct perf_event_query_bpf had an ids_len of 1,073,741,353 and thus causing a warning due to failed kcalloc() allocation out of the bpf_prog_array_copy_to_user() helper. Given we cannot attach more than 64 programs to a perf event, there's no point in allowing huge ids_len. Therefore, allow a buffer that would fix the maximum number of ids and also add a __GFP_NOWARN to the temporary ids buffer.

Fixes f371b304f12e ("bpf/tracing: allow user space to query prog array on the same tp")
Fixes 0911287ce32b ("bpf: fix bpf_prog_array_copy_to_user() issues")
Reported-by [email protected]
---
 kernel/bpf/core.c        | 2 +-
 kernel/trace/bpf_trace.c | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/core.c +1 -1
index 29ca920..d315b39 100644
@@ -1590,7 +1590,7 @@ int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
 	 * so always copy 'cnt' prog_ids to the user.
 	 * In a rare race the user will see zero prog_ids
 	 */
-	ids = kcalloc(cnt, sizeof(u32), GFP_USER);
+	ids = kcalloc(cnt, sizeof(u32), GFP_USER | __GFP_NOWARN);
 	if (!ids)
 		return -ENOMEM;
 	rcu_read_lock();
kernel/trace/bpf_trace.c +2 -0
index fc2838a..c0a9e31 100644
@@ -872,6 +872,8 @@ int perf_event_query_prog_array(struct perf_event *event, void __user *info)
 		return -EINVAL;
 	if (copy_from_user(&query, uquery, sizeof(query)))
 		return -EFAULT;
+	if (query.ids_len > BPF_TRACE_MAX_PROGS)
+		return -E2BIG;
 
 	mutex_lock(&bpf_event_mutex);
 	ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
Signature · 1 line
2.9.5
#1 lore raw
Quoted 8 lines · Daniel Borkmann

syzkaller tried to perform a prog query in perf_event_query_prog_array() where struct perf_event_query_bpf had an ids_len of 1,073,741,353 and thus causing a warning due to failed kcalloc() allocation out of the bpf_prog_array_copy_to_user() helper. Given we cannot attach more than 64 programs to a perf event, there's no point in allowing huge ids_len. Therefore, allow a buffer that would fix the maximum number of ids and also add a __GFP_NOWARN to the temporary ids buffer.

Quoted 4 lines
Fixes f371b304f12e ("bpf/tracing: allow user space to query prog array on the same tp")
Fixes 0911287ce32b ("bpf: fix bpf_prog_array_copy_to_user() issues")
Reported-by [email protected]

Applied to bpf tree, Thanks for the quick fix Daniel.