From: Eduard Zingerman <eddyz87@gmail.com>
To: Yonghong Song <yonghong.song@linux.dev>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next v4 07/13] bpf: Add verifier support for 16-byte returns in R0:R2
Date: Wed, 12 Aug 2026 15:12:52 -0700 [thread overview]
Message-ID: <e6f6247b319dde9f055092cd337a61292887b141.camel@gmail.com> (raw)
In-Reply-To: <20260811000947.2381981-1-yonghong.song@linux.dev>
On Mon, 2026-08-10 at 17:09 -0700, Yonghong Song wrote:
...
> @@ -9810,10 +9827,14 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
> struct bpf_func_state *caller, *callee;
> struct bpf_reg_state *r0;
> bool in_callback_fn;
> + u32 i, nregs;
> int err;
>
> callee = state->frame[state->curframe];
> r0 = &callee->regs[BPF_REG_0];
> + nregs = bpf_ret_reg_pair(env, callee->subprogno) ? 2 : 1;
> + if (nregs > 1)
> + env->prog->jit_required = 1;
Definitely let's move this to bpf_compute_subprog_ret_regs()
instead of setting it in multiple places.
> if (r0->type == PTR_TO_STACK) {
> /* technically it's ok to return caller's stack pointer
> * (or caller's caller's pointer) back to the caller,
> @@ -9849,8 +9870,23 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
> return -EFAULT;
> }
> } else {
> - /* return to the caller whatever r0 had in the callee */
> - caller->regs[BPF_REG_0] = *r0;
> + /*
> + * return to the caller whatever the callee had in the
> + * return register(s)
> + */
> + for (i = 0; i < nregs; i++)
> + caller->regs[ret_regs[i]] = callee->regs[ret_regs[i]];
> +
> + /*
> + * R2 carries only the upper half of a register pair return
> + * value. A stack pointer must not escape the callee (see the
> + * R0 case above), but there is no need to reject the whole
> + * program for it: hand the caller an uninitialized R2 instead,
> + * so that only a caller actually using the returned pointer
> + * fails.
> + */
> + if (nregs > 1 && caller->regs[BPF_REG_2].type == PTR_TO_STACK)
> + bpf_mark_reg_not_init(env, &caller->regs[BPF_REG_2]);
Why special casing this? What if caller does not use r0,
should r0 be reset in such a case as well?
Let's handle both r0 and r2 in one place.
> }
>
> /* for callbacks like bpf_loop or bpf_for_each_map_elem go back to callsite,
...
> @@ -16710,11 +16775,26 @@ static int check_global_subprog_return_code(struct bpf_verifier_env *env)
> {
> struct bpf_func_state *cur_frame = cur_func(env);
> u32 subprog = cur_frame->subprogno;
> + u32 i, nregs;
> + int err;
>
> if (subprog_returns_void(env, subprog))
> return 0;
>
> - return check_global_ret_scalar_reg(env, BPF_REG_0);
> + /*
> + * An arena pointer is only a legitimate return value when it is the
> + * whole of it, that is when it is returned in R0 alone. Both halves of
> + * a register pair carry a piece of a >8 byte scalar, so an arena
> + * pointer in either of them is a leak.
> + */
Why forbidding returning two arena pointers?
> + nregs = bpf_ret_reg_pair(env, subprog) ? 2 : 1;
> + for (i = 0; i < nregs; i++) {
> + err = check_global_ret_scalar_reg(env, ret_regs[i], nregs == 1);
> + if (err)
> + return err;
> + }
> +
> + return 0;
> }
>
> /* Bitmask with 1s for all caller saved registers */
> @@ -17203,10 +17283,16 @@ static int process_bpf_exit_full(struct bpf_verifier_env *env,
> */
> if (cur_frame->subprogno &&
> !cur_frame->in_async_callback_fn &&
> - !cur_frame->in_exception_callback_fn)
> + !cur_frame->in_exception_callback_fn) {
> err = check_global_subprog_return_code(env);
> - else
> + } else {
> + if (!cur_frame->subprogno && bpf_ret_reg_pair(env, 0)) {
> + verbose(env,
> + "return value larger than 8 bytes is not supported at program exit\n");
> + return -EINVAL;
> + }
Same as with callbacks, I don't see a reason to check this.
The purpose of the verifier is to avoid loading a program
that would accidentally bring down the kernel, this check
does not contribute towards this goal.
> err = check_return_code(env, BPF_REG_0, "R0");
> + }
> if (err)
> return err;
> return PROCESS_BPF_EXIT;
> @@ -19366,6 +19452,22 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
> return -EOPNOTSUPP;
> }
>
> + /*
> + * An extension replaces the target outright, so it has to match
> + * the target's return convention. Its own return value is capped
> + * at 8 bytes (a >8 byte program return is rejected at BPF_EXIT),
> + * so it can never fill the R0:R2 pair the target's callers read.
> + * This cannot be left to btf_check_type_match() above, which
> + * compares return types by btf_type->info only: an int carries no
> + * vlen, so a 16-byte __int128 and an 8-byte long compare equal.
> + */
Should the btf_check_type_match() be fixed?
> + if (prog_extension && tgt_info->fmodel.ret_size > 8) {
> + bpf_log(log,
> + "Cannot replace function %s with a >8 byte return value\n",
> + tname);
> + return -EOPNOTSUPP;
> + }
> +
> /*
> * *.multi programs don't need an address during program
> * verification, we just take the module ref if needed.
next prev parent reply other threads:[~2026-08-12 22:12 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 0:09 [PATCH bpf-next v4 00/13] bpf: Support aggregate return values up to 16 bytes Yonghong Song
2026-08-11 0:09 ` [PATCH bpf-next v4 01/13] bpf: Factor check_global_ret_scalar_reg() out of the global return check Yonghong Song
2026-08-11 0:09 ` [PATCH bpf-next v4 02/13] bpf: Add helpers to describe the R0:R2 return register pair Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 19:31 ` Yonghong Song
2026-08-12 20:07 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 03/13] bpf: Wire up JIT support for 16-byte kfunc returns Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 19:48 ` Yonghong Song
2026-08-12 20:42 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 04/13] bpf: Track R2 of register-pair returns in precision backtracking Yonghong Song
2026-08-12 21:16 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 05/13] bpf: Account R2 of register-pair returns in live register analysis Yonghong Song
2026-08-11 1:09 ` bot+bpf-ci
2026-08-12 19:55 ` Yonghong Song
2026-08-12 21:21 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 06/13] bpf: Reject callbacks returning more than 8 bytes Yonghong Song
2026-08-12 21:41 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 07/13] bpf: Add verifier support for 16-byte returns in R0:R2 Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 20:12 ` Yonghong Song
2026-08-12 22:12 ` Eduard Zingerman [this message]
2026-08-11 0:09 ` [PATCH bpf-next v4 08/13] bpf: Reject register-pair returns when the subprog BTF is unreliable Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 20:26 ` Yonghong Song
2026-08-12 22:24 ` Eduard Zingerman
2026-08-11 0:09 ` [PATCH bpf-next v4 09/13] bpf: Enable aggregate return types up to 16 bytes Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 20:29 ` Yonghong Song
2026-08-12 22:47 ` Eduard Zingerman
2026-08-11 0:10 ` [PATCH bpf-next v4 10/13] selftests/bpf: Add C tests for 16-byte returns in R0:R2 Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 20:49 ` Yonghong Song
2026-08-12 23:15 ` Eduard Zingerman
2026-08-11 0:10 ` [PATCH bpf-next v4 11/13] selftests/bpf: Add inline-asm and subprog tests for R0:R2 returns Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 21:08 ` Yonghong Song
2026-08-12 23:29 ` Eduard Zingerman
2026-08-13 0:09 ` Eduard Zingerman
2026-08-11 0:10 ` [PATCH bpf-next v4 12/13] selftests/bpf: Add tests for callbacks returning more than 8 bytes Yonghong Song
2026-08-11 1:24 ` bot+bpf-ci
2026-08-12 21:22 ` Yonghong Song
2026-08-13 0:10 ` Eduard Zingerman
2026-08-11 0:10 ` [PATCH bpf-next v4 13/13] Documentation/bpf: Document up to 16-byte kfunc return values in R0:R2 Yonghong Song
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=e6f6247b319dde9f055092cd337a61292887b141.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=yonghong.song@linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.