All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Yonghong Song <yhs@fb.com>, bpf <bpf@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Kernel Team <kernel-team@fb.com>,
	Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v4 3/4] bpf: Add a kfunc for generic type cast
Date: Sun, 20 Nov 2022 15:47:08 -0800	[thread overview]
Message-ID: <CAADnVQJ3U=R7qtbgdMDpnn41PvMREFECjGTfgFcP81TUDe_89g@mail.gmail.com> (raw)
In-Reply-To: <CAADnVQLMi-5Avxxd89+wpWksZnxGkCxzjDHrBLzuGUoVmp1Azw@mail.gmail.com>

On Sun, Nov 20, 2022 at 3:32 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Sun, Nov 20, 2022 at 2:34 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Mon, Nov 21, 2022 at 02:19:30AM +0530, Kumar Kartikeya Dwivedi wrote:
> > > On Mon, Nov 21, 2022 at 01:46:04AM IST, Alexei Starovoitov wrote:
> > > > On Sun, Nov 20, 2022 at 11:57 AM Yonghong Song <yhs@fb.com> wrote:
> > > > >
> > > > > @@ -8938,6 +8941,24 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
> > > > >                                 regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED;
> > > > >                                 regs[BPF_REG_0].btf = desc_btf;
> > > > >                                 regs[BPF_REG_0].btf_id = meta.ret_btf_id;
> > > > > +                       } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
> > > > > +                               if (!capable(CAP_PERFMON)) {
> > > > > +                                       verbose(env,
> > > > > +                                               "kfunc bpf_rdonly_cast requires CAP_PERFMON capability\n");
> > > > > +                                       return -EACCES;
> > > > > +                               }
> > > >
> > > > Just realized that bpf_cast_to_kern_ctx() has to be
> > > > gated by cap_perfmon as well.
> > > >
> > > > Also the direct capable(CAP_PERFMON) is not quite correct.
> > > > It should at least be perfmon_capable().
> > > > But even better to use env->allow_ptr_leaks here.
> > >
> > > Based on this, I wonder if this needs to be done for bpf_obj_new as well? It
> > > doesn't zero initialize the memory it returns (except special fields, which is
> > > required for correctness), so technically it allows leaking kernel addresses
> > > with just CAP_BPF (apart from capabilities needed for the specific program types
> > > it is available to).
> > >
> > > Should that also have a env->allow_ptr_leaks check?
> >
> > Yeah. Good point.
> > My first reaction was to audit everything where the verifier produces
> > PTR_TO_BTF_ID and gate it with allow_ptr_leaks.
> > But then it looks simpler to gate it once in check_ptr_to_btf_access().
> > Then bpf_rdonly_cast and everything wouldn't need those checks.
>
> Noticed that check_ptr_to_map_access is doing
> "Simulate access to a PTR_TO_BTF_ID"
> and has weird allow_ptr_to_map_access bool
> which is the same as allow_ptr_leaks.
> So I'm thinking we can remove allow_ptr_to_map_access
> and add allow_ptr_leaks check to btf_struct_access()
> which will cover all these cases.
>
> Also since bpf_cast_to_kern_ctx() is expected to be used out of
> networking progs and those progs are not always GPL we should add
> env->prog->gpl_compatible to btf_struct_access() too.

Since that follow up will cover bpf_rdonly_cast too
I've removed cap_perfmon check from this commit and will push
this series shortly.

  reply	other threads:[~2022-11-20 23:47 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-20 19:54 [PATCH bpf-next v4 0/4] bpf: Implement two type cast kfuncs Yonghong Song
2022-11-20 19:54 ` [PATCH bpf-next v4 1/4] bpf: Add support for kfunc set with common btf_ids Yonghong Song
2022-11-20 19:54 ` [PATCH bpf-next v4 2/4] bpf: Add a kfunc to type cast from bpf uapi ctx to kernel ctx Yonghong Song
2022-11-20 19:54 ` [PATCH bpf-next v4 3/4] bpf: Add a kfunc for generic type cast Yonghong Song
2022-11-20 20:16   ` Alexei Starovoitov
2022-11-20 20:49     ` Kumar Kartikeya Dwivedi
2022-11-20 22:34       ` Alexei Starovoitov
2022-11-20 23:32         ` Alexei Starovoitov
2022-11-20 23:47           ` Alexei Starovoitov [this message]
2022-11-20 19:54 ` [PATCH bpf-next v4 4/4] bpf: Add type cast unit tests Yonghong Song
2022-11-21  0:00 ` [PATCH bpf-next v4 0/4] bpf: Implement two type cast kfuncs patchwork-bot+netdevbpf
2022-11-21  2:10 ` John Fastabend
2022-11-21 17:29   ` Yonghong Song
2022-11-22  1:48     ` John Fastabend
2022-11-22  4:52       ` Alexei Starovoitov
2022-11-23  3:18         ` John Fastabend
2022-11-23 20:46           ` Alexei Starovoitov
2022-11-29 16:30             ` Alan Maguire

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='CAADnVQJ3U=R7qtbgdMDpnn41PvMREFECjGTfgFcP81TUDe_89g@mail.gmail.com' \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=memxor@gmail.com \
    --cc=yhs@fb.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.