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 03/11] bpf: Support stack arguments for bpf functions
Date: Tue, 7 Apr 2026 21:38:59 -0700 [thread overview]
Message-ID: <cf64e36f-1552-4c3d-bb1c-4b352a248f8b@linux.dev> (raw)
In-Reply-To: <34107b0f560d5c960a2a55fef331b8bb728735e1e4d1155ed7ed9511c696a296@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
>> index 84699a428077..52a61021613b 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
> [ ... ]
>
>> +/* Validate that a stack arg access is 8-byte sized and aligned. */
>> +static int check_stack_arg_access(struct bpf_verifier_env *env,
>> + struct bpf_insn *insn, const char *op)
>> +{
>> + int size = bpf_size_to_bytes(BPF_SIZE(insn->code));
>> +
>> + if (size != BPF_REG_SIZE) {
>> + verbose(env, "stack arg %s must be %d bytes, got %d\n",
>> + op, BPF_REG_SIZE, size);
>> + return -EINVAL;
>> + }
>> + if (insn->off % BPF_REG_SIZE) {
>> + verbose(env, "stack arg %s offset %d not aligned to %d\n",
>> + op, insn->off, BPF_REG_SIZE);
>> + return -EINVAL;
>> + }
>> + return 0;
>> +}
> Should this also reject non-negative offsets? The commit message and
> check_stack_arg_write() comment both state that off is a negative
> offset from the stack arg frame pointer, and the diagram shows only
> negative offsets (r12-8, r12-16, etc.).
We should. I will add more checking about positive or negative offset
(as applied) in the next revision.
>
> If a crafted BPF program uses a positive offset (e.g., off=+8), this
> function accepts it because +8 is 8-byte aligned. Then in
> check_stack_arg_write():
>
> int spi = (-off - 1) / BPF_REG_SIZE;
>
> With off=+8, spi becomes (-8 - 1) / 8 = -1. The grow_stack_arg_slots()
> call receives a negative size, which round_up() turns to 0, so no
> allocation happens. The subsequent access to stack_arg_slots[-1] is
> an out-of-bounds write (heap underflow if previously allocated, or a
> crash if stack_arg_slots is NULL).
>
> The same issue applies to the read path in check_stack_arg_read()
> where -off becomes negative, passing the depth check, and spi
> again computes to -1.
>
> resolve_pseudo_ldimm64() explicitly allows BPF_REG_STACK_ARG_BASE
> in both src_reg and dst_reg positions, so a BPF program can encode
> these instructions.
>
> Something like the following may be sufficient:
>
> if (insn->off >= 0) {
> verbose(env, "stack arg %s offset must be negative\n", op);
> return -EINVAL;
> }
>
>> +
>> +/*
>> + * Write a value to the stack arg area.
>> + * off is the negative offset from the stack arg frame pointer.
>> + * Callers ensures off is 8-byte aligned and size is BPF_REG_SIZE.
>> + */
>> +static int check_stack_arg_write(struct bpf_verifier_env *env, struct bpf_func_state *state,
>> + int off, int value_regno)
>> +{
>> + int spi = (-off - 1) / BPF_REG_SIZE;
> [ ... ]
>
>> + } else {
>> + /* BPF_ST: store immediate, treat as scalar */
>> + reg = &state->stack_arg_slots[spi].spilled_ptr;
>> + reg->type = SCALAR_VALUE;
>> + __mark_reg_known(reg, (u32)env->prog->insnsi[env->insn_idx].imm);
>> + for (i = 0; i < BPF_REG_SIZE; i++)
>> + state->stack_arg_slots[spi].slot_type[i] = STACK_MISC;
>> + }
>> + return 0;
>> +}
> Is the (u32) cast on the imm field intentional here? The regular
> stack path in check_stack_write_fixed_off() passes insn->imm
Yes, good point. Will remove (u32).
> directly to __mark_reg_known() without a cast:
>
> __mark_reg_known(tmp_reg, insn->imm);
>
> Since insn->imm is __s32 and __mark_reg_known() takes u64, the
> regular path sign-extends (e.g., imm=-1 becomes 0xffffffffffffffff).
> The (u32) cast here zero-extends instead (imm=-1 becomes
> 0x00000000ffffffff).
>
> BPF_ST with BPF_DW stores the 32-bit immediate sign-extended to
> 64 bits at runtime, so the verifier's tracked value would not match
> the actual runtime value for negative immediates.
>
>
> ---
> 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
next prev parent reply other threads:[~2026-04-08 4:39 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 [this message]
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
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=cf64e36f-1552-4c3d-bb1c-4b352a248f8b@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