Linux Trace Kernel
 help / color / mirror / Atom feed
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: sashiko-bot@kernel.org
Cc: Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/2] riscv: kprobes: add KUnit test for LR/SC sequence rejection
Date: Thu, 15 Oct 2026 22:22:02 +0900	[thread overview]
Message-ID: <20261015132203.D87721F000C4@smtp.kernel.org> (raw)
In-Reply-To: <sashiko-outbox-171103@kernel.org>

On Wed, 14 Oct 2026 09:54:54 +0000, sashiko-bot@kernel.org <sashiko-bot@kernel.org> wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] kprobes: stack-allocated kprobe leak and UAF on unexpected test failure
> --
>
> commit 10b8cab517db949a43dcca583dbcd1f20f6d68e8
> Author: Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>
>
> riscv: kprobes: add KUnit test for LR/SC sequence rejection
>
> This commit extends the kprobes KUnit test to verify that register_kprobe()
> correctly handles an LR/SC sequence. It asserts that probes are accepted
> around the sequence but rejected if placed directly on the LR, SC, or any
> instruction inside the sequence.
>
> > diff --git a/arch/riscv/kernel/tests/kprobes/test-kprobes.c b/arch/riscv/kernel/tests/kprobes/test-kprobes.c
> > index 027424a3ff7b1..2727537569739 100644
> > --- a/arch/riscv/kernel/tests/kprobes/test-kprobes.c
> > +++ b/arch/riscv/kernel/tests/kprobes/test-kprobes.c
>
> [ ... ]
>
> > +static void test_kprobe_lrsc(struct kunit *test)
> > +{
> > +	/*
> > +	 * test_kprobes_lrsc_offsets[] points, in order, at an instruction
> > +	 * right before an LR/SC sequence, the LR itself, an instruction
> > +	 * inside the sequence, the SC, and an instruction right after it.
> > +	 */
> > +	bool reject[] = { false, true, true, true, false };
> > +	int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(reject); i++) {
> > +		struct kprobe kp = {};
> > +
> > +		kp.pre_handler = kprobe_dummy_handler;
> > +		kp.addr = (kprobe_opcode_t *)test_kprobes_lrsc_offsets[i];
> > +
> > +		if (reject[i]) {
> > +			KUNIT_EXPECT_LT(test, register_kprobe(&kp), 0);
> > +		} else {
> > +			KUNIT_EXPECT_EQ(test, register_kprobe(&kp), 0);
> > +			unregister_kprobe(&kp);
> > +		}
>
> [Severity: High]
> Does this leave the stack-allocated kprobe registered if register_kprobe() 
> unexpectedly succeeds? 

Good catch!

Xiaofeng, can you fix it?

Thanks,

>
> In the rejection path where reject[i] is true, KUNIT_EXPECT_LT() will log 
> a test failure if register_kprobe(&kp) returns 0, but execution will
> continue. Because unregister_kprobe(&kp) is only called in the else block, 
> the kprobe remains in the global kprobe list.
>
> Since struct kprobe kp is allocated on the stack within the loop, advancing
> to the next iteration zeroes out the reused stack memory. When the function 
> eventually returns, the stack frame is popped. This would leave a dangling
> pointer in the kprobes subsystem, causing a use-after-free or kernel panic 
> on subsequent kprobe operations or interrupts.
>
> Should there be a check to unregister the kprobe if the registration 
> unexpectedly succeeds in the reject path?
>
> > +	}
> > +}
>
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261014094415.374-1-yuanxiaofeng@eswincomputing.com?part=2

-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

      reply	other threads:[~2026-10-15 13:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260828091202.1124-1-yuanxiaofeng@eswincomputing.com>
2026-09-22 15:40 ` [PATCH v4 0/2] riscv: kprobes: reject probes inside LR/SC sequences Nam Cao
2026-10-14  9:44 ` [PATCH v5 " Xiaofeng Yuan
2026-10-15 13:10   ` Masami Hiramatsu
2026-10-14  9:44 ` [PATCH v5 1/2] " Xiaofeng Yuan
2026-10-15 13:19   ` Masami Hiramatsu
2026-10-14  9:44 ` [PATCH v5 2/2] riscv: kprobes: add KUnit test for LR/SC sequence rejection Xiaofeng Yuan
2026-10-14  9:54   ` sashiko-bot
2026-10-15 13:22     ` Masami Hiramatsu [this message]

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=20261015132203.D87721F000C4@smtp.kernel.org \
    --to=mhiramat@kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=yuanxiaofeng@eswincomputing.com \
    /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