Skip to content

Commit 57d4853

Browse files
laoarAlexei Starovoitov
authored andcommitted
bpf: Add a common helper bpf_copy_to_user()
Add a common helper bpf_copy_to_user(), which will be used at multiple places. No functional change. Signed-off-by: Yafang Shao <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent cd3910d commit 57d4853

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

kernel/bpf/syscall.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,6 +3295,25 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
32953295
raw_tp_link->btp->tp->name);
32963296
}
32973297

3298+
static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
3299+
u32 len)
3300+
{
3301+
if (ulen >= len + 1) {
3302+
if (copy_to_user(ubuf, buf, len + 1))
3303+
return -EFAULT;
3304+
} else {
3305+
char zero = '\0';
3306+
3307+
if (copy_to_user(ubuf, buf, ulen - 1))
3308+
return -EFAULT;
3309+
if (put_user(zero, ubuf + ulen - 1))
3310+
return -EFAULT;
3311+
return -ENOSPC;
3312+
}
3313+
3314+
return 0;
3315+
}
3316+
32983317
static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
32993318
struct bpf_link_info *info)
33003319
{
@@ -3313,20 +3332,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
33133332
if (!ubuf)
33143333
return 0;
33153334

3316-
if (ulen >= tp_len + 1) {
3317-
if (copy_to_user(ubuf, tp_name, tp_len + 1))
3318-
return -EFAULT;
3319-
} else {
3320-
char zero = '\0';
3321-
3322-
if (copy_to_user(ubuf, tp_name, ulen - 1))
3323-
return -EFAULT;
3324-
if (put_user(zero, ubuf + ulen - 1))
3325-
return -EFAULT;
3326-
return -ENOSPC;
3327-
}
3328-
3329-
return 0;
3335+
return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
33303336
}
33313337

33323338
static const struct bpf_link_ops bpf_raw_tp_link_lops = {

0 commit comments

Comments
 (0)