From: Yonghong Song <yhs@fb.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Lorenzo Bianconi <lorenzo@kernel.org>,
bpf@vger.kernel.org, netdev@vger.kernel.org, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org, davem@davemloft.net,
kuba@kernel.org, edumazet@google.com, pabeni@redhat.com,
pablo@netfilter.org, fw@strlen.de,
netfilter-devel@vger.kernel.org, lorenzo.bianconi@redhat.com,
brouer@redhat.com, toke@redhat.com
Subject: Re: [PATCH v3 bpf-next 1/5] bpf: Add support for forcing kfunc args to be referenced
Date: Wed, 18 May 2022 11:23:12 -0700 [thread overview]
Message-ID: <05413ff9-3b4b-6100-be54-95c613072fa9@fb.com> (raw)
In-Reply-To: <20220518180613.su37c23ckgc5irmu@apollo.legion>
On 5/18/22 11:06 AM, Kumar Kartikeya Dwivedi wrote:
> On Wed, May 18, 2022 at 11:28:12PM IST, Yonghong Song wrote:
>>
>>
>> On 5/18/22 3:43 AM, Lorenzo Bianconi wrote:
>>> From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>>>
>>> Similar to how we detect mem, size pairs in kfunc, teach verifier to
>>> treat __ref suffix on argument name to imply that it must be a
>>> referenced pointer when passed to kfunc. This is required to ensure that
>>> kfunc that operate on some object only work on acquired pointers and not
>>> normal PTR_TO_BTF_ID with same type which can be obtained by pointer
>>> walking. Release functions need not specify such suffix on release
>>> arguments as they are already expected to receive one referenced
>>> argument.
>>>
>>> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>>> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
>>> ---
>>> kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++----------
>>> net/bpf/test_run.c | 5 +++++
>>> 2 files changed, 35 insertions(+), 10 deletions(-)
>>>
>>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>>> index 2f0b0440131c..83a354732d96 100644
>>> --- a/kernel/bpf/btf.c
>>> +++ b/kernel/bpf/btf.c
>>> @@ -6021,18 +6021,13 @@ static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,
>>> return true;
>>> }
>>> -static bool is_kfunc_arg_mem_size(const struct btf *btf,
>>> - const struct btf_param *arg,
>>> - const struct bpf_reg_state *reg)
>>> +static bool btf_param_match_suffix(const struct btf *btf,
>>> + const struct btf_param *arg,
>>> + const char *suffix)
>>> {
>>> - int len, sfx_len = sizeof("__sz") - 1;
>>> - const struct btf_type *t;
>>> + int len, sfx_len = strlen(suffix);
>>> const char *param_name;
>>> - t = btf_type_skip_modifiers(btf, arg->type, NULL);
>>> - if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE)
>>> - return false;
>>> -
>>> /* In the future, this can be ported to use BTF tagging */
>>> param_name = btf_name_by_offset(btf, arg->name_off);
>>> if (str_is_empty(param_name))
>>> @@ -6041,12 +6036,31 @@ static bool is_kfunc_arg_mem_size(const struct btf *btf,
>>> if (len < sfx_len)
>>> return false;
>>> param_name += len - sfx_len;
>>> - if (strncmp(param_name, "__sz", sfx_len))
>>> + if (strncmp(param_name, suffix, sfx_len))
>>> return false;
>>> return true;
>>> }
>>> +static bool is_kfunc_arg_ref(const struct btf *btf,
>>> + const struct btf_param *arg)
>>> +{
>>> + return btf_param_match_suffix(btf, arg, "__ref");
>>
>> Do we also need to do btf_type_skip_modifiers and to ensure
>> the type after skipping modifiers are a pointer type?
>> The current implementation should work for
>> bpf_kfunc_call_test_ref(), but with additional checking
>> we may avoid some accidental mistakes.
>>
>
> The point where this check happens, arg[i].type is already known to be a pointer
> type, after skipping modifiers.
Okay, maybe we can add a comment here like
/* the arg has been verified as a pointer */
But anyway, this is minor.
Acked-by: Yonghong Song <yhs@fb.com>
>
>>> +}
>>> +
>>> +static bool is_kfunc_arg_mem_size(const struct btf *btf,
>>> + const struct btf_param *arg,
>>> + const struct bpf_reg_state *reg)
>>> +{
>>> + const struct btf_type *t;
>>> +
>>> + t = btf_type_skip_modifiers(btf, arg->type, NULL);
>>> + if (!btf_type_is_scalar(t) || reg->type != SCALAR_VALUE)
>>> + return false;
>>> +
>>> + return btf_param_match_suffix(btf, arg, "__sz");
>>> +}
>>> +
>>> static int btf_check_func_arg_match(struct bpf_verifier_env *env,
>>> const struct btf *btf, u32 func_id,
>>> struct bpf_reg_state *regs,
>>> @@ -6115,6 +6129,12 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
>>> return -EINVAL;
>>> }
>>> + /* Check if argument must be a referenced pointer */
>>> + if (is_kfunc && is_kfunc_arg_ref(btf, args + i) && !reg->ref_obj_id) {
>>> + bpf_log(log, "R%d must be referenced\n", regno);
>>> + return -EINVAL;
>>> + }
>>> +
>>> ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
>>> ref_tname = btf_name_by_offset(btf, ref_t->name_off);
>>> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
>>> index 4d08cca771c7..adbc7dd18511 100644
>>> --- a/net/bpf/test_run.c
>>> +++ b/net/bpf/test_run.c
>>> @@ -690,6 +690,10 @@ noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
>>> {
>>> }
>>> +noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p__ref)
>>> +{
>>> +}
>>> +
>>> __diag_pop();
>> [...]
>
> --
> Kartikeya
next prev parent reply other threads:[~2022-05-18 18:23 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-18 10:43 [PATCH v3 bpf-next 0/5] net: netfilter: add kfunc helper to update ct timeout Lorenzo Bianconi
2022-05-18 10:43 ` [PATCH v3 bpf-next 1/5] bpf: Add support for forcing kfunc args to be referenced Lorenzo Bianconi
2022-05-18 17:58 ` Yonghong Song
2022-05-18 18:06 ` Kumar Kartikeya Dwivedi
2022-05-18 18:23 ` Yonghong Song [this message]
2022-05-18 10:43 ` [PATCH v3 bpf-next 2/5] selftests/bpf: Add verifier selftests for forced kfunc ref args Lorenzo Bianconi
2022-05-18 18:25 ` Yonghong Song
2022-05-18 10:43 ` [PATCH v3 bpf-next 3/5] net: netfilter: add kfunc helper to update ct timeout Lorenzo Bianconi
2022-05-18 20:42 ` Toke Høiland-Jørgensen
2022-05-18 10:43 ` [PATCH v3 bpf-next 4/5] net: netfilter: add kfunc helper to add a new ct entry Lorenzo Bianconi
2022-05-18 20:47 ` Toke Høiland-Jørgensen
2022-05-18 21:08 ` Lorenzo Bianconi
2022-05-18 22:14 ` Alexei Starovoitov
2022-05-18 22:43 ` Kumar Kartikeya Dwivedi
2022-05-19 10:45 ` Toke Høiland-Jørgensen
2022-05-19 11:23 ` Kumar Kartikeya Dwivedi
2022-05-19 17:00 ` Yonghong Song
2022-05-19 17:10 ` Kumar Kartikeya Dwivedi
2022-05-19 2:06 ` kernel test robot
2022-05-18 10:43 ` [PATCH v3 bpf-next 5/5] selftests/bpf: add selftest for bpf_xdp_ct_add and bpf_ct_refresh_timeout kfunc Lorenzo Bianconi
2022-05-18 18:55 ` Yonghong Song
2022-05-18 20:38 ` Lorenzo Bianconi
2022-05-20 22:04 ` Andrii Nakryiko
2022-05-21 9:56 ` Lorenzo Bianconi
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=05413ff9-3b4b-6100-be54-95c613072fa9@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brouer@redhat.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=kuba@kernel.org \
--cc=lorenzo.bianconi@redhat.com \
--cc=lorenzo@kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--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