From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
<martin.lau@kernel.org>
Cc: <andrii@kernel.org>, <kernel-team@meta.com>
Subject: [PATCH bpf-next 07/10] bpf: unify async callback and program retval checks
Date: Tue, 21 Nov 2023 17:16:53 -0800 [thread overview]
Message-ID: <20231122011656.1105943-8-andrii@kernel.org> (raw)
In-Reply-To: <20231122011656.1105943-1-andrii@kernel.org>
Use common logic to verify program return values and async callback
return values. This allows to avoid duplication of any extra steps
necessary, like precision marking, which will be added in the next
patch.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
kernel/bpf/verifier.c | 30 ++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index f2bf7593289b..87d720d44e0c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -358,7 +358,7 @@ static void verbose_invalid_scalar(struct bpf_verifier_env *env,
char tn_buf[48];
bool unknown = true;
- verbose(env, "At %s the register %s has", ctx, reg_name);
+ verbose(env, "%s the register %s has", ctx, reg_name);
if (reg->umin_value > 0) {
verbose(env, " umin=%llu", reg->umin_value);
unknown = false;
@@ -9532,7 +9532,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
/* enforce R0 return value range */
if (!retval_range_within(callee->callback_ret_range, r0)) {
verbose_invalid_scalar(env, r0, callee->callback_ret_range,
- "callback return", "R0");
+ "At callback return", "R0");
return -EINVAL;
}
} else {
@@ -14858,11 +14858,11 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
static int check_return_code(struct bpf_verifier_env *env, int regno, const char *reg_name)
{
+ const char *exit_ctx = "At program exit";
struct tnum enforce_attach_type_range = tnum_unknown;
const struct bpf_prog *prog = env->prog;
struct bpf_reg_state *reg;
struct bpf_retval_range range = retval_range(0, 1);
- struct bpf_retval_range const_0 = retval_range(0, 0);
enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
int err;
struct bpf_func_state *frame = env->cur_state->frame[0];
@@ -14904,17 +14904,9 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
if (frame->in_async_callback_fn) {
/* enforce return zero from async callbacks like timer */
- if (reg->type != SCALAR_VALUE) {
- verbose(env, "In async callback the register R%d is not a known value (%s)\n",
- regno, reg_type_str(env, reg->type));
- return -EINVAL;
- }
-
- if (!retval_range_within(const_0, reg)) {
- verbose_invalid_scalar(env, reg, const_0, "async callback", reg_name);
- return -EINVAL;
- }
- return 0;
+ exit_ctx = "At async callback return";
+ range = retval_range(0, 0);
+ goto enforce_retval;
}
if (is_subprog && !frame->in_exception_callback_fn) {
@@ -15004,15 +14996,17 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
return 0;
}
+enforce_retval:
if (reg->type != SCALAR_VALUE) {
- verbose(env, "At program exit the register R%d is not a known value (%s)\n",
- regno, reg_type_str(env, reg->type));
+ verbose(env, "%s the register R%d is not a known value (%s)\n",
+ exit_ctx, regno, reg_type_str(env, reg->type));
return -EINVAL;
}
if (!retval_range_within(range, reg)) {
- verbose_invalid_scalar(env, reg, range, "program exit", reg_name);
- if (prog->expected_attach_type == BPF_LSM_CGROUP &&
+ verbose_invalid_scalar(env, reg, range, exit_ctx, reg_name);
+ if (!is_subprog &&
+ prog->expected_attach_type == BPF_LSM_CGROUP &&
prog_type == BPF_PROG_TYPE_LSM &&
!prog->aux->attach_func_proto->type)
verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n");
--
2.34.1
next prev parent reply other threads:[~2023-11-22 1:17 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-22 1:16 [PATCH bpf-next 00/10] BPF verifier retval logic fixes Andrii Nakryiko
2023-11-22 1:16 ` [PATCH bpf-next 01/10] bpf: rearrange bpf_func_state fields to save a bit of memory Andrii Nakryiko
2023-11-22 15:12 ` Eduard Zingerman
2023-11-22 1:16 ` [PATCH bpf-next 02/10] bpf: provide correct register name for exception callback retval check Andrii Nakryiko
2023-11-22 15:12 ` Eduard Zingerman
2023-11-23 1:44 ` Alexei Starovoitov
2023-11-24 3:39 ` Andrii Nakryiko
2023-11-22 1:16 ` [PATCH bpf-next 03/10] bpf: enforce precision of R0 on callback return Andrii Nakryiko
2023-11-22 15:12 ` Eduard Zingerman
2023-11-22 1:16 ` [PATCH bpf-next 04/10] bpf: enforce exact retval range on subprog/callback exit Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
2023-11-22 17:45 ` Andrii Nakryiko
2023-11-27 10:55 ` Shung-Hsi Yu
2023-11-27 18:19 ` Andrii Nakryiko
2023-11-22 1:16 ` [PATCH bpf-next 05/10] selftests/bpf: add selftest validating callback result is enforced Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
2023-11-22 17:46 ` Andrii Nakryiko
2023-11-22 1:16 ` [PATCH bpf-next 06/10] bpf: enforce precise retval range on program exit Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
2023-11-22 1:16 ` Andrii Nakryiko [this message]
2023-11-22 15:13 ` [PATCH bpf-next 07/10] bpf: unify async callback and program retval checks Eduard Zingerman
2023-11-22 1:16 ` [PATCH bpf-next 08/10] bpf: enforce precision of R0 on program/async callback return Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
2023-11-22 1:16 ` [PATCH bpf-next 09/10] selftests/bpf: validate async callback return value check correctness Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
2023-11-22 1:16 ` [PATCH bpf-next 10/10] selftests/bpf: adjust global_func15 test to validate prog exit precision Andrii Nakryiko
2023-11-22 15:13 ` Eduard Zingerman
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=20231122011656.1105943-8-andrii@kernel.org \
--to=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@meta.com \
--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