From: Lorenzo Bianconi <lorenzo@kernel.org>
To: bpf@vger.kernel.org
Cc: 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,
memxor@gmail.com, yhs@fb.com
Subject: [PATCH v4 bpf-next 01/14] bpf: Add support for forcing kfunc args to be referenced
Date: Thu, 26 May 2022 23:34:49 +0200 [thread overview]
Message-ID: <2c1777dfbc5d3ff06f45bc77dac218db9bdae119.1653600577.git.lorenzo@kernel.org> (raw)
In-Reply-To: <cover.1653600577.git.lorenzo@kernel.org>
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.
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
kernel/bpf/btf.c | 42 ++++++++++++++++++++++++++++++++----------
net/bpf/test_run.c | 5 +++++
2 files changed, 37 insertions(+), 10 deletions(-)
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 7bccaa4646e5..9f8dec0ab924 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6023,18 +6023,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))
@@ -6043,12 +6038,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");
+}
+
+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,
@@ -6117,6 +6131,14 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
return -EINVAL;
}
+ /* Check if argument must be a referenced pointer, args + i has
+ * been verified to be a pointer (after skipping modifiers).
+ */
+ 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 56f059b3c242..4b796e0a9667 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -691,6 +691,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();
ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
@@ -714,6 +718,7 @@ BTF_ID(func, bpf_kfunc_call_test_fail3)
BTF_ID(func, bpf_kfunc_call_test_mem_len_pass1)
BTF_ID(func, bpf_kfunc_call_test_mem_len_fail1)
BTF_ID(func, bpf_kfunc_call_test_mem_len_fail2)
+BTF_ID(func, bpf_kfunc_call_test_ref)
BTF_SET_END(test_sk_check_kfunc_ids)
BTF_SET_START(test_sk_acquire_kfunc_ids)
--
2.35.3
next prev parent reply other threads:[~2022-05-26 21:35 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-26 21:34 [PATCH v4 bpf-next 00/14] net: netfilter: add kfunc helper to update ct timeout Lorenzo Bianconi
2022-05-26 21:34 ` Lorenzo Bianconi [this message]
2022-05-26 21:34 ` [PATCH v4 bpf-next 02/14] bpf: Print multiple type flags in verifier log Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 03/14] bpf: Support rdonly PTR_TO_BTF_ID for pointer to const return value Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 04/14] bpf: Support storing rdonly PTR_TO_BTF_ID in BPF maps Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 05/14] bpf: Support passing rdonly PTR_TO_BTF_ID to kfunc Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 06/14] bpf: Whitelist some fields in nf_conn for BPF_WRITE Lorenzo Bianconi
2022-05-26 21:45 ` Florian Westphal
2022-05-27 11:36 ` Kumar Kartikeya Dwivedi
2022-05-27 12:02 ` Florian Westphal
2022-05-26 23:55 ` kernel test robot
2022-05-27 7:34 ` kernel test robot
2022-05-27 9:17 ` kernel test robot
2022-05-26 21:34 ` [PATCH v4 bpf-next 07/14] bpf: Define acquire-release pairs for kfuncs Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 08/14] selftests/bpf: Add verifier tests for forced kfunc ref args Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 09/14] selftests/bpf: Add C tests for rdonly PTR_TO_BTF_ID Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 10/14] selftests/bpf: Add verifier " Lorenzo Bianconi
2022-05-26 21:34 ` [PATCH v4 bpf-next 11/14] net: netfilter: add kfunc helper to update ct timeout Lorenzo Bianconi
2022-05-26 21:35 ` [PATCH v4 bpf-next 12/14] net: netfilter: add kfunc helpers to alloc and insert a new ct entry Lorenzo Bianconi
2022-05-26 21:35 ` [PATCH v4 bpf-next 13/14] selftests/bpf: add selftest for bpf_xdp_ct_add and bpf_ct_refresh_timeout kfunc Lorenzo Bianconi
2022-05-26 21:35 ` [PATCH v4 bpf-next 14/14] selftests/bpf: Add negative tests for bpf_nf Lorenzo Bianconi
2022-06-11 20:11 ` [PATCH v4 bpf-next 00/14] net: netfilter: add kfunc helper to update ct timeout Alexei Starovoitov
2022-06-13 16:14 ` Kumar Kartikeya Dwivedi
2022-06-13 22:15 ` Alexei Starovoitov
2022-06-14 2:23 ` Kumar Kartikeya Dwivedi
2022-06-17 20:45 ` 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=2c1777dfbc5d3ff06f45bc77dac218db9bdae119.1653600577.git.lorenzo@kernel.org \
--to=lorenzo@kernel.org \
--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=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 \
--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).