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 v4 08/12] bpf: Unify release handling for helpers and kfuncs
Date: Wed, 06 May 2026 23:59:14 +0000	[thread overview]
Message-ID: <20260506235915.687C5C2BCB0@smtp.kernel.org> (raw)
In-Reply-To: <20260506142709.2298255-9-ameryhung@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Hardcoding meta->release_regno = BPF_REG_1 for KF_RELEASE kfuncs creates a Use-After-Free vulnerability for kfuncs releasing non-first arguments.
- [Medium] Incorrect evaluation of the release_dynptr flag leads to false rejection of valid BPF programs.
--

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

bpf: Unify release handling for helpers and kfuncs

Introduce release_reg() to consolidate the release logic shared by both
helpers and kfuncs. The patch determines release_regno from the function
prototype or metadata before argument checking.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 542912c7983f7..e40f09cefa291 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
[ ... ]
> @@ -9870,37 +9928,11 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
>  	}
>  
>  	if (meta.release_regno) {
[ ... ]
> +		struct bpf_reg_state *reg = &regs[meta.release_regno];
> +		bool convert_rcu = (func_id == BPF_FUNC_kptr_xchg) && in_rcu_cs(env) &&
> +				   (reg->type & MEM_ALLOC) && (reg->type & MEM_PERCPU);
> +
> +		err = release_reg(env, reg, convert_rcu, !!meta.dynptr.ref_obj_id);
>  		if (err)
>  			return err;
>  	}

Does passing !!meta.dynptr.ref_obj_id as the release_dynptr flag cause valid
programs to be rejected?

If a kfunc or helper accepts both a normal PTR_TO_BTF_ID to be released and
a referenced dynptr as a normal (non-release) argument,
meta.dynptr.ref_obj_id would be set. This would cause release_reg() to
mistakenly invoke unmark_stack_slots_dynptr() on the non-dynptr release
register, returning -EINVAL.

Could the release condition be strictly derived from the argument type of
the specific release register instead?

[ ... ]
> @@ -12083,6 +12107,9 @@ int bpf_fetch_kfunc_arg_meta(struct bpf_verifier_env *env,
>  
>  	meta->kfunc_flags = *kfunc.flags;
>  
> +	if (is_kfunc_release(meta))
> +		meta->release_regno = BPF_REG_1;
> +
>  	return 0;
>  }

By unconditionally hardcoding BPF_REG_1 here, could this create a
use-after-free if a kernel module defines a release kfunc that frees an
argument other than the first?

If a BPF program passes a dummy referenced object to R1 and the actual
target object to R2, the verifier might drop the reference for R1 while
retaining R2, even though the kfunc frees R2 at runtime.

Should we still rely on dynamically discovering the release register based
on the argument type to avoid this?

[ ... ]
> @@ -12622,13 +12638,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
[ ... ]
>  	if (meta.release_regno) {
> -		struct bpf_reg_state *reg = &regs[meta.release_regno];
> -
> -		if (meta.dynptr.ref_obj_id) {
> -			err = unmark_stack_slots_dynptr(env, reg);
> -		} else {
> -			err = release_reference(env, reg->ref_obj_id);
> -		}
> +		err = release_reg(env, &regs[meta.release_regno], false, !!meta.dynptr.ref_obj_id);
>  		if (err)
>  			return err;
>  	}

Similar to the check_helper_call() case above, does this global dynptr
metadata check safely handle mixed arguments when releasing registers?

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

  reply	other threads:[~2026-05-06 23:59 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-06 14:26 [PATCH bpf-next v4 00/12] Refactor verifier object relationship tracking Amery Hung
2026-05-06 14:26 ` [PATCH bpf-next v4 01/12] bpf: Simplify mark_stack_slot_obj_read() and callers Amery Hung
2026-05-11 17:17   ` Eduard Zingerman
2026-05-06 14:26 ` [PATCH bpf-next v4 02/12] bpf: Unify dynptr handling in the verifier Amery Hung
2026-05-06 15:27   ` bot+bpf-ci
2026-05-07 12:22     ` Amery Hung
2026-05-06 14:26 ` [PATCH bpf-next v4 03/12] bpf: Assign reg->id when getting referenced kptr from ctx Amery Hung
2026-05-06 15:27   ` bot+bpf-ci
2026-05-07 12:38     ` Amery Hung
2026-05-11 21:31   ` Eduard Zingerman
2026-05-06 14:27 ` [PATCH bpf-next v4 04/12] bpf: Preserve reg->id of pointer objects after null-check Amery Hung
2026-05-11 21:48   ` Eduard Zingerman
2026-05-06 14:27 ` [PATCH bpf-next v4 05/12] bpf: Refactor object relationship tracking and fix dynptr UAF bug Amery Hung
2026-05-06 15:27   ` bot+bpf-ci
2026-05-07 12:20     ` Amery Hung
2026-05-06 21:54   ` sashiko-bot
2026-05-07 12:52     ` Amery Hung
2026-05-12  2:28   ` Eduard Zingerman
2026-05-06 14:27 ` [PATCH bpf-next v4 06/12] bpf: Remove redundant dynptr arg check for helper Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 07/12] bpf: Unify referenced object tracking in verifier Amery Hung
2026-05-06 22:48   ` sashiko-bot
2026-05-07 12:55     ` Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 08/12] bpf: Unify release handling for helpers and kfuncs Amery Hung
2026-05-06 23:59   ` sashiko-bot [this message]
2026-05-07 13:23     ` Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 09/12] selftests/bpf: Test creating dynptr from dynptr data and slice Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 10/12] selftests/bpf: Test using dynptr after freeing the underlying object Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 11/12] selftests/bpf: Test using slice after invalidating dynptr clone Amery Hung
2026-05-06 14:27 ` [PATCH bpf-next v4 12/12] 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=20260506235915.687C5C2BCB0@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ameryhung@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko@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