public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
	jose.marchesi@oracle.com, kernel-team@fb.com,
	martin.lau@kernel.org, eddyz87@gmail.com, clm@meta.com,
	ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v3 05/11] bpf: Support stack arguments for kfunc calls
Date: Tue, 7 Apr 2026 21:53:35 -0700	[thread overview]
Message-ID: <2701e70f-826c-4e85-a577-e6358401b44f@linux.dev> (raw)
In-Reply-To: <adcc0eb54109467ffa3e970608a73ba0b39230928555d09663cd771441c010dd@mail.kernel.org>



On 4/5/26 11:20 AM, bot+bpf-ci@kernel.org wrote:
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
> [ ... ]
>
>> +static struct bpf_reg_state *get_kfunc_arg_reg(struct bpf_verifier_env *env, int argno)
>> +{
>> +	struct bpf_func_state *caller;
>> +	int spi;
>> +
>> +	if (argno < MAX_BPF_FUNC_REG_ARGS)
>> +		return &cur_regs(env)[argno + 1];
>> +
>> +	caller = cur_func(env);
>> +	spi = caller->incoming_stack_arg_depth / BPF_REG_SIZE + (argno - MAX_BPF_FUNC_REG_ARGS);
>> +	return &caller->stack_arg_slots[spi].spilled_ptr;
>> +}
>> +
>>   static enum kfunc_ptr_arg_type
>>   get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
>>   		       struct bpf_kfunc_call_arg_meta *meta,
> [ ... ]
>
>>   {
>>   	u32 regno = argno + 1;
>>   	struct bpf_reg_state *regs = cur_regs(env);
>>   	struct bpf_reg_state *reg = &regs[regno];
> When check_kfunc_args() calls this function for a stack arg (argno >= 5),
> the stack arg state was placed into regs[BPF_REG_1], but this local
> reg reads regs[argno + 1] instead, which is a callee-saved register
> (BPF_REG_6 through BPF_REG_9) or the frame pointer (BPF_REG_10).
>
> For argno >= 10 (MAX_BPF_FUNC_ARGS is 12), this reads past
> regs[MAX_BPF_REG - 1] into adjacent struct fields, since
> bpf_func_state.regs[] only has 11 entries (BPF_REG_0..BPF_REG_10).
>
> This reg is used later for register_is_null() and base_type(reg->type),
> which would cause wrong argument type classification for pointer-typed
> stack args.  The is_kfunc_arg_mem_size() lookahead a few lines below
> was correctly updated to use get_kfunc_arg_reg() -- should this local
> reg use the same helper?

To use BPF_REG_1 to represent the stack argument is a hack. It may confuse
people and it may have some subtle issues due to this.

In the next revision, I will discard this patch and implement it properly.
In most cases, for related functions w.r.t. stack argument validation,
I will ensure functions has parameters like (reg_state, regno, argno).
If the argument is a stack argument, regno will be -1 (not a register).
In cases where argno is not really available, argno will be -1. But in
any case, at least one of them is positive. The verification log will
print out message properly. If regno is non-negative, log 'regno' is needed.
Otherwise, log 'argno'.

>
>> 	if (argno + 1 < nargs &&
>> -	    (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], &regs[regno + 1]) ||
>> -	     is_kfunc_arg_const_mem_size(meta->btf, &args[argno + 1], &regs[regno + 1])))
>> +	    (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1], get_kfunc_arg_reg(env, argno + 1)) ||
>> +	     is_kfunc_arg_const_mem_size(meta->btf, &args[argno + 1], get_kfunc_arg_reg(env, argno + 1))))
>>   		arg_mem_size = true;
> [ ... ]
>
>> 		case KF_ARG_PTR_TO_MEM_SIZE:
>>   		{
>>   			struct bpf_reg_state *buff_reg = &regs[regno];
>>   			const struct btf_param *buff_arg = &args[i];
>> -			struct bpf_reg_state *size_reg = &regs[regno + 1];
>> +			struct bpf_reg_state *size_reg;
>>   			const struct btf_param *size_arg = &args[i + 1];
>> +			struct bpf_reg_state saved_size_reg = {};
>> +			bool size_is_stack_arg = false;
>> +
>> +			if (i >= MAX_BPF_FUNC_REG_ARGS) {
>> +				size_is_stack_arg = true;
>> +				saved_size_reg = regs[regno + 1];
>> +				regs[regno + 1] = *get_kfunc_arg_reg(env, i + 1);
>> +				verbose(env, "Use reg %d to represent mem_size\n", regno + 1);
>> +			}
>> +			size_reg = &regs[regno + 1];
> Is there a missing case when the pointer arg is the last register arg
> (i == MAX_BPF_FUNC_REG_ARGS - 1, i.e. i == 4) and the size arg is
> the first stack arg (i + 1 == 5)?

Yes. This is something I intend to support. arg4 (starting from arg0)
is a memory pointer at reg5, and arg5 represents the memory size at
the first stack argument.

>
> In that case i < MAX_BPF_FUNC_REG_ARGS, so is_stack_arg is false and
> regno is 5 (BPF_REG_5).  The condition "i >= MAX_BPF_FUNC_REG_ARGS"
> is false, so size_is_stack_arg stays false.  Then size_reg becomes
> &regs[6], which is BPF_REG_6 (a callee-saved register) instead of
> the stack arg slot for arg 5.

In this case, BPF_REG_6 will be saved first, and then BPF_REG_6 will
have the first stack argument for future verification.

But in any case, this patch set is a hack. I will have proper
implementation in the next revision.

>
> The get_kfunc_ptr_arg_type() lookahead correctly handles this boundary
> with get_kfunc_arg_reg(env, argno + 1), so the arg gets classified as
> KF_ARG_PTR_TO_MEM_SIZE, but then the actual size check reads from the
> wrong register.
>
> Would something like "if (i + 1 >= MAX_BPF_FUNC_REG_ARGS)" be the
> right condition here?
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24006922024


  reply	other threads:[~2026-04-08  4:53 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-05 17:25 [PATCH bpf-next v3 00/11] bpf: Support stack arguments for BPF functions and kfuncs Yonghong Song
2026-04-05 17:25 ` [PATCH bpf-next v3 01/11] bpf: Introduce bpf register BPF_REG_STACK_ARG_BASE Yonghong Song
2026-04-05 17:25 ` [PATCH bpf-next v3 02/11] bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments Yonghong Song
2026-04-05 17:25 ` [PATCH bpf-next v3 03/11] bpf: Support stack arguments for bpf functions Yonghong Song
2026-04-05 18:20   ` bot+bpf-ci
2026-04-08  4:38     ` Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 04/11] bpf: Refactor process_iter_arg() to have proper argument index Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 05/11] bpf: Support stack arguments for kfunc calls Yonghong Song
2026-04-05 18:20   ` bot+bpf-ci
2026-04-08  4:53     ` Yonghong Song [this message]
2026-04-05 17:26 ` [PATCH bpf-next v3 06/11] bpf: Reject stack arguments in non-JITed programs Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 07/11] bpf: Enable stack argument support for x86_64 Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 08/11] bpf,x86: Implement JIT support for stack arguments Yonghong Song
2026-04-05 18:20   ` bot+bpf-ci
2026-04-08  4:40     ` Yonghong Song
2026-04-08  4:42     ` Yonghong Song
2026-04-05 20:36   ` Alexei Starovoitov
2026-04-06  4:14     ` Yonghong Song
2026-04-06  4:54       ` Alexei Starovoitov
2026-04-06  4:59         ` Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 09/11] selftests/bpf: Add tests for BPF function " Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 10/11] selftests/bpf: Add negative test for greater-than-8-byte kfunc stack argument Yonghong Song
2026-04-05 17:26 ` [PATCH bpf-next v3 11/11] selftests/bpf: Add verifier tests for stack argument validation 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=2701e70f-826c-4e85-a577-e6358401b44f@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=clm@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jose.marchesi@oracle.com \
    --cc=kernel-team@fb.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