bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Udip Pant <udippant@fb.com>
To: Yonghong Song <yhs@fb.com>, Alexei Starovoitov <ast@kernel.org>,
	"Martin Lau" <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Andrii Nakryiko <andriin@fb.com>,
	"David S . Miller" <davem@davemloft.net>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 bpf 1/2] bpf: verifier: check for packet data access based on target prog
Date: Fri, 21 Aug 2020 19:07:26 +0000	[thread overview]
Message-ID: <F6EEEFF4-F749-4D51-9366-1B1845EF0526@fb.com> (raw)
In-Reply-To: <d9df934c-4b64-1e28-cc7e-fb03939d687d@fb.com>



> On 8/20/20, 11:17 PM, "Yonghong Song" <yhs@fb.com> wrote:
>
>
>
> On 8/20/20 11:13 PM, Yonghong Song wrote:
>> 
>> 
>> On 8/20/20 5:28 PM, Udip Pant wrote:
>>> While using dynamic program extension (of type BPF_PROG_TYPE_EXT), we
>>> need to check the program type of the target program to grant the read /
>>> write access to the packet data.
>>>
>>> The BPF_PROG_TYPE_EXT type can be used to extend types such as XDP, SKB
>>> and others. Since the BPF_PROG_TYPE_EXT program type on itself is just a
>>> placeholder for those, we need this extended check for those target
>>> programs to actually work while using this option.
>>>
>>> Tested this with a freplace xdp program. Without this patch, the
>>> verifier fails with error 'cannot write into packet'.
>>>
>>> Signed-off-by: Udip Pant <udippant@fb.com>
>>> ---
>>>   kernel/bpf/verifier.c | 6 +++++-
>>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index ef938f17b944..4d7604430994 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -2629,7 +2629,11 @@ static bool may_access_direct_pkt_data(struct 
>>> bpf_verifier_env *env,
>>>                          const struct bpf_call_arg_meta *meta,
>>>                          enum bpf_access_type t)
>>>   {
>>> -    switch (env->prog->type) {
>>> +    struct bpf_prog *prog = env->prog;
>>> +    enum bpf_prog_type prog_type = prog->aux->linked_prog ?
>>> +          prog->aux->linked_prog->type : prog->type;
>> 
>> I checked the verifier code. There are several places where
>> prog->type is checked and EXT program type will behave differently
>> from the linked program.
>> 
>> Maybe abstract the the above logic to one static function like
>> 
>> static enum bpf_prog_type resolved_prog_type(struct bpf_prog *prog)
>> {
>>      return prog->aux->linked_prog ? prog->aux->linked_prog->type
>>                        : prog->type;
>> }
>> 

Sure.

>> This function can then be used in different places to give the resolved
>> prog type.
>> 
>> Besides here checking pkt access permission,
>> another possible places to consider is return value
>> in function check_return_code(). Currently,
>> for EXT program, the result value can be anything. This may need to
>> be enforced. Could you take a look? It could be others as well.
>> You can take a look at verifier.c by searching "prog->type".
>

Yeah there are few other places in the verifier where it decides without resolving for the 'extended' type. But I am not too sure if it makes sense to extend this logic as part of this commit. For example, as you mentioned, in the check_return_code() it explicitly ignores the return type for the EXT prog (kernel/bpf/verifier.c#L7446).  Likewise, I noticed similar issue inside the check_ld_abs(), where it checks for may_access_skb(env->prog->type).   

I'm happy to extend this logic there as well if deemed appropriate. 

> Note that if the EXT program tries to replace a global subprogram,
> then return value cannot be enforced, just as what Patch #2 example shows.
>
>> 
>>> +
>>> +    switch (prog_type) {
>>>       /* Program types only with direct read access go here! */
>>>       case BPF_PROG_TYPE_LWT_IN:
>>>       case BPF_PROG_TYPE_LWT_OUT:
>>>


  reply	other threads:[~2020-08-21 19:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-21  0:28 [PATCH v2 bpf 1/2] bpf: verifier: check for packet data access based on target prog Udip Pant
2020-08-21  0:28 ` [PATCH v2 bpf 2/2] selftests/bpf: add test for freplace program with write access Udip Pant
2020-08-21  6:19   ` Yonghong Song
2020-08-21  6:13 ` [PATCH v2 bpf 1/2] bpf: verifier: check for packet data access based on target prog Yonghong Song
2020-08-21  6:17   ` Yonghong Song
2020-08-21 19:07     ` Udip Pant [this message]
2020-08-21 20:53       ` Yonghong Song
2020-08-21 21:05         ` Alexei Starovoitov

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=F6EEEFF4-F749-4D51-9366-1B1845EF0526@fb.com \
    --to=udippant@fb.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=kafai@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.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).