Skip to content

Commit d6d1e6c

Browse files
Hou Taoborkmann
authored andcommitted
bpf: Limit the number of kprobes when attaching program to multiple kprobes
An abnormally big cnt may also be assigned to kprobe_multi.cnt when attaching multiple kprobes. It will trigger the following warning in kvmalloc_node(): if (unlikely(size > INT_MAX)) { WARN_ON_ONCE(!(flags & __GFP_NOWARN)); return NULL; } Fix the warning by limiting the maximal number of kprobes in bpf_kprobe_multi_link_attach(). If the number of kprobes is greater than MAX_KPROBE_MULTI_CNT, the attachment will fail and return -E2BIG. Fixes: 0dcac27 ("bpf: Add multi kprobe link") Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8b2efe5 commit d6d1e6c

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

kernel/trace/bpf_trace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
4444

4545
#define MAX_UPROBE_MULTI_CNT (1U << 20)
46+
#define MAX_KPROBE_MULTI_CNT (1U << 20)
4647

4748
#ifdef CONFIG_MODULES
4849
struct bpf_trace_module {
@@ -2972,6 +2973,8 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
29722973
cnt = attr->link_create.kprobe_multi.cnt;
29732974
if (!cnt)
29742975
return -EINVAL;
2976+
if (cnt > MAX_KPROBE_MULTI_CNT)
2977+
return -E2BIG;
29752978

29762979
size = cnt * sizeof(*addrs);
29772980
addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);

0 commit comments

Comments
 (0)