From: sashiko-bot@kernel.org
To: "Amery Hung" <ameryhung@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v5 10/14] bpf: Fix dynptr ref counting to scan all call frames
Date: Tue, 19 May 2026 19:03:29 +0000 [thread overview]
Message-ID: <20260519190330.207401F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260519181314.2731658-11-ameryhung@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The macro `bpf_for_each_reg_in_vstate_mask` incorrectly computes the `stack` pointer when iterating over outgoing stack arguments. The new `dynptr_ref_cnt` function uses this macro and dereferences the invalid pointer, leading to a verifier crash or security bypass.
--
commit 2fbeda3e91d76b77c3a4fe5a3269d7a89c9d8588
Author: Amery Hung <ameryhung@gmail.com>
bpf: Fix dynptr ref counting to scan all call frames
This commit extracts dynptr reference counting into a new dynptr_ref_cnt()
function to check whether a referenced dynptr can be overwritten. It uses
bpf_for_each_reg_in_vstate_mask() to scan dynptr stack slots across all
call frames instead of just the current frame.
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index b7984c42ca145..97eb8033d732e 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -786,10 +786,29 @@ static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_
[ ... ]
> +static int dynptr_ref_cnt(struct bpf_verifier_env *env, int v_parent_id)
> +{
> + struct bpf_stack_state *stack;
> + struct bpf_func_state *state;
> + struct bpf_reg_state *reg;
> + int ref_cnt = 0;
> +
> + bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, stack, 1 << STACK_DYNPTR, ({
> + if (!stack || stack->slot_type[0] != STACK_DYNPTR)
> + continue;
[Severity: Critical]
Does bpf_for_each_reg_in_vstate_mask() safely provide the stack pointer when
iterating over outgoing stack arguments?
Looking at the macro definition in include/linux/bpf_verifier.h:
bpf_for_each_spilled_stack_arg(___j, __state, __reg) { \
if (!__reg) \
continue; \
__stack = &__state->stack[___j]; \
(void)(__expr); \
}
When iterating over outgoing stack arguments, it calculates __stack using the
local stack array (__state->stack). Since out_stack_arg_cnt is independent
of allocated_stack, __state->stack could be NULL.
If ___j > 0 and __state->stack is NULL, __stack would evaluate to a non-NULL
offset. Would this bypass the !stack check above and result in an
out-of-bounds read or page fault when evaluating stack->slot_type[0]?
Additionally, if __state->stack is valid, could this evaluate the same local
stack slot twice, artificially inflating ref_cnt and potentially bypassing
the ref_cnt <= 1 check?
> + if (!stack->spilled_ptr.dynptr.first_slot)
> + continue;
> + if (stack->spilled_ptr.parent_id == v_parent_id)
> + ref_cnt++;
> + }));
> +
> + return ref_cnt;
> +}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260519181314.2731658-1-ameryhung@gmail.com?part=10
next prev parent reply other threads:[~2026-05-19 19:03 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-19 18:12 [PATCH bpf-next v5 00/14] Refactor verifier object relationship tracking Amery Hung
2026-05-19 18:12 ` [PATCH bpf-next v5 01/14] bpf: Simplify mark_stack_slot_obj_read() and callers Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 02/14] bpf: Unify dynptr handling in the verifier Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 03/14] bpf: Assign reg->id when getting referenced kptr from ctx Amery Hung
2026-05-19 18:56 ` bot+bpf-ci
2026-05-19 20:17 ` Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 04/14] bpf: Preserve reg->id of pointer objects after null-check Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 05/14] bpf: Refactor object relationship tracking and fix dynptr UAF bug Amery Hung
2026-05-19 18:45 ` sashiko-bot
2026-05-19 18:51 ` Amery Hung
2026-05-20 21:47 ` Eduard Zingerman
2026-05-21 7:18 ` Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 06/14] bpf: Remove redundant dynptr arg check for helper Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 07/14] bpf: Unify referenced object tracking in verifier Amery Hung
2026-05-19 18:55 ` sashiko-bot
2026-05-19 19:57 ` Amery Hung
2026-05-20 22:28 ` Eduard Zingerman
2026-05-19 18:13 ` [PATCH bpf-next v5 08/14] bpf: Unify release handling for helpers and kfuncs Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 09/14] bpf: Fold ref_obj_id into id and introduce virtual references Amery Hung
2026-05-19 18:59 ` sashiko-bot
2026-05-19 20:13 ` Amery Hung
2026-05-22 5:40 ` Eduard Zingerman
2026-05-19 18:13 ` [PATCH bpf-next v5 10/14] bpf: Fix dynptr ref counting to scan all call frames Amery Hung
2026-05-19 19:03 ` sashiko-bot [this message]
2026-05-19 20:05 ` Amery Hung
2026-05-20 19:59 ` Eduard Zingerman
2026-05-20 22:41 ` Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 11/14] selftests/bpf: Test creating dynptr from dynptr data and slice Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 12/14] selftests/bpf: Test using dynptr after freeing the underlying object Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 13/14] selftests/bpf: Test using slice after invalidating dynptr clone Amery Hung
2026-05-19 18:13 ` [PATCH bpf-next v5 14/14] selftests/bpf: Test using file dynptr after the reference on file is dropped Amery Hung
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=20260519190330.207401F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=ameryhung@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=sashiko-reviews@lists.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