BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 21/49] bpf: Compare states by join_key, then by value
Date: Fri, 20 Nov 2026 08:22:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-224719@kernel.org> (raw)
In-Reply-To: <20261120074430.2704488-22-alexei.starovoitov@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: pointer leak via atomic RMW on unreadable spill in
  stacksafe()
- [Medium] bpf: 8-byte overread of 4-byte refs[i].ptr on 32-bit in
  same_refs()
--

commit e3beba5c290061bf6cb6bccd8d16009036af5591
Author: Alexei Starovoitov <ast@kernel.org>

bpf: Compare states by join_key, then by value

This commit groups non-joinable verifier state fields into join_key structs
for bytewise comparison and hashing, and simplifies register and stack
subsumption checks in states.c.

> diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
> index bb8fa008a8a5a..174b1a613fb98 100644
> --- a/kernel/bpf/states.c
> +++ b/kernel/bpf/states.c
[ ... ]
> @@ -295,6 +391,13 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
>  		if (old_type == STACK_INVALID || old_type == STACK_POISON)
>  			continue;
>  
> +		/* it can be filled into a register that is unreadable */
> +		if (old_type == STACK_SPILL && old_slot->spilled_ptr.type == NOT_INIT &&
> +		    (env->allow_uninit_stack ||
> +		     (cur_slot && cur_slot->slot_type[im] != STACK_INVALID &&
> +		      cur_slot->slot_type[im] != STACK_POISON)))
> +			continue;

[Severity: High]
Can this leak a spilled pointer when !env->allow_ptr_leaks?

This lets an unreadable spill (STACK_SPILL with spilled_ptr.type == NOT_INIT)
in old subsume a spilled pointer in cur.  If the subsuming path executes a
64-bit atomic RMW on that stack slot (such as lock *(u64 *)(r10 - 8) += r2),
check_atomic_rmw() checks the read with dst_regno == -1:

kernel/bpf/verifier.c:check_stack_read_fixed_off() {
    ...
		} else if (dst_regno >= 0) {
			/* restore register state from stack */
			state->regs[dst_regno] = *reg;
			bpf_equations_fill(env, reg_state, spi, size);
		} else if (__is_pointer_value(env->allow_ptr_leaks, reg)) {
			...
			return -EACCES;
		}
    ...
}

Because is_pointer_regtype(NOT_INIT) is false, __is_pointer_value() returns
false for the unreadable spill, and the atomic write step scrubs the slot to
STACK_MISC.  A subsequent 64-bit load from the slot then reads the pointer
from the subsumed path as a SCALAR_VALUE.

[ ... ]

> @@ -398,6 +485,114 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
>  	return true;
>  }
>  
> +static const struct bpf_stack_state *held_slot(const struct bpf_func_state *f, u32 i)
> +{
> +	return i < bpf_stack_nr_slots(f) && bpf_slot_is_held(bpf_stack_slot(f, i)) ?
> +	       bpf_stack_slot(f, i) : NULL;
> +}
> +
> +static bool same_refs(const struct bpf_verifier_state *a, const struct bpf_verifier_state *b)
> +{
> +	u32 i;
> +
> +	for (i = 0; i < a->acquired_refs; i++)
> +		if (a->refs[i].type != b->refs[i].type || a->refs[i].id != b->refs[i].id ||
> +		    memcmp(&a->refs[i].ptr, &b->refs[i].ptr, sizeof(u64)))
> +			return false;
> +	return true;
> +}
[ ... ]
> +u64 bpf_hash_join_key(const struct bpf_verifier_env *env, const struct bpf_verifier_state *st)
> +{
> +	u64 hash = bpf_hash64(&st->join_key, sizeof(st->join_key), env->hash_seed +
> +			      ((u64)st->instance_id << 32 | st->insn_idx));
> +	u32 fr, i, trip;
> +	const struct bpf_stack_state *slot;
> +	const struct loop_stack_entry *e;
> +	const struct bpf_func_state *f;
> +
> +	for (i = 0; i < st->acquired_refs; i++)
> +		hash = bpf_hash64(&st->refs[i].ptr, sizeof(u64), hash + st->refs[i].type);

[Severity: Medium]
On 32-bit architectures, does passing &refs[i].ptr with sizeof(u64) to
memcmp() in same_refs() and bpf_hash64() in bpf_hash_join_key() overread the
4-byte void *ptr union member?

Although the enclosing anonymous union in struct bpf_reference_state is 8
bytes wide because of struct { int parent_id; u32 ctx_arg; }, &refs[i].ptr has
type void * const * and a 4-byte sub-object size on 32-bit targets, which
triggers -Wstringop-overread and CONFIG_FORTIFY_SOURCE read-overflow checks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120074430.2704488-1-alexei.starovoitov@gmail.com?part=21

  reply	other threads:[~2026-11-20  8:22 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  7:41 [PATCH bpf-next 00/49] bpf: Verify in passes, with equations in the state Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 01/49] bpf: Look for bpf_clone_redirect() once in check_recursion() Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 02/49] bpf: Make stack liveness faster Alexei Starovoitov
2026-11-20  8:01   ` sashiko-bot
2026-11-20  8:24   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 03/49] bpf: Delete the old main pass of the verifier Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 04/49] bpf: Delete scalar evolution Alexei Starovoitov
2026-11-20  8:03   ` sashiko-bot
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 05/49] bpf: Limit the cost and the memory of a load Alexei Starovoitov
2026-11-20  8:00   ` sashiko-bot
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 06/49] bpf: Record what each insn reads and writes Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 07/49] bpf: Name an id after the xreg that has it Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 08/49] bpf: Do not fail a check because a state has fewer values Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  8:39   ` sashiko-bot
2026-11-20  7:41 ` [PATCH bpf-next 09/49] bpf: Check fixups in the main pass and keep a snapshot of them Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 10/49] bpf: Reject a program with a call that needs two rewrites Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 11/49] bpf: Put a barrier where no mask fits Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 12/49] bpf: Keep more of what is known about a number Alexei Starovoitov
2026-11-20  8:18   ` sashiko-bot
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 13/49] bpf: Remember how many bytes of a packet are checked Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 14/49] bpf: Mark a state that no run of the program has Alexei Starovoitov
2026-11-20  8:24   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 15/49] bpf: Peel the first trips of a loop and join the other trips Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 16/49] bpf: Add equations and their arithmetic Alexei Starovoitov
2026-11-20  8:24   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 17/49] bpf: Keep a table of equations in each state Alexei Starovoitov
2026-11-20  8:17   ` sashiko-bot
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 18/49] bpf: Refine all related numbers after a jump Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 19/49] bpf: Make equations from ALU, spill, fill and call insns Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  8:26   ` sashiko-bot
2026-11-20  7:41 ` [PATCH bpf-next 20/49] bpf: Delete the equations that say nothing Alexei Starovoitov
2026-11-20  8:25   ` bot+bpf-ci
2026-11-20  7:41 ` [PATCH bpf-next 21/49] bpf: Compare states by join_key, then by value Alexei Starovoitov
2026-11-20  8:22   ` sashiko-bot [this message]
2026-11-20  7:41 ` [PATCH bpf-next 22/49] bpf: Continue a pass after an unproven insn Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 23/49] bpf: Find the xregs that an unproven insn depends on Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 24/49] bpf: Keep two states apart if data and control deps differ Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 25/49] bpf: Join any two states that have the same join_key Alexei Starovoitov
2026-11-20  8:25   ` sashiko-bot
2026-11-20  7:41 ` [PATCH bpf-next 26/49] bpf: Check the equations when a state is pruned or joined Alexei Starovoitov
2026-11-20  7:41 ` [PATCH bpf-next 27/49] bpf: Guess the equations of a join Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 28/49] bpf: Prune or join each state that arrives Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 29/49] bpf: Order the pending states and free the states that are done Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 30/49] bpf: Get the trip count from the trip index Alexei Starovoitov
2026-11-20  8:22   ` sashiko-bot
2026-11-20  7:42 ` [PATCH bpf-next 31/49] bpf: Give a loop a checked trip count or may_goto Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 32/49] bpf: Use the equations in the join rule Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 33/49] bpf: Verify a static function once for each instance Alexei Starovoitov
2026-11-20  8:33   ` sashiko-bot
2026-11-20  7:42 ` [PATCH bpf-next 34/49] bpf: Partition the states after an error Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 35/49] bpf: Reproduce an error on one path, with no joins Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 36/49] bpf: Do not let a join raise what a program requires Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 37/49] bpf: Verify in passes Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 38/49] bpf: Make a returned pointer to the stack unreadable Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 39/49] bpf: Keep two states apart if a live xreg has two types Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 40/49] bpf: Check the body of a counted loop once Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 41/49] bpf: Refine the copies of a number that AND or OR splits Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 42/49] bpf: Allow a range of offsets for a constant string Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 43/49] bpf: Keep spilled pointers at a range of stack offsets Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 44/49] selftests/bpf: Adjust tests to the new log text Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 45/49] selftests/bpf: Adjust tests that get another verdict or may_goto Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 46/49] selftests/bpf: Test barriers and far pointers Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 47/49] selftests/bpf: Test passes, instances, precision and the failing path Alexei Starovoitov
2026-11-20  8:30   ` sashiko-bot
2026-11-20  7:42 ` [PATCH bpf-next 48/49] selftests/bpf: Test equations Alexei Starovoitov
2026-11-20  7:42 ` [PATCH bpf-next 49/49] bpf, docs: Rewrite verifier.rst Alexei Starovoitov

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=sashiko-outbox-224719@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexei.starovoitov@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