All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Subject: Re: [PATCH bpf-next] bpf: Silence Coverity warning for find_kfunc_desc_btf
Date: Fri, 8 Oct 2021 13:39:46 -0700	[thread overview]
Message-ID: <CAEf4BzYpp+VqqL4n1N7-Uw8sySVgvaCagEcVicgumtpK-y68aw@mail.gmail.com> (raw)
In-Reply-To: <20211008170751.690570-1-memxor@gmail.com>

On Fri, Oct 8, 2021 at 10:07 AM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> The helper function returns a pointer that in the failure case encodes
> an error in the struct btf pointer. The current code lead to Coverity
> warning about the use of the invalid pointer:
>
>  *** CID 1507963:  Memory - illegal accesses  (USE_AFTER_FREE)
>  /kernel/bpf/verifier.c: 1788 in find_kfunc_desc_btf()
>  1782                          return ERR_PTR(-EINVAL);
>  1783                  }
>  1784
>  1785                  kfunc_btf = __find_kfunc_desc_btf(env, offset, btf_modp);
>  1786                  if (IS_ERR_OR_NULL(kfunc_btf)) {
>  1787                          verbose(env, "cannot find module BTF for func_id %u\n", func_id);
>  >>>      CID 1507963:  Memory - illegal accesses  (USE_AFTER_FREE)
>  >>>      Using freed pointer "kfunc_btf".
>  1788                          return kfunc_btf ?: ERR_PTR(-ENOENT);
>  1789                  }
>  1790                  return kfunc_btf;
>  1791          }
>  1792          return btf_vmlinux ?: ERR_PTR(-ENOENT);
>  1793     }
>
> Daniel suggested the use of ERR_CAST so that the intended use is clear
> to Coverity, but on closer look it seems that we never return NULL from
> the helper, hence it can just be switched to checking for IS_ERR and
> returning the pointer, similar to the cases elsewhere in the kernel.
>
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  kernel/bpf/verifier.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 20900a1bac12..2551b6be8d42 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -1783,10 +1783,8 @@ static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env,
>                 }
>
>                 kfunc_btf = __find_kfunc_desc_btf(env, offset, btf_modp);

Seems like __find_kfunc_desc_btf() already logs most of possible
reasons for the failure, with possibly btf_get_by_fd failure being the
biggest user-visible omission. If you complete __find_kfunc_desc_btf
logging and just pass through its result here without extra check,
wouldn't it work?

> -               if (IS_ERR_OR_NULL(kfunc_btf)) {
> +               if (IS_ERR(kfunc_btf))
>                         verbose(env, "cannot find module BTF for func_id %u\n", func_id);
> -                       return kfunc_btf ?: ERR_PTR(-ENOENT);
> -               }
>                 return kfunc_btf;
>         }
>         return btf_vmlinux ?: ERR_PTR(-ENOENT);
> --
> 2.33.0
>

  reply	other threads:[~2021-10-08 20:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-08 17:07 [PATCH bpf-next] bpf: Silence Coverity warning for find_kfunc_desc_btf Kumar Kartikeya Dwivedi
2021-10-08 20:39 ` Andrii Nakryiko [this message]
2021-10-08 22:27   ` [PATCH bpf-next v2] " Kumar Kartikeya Dwivedi
2021-10-09  4:09   ` [PATCH bpf-next v3] " Kumar Kartikeya Dwivedi
2021-10-20 16:57     ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4BzYpp+VqqL4n1N7-Uw8sySVgvaCagEcVicgumtpK-y68aw@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=memxor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.