bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matteo Croce <mcroce@linux.microsoft.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next] bpf: limit bpf_core_types_are_compat() recursion
Date: Wed, 15 Dec 2021 15:53:38 +0100	[thread overview]
Message-ID: <CAFnufp33Dm_5gffiFYQ+Maf4Bj9fE3WLMpFf3cJ=F5mm71mTEQ@mail.gmail.com> (raw)
In-Reply-To: <CAADnVQJRVpL0HL=Lz8_e-ZU5y0WrQ_Z0KvQXF2w8rE660Jr62g@mail.gmail.com>

On Wed, Dec 15, 2021 at 6:57 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Fri, Dec 10, 2021 at 9:20 AM Matteo Croce <mcroce@linux.microsoft.com> wrote:
> >
> > From: Matteo Croce <mcroce@microsoft.com>
> >
> > In userspace, bpf_core_types_are_compat() is a recursive function which
> > can't be put in the kernel as is.
> > Limit the recursion depth to 2, to avoid potential stack overflows
> > in kernel.
> >
> > Signed-off-by: Matteo Croce <mcroce@microsoft.com>
>
> Thank you for taking a stab at it!
>
> > +#define MAX_TYPES_ARE_COMPAT_DEPTH 2
> > +
> > +static
> > +int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
> > +                               const struct btf *targ_btf, __u32 targ_id,
> > +                               int level)
> > +{
> > +       const struct btf_type *local_type, *targ_type;
> > +       int depth = 32; /* max recursion depth */
> > +
> > +       if (level <= 0)
> > +               return -EINVAL;
> > +
> > +       /* caller made sure that names match (ignoring flavor suffix) */
> > +       local_type = btf_type_by_id(local_btf, local_id);
> > +       targ_type = btf_type_by_id(targ_btf, targ_id);
> > +       if (btf_kind(local_type) != btf_kind(targ_type))
> > +               return 0;
> > +
> > +recur:
> > +       depth--;
> > +       if (depth < 0)
> > +               return -EINVAL;
> > +
> > +       local_type = btf_type_skip_modifiers(local_btf, local_id, &local_id);
> > +       targ_type = btf_type_skip_modifiers(targ_btf, targ_id, &targ_id);
> > +       if (!local_type || !targ_type)
> > +               return -EINVAL;
> > +
> > +       if (btf_kind(local_type) != btf_kind(targ_type))
> > +               return 0;
> > +
> > +       switch (btf_kind(local_type)) {
> > +       case BTF_KIND_UNKN:
> > +       case BTF_KIND_STRUCT:
> > +       case BTF_KIND_UNION:
> > +       case BTF_KIND_ENUM:
> > +       case BTF_KIND_FWD:
> > +               return 1;
> > +       case BTF_KIND_INT:
> > +               /* just reject deprecated bitfield-like integers; all other
> > +                * integers are by default compatible between each other
> > +                */
> > +               return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
> > +       case BTF_KIND_PTR:
> > +               local_id = local_type->type;
> > +               targ_id = targ_type->type;
> > +               goto recur;
> > +       case BTF_KIND_ARRAY:
> > +               local_id = btf_array(local_type)->type;
> > +               targ_id = btf_array(targ_type)->type;
> > +               goto recur;
> > +       case BTF_KIND_FUNC_PROTO: {
> > +               struct btf_param *local_p = btf_params(local_type);
> > +               struct btf_param *targ_p = btf_params(targ_type);
> > +               __u16 local_vlen = btf_vlen(local_type);
> > +               __u16 targ_vlen = btf_vlen(targ_type);
> > +               int i, err;
> > +
> > +               if (local_vlen != targ_vlen)
> > +                       return 0;
> > +
> > +               for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
> > +                       btf_type_skip_modifiers(local_btf, local_p->type, &local_id);
> > +                       btf_type_skip_modifiers(targ_btf, targ_p->type, &targ_id);
>
>
> Maybe do a level check here?
> Since calling it and immediately returning doesn't conserve
> the stack.
> If it gets called it can finish fine, but
> calling it again would be too much.
> In other words checking the level here gives us
> room for one more frame.
>

I thought that the compiler was smart enough to return before
allocating most of the frame.
I tried and this is true only with gcc, not with clang.

> > +                       err = __bpf_core_types_are_compat(local_btf, local_id,
> > +                                                         targ_btf, targ_id,
> > +                                                         level - 1);
> > +                       if (err <= 0)
> > +                               return err;
> > +               }
> > +
> > +               /* tail recurse for return type check */
> > +               btf_type_skip_modifiers(local_btf, local_type->type, &local_id);
> > +               btf_type_skip_modifiers(targ_btf, targ_type->type, &targ_id);
> > +               goto recur;
> > +       }
> > +       default:
> > +               pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
> > +                       btf_type_str(local_type), local_id, targ_id);
>
> That should be bpf_log() instead.
>

To do that I need a struct bpf_verifier_log, which is not present
there, neither in bpf_core_spec_match() or bpf_core_apply_relo_insn().
Should we drop the message at all?

> > +               return 0;
> > +       }
> > +}
>
> Please add tests that exercise this logic by enabling
> additional lskels and a new test that hits the recursion limit.
> I suspect we don't have such case in selftests.
>
> Thanks!

Will do!

Regards,
-- 
per aspera ad upstream

  reply	other threads:[~2021-12-15 14:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 17:20 [PATCH bpf-next] bpf: limit bpf_core_types_are_compat() recursion Matteo Croce
2021-12-15  5:56 ` Alexei Starovoitov
2021-12-15 14:53   ` Matteo Croce [this message]
2021-12-15 17:29     ` Alexei Starovoitov
2021-12-15 18:21       ` Matteo Croce
2021-12-17 19:31         ` Matteo Croce
2021-12-21  6:33           ` Yonghong Song
2022-01-28  5:31             ` Alexei Starovoitov
2022-01-28 18:51               ` Matteo Croce
2022-01-28 20:08                 ` Alexei Starovoitov
2022-01-29  0:35                   ` Matteo Croce
2022-01-29  1:11                     ` Alexei Starovoitov
2022-02-02 18:30                       ` Matteo Croce

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='CAFnufp33Dm_5gffiFYQ+Maf4Bj9fE3WLMpFf3cJ=F5mm71mTEQ@mail.gmail.com' \
    --to=mcroce@linux.microsoft.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.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).