All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v1 16/29] bpf: Describe locks at throw sites
Date: Wed, 11 Nov 2026 16:04:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-209246@kernel.org> (raw)
In-Reply-To: <8861d3d06eadcff128742ac81b2ba9c54f71c523.1794399387.git.memxor@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] bpf: s16 overflow in ref_locations() when computing lock offset
  adjustment
--

commit b280bb740c33eccf7a76373a4566fec46d049b84
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>

bpf: Describe locks at throw sites

This commit records held locks in throw site descriptors either by their fixed
address for single-element array maps or relative to a stack slot or register
holding a pointer to the containing object, avoiding hidden stack slots when
every visit to the site agrees on the lock.

> diff --git a/kernel/bpf/frame_desc.c b/kernel/bpf/frame_desc.c
> index 5cb837688f90c..72dc6597321a9 100644
> --- a/kernel/bpf/frame_desc.c
> +++ b/kernel/bpf/frame_desc.c

[ ... ]

> @@ -805,42 +844,50 @@ static void ref_locations(struct bpf_verifier_env *env, const struct bpf_verifie
>  	const struct bpf_res_acq *acq = &env->res_acqs[ref->acq - 1];
>  	const struct bpf_func_state *frame = st->frame[f];
>  	s16 adj, lim = env->subprog_info[frame->subprogno].fastcall_stack_end;
> +	bool lock = ref->type & REF_TYPE_LOCK_MASK, ptr;
>  	const struct bpf_stack_state *slot;
>  	int k;
>  
> +	memset(loc->at, 0, sizeof(loc->at));
>  	loc->cnt = 0;
>  	loc->obj = false;
> -	if (ref->type & REF_TYPE_LOCK_MASK)
> +	loc->fixed = lock && env->resources[acq->res].addr;
> +	if (loc->fixed) {
> +		loc->cnt = 1;
>  		return;
> +	}
> +	/* a pointer the frame keeps: to the object, or to what a lock with an id is in */
> +	ptr = lock ? ref->id && !(acq->flags & BPF_RES_F_NO_PTR) :
> +		     ref->type == REF_TYPE_PTR && !(acq->flags & BPF_RES_F_ARG);
>  	for (k = 0; k < bpf_stack_nr_slots(frame) && loc->cnt < BPF_RES_LOC_MAX; k++) {
>  		slot = bpf_stack_slot(frame, k);
>  		adj = 0;
>  		if (spi_off(k) < lim)
>  			break;
> -		if (stack_slot_is_object(slot, ref->id)) {
> +		if (!lock && stack_slot_is_object(slot, ref->id)) {
>  			/* the handle of a dynptr is its first word, as when it was created */
>  			if (slot->slot_type[BPF_REG_SIZE - 1] != STACK_DYNPTR) {
>  				loc->obj = true;
>  				loc->cnt = 0;
>  			}
> -		} else if (ref->type != REF_TYPE_PTR || (acq->flags & BPF_RES_F_ARG) ||
> -			   !bpf_is_spilled_reg(slot) || !reg_ref_off(&slot->spilled_ptr, ref->id, &adj)) {
> +		} else if (!ptr || !bpf_is_spilled_reg(slot) ||
> +			   !reg_ref_off(&slot->spilled_ptr, ref->id, &adj)) {
>  			continue;
>  		}
>  		loc->at[loc->cnt].off = spi_off(k);
> -		loc->at[loc->cnt].reg = false;
> -		loc->at[loc->cnt++].adj = adj;
> +		/* the acquisition of a lock has how far the lock is into what it is in */
> +		loc->at[loc->cnt++].adj = lock ? adj - acq->adj : adj;
>  		if (loc->obj)
>  			return;
>  	}
> -	if (!env->describe_regs || ref->type != REF_TYPE_PTR || (acq->flags & BPF_RES_F_ARG))
> +	if (!env->describe_regs || !ptr)
>  		return;
>  	for (k = 0; k < (throws ? ARRAY_SIZE(site_regs) : 4) && loc->cnt < BPF_RES_LOC_MAX; k++) {
>  		if (!reg_ref_off(&frame->regs[site_regs[k]], ref->id, &adj))
>  			continue;
>  		loc->at[loc->cnt].off = site_regs[k];
>  		loc->at[loc->cnt].reg = true;
> -		loc->at[loc->cnt++].adj = adj;
> +		loc->at[loc->cnt++].adj = lock ? adj - acq->adj : adj;
>  	}
>  }

[Severity: High]
Can adj - acq->adj underflow the s16 loc->at[].adj field in both loops?

resource_of_lock() checks that acq->adj fits in s16 (0..32767) and
reg_ref_off() checks that the pointer's constant var_off fits in s16
(-32768..32767), but when the spilled or register pointer has been adjusted to
a negative offset and acq->adj > 0 such that adj - acq->adj < S16_MIN, storing
the difference into loc->at[].adj wraps by +65536.  On unwind,
bpf_unwind_release_desc() computes handle = (void *)(long)(val - ent->adj) and
passes an out-of-bounds address 65536 bytes below the lock to
bpf_spin_unlock() or bpf_res_spin_unlock(), corrupting kernel memory and
leaving the lock held.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1794399387.git.memxor@gmail.com?part=16

  parent reply	other threads:[~2026-11-11 16:04 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 12:56 [PATCH bpf-next v1 00/29] Exceptions - Resource Cleanup - Redux Kumar Kartikeya Dwivedi
2026-11-11 12:56 ` [PATCH bpf-next v1 01/29] bpf: Record where JITs save callee-saved registers Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 02/29] bpf: Add bpf_throw_unwind() and an arch entry hook Kumar Kartikeya Dwivedi
2026-11-11 14:45   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 03/29] bpf, x86: Capture registers in a native bpf_throw() entry Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 04/29] bpf, arm64: " Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 05/29] bpf: Restore callee-saved registers from unwound frames Kumar Kartikeya Dwivedi
2026-11-11 14:41   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 06/29] bpf: Keep last insn's aux data in add_hidden_subprog() Kumar Kartikeya Dwivedi
2026-11-11 14:38   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 07/29] bpf: Add one allocator for hidden stack slots Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 15:15   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 08/29] bpf: Add flags and release kfuncs to the dtor registry Kumar Kartikeya Dwivedi
2026-11-11 13:33   ` bot+bpf-ci
2026-11-11 15:36   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 09/29] bpf: Register cleanup destructors for more types Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 15:00   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 10/29] bpf: Record where JITed frames keep the BPF stack Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 11/29] bpf: Give every reference an owner frame Kumar Kartikeya Dwivedi
2026-11-11 13:33   ` bot+bpf-ci
2026-11-11 12:57 ` [PATCH bpf-next v1 12/29] bpf: Release resources in hidden slots on bpf_throw() Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 15:30   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 13/29] bpf: Track held resources in hidden stack slots Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 16:12   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 14/29] bpf: Describe held resources at throw sites Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 12:57 ` [PATCH bpf-next v1 15/29] bpf: Describe handles in registers " Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 16:11   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 16/29] bpf: Describe locks " Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 16:04   ` sashiko-bot [this message]
2026-11-11 12:57 ` [PATCH bpf-next v1 17/29] bpf: Apply hidden slot limits only when a slot is needed Kumar Kartikeya Dwivedi
2026-11-11 16:31   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 18/29] bpf: Allow a callee to leave resources with its caller Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 16:41   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 19/29] bpf: Allow a callee to release its caller's resources Kumar Kartikeya Dwivedi
2026-11-11 17:02   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 20/29] bpf: Transfer a reference to the callee that releases it Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 16:57   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 21/29] bpf: Support callx when a callee releases resources Kumar Kartikeya Dwivedi
2026-11-11 14:04   ` bot+bpf-ci
2026-11-11 17:13   ` sashiko-bot
2026-11-11 12:57 ` [PATCH bpf-next v1 22/29] selftests/bpf: Test rejected throws that hold resources Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 23/29] selftests/bpf: Test resource cleanup on bpf_throw() Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 24/29] selftests/bpf: Test cleanup of handles on the stack Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 25/29] selftests/bpf: Test cleanup of handles in registers Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 26/29] selftests/bpf: Test callees releasing caller resources Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 27/29] selftests/bpf: Test resources a callee leaves with its caller Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 28/29] selftests/bpf: Test cleanup of locks described at sites Kumar Kartikeya Dwivedi
2026-11-11 12:57 ` [PATCH bpf-next v1 29/29] selftests/bpf: Test callee releases through callx Kumar Kartikeya Dwivedi
2026-11-11 20:33 ` [PATCH bpf-next v1 00/29] Exceptions - Resource Cleanup - Redux 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-209246@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=memxor@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.