BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Amery Hung" <ameryhung@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v5 05/14] bpf: Refactor object relationship tracking and fix dynptr UAF bug
Date: Tue, 19 May 2026 18:45:37 +0000	[thread overview]
Message-ID: <20260519184538.7A705C2BCB3@smtp.kernel.org> (raw)
In-Reply-To: <20260519181314.2731658-6-ameryhung@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Incorrect indexing of `__state->stack` for outgoing stack arguments in the `bpf_for_each_reg_in_vstate_mask` macro leads to out-of-bounds heap access and potential memory corruption.
--

commit 936dee8aae85e40857f1379da85e7a73536e2261
Author: Amery Hung <ameryhung@gmail.com>

bpf: Refactor object relationship tracking and fix dynptr UAF bug

This commit refactors object relationship tracking in the verifier and fixes
a dynptr use-after-free bug where file/skb dynptrs are not invalidated when
the parent referenced object is freed.

> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
> index 2cb4c7b0eab68..d83b9fbfef7f0 100644
> --- a/include/linux/bpf_verifier.h
> +++ b/include/linux/bpf_verifier.h

[ ... ]

> @@ -585,7 +591,7 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
>  	     iter < frame->out_stack_arg_cnt;                          \
>  	     iter++, reg = bpf_get_spilled_stack_arg(iter, frame))
>  
> -#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __mask, __expr)   \
> +#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr)   \
>  	({                                                               \
>  		struct bpf_verifier_state *___vstate = __vst;            \
>  		int ___i, ___j;                                          \

[ ... ]

> @@ -600,19 +607,26 @@ bpf_get_spilled_stack_arg(int slot, struct bpf_func_state *frame)
>  			bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
>  				if (!__reg)                              \
>  					continue;                        \
> +				__stack = &__state->stack[___j];         \
>  				(void)(__expr);                          \
>  			}                                                \
>  			bpf_for_each_spilled_stack_arg(___j, __state, __reg) { \
>  				if (!__reg)                              \
>  					continue;                        \
> +				__stack = &__state->stack[___j];         \

[Severity: Critical]
Is it safe to index the stack array using the out_stack_arg_cnt index here?

In the bpf_for_each_spilled_stack_arg loop, ___j represents the index into
the stack_arg_regs array, which is bounded by out_stack_arg_cnt. However,
this line sets __stack = &__state->stack[___j], which indexes the local
function stack array (bounded by allocated_stack / BPF_REG_SIZE).

Since a BPF program can push stack arguments without allocating a local
stack, ___j might exceed the local stack size, computing an out-of-bounds
pointer.

When release_reference() iterates over these registers, it evaluates
stack->slot_type[BPF_REG_SIZE - 1], which reads out-of-bounds memory.
If this memory coincidentally contains the value for STACK_DYNPTR (2),
could this result in invalidate_dynptr() being called erroneously,
writing STACK_INVALID across multiple slots and corrupting adjacent
kernel memory?

>  				(void)(__expr);                          \
>  			}						 \
>  		}                                                        \
>  	})

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260519181314.2731658-1-ameryhung@gmail.com?part=5

  reply	other threads:[~2026-05-19 18:45 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 [this message]
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
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=20260519184538.7A705C2BCB3@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