linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Greg KH <gregkh@linuxfoundation.org>,
	Jiri Kosina <jikos@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Dave Marchevsky <davemarchevsky@fb.com>,
	Joe Stringer <joe@cilium.io>, Jonathan Corbet <corbet@lwn.net>,
	Tero Kristo <tero.kristo@linux.intel.com>,
	LKML <linux-kernel@vger.kernel.org>,
	"open list:HID CORE LAYER" <linux-input@vger.kernel.org>,
	Network Development <netdev@vger.kernel.org>,
	bpf <bpf@vger.kernel.org>,
	"open list:KERNEL SELFTEST FRAMEWORK" 
	<linux-kselftest@vger.kernel.org>,
	"open list:DOCUMENTATION" <linux-doc@vger.kernel.org>
Subject: Re: [PATCH bpf-next v8 02/24] bpf/verifier: allow kfunc to read user provided context
Date: Wed, 24 Aug 2022 11:57:07 +0200	[thread overview]
Message-ID: <5bba0f0e-544e-85ef-627b-6dd35244871a@redhat.com> (raw)
In-Reply-To: <CAO-hwJK5v8An5W48x2TDH=iNb49iEbC8uGwMbdCak0Bjnmea+w@mail.gmail.com>



On 7/25/22 18:36, Benjamin Tissoires wrote:
> On Fri, Jul 22, 2022 at 6:16 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
>>
>> On Fri, Jul 22, 2022 at 1:46 AM Benjamin Tissoires
>> <benjamin.tissoires@redhat.com> wrote:
>>>
>>> When a kfunc was trying to access data from context in a syscall eBPF
>>> program, the verifier was rejecting the call.
>>> This is because the syscall context is not known at compile time, and
>>> so we need to check this when actually accessing it.
>>>
>>> Check for the valid memory access and allow such situation to happen.
>>>
>>> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>>> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
>>>
>>> ---
>>>
>>> changes in v8:
>>> - fixup comment
>>> - return -EACCESS instead of -EINVAL for consistency
>>>
>>> changes in v7:
>>> - renamed access_t into atype
>>> - allow zero-byte read
>>> - check_mem_access() to the correct offset/size
>>>
>>> new in v6
>>> ---
>>>   kernel/bpf/verifier.c | 21 +++++++++++++++++++++
>>>   1 file changed, 21 insertions(+)
>>>
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index 7c1e056624f9..c807c5d7085a 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -248,6 +248,7 @@ struct bpf_call_arg_meta {
>>>          struct bpf_map *map_ptr;
>>>          bool raw_mode;
>>>          bool pkt_access;
>>> +       bool is_kfunc;
>>>          u8 release_regno;
>>>          int regno;
>>>          int access_size;
>>> @@ -5170,6 +5171,7 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
>>>                                     struct bpf_call_arg_meta *meta)
>>>   {
>>>          struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
>>> +       enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
>>>          u32 *max_access;
>>>
>>>          switch (base_type(reg->type)) {
>>> @@ -5223,6 +5225,24 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
>>>                                  env,
>>>                                  regno, reg->off, access_size,
>>>                                  zero_size_allowed, ACCESS_HELPER, meta);
>>> +       case PTR_TO_CTX:
>>> +               /* in case of a kfunc called in a program of type SYSCALL, the context is
>>> +                * user supplied, so not computed statically.
>>> +                * Dynamically check it now
>>> +                */
>>> +               if (prog_type == BPF_PROG_TYPE_SYSCALL && meta && meta->is_kfunc) {
>>
>> prog_type check looks a bit odd here.
>> Can we generalize with
>> if (!env->ops->convert_ctx_access
> 
> Yep, seems to be working fine for my use case and the test cases I
> have in this series.
> 
>>
>> In other words any program type that doesn't have ctx rewrites can
>> use helpers to access ctx fields ?
>>
>> Also why kfunc only?
>> It looks safe to allow normal helpers as well.
> 
> Well, not sure what is happening here, but if I remove the check for
> kfunc, the test for PTR_TO_CTX == NULL and size == 0 gives me a
> -EINVAL.

I finally managed to track down the issue.

The reason was that if we now call check_mem_access for every function 
check, but also subprogs. And so we ensure that a subprog can access 
context.

This is all fine, but that test now tags the subprog accessing the 
context, even if it is actually null and not accessing it in the code.

So to restore the previous behavior, I am storing 
env->prog->aux->max_ctx_offset in btf_check_subprog_arg_match() and 
restore it after the call to check for the arguments.

See the v9 for the detail in the code.

Cheers,
Benjamin

> 
> The original reason for kfunc only was because I wanted to scope the
> changes to something I can control, but now I am completely out of
> ideas on why the NULL test fails if it enters the if branch.
> 
> Unfortunately I won't have a lot of time this week to tackle this (I
> am on holiday with my family), and next will be tough too (at home but
> doing renovations).
> 
> I can send the fixup to remove the prog_type check as I just made sure
> it works with the selftests. But I won't be able to dig further why it
> fails without the kfunc check, because not enough time and
> concentration.
> 
> Cheers,
> Benjamin


  reply	other threads:[~2022-08-24  9:57 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-21 15:36 [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 01/24] selftests/bpf: fix config for CLS_BPF Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 02/24] bpf/verifier: allow kfunc to read user provided context Benjamin Tissoires
2022-07-21 20:15   ` Kumar Kartikeya Dwivedi
2022-07-22  8:43     ` Benjamin Tissoires
2022-07-22  8:45   ` [PATCH bpf-next v8 " Benjamin Tissoires
2022-07-22 16:16     ` Alexei Starovoitov
2022-07-25 16:36       ` Benjamin Tissoires
2022-08-24  9:57         ` Benjamin Tissoires [this message]
2022-07-21 15:36 ` [PATCH bpf-next v7 03/24] bpf/verifier: do not clear meta in check_mem_size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 04/24] selftests/bpf: add test for accessing ctx from syscall program type Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 05/24] bpf/verifier: allow kfunc to return an allocated mem Benjamin Tissoires
2022-07-21 21:05   ` Kumar Kartikeya Dwivedi
2022-07-21 15:36 ` [PATCH bpf-next v7 06/24] selftests/bpf: Add tests for kfunc returning a memory pointer Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 07/24] bpf: prepare for more bpf syscall to be used from kernel and user space Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 08/24] libbpf: add map_get_fd_by_id and map_delete_elem in light skeleton Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 09/24] HID: core: store the unique system identifier in hid_device Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 10/24] HID: export hid_report_type to uapi Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 11/24] HID: convert defines of HID class requests into a proper enum Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 12/24] HID: Kconfig: split HID support and hid-core compilation Benjamin Tissoires
2022-07-28 14:14   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 13/24] HID: initial BPF implementation Benjamin Tissoires
2022-07-28 14:15   ` Greg KH
2022-08-24  9:41     ` Benjamin Tissoires
2022-07-28 14:19   ` Greg KH
2022-08-24  9:48     ` Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 14/24] selftests/bpf: add tests for the HID-bpf initial implementation Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 15/24] HID: bpf: allocate data memory for device_event BPF programs Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 16/24] selftests/bpf/hid: add test to change the report size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 17/24] HID: bpf: introduce hid_hw_request() Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 18/24] selftests/bpf: add tests for bpf_hid_hw_request Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 19/24] HID: bpf: allow to change the report descriptor Benjamin Tissoires
2022-07-28 15:10   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 20/24] selftests/bpf: add report descriptor fixup tests Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 21/24] selftests/bpf: Add a test for BPF_F_INSERT_HEAD Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 22/24] samples/bpf: add new hid_mouse example Benjamin Tissoires
2022-07-28 15:13   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 23/24] HID: bpf: add Surface Dial example Benjamin Tissoires
2022-07-28 15:12   ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 24/24] Documentation: add HID-BPF docs Benjamin Tissoires
2022-08-25  2:52   ` Bagas Sanjaya
2022-08-30 13:30     ` Benjamin Tissoires
2022-08-03  7:41 ` [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Tero Kristo

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=5bba0f0e-544e-85ef-627b-6dd35244871a@redhat.com \
    --to=benjamin.tissoires@redhat.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@fb.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=joe@cilium.io \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=songliubraving@fb.com \
    --cc=tero.kristo@linux.intel.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 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).