BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [RFC PATCH bpf-next v1 07/16] bpf: Trust typed pointer fields of typed arena objects
Date: Sun, 27 Sep 2026 00:03:26 +0000	[thread overview]
Message-ID: <20260927000328.676401F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260926233503.3114147-8-memxor@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: refcount leak in bpf_typed_arena causes saturation and WARN
--

commit 824870737e5e6082cced029879731300754bd906
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Subject: bpf: Trust typed pointer fields of typed arena objects

This commit introduces logic to track and trust typed pointer fields
within typed arena objects. A load from these fields returns an
unsanitized pointer which is considered trustworthy without additional
checks, and writes ensure only valid typed arena pointers or null are
stored.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
[ ... ]
> @@ -6328,6 +6348,61 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
[ ... ]
> +static int check_typed_ptr_field_access(struct bpf_verifier_env *env, struct bpf_reg_state *regs,
> +					struct bpf_reg_state *reg, const char *tname, u32 btf_id,
> +					enum bpf_access_type atype, int value_regno)
> +{
> +	struct bpf_insn *insn = &env->prog->insnsi[env->insn_idx];
> +	u8 class = BPF_CLASS(insn->code);
> +	struct bpf_reg_state *val;
> +	struct bpf_typed_arena *ta;
> +
> +	if (BPF_MODE(insn->code) != BPF_MEM || BPF_SIZE(insn->code) != BPF_DW ||
> +	    (class != BPF_LDX && class != BPF_STX && class != BPF_ST)) {
> +		verbose(env, "typed pointer field of struct %s must be accessed with a 64-bit load or store\n",
> +			tname);
> +		return -EACCES;
> +	}
> +	if (atype == BPF_READ) {
> +		/*
> +		 * The pointer leads into the pointee's typed arena, which the
> +		 * program may never cast to or allocate from: register it here,
> +		 * as a cast would, so that the sanitization has a slice to mask
> +		 * into and the slice exists for the map's lifetime.
> +		 */
> +		ta = typed_arena_register(env, btf_id);

[Severity: Medium]
Could this lead to a refcount leak for the typed arena when the program
is unloaded?

When typed_arena_register() is called here, it acquires a reference to
the typed arena via bpf_typed_arena_get() which increments ta->refcnt.
However, the program unload path in bpf_prog_free_deferred() skips
releasing the reference acquired during load:

kernel/bpf/core.c:bpf_prog_free_deferred() {
    ...
    /* The typed arenas outlive the program; only the load's failure retracts them. */
    kfree(aux->typed_arenas);
    ...
}

Because the reference is never decremented after a successful load, the
refcount will monotonically increase by one per load and unload cycle.

Since this path is reachable by any user capable of loading BPF programs,
is it possible for a user to repeatedly load and unload a program to
saturate the refcount and trigger a kernel WARN?

> +		if (IS_ERR(ta))
> +			return PTR_ERR(ta);
> +		return mark_btf_ld_reg(env, regs, value_regno, PTR_TO_BTF_ID, reg->btf, btf_id,
> +				       MEM_ARENA | PTR_UNSANITIZED);
> +	}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260926233503.3114147-1-memxor@gmail.com?part=7

  reply	other threads:[~2026-09-27  0:03 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-26 23:34 [RFC PATCH bpf-next v1 00/16] BPF typed arenas Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 01/16] mm/vmalloc: Add get_vm_area_align() Kumar Kartikeya Dwivedi
2026-09-26 23:42   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 02/16] bpf: Introduce BPF typed arenas Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 03/16] bpf: Back typed arena chunks with scratch on demand Kumar Kartikeya Dwivedi
2026-09-26 23:56   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 04/16] bpf: Add the typed_arena_cast instruction Kumar Kartikeya Dwivedi
2026-09-26 23:55   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 05/16] bpf: Allow scalar and atomic access to typed arena objects Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 06/16] bpf: Support special fields in " Kumar Kartikeya Dwivedi
2026-09-26 23:59   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 07/16] bpf: Trust typed pointer fields of " Kumar Kartikeya Dwivedi
2026-09-27  0:03   ` sashiko-bot [this message]
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 08/16] bpf: Canonicalize loaded typed arena pointers where they are used Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 09/16] bpf: Add typed arena page allocation and release kfuncs Kumar Kartikeya Dwivedi
2026-09-26 23:55   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 10/16] bpf: Let typed_arena_cast copy pointers the verifier already trusts Kumar Kartikeya Dwivedi
2026-09-26 23:49   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 11/16] libbpf: Support the typed_arena_cast instruction Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 12/16] selftests/bpf: Build BPF objects with compiler-inserted typed arena casts Kumar Kartikeya Dwivedi
2026-09-26 23:46   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 13/16] selftests/bpf: Test typed arena casts and registration Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 14/16] selftests/bpf: Test typed arena object access, kptrs and typed pointer fields Kumar Kartikeya Dwivedi
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 15/16] selftests/bpf: Test typed arena page allocation and release Kumar Kartikeya Dwivedi
2026-09-26 23:46   ` sashiko-bot
2026-09-26 23:34 ` [RFC PATCH bpf-next v1 16/16] selftests/bpf: Exercise typed arenas at run time Kumar Kartikeya Dwivedi
2026-09-26 23:50   ` sashiko-bot

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=20260927000328.676401F000FF@smtp.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox