2022-03-23 15:15:39

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH] bpf: use vmemdup_user instead of kvmalloc and copy_from_user

On Tue, Mar 22, 2022 at 4:29 AM Yihao Han <[email protected]> wrote:
>
> fix memdup_user.cocci warning:
> kernel/trace/bpf_trace.c:2450:12-20: WARNING opportunity
> for vmemdup_user
>
> Signed-off-by: Yihao Han <[email protected]>
> ---
> kernel/trace/bpf_trace.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 7fa2ebc07f60..aff2461c1ea2 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -2447,13 +2447,9 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
>
> ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
> if (ucookies) {
> - cookies = kvmalloc(size, GFP_KERNEL);
> - if (!cookies) {
> - err = -ENOMEM;
> - goto error;
> - }
> - if (copy_from_user(cookies, ucookies, size)) {
> - err = -EFAULT;
> + cookies = vmemdup_user(ucookies, size);

does kvfree() handle ERR_PTR() values properly? I doubt so. Did you
validate your change or just blindly applied some tool?

> + if (IS_ERR(cookies)) {
> + err = PTR_ERR(cookies);
> goto error;
> }
> }
> --
> 2.17.1
>