Skip to content

Commit d08af2c

Browse files
Dan Carpenteranakryiko
authored andcommitted
bpf: Use safer kvmalloc_array() where possible
The kvmalloc_array() function is safer because it has a check for integer overflows. These sizes come from the user and I was not able to see any bounds checking so an integer overflow seems like a realistic concern. Fixes: 0dcac27 ("bpf: Add multi kprobe link") Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/Yo9VRVMeHbALyjUH@kili
1 parent e0491b1 commit d08af2c

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kernel/trace/bpf_trace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,11 +2263,11 @@ static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32
22632263
int err = -ENOMEM;
22642264
unsigned int i;
22652265

2266-
syms = kvmalloc(cnt * sizeof(*syms), GFP_KERNEL);
2266+
syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
22672267
if (!syms)
22682268
goto error;
22692269

2270-
buf = kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL);
2270+
buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
22712271
if (!buf)
22722272
goto error;
22732273

@@ -2464,7 +2464,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
24642464
return -EINVAL;
24652465

24662466
size = cnt * sizeof(*addrs);
2467-
addrs = kvmalloc(size, GFP_KERNEL);
2467+
addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
24682468
if (!addrs)
24692469
return -ENOMEM;
24702470

@@ -2489,7 +2489,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
24892489

24902490
ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
24912491
if (ucookies) {
2492-
cookies = kvmalloc(size, GFP_KERNEL);
2492+
cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
24932493
if (!cookies) {
24942494
err = -ENOMEM;
24952495
goto error;

0 commit comments

Comments
 (0)