BPF List
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, "Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>
Subject: Re: [PATCH bpf-next v3 06/13] bpf: Prevent escaping of kptr loaded from maps
Date: Tue, 22 Mar 2022 12:48:18 +0530	[thread overview]
Message-ID: <20220322071818.u7qb5ariyzkum3lm@apollo> (raw)
In-Reply-To: <CAEf4BzbMKspdkz2N39+uO-pqQjBRXHGP5+Y6WfNAnUksPpos4Q@mail.gmail.com>

On Tue, Mar 22, 2022 at 11:28:26AM IST, Andrii Nakryiko wrote:
> On Sun, Mar 20, 2022 at 8:55 AM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > While we can guarantee that even for unreferenced kptr, the object
> > pointer points to being freed etc. can be handled by the verifier's
> > exception handling (normal load patching to PROBE_MEM loads), we still
> > cannot allow the user to pass these pointers to BPF helpers and kfunc,
> > because the same exception handling won't be done for accesses inside
> > the kernel. The same is true if a referenced pointer is loaded using
> > normal load instruction. Since the reference is not guaranteed to be
> > held while the pointer is used, it must be marked as untrusted.
> >
> > Hence introduce a new type flag, PTR_UNTRUSTED, which is used to mark
> > all registers loading unreferenced and referenced kptr from BPF maps,
> > and ensure they can never escape the BPF program and into the kernel by
> > way of calling stable/unstable helpers.
> >
> > In check_ptr_to_btf_access, the !type_may_be_null check to reject type
> > flags is still correct, as apart from PTR_MAYBE_NULL, only MEM_USER,
> > MEM_PERCPU, and PTR_UNTRUSTED may be set for PTR_TO_BTF_ID. The first
> > two are checked inside the function and rejected using a proper error
> > message, but we still want to allow dereference of untrusted case.
> >
> > Also, we make sure to inherit PTR_UNTRUSTED when chain of pointers are
> > walked, so that this flag is never dropped once it has been set on a
> > PTR_TO_BTF_ID (i.e. trusted to untrusted transition can only be in one
> > direction).
> >
> > In convert_ctx_accesses, extend the switch case to consider untrusted
> > PTR_TO_BTF_ID in addition to normal PTR_TO_BTF_ID for PROBE_MEM
> > conversion for BPF_LDX.
> >
> > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > ---
> >  include/linux/bpf.h   | 10 +++++++++-
> >  kernel/bpf/verifier.c | 34 +++++++++++++++++++++++++++-------
> >  2 files changed, 36 insertions(+), 8 deletions(-)
> >
>
> [...]
>
> > -       if (reg->type != PTR_TO_BTF_ID && reg->type != PTR_TO_BTF_ID_OR_NULL)
> > -               goto bad_type;
> > +       if (off_desc->flags & BPF_MAP_VALUE_OFF_F_REF) {
> > +               if (reg->type != PTR_TO_BTF_ID &&
> > +                   reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL))
> > +                       goto bad_type;
> > +       } else { /* only unreferenced case accepts untrusted pointers */
> > +               if (reg->type != PTR_TO_BTF_ID &&
> > +                   reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL) &&
> > +                   reg->type != (PTR_TO_BTF_ID | PTR_UNTRUSTED) &&
> > +                   reg->type != (PTR_TO_BTF_ID | PTR_MAYBE_NULL | PTR_UNTRUSTED))
>
> use base_type(), Luke! ;)
>

Ack, will switch.

> > +                       goto bad_type;
> > +       }
> >
> >         if (!btf_is_kernel(reg->btf)) {
> >                 verbose(env, "R%d must point to kernel BTF\n", regno);
>
> [...]

--
Kartikeya

  reply	other threads:[~2022-03-22  7:18 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-20 15:54 [PATCH bpf-next v3 00/13] Introduce typed pointer support in BPF maps Kumar Kartikeya Dwivedi
2022-03-20 15:54 ` [PATCH bpf-next v3 01/13] bpf: Make btf_find_field more generic Kumar Kartikeya Dwivedi
2022-03-20 15:54 ` [PATCH bpf-next v3 02/13] bpf: Move check_ptr_off_reg before check_map_access Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 03/13] bpf: Allow storing unreferenced kptr in map Kumar Kartikeya Dwivedi
2022-03-21 23:39   ` Joanne Koong
2022-03-22  7:04     ` Kumar Kartikeya Dwivedi
2022-03-22 20:22       ` Andrii Nakryiko
2022-03-25 14:51         ` Kumar Kartikeya Dwivedi
2022-03-22  5:45   ` Andrii Nakryiko
2022-03-22  7:16     ` Kumar Kartikeya Dwivedi
2022-03-22  7:43       ` Kumar Kartikeya Dwivedi
2022-03-22 18:52       ` Andrii Nakryiko
2022-03-25 14:42         ` Kumar Kartikeya Dwivedi
2022-03-25 22:59           ` Andrii Nakryiko
2022-03-22 18:06   ` Martin KaFai Lau
2022-03-25 14:45     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 04/13] bpf: Indicate argument that will be released in bpf_func_proto Kumar Kartikeya Dwivedi
2022-03-22  1:47   ` Joanne Koong
2022-03-22  7:34     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 05/13] bpf: Allow storing referenced kptr in map Kumar Kartikeya Dwivedi
2022-03-22 20:59   ` Martin KaFai Lau
2022-03-25 14:57     ` Kumar Kartikeya Dwivedi
2022-03-25 23:39       ` Martin KaFai Lau
2022-03-26  1:01         ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 06/13] bpf: Prevent escaping of kptr loaded from maps Kumar Kartikeya Dwivedi
2022-03-22  5:58   ` Andrii Nakryiko
2022-03-22  7:18     ` Kumar Kartikeya Dwivedi [this message]
2022-03-20 15:55 ` [PATCH bpf-next v3 07/13] bpf: Adapt copy_map_value for multiple offset case Kumar Kartikeya Dwivedi
2022-03-22 20:38   ` Andrii Nakryiko
2022-03-25 15:06     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 08/13] bpf: Populate pairs of btf_id and destructor kfunc in btf Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 09/13] bpf: Wire up freeing of referenced kptr Kumar Kartikeya Dwivedi
2022-03-22 20:51   ` Andrii Nakryiko
2022-03-25 14:50     ` Kumar Kartikeya Dwivedi
2022-03-22 21:10   ` Alexei Starovoitov
2022-03-25 15:07     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 10/13] bpf: Teach verifier about kptr_get kfunc helpers Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 11/13] libbpf: Add kptr type tag macros to bpf_helpers.h Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 12/13] selftests/bpf: Add C tests for kptr Kumar Kartikeya Dwivedi
2022-03-22 21:00   ` Andrii Nakryiko
2022-03-25 14:52     ` Kumar Kartikeya Dwivedi
2022-03-24  9:10   ` Jiri Olsa
2022-03-25 14:52     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 13/13] selftests/bpf: Add verifier " Kumar Kartikeya Dwivedi

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=20220322071818.u7qb5ariyzkum3lm@apollo \
    --to=memxor@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=toke@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox