All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ning Ding" <dingning04@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf v2 2/2] bpf: Reject untrusted pointers in refcount_acquire
Date: Mon, 27 Jul 2026 00:13:54 +0000	[thread overview]
Message-ID: <20260727001354.B3F1E1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260726235030.1152542-3-dingning04@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] check_ptr_to_btf_access() incorrectly allows writes to PTR_UNTRUSTED allocated objects or triggers a verifier_bug WARN_ONCE when reading from them.
--

--- Patch [2]: [PATCH bpf v2 2/2] bpf: Reject untrusted pointers in refcount_acquire ---
commit 80e6c61503b2eee2d4804b0a7ec8927827ed7d92
Author: Ning Ding <dingning04@gmail.com>

bpf: Reject untrusted pointers in refcount_acquire

This commit fixes an issue where a refcounted map kptr could still be passed
to bpf_refcount_acquire() after being marked PTR_UNTRUSTED due to
bpf_rcu_read_unlock(). It updates check_kfunc_args() to reject PTR_UNTRUSTED
arguments for KF_ARG_PTR_TO_REFCOUNTED_KPTR.

Link: https://lore.kernel.org/r/20260726021304.97ED91F000E9@smtp.kernel.org

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 1e7343b625de5..63b1d997fe804 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -12414,6 +12414,11 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
>  			meta->subprogno = reg->subprogno;
>  			break;
>  		case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
> +			if (reg->type & PTR_UNTRUSTED) {
> +				verbose(env, "%s is an untrusted refcounted kptr\n",
> +					reg_arg_name(env, argno));
> +				return -EACCES;
> +			}

[Severity: Critical]
This is a pre-existing issue, but I noticed a similar problem with
PTR_UNTRUSTED handling in check_ptr_to_btf_access() for allocated objects:

kernel/bpf/verifier.c:check_ptr_to_btf_access() {
...
		if (atype != BPF_READ && !type_is_ptr_alloc_obj(reg->type)) {
			verbose(env, "only read is supported\n");
			return -EACCES;
		}

		if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) &&
		    !(reg->type & MEM_RCU) && !reg_is_referenced(env, reg)) {
			verifier_bug(env, "allocated object must have a referenced id");
			return -EFAULT;
		}
...
}

Does this logic incorrectly allow writes to PTR_UNTRUSTED allocated objects
or trigger a verifier_bug WARN_ONCE when reading from them?

If a program loads a program-allocated kptr containing an RCU-protected field
inside bpf_rcu_read_lock(), the verifier marks it MEM_ALLOC | MEM_RCU (and
NON_OWN_REF if it contains a bpf_rb_node or bpf_list_node). When the program
calls bpf_rcu_read_unlock(), it strips MEM_RCU and adds PTR_UNTRUSTED.

If the program subsequently tries to read or write this pointer,
type_is_ptr_alloc_obj(reg->type) evaluates to true because it ignores
PTR_UNTRUSTED, bypassing the read-only check for writes.

Then, if the pointer lacks NON_OWN_REF, it hits the type_is_alloc() condition
and triggers a verifier_bug(), leading to a BPF_WARN_ONCE. If it has
NON_OWN_REF, it bypasses the verifier_bug() check entirely, and proceeds to
allow a memory write to the untrusted pointer.

Could a malicious BPF program exploit this to achieve a local denial of
service via panic_on_warn or a use-after-free memory write?

>  			if (!type_is_ptr_alloc_obj(reg->type)) {
>  				verbose(env, "%s is neither owning or non-owning ref\n",
>  					reg_arg_name(env, argno));

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260726235030.1152542-1-dingning04@gmail.com?part=2

  reply	other threads:[~2026-07-27  0:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-26 23:50 [PATCH bpf v2 0/2] bpf: Fix refcount_acquire handling for borrowed kptrs Ning Ding
2026-07-26 23:50 ` [PATCH bpf v2 1/2] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs Ning Ding
2026-07-27  4:50   ` Greg KH
2026-07-26 23:50 ` [PATCH bpf v2 2/2] bpf: Reject untrusted pointers in refcount_acquire Ning Ding
2026-07-27  0:13   ` sashiko-bot [this message]
2026-07-27  0:40     ` Ning Ding
2026-07-27  4:50   ` Greg KH

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=20260727001354.B3F1E1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dingning04@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.