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 v5 11/16] bpf: Support stack arguments for kfunc calls
Date: Sat, 18 Apr 2026 11:04:15 -0700	[thread overview]
Message-ID: <0c5bbdea-a1be-4ab0-973b-197a3b15d959@linux.dev> (raw)
In-Reply-To: <9ed0516c340a9717f644ca8d69a474f94774ddbdf4cb39b0b932029612871d35@mail.kernel.org>



On 4/17/26 6:04 PM, 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
> [ ... ]
>
>> @@ -11653,6 +11659,19 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_kfunc_call_arg_meta *meta)
>> +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 = out_arg_spi(caller, argno - MAX_BPF_FUNC_REG_ARGS);
>> +	return &caller->stack_arg_regs[spi];
>> +}
> Can the verifier crash here?  Looking at get_kfunc_arg_reg, when
> argno >= MAX_BPF_FUNC_REG_ARGS, the function returns
> &caller->stack_arg_regs[spi] without checking whether stack_arg_regs
> is NULL.
>
> The stack_arg_regs array is only allocated by grow_stack_arg_slots,
> which is called from check_stack_arg_write when the program writes
> outgoing stack args via r11.  If a malformed BPF program invokes a
> kfunc without emitting any stack stores beforehand, stack_arg_regs
> will remain NULL.
>
> Consider get_kfunc_ptr_arg_type calling get_kfunc_arg_reg to peek at
> argument i+1 when checking for buffer+__sz pairs:
>
> kernel/bpf/verifier.c:get_kfunc_ptr_arg_type() {
>      if (argno + 1 < nargs &&
>          (is_kfunc_arg_mem_size(meta->btf, &args[argno + 1],
>                                  get_kfunc_arg_reg(env, argno + 1)) ||
>           ...
> }
>
> When argno=4 (pointer in r5) and argno+1=5 (size in first stack slot),
> get_kfunc_arg_reg(env, 5) enters the stack-arg branch and dereferences
> caller->stack_arg_regs even though it may be NULL.  If the arg type is
> a scalar (which __sz/__szk suffixes imply), is_kfunc_arg_mem_size will
> try to read reg->type, causing a NULL pointer dereference.
>
> The is_stack_arg_slot_initialized gate you added in check_kfunc_args
> only fires when the loop reaches i=5; by then the peek at i=4 has
> already dereferenced the NULL pointer.
>
> Should get_kfunc_arg_reg verify that stack_arg_regs is non-NULL and
> spi < stack_arg_depth/BPF_REG_SIZE before returning the pointer?
> Alternatively, should check_kfunc_call add an out_stack_arg_mask
> pre-check mirroring the bpf-to-bpf gate that prevents
> check_func_arg_reg_off from reading uninitialized stack slots?

Good points. Indeed we should check validity of spi in
   spi = out_arg_spi(caller, argno - MAX_BPF_FUNC_REG_ARGS)

Will fix.

>
>> @@ -11660,8 +11679,6 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
>>   		       const char *ref_tname, const struct btf_param *args,
>>   		       int argno, int nargs, struct bpf_reg_state *reg)
>>   {
>> -	u32 regno = argno + 1;
>> -	struct bpf_reg_state *regs = cur_regs(env);
>>   	bool arg_mem_size = false;
>>
>>   	if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] ||
>> @@ -11670,8 +11687,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
>>   		return KF_ARG_PTR_TO_CTX;
>>
>>   	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;
> [ ... ]
>
>> @@ -20145,6 +20209,16 @@ int bpf_fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>   	if (!bpf_jit_supports_far_kfunc_call())
>>   		insn->imm = BPF_CALL_IMM(desc->addr);
>>
>> +	/*
>> +	 * After resolving the kfunc address, insn->off is no longer needed
>> +	 * for BTF fd index. Repurpose it to store the number of stack args
>> +	 * so the JIT can marshal them.
>> +	 */
>> +	if (desc->func_model.nr_args > MAX_BPF_FUNC_REG_ARGS)
>> +		insn->off = desc->func_model.nr_args - MAX_BPF_FUNC_REG_ARGS;
>> +	else
>> +		insn->off = 0;
>> +
> This concern was raised by the bpf-ci bot and sashiko-bot in the v5
> review thread but does not appear to have been addressed.
>
> Overwriting insn->off destroys the BTF file descriptor index required
> by bpf_jit_find_kfunc_model() for module kfunc lookups.  Looking at
> the bsearch in kernel/bpf/fixups.c, it uses (insn->imm, insn->off) as
> the key:
>
> kernel/bpf/fixups.c:bpf_jit_find_kfunc_model() {
>      key.addr = insn->imm;
>      key.offset = insn->off;
>      entry = bsearch(&key, tab, tab_count, sizeof(*tab), kfunc_addr_cmp);
> }
>
> For module kfuncs, insn->off stores a non-zero btf_fd_idx set during
> find_kfunc_desc.  After this patch overwrites it with the stack arg
> count (or 0), the bsearch will fail to find descriptors that were
> stored with offset=btf_fd_idx.
>
> This breaks module kfunc calls on architectures using JIT far calls
> (s390x, loongarch, powerpc) for any module kfunc with 5 or fewer args
> (insn->off zeroed) or with more than 5 args (insn->off now contains
> the stack arg count instead of btf_fd_idx, still mismatching
> desc->offset).  Vmlinux kfuncs with more than 5 args also get a
> nonzero insn->off that no longer matches desc->offset=0.
>
> Can the stack arg count be communicated to the JIT without clobbering
> the btf_fd_idx that bsearch still keys on?
>
> The original concern was raised here:
> https://lore.kernel.org/bpf/e5a6e801c10994cc2e03b0cd57e76152eb234493a6b7395a9859feade4a6575b@mail.kernel.org/
>
>
> ---
> 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/24592562564


  reply	other threads:[~2026-04-18 18:04 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-17  3:46 [PATCH bpf-next v5 00/16] bpf: Support stack arguments for BPF functions and kfuncs Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 01/16] bpf: Remove unused parameter from check_map_kptr_access() Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 02/16] bpf: Refactor to avoid redundant calculation of bpf_reg_state Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 03/16] bpf: Refactor to handle memory and size together Yonghong Song
2026-04-17  4:49   ` sashiko-bot
2026-04-18 16:40     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 16:47     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 04/16] bpf: Prepare verifier logs for upcoming kfunc stack arguments Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 05/16] bpf: Introduce bpf register BPF_REG_PARAMS Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 06/16] bpf: Limit the scope of BPF_REG_PARAMS usage Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 16:48     ` Yonghong Song
2026-04-17  4:50   ` sashiko-bot
2026-04-18 16:50     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 16:54     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 07/16] bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 17:00     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 17:03     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 08/16] bpf: Support stack arguments for bpf functions Yonghong Song
2026-04-17  4:35   ` sashiko-bot
2026-04-18 17:10     ` Yonghong Song
2026-04-17  4:43   ` bot+bpf-ci
2026-04-18 17:11     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-17  3:47 ` [PATCH bpf-next v5 09/16] bpf: Reject stack arguments in non-JITed programs Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18 17:17     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-17  3:47 ` [PATCH bpf-next v5 10/16] bpf: Reject stack arguments if tail call reachable Yonghong Song
2026-04-17  4:08   ` sashiko-bot
2026-04-18 17:18     ` Yonghong Song
2026-04-18 17:37     ` Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 17:24     ` Yonghong Song
2026-04-17  3:47 ` [PATCH bpf-next v5 11/16] bpf: Support stack arguments for kfunc calls Yonghong Song
2026-04-17  4:40   ` sashiko-bot
2026-04-18 17:46     ` Yonghong Song
2026-04-17  4:43   ` bot+bpf-ci
2026-04-18 17:57     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-18 18:04     ` Yonghong Song [this message]
2026-04-17  3:47 ` [PATCH bpf-next v5 12/16] bpf: Enable stack argument support for x86_64 Yonghong Song
2026-04-17  4:30   ` bot+bpf-ci
2026-04-17  5:03   ` sashiko-bot
2026-04-18 18:07     ` Yonghong Song
2026-04-18  1:04   ` bot+bpf-ci
2026-04-17  3:48 ` [PATCH bpf-next v5 13/16] bpf,x86: Implement JIT support for stack arguments Yonghong Song
2026-04-17  4:44   ` sashiko-bot
2026-04-18 16:43     ` Puranjay Mohan
2026-04-18 18:15     ` Yonghong Song
2026-04-18  1:20   ` bot+bpf-ci
2026-04-18 18:23     ` Yonghong Song
2026-04-17  3:48 ` [PATCH bpf-next v5 14/16] selftests/bpf: Add tests for BPF function " Yonghong Song
2026-04-17  4:20   ` sashiko-bot
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 18:26     ` Yonghong Song
2026-04-17  3:48 ` [PATCH bpf-next v5 15/16] selftests/bpf: Add negative test for greater-than-8-byte kfunc stack argument Yonghong Song
2026-04-17  4:28   ` sashiko-bot
2026-04-18 18:29     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-17  3:48 ` [PATCH bpf-next v5 16/16] selftests/bpf: Add verifier tests for stack argument validation Yonghong Song
2026-04-17  4:38   ` sashiko-bot
2026-04-18 18:36     ` Yonghong Song
2026-04-18  0:52   ` bot+bpf-ci
2026-04-18 16:39 ` [PATCH bpf-next v5 00/16] bpf: Support stack arguments for BPF functions and kfuncs Puranjay Mohan
2026-04-18 18:47   ` Alexei Starovoitov
2026-04-18 18:54     ` Yonghong Song
2026-04-18 17:06 ` Puranjay Mohan

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=0c5bbdea-a1be-4ab0-973b-197a3b15d959@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