All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiri Olsa" <jolsa@kernel.org>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 4/4] selftests/bpf: Verify classic uprobe attach types
Date: Tue, 03 Nov 2026 09:56:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-194987@kernel.org> (raw)
In-Reply-To: <20261103093400.112178-5-jolsa@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] bpf: classic uprobes with expected_attach_type 0 still use private stack
- [High] bpf: unchecked kprobe expected_attach_type breaks uprobe feature probe
--

commit 9ee5232694f8a364dae370cb864edf0a6d7ff502
Author: Jiri Olsa <jolsa@kernel.org>

selftests/bpf: Verify classic uprobe attach types

This commit adds assertions to the uprobe selftest to check that loaded
classic uprobe and uretprobe programs keep BPF_TRACE_UPROBE as their
expected attach type.

> diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe.c b/tools/testing/selftests/bpf/prog_tests/uprobe.c
> index 86404476c1daf..7d372552f5eab 100644
> --- a/tools/testing/selftests/bpf/prog_tests/uprobe.c
> +++ b/tools/testing/selftests/bpf/prog_tests/uprobe.c
> @@ -44,6 +44,10 @@ static void test_uprobe_attach(void)
>  	skel = test_uprobe__open_and_load();
>  	if (!ASSERT_OK_PTR(skel, "skel_open"))
>  		return;
> +	ASSERT_EQ(bpf_program__expected_attach_type(skel->progs.test1),
> +		  BPF_TRACE_UPROBE, "uprobe_attach_type");
> +	ASSERT_EQ(bpf_program__expected_attach_type(skel->progs.test3),
> +		  BPF_TRACE_UPROBE, "uretprobe_attach_type");

[Severity: High]
Does the feature probe behind this expected_attach_type assignment actually
detect kernel support for BPF_TRACE_UPROBE?

In tools/lib/bpf/libbpf.c, libbpf_prepare_prog_load() sets
expected_attach_type when FEAT_UPROBE_ATTACH_TYPE is supported:

	/* set uprobe attach_type if supported */
	if ((def & SEC_UPROBE) && kernel_supports(prog->obj, FEAT_UPROBE_ATTACH_TYPE)) {
		prog->expected_attach_type = BPF_TRACE_UPROBE;
		opts->expected_attach_type = BPF_TRACE_UPROBE;
	}

In tools/lib/bpf/features.c, probe_kern_uprobe_attach_type() probes this by
calling bpf_prog_load() with BPF_PROG_TYPE_KPROBE and
opts.expected_attach_type = BPF_TRACE_UPROBE:

	fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", insns,
			   ARRAY_SIZE(insns), &opts);
	return probe_fd(fd);

However, in kernel/bpf/syscall.c, bpf_prog_load_check_attach() does not have
a case for BPF_PROG_TYPE_KPROBE and falls into the default case:

	case BPF_PROG_TYPE_SYSCALL:
	case BPF_PROG_TYPE_EXT:
		if (expected_attach_type)
			return -EINVAL;
		fallthrough;
	default:
		return 0;

Because BPF_PROG_LOAD does not validate expected_attach_type for
BPF_PROG_TYPE_KPROBE, wouldn't probe_kern_uprobe_attach_type() always
succeed on older kernels too, causing libbpf to unconditionally set
BPF_TRACE_UPROBE while current kernels accept arbitrary invalid
expected_attach_type values for kprobe programs? Also, since
test_uprobe_attach() only checks the userspace getter
bpf_program__expected_attach_type(), should the test also verify that the
kernel rejects attaching a BPF_TRACE_UPROBE program to a non-uprobe perf
event?

[Severity: Critical]
In addition, can a classic uprobe program loaded with expected_attach_type
set to 0 still be assigned a per-CPU private stack and corrupt stack memory?

In kernel/bpf/verifier.c, bpf_enable_priv_stack() only disables the private
stack when expected_attach_type is explicitly set to one of the uprobe
attach types:

	case BPF_PROG_TYPE_KPROBE:
		/* Disable private stack for uprobe programs. */
		if (prog->expected_attach_type == BPF_TRACE_UPROBE ||
		    prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI ||
		    prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION)
			return NO_PRIV_STACK;
		fallthrough;

And in kernel/events/core.c, __perf_event_set_bpf_prog() still allows
attaching a BPF_PROG_TYPE_KPROBE program with expected_attach_type == 0 (and
jits_use_priv_stack == true) to a uprobe perf event:

	if (prog->expected_attach_type == BPF_TRACE_UPROBE && !is_uprobe)
		return -EINVAL;

	if (prog->type == BPF_PROG_TYPE_KPROBE && prog->sleepable && !is_uprobe)
		/* only uprobe programs are allowed to be sleepable */
		return -EINVAL;

If an existing userspace loader (or a sleepable BPF_PROG_TYPE_KPROBE program
loaded with expected_attach_type == 0) loads a program with stack depth >=
BPF_PRIV_STACK_MIN_SIZE and attaches it to a uprobe event, won't it still
receive PRIV_STACK_ADAPTIVE and overwrite the per-CPU private stack if
preempted on the same CPU?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261103093400.112178-1-jolsa@kernel.org?part=4

  reply	other threads:[~2026-11-03  9:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  9:33 [PATCH bpf-next 0/4] bpf: Disable private stacks for uprobe programs Jiri Olsa
2026-11-03  9:33 ` [PATCH bpf-next 1/4] bpf: Disable private stack " Jiri Olsa
2026-11-03  9:33 ` [PATCH bpf-next 2/4] bpf: Add BPF_TRACE_UPROBE attach type Jiri Olsa
2026-11-03  9:59   ` sashiko-bot
2026-11-03  9:33 ` [PATCH bpf-next 3/4] libbpf: Detect BPF_TRACE_UPROBE support Jiri Olsa
2026-11-03  9:54   ` sashiko-bot
2026-11-03  9:34 ` [PATCH bpf-next 4/4] selftests/bpf: Verify classic uprobe attach types Jiri Olsa
2026-11-03  9:56   ` sashiko-bot [this message]
2026-11-04  4:33 ` [PATCH bpf-next 0/4] bpf: Disable private stacks for uprobe programs Alexei Starovoitov
2026-11-04 14:17   ` Jiri Olsa

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-194987@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jolsa@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 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.