public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
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>,
	"Jose E . Marchesi" <jose.marchesi@oracle.com>,
	kernel-team@fb.com,  Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next 01/18] bpf: Support stack arguments for bpf functions
Date: Wed, 29 Apr 2026 18:38:54 -0700	[thread overview]
Message-ID: <fc0055b08c4788204287fe05e1e270c65bb17b5a.camel@gmail.com> (raw)
In-Reply-To: <6b9f2ad0-e46e-4a0e-b395-c0ca45bf682c@linux.dev>

On Wed, 2026-04-29 at 23:52 +0100, Yonghong Song wrote:

[...]

> > But this is a very partial check, the max_out_stack_arg_depth is
> > computed per-subprogram, not per-call. As far as I understand the
> > design, it can't be computed per-call at all. Meaning that if there
> > are, say, two calls:
> > - foo(1,2,3,4,5,6,7)   // where foo expects only 6 parameters
> > - bar(1,2,3,4,5,6,7,8) // where bar expects only 7 parameters
> > 
> > In this case:
> > - Verifier won't know which of the two calls is bogus, so won't be
> >    able to point user to the instruction where error occurs.
> > - This is not a safety condition, meaning that kernel state is not
> >    broken if more arguments are pushed onto stack (and if it *is* a
> >    safety condition, then we need to figure out something two check
> >    both calls above).
> 
> You are right in the sense, we do not reject at any callsite
> since we do not know whether it is exclusively used or shared.
> For example,
>     *(u64 *)(r11 - 24) = ...;
>     if (...) {
>       *(u64 *)(r11 - 16) = ...;
>       *(u64 *)(r11 - 8) = ...;
>       subprog_7args;
>     } else {
>       ...
>     }
> 
> We cannot warn at subprog_7args() since it is not clear
> whether '*(u64 *)(r11 - 24) = ...' is exclusively used
> by subprog_7args or other, unless we keep track of
> stack arguments. but I think this is not needed.
> The above check is in function bpf_fixup_call_args()
> where all subprog's have been processed. So we know
> *maximum* stack arg count, e.g.,
>      subprog_7args()
>      subprog_9args()
> We now the maximum stack argument count is 4 and
> this maximum stack argument count will be used in jit.
> 
> If we see '*(u64 *)(r11 - 40)', we will know it will
> not be used for any kfunc or bpf functions.
> 
> This is a little bit tying to JIT implementation.
> In JIT, the stack args (excepting the first few based
> on arch ABI) will consume some stack slot based on
> native arch calling convention. for example, for x86_64,
> 
>        high address
>        +-------------------------+
>        | incoming stack arg N    |  [rbp + 16 + (N-7)*8]  (from caller)
>        | ...                     |
>        | incoming stack arg 7    |  [rbp + 16]
>        +-------------------------+
>        | return address          |  [rbp + 8]
>        | saved rbp               |  [rbp]
>        +-------------------------+
>        | BPF program stack       |  (round_up(stack_depth, 8) bytes)
>        +-------------------------+
>        | callee-saved regs       |  (r12, rbx, r13, r14, r15 as needed)
>        +-------------------------+
>        | outgoing arg M          |  [rsp + (M-7)*8]
>        | ...                     |
>        | outgoing arg 7          |  [rsp]
>        +-------------------------+  rsp
>        low address
> 
> The native off is:
> 	native_off = outgoing_arg_base - outgoing_rsp - bpf_off - 16
> 
> So
>    r11 - 8:   r9 = <...>
>    r11 - 16:  outgoing arg 7
>               native_off = outgoing_arg_base - outgoing_rsp
>    r11 - 24:  outgoing arg 8:
>               native_off = outgoing_arg_base - outgoing_rsp + 8
>    r11 - 32:  outgoing arg 9:
>               native_off = outgoing_arg_base - outgoing_rsp + 16
> 
> Then we have
>    r11 - 40: native_off = outgoing_arg_base - outgoing_rsp + 24
> 
> This will have a problem as it may overwrite callee-saved regs.
> So we need to reject it.

I see, thank you for explanation.

[...]

> > > > > +	caller = vstate->frame[vstate->curframe - 1];
> > > > > +	arg = &caller->stack_arg_regs[spi];
> > > > > +	cur = vstate->frame[vstate->curframe];
> > > > > +
> > > > > +	if (is_spillable_regtype(arg->type))
> > > > > +		copy_register_state(&cur->regs[dst_regno], arg);
> > > > > +	else
> > > > > +		mark_reg_unknown(env, cur->regs, dst_regno);
> > > > For stack writes we report error in such situations,
> > > > should the same be done here?
> > > We should be fine here.
> > This is not a bug, sure, but it would be nice to have consistent
> > behavior for similar situations.
> 
> Okay, I figured out a better solution like below:
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 2f412128d76a..5fa16287353c 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -4159,11 +4159,7 @@ static int check_stack_arg_read(struct bpf_verifier_env *env, struct bpf_func_st
>          caller = vstate->frame[vstate->curframe - 1];
>          arg = &caller->stack_arg_regs[spi];
>          cur = vstate->frame[vstate->curframe];
> -
> -       if (is_spillable_regtype(arg->type))
> -               copy_register_state(&cur->regs[dst_regno], arg);
> -       else
> -               mark_reg_unknown(env, cur->regs, dst_regno);
> +       copy_register_state(&cur->regs[dst_regno], arg);

Hm, makes sense.

>          return bpf_push_jmp_history(env, env->cur_state,
>                                      insn_stack_arg_access_flags(state->frameno, spi), 0);
>   }
> 
> and
> 
> @@ -952,7 +951,8 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
>          if (!stacksafe(env, old, cur, &env->idmap_scratch, exact))
>                  return false;
>   
> -       if (!stack_arg_safe(env, old, cur, &env->idmap_scratch, exact))
> +       if (!stack_arg_safe(env, old, cur, &env->idmap_scratch,
> +                           exact == NOT_EXACT ? RANGE_WITHIN : exact))
>                  return false;
> 
> <btw, copy_register_state() will be replaced with simple assignment aftre refactoring>
> 
> In stack_arg_safe, with NOT_EXACT seems not enough for precision tracking, so
> using RANGE_WITHIN can ensure proper pruning.

Could you please elaborate a bit?

  reply	other threads:[~2026-04-30  1:38 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-24 17:14 [PATCH bpf-next 00/18] bpf: Support stack arguments for BPF functions and kfuncs Yonghong Song
2026-04-24 17:14 ` [PATCH bpf-next 01/18] bpf: Support stack arguments for bpf functions Yonghong Song
2026-04-24 18:13   ` bot+bpf-ci
2026-04-25  5:09     ` Yonghong Song
2026-04-27 20:40       ` Yonghong Song
2026-04-28 14:29   ` Eduard Zingerman
2026-04-28 16:47     ` Yonghong Song
2026-04-28 23:50       ` Yonghong Song
2026-04-29  0:28       ` Eduard Zingerman
2026-04-29 22:52         ` Yonghong Song
2026-04-30  1:38           ` Eduard Zingerman [this message]
2026-05-02 17:03   ` Alexei Starovoitov
2026-05-02 21:54     ` Yonghong Song
2026-04-24 17:14 ` [PATCH bpf-next 02/18] bpf: Add precision marking and backtracking for stack argument slots Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-25  5:10     ` Yonghong Song
2026-04-28 16:46   ` Eduard Zingerman
2026-04-28 20:54     ` Yonghong Song
2026-04-24 17:14 ` [PATCH bpf-next 03/18] bpf: Refactor record_call_access() to extract per-arg logic Yonghong Song
2026-04-29  0:51   ` Eduard Zingerman
2026-04-29 22:55     ` Yonghong Song
2026-04-24 17:14 ` [PATCH bpf-next 04/18] bpf: Extend liveness analysis to track stack argument slots Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-25  5:11     ` Yonghong Song
2026-04-29 12:22   ` Eduard Zingerman
2026-04-29 22:55     ` Yonghong Song
2026-04-24 17:14 ` [PATCH bpf-next 05/18] bpf: Reject stack arguments in non-JITed programs Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-29 12:27   ` Eduard Zingerman
2026-04-24 17:15 ` [PATCH bpf-next 06/18] bpf: Prepare architecture JIT support for stack arguments Yonghong Song
2026-04-24 17:48   ` bot+bpf-ci
2026-04-25  5:17     ` Yonghong Song
2026-04-29 12:37   ` Eduard Zingerman
2026-04-24 17:15 ` [PATCH bpf-next 07/18] bpf: Enable r11 based insns Yonghong Song
2026-04-29 12:48   ` Eduard Zingerman
2026-04-24 17:15 ` [PATCH bpf-next 08/18] bpf: Support stack arguments for kfunc calls Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-25  5:19     ` Yonghong Song
2026-04-24 17:15 ` [PATCH bpf-next 09/18] bpf: Reject stack arguments if tail call reachable Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-24 17:15 ` [PATCH bpf-next 10/18] bpf,x86: Implement JIT support for stack arguments Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-25  5:29     ` Yonghong Song
2026-04-24 17:16 ` [PATCH bpf-next 11/18] selftests/bpf: Add tests for BPF function " Yonghong Song
2026-04-24 17:16 ` [PATCH bpf-next 12/18] selftests/bpf: Add tests for stack argument validation Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 13/18] selftests/bpf: Add verifier " Yonghong Song
2026-04-24 17:48   ` bot+bpf-ci
2026-04-25  5:33     ` Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 14/18] selftests/bpf: Add BTF fixup for __naked subprog parameter names Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 15/18] selftests/bpf: Add precision backtracking test for stack arguments Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 16/18] bpf, arm64: Map BPF_REG_0 to x8 instead of x7 Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 17/18] bpf, arm64: Add JIT support for stack arguments Yonghong Song
2026-04-24 18:00   ` bot+bpf-ci
2026-04-27  9:06     ` Puranjay Mohan
2026-04-27 20:42       ` Yonghong Song
2026-04-24 17:17 ` [PATCH bpf-next 18/18] selftests/bpf: Enable stack argument tests for arm64 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=fc0055b08c4788204287fe05e1e270c65bb17b5a.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=jose.marchesi@oracle.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox