BPF List
 help / color / mirror / Atom feed
From: Joanne Koong <joannelkoong@gmail.com>
To: bpf@vger.kernel.org
Cc: daniel@iogearbox.net, martin.lau@kernel.org, andrii@kernel.org,
	ast@kernel.org, Kernel-team@fb.com,
	Joanne Koong <joannelkoong@gmail.com>
Subject: [PATCH bpf-next v1 6/8] bpf: Add verifier support for custom callback return range
Date: Wed,  7 Sep 2022 17:02:52 -0700	[thread overview]
Message-ID: <20220908000254.3079129-7-joannelkoong@gmail.com> (raw)
In-Reply-To: <20220908000254.3079129-1-joannelkoong@gmail.com>

This is ported from Dave Marchevsky's patch [0], which is needed for the
bpf iterators callback to return values beyond the 0, 1 range.

For comments, please refer to Dave's patch.

[0] https://lore.kernel.org/bpf/20220830172759.4069786-2-davemarchevsky@fb.com/

Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
 include/linux/bpf_verifier.h | 1 +
 kernel/bpf/verifier.c        | 5 +++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 8fbc1d05281e..63cda732ee10 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -248,6 +248,7 @@ struct bpf_func_state {
 	 */
 	u32 async_entry_cnt;
 	bool in_callback_fn;
+	struct tnum callback_ret_range;
 	bool in_async_callback_fn;
 
 	/* The following fields should be last. See copy_func_state() */
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 8c8c101513f5..2eb2a4410344 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1814,6 +1814,7 @@ static void init_func_state(struct bpf_verifier_env *env,
 	state->callsite = callsite;
 	state->frameno = frameno;
 	state->subprogno = subprogno;
+	state->callback_ret_range = tnum_range(0, 1);
 	init_reg_state(env, state);
 	mark_verifier_state_scratched(env);
 }
@@ -6974,6 +6975,7 @@ static int set_find_vma_callback_state(struct bpf_verifier_env *env,
 	__mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
 	__mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_callback_fn = true;
+	callee->callback_ret_range = tnum_range(0, 1);
 	return 0;
 }
 
@@ -7000,8 +7002,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 	state->curframe--;
 	caller = state->frame[state->curframe];
 	if (callee->in_callback_fn) {
-		/* enforce R0 return value range [0, 1]. */
-		struct tnum range = tnum_range(0, 1);
+		struct tnum range = callee->callback_ret_range;
 
 		if (r0->type != SCALAR_VALUE) {
 			verbose(env, "R0 not a scalar value\n");
-- 
2.30.2


  parent reply	other threads:[~2022-09-08  0:07 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08  0:02 [PATCH bpf-next v1 0/8] Dynptr convenience helpers Joanne Koong
2022-09-08  0:02 ` [PATCH bpf-next v1 1/8] bpf: Add bpf_dynptr_data_rdonly Joanne Koong
2022-09-09 15:29   ` Song Liu
2022-09-09 15:32     ` Alexei Starovoitov
2022-09-09 15:59       ` Song Liu
2022-09-09 15:51   ` Shmulik Ladkani
2022-09-08  0:02 ` [PATCH bpf-next v1 2/8] bpf: Add bpf_dynptr_trim and bpf_dynptr_advance Joanne Koong
2022-09-09 15:32   ` Song Liu
2022-09-09 16:16   ` Shmulik Ladkani
2022-09-28 22:14   ` Andrii Nakryiko
2022-09-08  0:02 ` [PATCH bpf-next v1 3/8] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly Joanne Koong
2022-09-09 15:46   ` Song Liu
2022-09-09 21:28     ` Joanne Koong
2022-09-09 23:17       ` Song Liu
2022-09-08  0:02 ` [PATCH bpf-next v1 4/8] bpf: Add bpf_dynptr_get_size and bpf_dynptr_get_offset Joanne Koong
2022-09-09 16:52   ` Shmulik Ladkani
2022-09-09 20:37     ` Joanne Koong
2022-09-08  0:02 ` [PATCH bpf-next v1 5/8] bpf: Add bpf_dynptr_clone Joanne Koong
2022-09-09 16:41   ` Shmulik Ladkani
2022-09-09 22:18     ` Joanne Koong
2022-09-10  5:31       ` Shmulik Ladkani
2022-09-28 22:34         ` Andrii Nakryiko
2022-09-28 22:29   ` Andrii Nakryiko
2022-09-08  0:02 ` Joanne Koong [this message]
2022-09-08  0:02 ` [PATCH bpf-next v1 7/8] bpf: Add bpf_dynptr_iterator Joanne Koong
2022-09-19  0:07   ` Kumar Kartikeya Dwivedi
2022-09-28 22:47     ` Andrii Nakryiko
2022-09-28 22:41   ` Andrii Nakryiko
2022-09-29  0:31     ` Kumar Kartikeya Dwivedi
2022-09-29  0:43       ` Andrii Nakryiko
2022-10-02 16:45         ` Kumar Kartikeya Dwivedi
2022-10-03 18:39           ` Andrii Nakryiko
2022-09-08  0:02 ` [PATCH bpf-next v1 8/8] selftests/bpf: Tests for dynptr convenience helpers Joanne Koong

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=20220908000254.3079129-7-joannelkoong@gmail.com \
    --to=joannelkoong@gmail.com \
    --cc=Kernel-team@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=martin.lau@kernel.org \
    /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