From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Joanne Koong <joannelkoong@gmail.com>
Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>,
bpf@vger.kernel.org, martin.lau@kernel.org, andrii@kernel.org,
ast@kernel.org, daniel@iogearbox.net, netdev@vger.kernel.org,
toke@kernel.org
Subject: Re: [PATCH v13 bpf-next 03/10] bpf: Allow initializing dynptrs in kfuncs
Date: Tue, 7 Mar 2023 15:53:06 -0800 [thread overview]
Message-ID: <CAEf4BzbJTwG6cZ_Oq+ViqR4BiZ+VyVn0q9iYZbyb21ZwdLP9Wg@mail.gmail.com> (raw)
In-Reply-To: <CAJnrk1ZF5FEtXKsMEnwbLu5qr-mQ6-j9+PK2j1NEf=hLE1CCKQ@mail.gmail.com>
On Mon, Mar 6, 2023 at 10:54 PM Joanne Koong <joannelkoong@gmail.com> wrote:
>
> On Sun, Mar 5, 2023 at 11:36 PM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > On Wed, Mar 01, 2023 at 04:49:46PM CET, Joanne Koong wrote:
> > > This change allows kfuncs to take in an uninitialized dynptr as a
> > > parameter. Before this change, only helper functions could successfully
> > > use uninitialized dynptrs. This change moves the memory access check
> > > (including stack state growing and slot marking) into
> > > process_dynptr_func(), which both helpers and kfuncs call into.
> > >
> > > Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
> > > ---
> > > kernel/bpf/verifier.c | 67 ++++++++++++++-----------------------------
> > > 1 file changed, 22 insertions(+), 45 deletions(-)
> > >
> > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > > index e0e00509846b..82e39fc5ed05 100644
> > > --- a/kernel/bpf/verifier.c
> > > +++ b/kernel/bpf/verifier.c
> > > @@ -268,7 +268,6 @@ struct bpf_call_arg_meta {
> > > u32 ret_btf_id;
> > > u32 subprogno;
> > > struct btf_field *kptr_field;
> > > - u8 uninit_dynptr_regno;
> > > };
> > >
> > > struct btf *btf_vmlinux;
> > > @@ -6225,10 +6224,11 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
> > > * Helpers which do not mutate the bpf_dynptr set MEM_RDONLY in their argument
> > > * type, and declare it as 'const struct bpf_dynptr *' in their prototype.
> > > */
> > > -static int process_dynptr_func(struct bpf_verifier_env *env, int regno,
> > > - enum bpf_arg_type arg_type, struct bpf_call_arg_meta *meta)
> > > +static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
> > > + enum bpf_arg_type arg_type)
> > > {
> > > struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
> > > + int err;
> > >
> > > /* MEM_UNINIT and MEM_RDONLY are exclusive, when applied to an
> > > * ARG_PTR_TO_DYNPTR (or ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_*):
> > > @@ -6254,23 +6254,23 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno,
> > > * to.
> > > */
> > > if (arg_type & MEM_UNINIT) {
> > > + int i;
> > > +
> > > if (!is_dynptr_reg_valid_uninit(env, reg)) {
> > > verbose(env, "Dynptr has to be an uninitialized dynptr\n");
> > > return -EINVAL;
> > > }
> > >
> > > - /* We only support one dynptr being uninitialized at the moment,
> > > - * which is sufficient for the helper functions we have right now.
> > > - */
> > > - if (meta->uninit_dynptr_regno) {
> > > - verbose(env, "verifier internal error: multiple uninitialized dynptr args\n");
> > > - return -EFAULT;
> > > + /* we write BPF_DW bits (8 bytes) at a time */
> > > + for (i = 0; i < BPF_DYNPTR_SIZE; i += 8) {
> > > + err = check_mem_access(env, insn_idx, regno,
> > > + i, BPF_DW, BPF_WRITE, -1, false);
> > > + if (err)
> > > + return err;
> > > }
> >
> > I am not sure moving check_mem_access into process_dynptr_func is the right
> > thing to do. Not sure if a problem already, but sooner or later it might be.
> >
> > The side effects of the call should take effect on the current state only after
> > we have gone through all arguments for the helper/kfunc call. In this case we
> > will now do stack access while processing the dynptr arg, which may affect the
> > state of stack we see through other memory arguments coming later.
> >
> > I think it is better to do it after argument processing is done, similar to
> > existing meta.access_size handling which is done after check_func_arg loop (for
> > the same reasons).
> >
>
> Thanks for taking a look. I don't have a strong preference for either
> so if you do feel strongly about doing the check_mem_access() only
> after argument processing, I'm happy to change it. The
> check_mem_access() call on the dyntpr will mark only the dynptr stack
> slots, so I don't fully see how it may affect the state of stack
> through other memory arguments coming later, but I do see your point
> about keeping the logic more separated out.
FWIW, I did a similar approach for iters as well. And I suspect it's
not the only place where we do similar things while processing helper
arguments, etc.
Let's keep this in mind, but I wouldn't necessarily go complicating
code right now with more of "let's record some info for later" and
then "ok, we recorded something before, let's act on it".
>
> > > [...]
next prev parent reply other threads:[~2023-03-07 23:53 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-01 15:49 [PATCH v13 bpf-next 00/10] Add skb + xdp dynptrs Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 01/10] bpf: Support "sk_buff" and "xdp_buff" as valid kfunc arg types Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 02/10] bpf: Refactor process_dynptr_func Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 03/10] bpf: Allow initializing dynptrs in kfuncs Joanne Koong
2023-03-06 7:36 ` Kumar Kartikeya Dwivedi
2023-03-07 6:53 ` Joanne Koong
2023-03-07 23:53 ` Andrii Nakryiko [this message]
2023-03-01 15:49 ` [PATCH v13 bpf-next 04/10] bpf: Define no-ops for externally called bpf dynptr functions Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 05/10] bpf: Refactor verifier dynptr into get_dynptr_arg_reg Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 06/10] bpf: Add __uninit kfunc annotation Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 07/10] bpf: Add skb dynptrs Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 08/10] bpf: Add xdp dynptrs Joanne Koong
2023-03-01 15:49 ` [PATCH v13 bpf-next 09/10] bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr Joanne Koong
2023-03-02 3:29 ` kernel test robot
2023-03-02 3:53 ` Joanne Koong
2023-03-06 7:10 ` Kumar Kartikeya Dwivedi
2023-03-07 2:23 ` Alexei Starovoitov
2023-03-07 10:22 ` Kumar Kartikeya Dwivedi
2023-03-07 15:45 ` Alexei Starovoitov
2023-03-07 17:35 ` Kumar Kartikeya Dwivedi
2023-03-08 0:01 ` Andrii Nakryiko
2023-03-10 21:15 ` Alexei Starovoitov
2023-03-10 21:29 ` Andrii Nakryiko
2023-03-10 21:54 ` Kumar Kartikeya Dwivedi
2023-03-10 21:54 ` Alexei Starovoitov
2023-03-13 6:31 ` Joanne Koong
2023-03-13 14:41 ` Kumar Kartikeya Dwivedi
2023-03-16 18:55 ` Andrii Nakryiko
2023-03-27 7:47 ` Joanne Koong
2023-03-28 21:42 ` Andrii Nakryiko
2023-04-09 0:22 ` Joanne Koong
2023-04-12 19:05 ` Andrii Nakryiko
2023-03-10 21:38 ` Kumar Kartikeya Dwivedi
2023-03-10 21:49 ` Alexei Starovoitov
2023-03-01 15:49 ` [PATCH v13 bpf-next 10/10] selftests/bpf: tests for using dynptrs to parse skb and xdp buffers Joanne Koong
2023-03-01 18:08 ` Alexei Starovoitov
2023-03-01 18:43 ` Andrii Nakryiko
2023-03-02 4:28 ` Joanne Koong
2023-03-08 1:55 ` Ilya Leoshkevich
2023-03-08 7:22 ` Joanne Koong
2023-03-08 14:24 ` Ilya Leoshkevich
2023-03-09 8:13 ` Joanne Koong
2023-03-10 3:40 ` Ilya Leoshkevich
2023-03-10 5:12 ` Stanislav Fomichev
2023-03-10 17:43 ` Alexei Starovoitov
2023-03-01 18:10 ` [PATCH v13 bpf-next 00/10] Add skb + xdp dynptrs patchwork-bot+netdevbpf
2023-03-08 8:16 ` Jakub Kicinski
2023-03-08 17:08 ` Andrii Nakryiko
2023-03-08 17:28 ` Jakub Kicinski
2023-03-08 19:02 ` 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=CAEf4BzbJTwG6cZ_Oq+ViqR4BiZ+VyVn0q9iYZbyb21ZwdLP9Wg@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=joannelkoong@gmail.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=toke@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).