All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vladimir Kondratiev" <vladimir.kondratiev@mobileye.com>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/3] riscv: probes: reject probes inside an LR/SC sequence
Date: Wed, 25 Nov 2026 16:31:06 +0000	[thread overview]
Message-ID: <sashiko-outbox-233758@kernel.org> (raw)
In-Reply-To: <20261125-probes-in-lrsc-v5-2-38a32e53fcc2@mobileye.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] riscv: probes: premature return in riscv_probe_in_lrsc_body()
  on branch exit
- [Medium] riscv: probes: uprobe_in_lrsc_body() accepts probe when first
  page read fails
--

commit a67fed45f5b4b65e5ea0692a072a54d9eda46839
Author: Vladimir Kondratiev <vladimir.kondratiev@mobileye.com>

riscv: probes: reject probes inside an LR/SC sequence

This commit rejects kprobes and uprobes placed inside a constrained LR/SC
sequence by scanning forward through reachable instructions for an SC
instruction.

> diff --git a/arch/riscv/kernel/probes/decode-insn.c b/arch/riscv/kernel/probes/decode-insn.c
> index 433d9035b8882..cfc59964cda88 100644
> --- a/arch/riscv/kernel/probes/decode-insn.c
> +++ b/arch/riscv/kernel/probes/decode-insn.c
> @@ -4,11 +4,182 @@

[ ... ]

> +bool __kprobes riscv_probe_in_lrsc_body(const u16 *buf, size_t len)
> +{
> +	bool reachable_idx[RISCV_LRSC_SCAN_BYTES / 2] = { false };
> +	int idx = -1;
> +
> +	reachable_idx[0] = true; /* instruction at idx 0 is reachable */
> +	len = min(len, RISCV_LRSC_SCAN_BYTES);
> +
> +	if (len < 4) /* SC is 32 bit instruction */
> +		return false;
> +
> +	for (;;) {
> +		s32 imm;
> +
> +		/* find next nearest reachable idx */
> +		while (++idx < ARRAY_SIZE(reachable_idx) && !reachable_idx[idx])
> +			;
> +		if (idx >= ARRAY_SIZE(reachable_idx))
> +			return false;
> +
> +		/* fetch full instruction, 16 or 32 bit */
> +		size_t off = idx * 2; /* buffer offset in bytes */
> +		u32 insn = buf[idx];
> +		size_t insn_len = GET_INSN_LENGTH(insn);
> +
> +		if (insn_len == 4) {
> +			if (off + 4 > len)
> +				return false;
> +			insn |= (u32)buf[idx + 1] << 16;
> +		}
> +
> +		/* Validate current instruction */
> +		/* If we hit an SC, the forward path successfully completes the atomic block */
> +		if (riscv_insn_is_sc(insn))
> +			return true;
> +
> +		if (idx == 0) {
> +			if (!riscv_insn_is_lr(insn) && !riscv_insn_is_lrsc_body(insn))
> +				return false;
> +		} else {
> +			if (!riscv_insn_is_lrsc_body(insn))
> +				return false;
> +		}

[Severity: Medium]
Should the idx > 0 check (and the off + 4 > len check above) use continue
instead of return false so that hitting a non-body instruction on one branch
path does not abort the scan of other pending entries in reachable_idx[]?

When a conditional branch inside an LR/SC loop jumps forward to the sc block
and falls through to an exit instruction such as ret (for example: 0: lr.w a0,
(a1); beq a0, a2, 1f; ret; 1: sc.w a3, a4, (a1); bnez a3, 0b), reachable_idx[]
has both the fallthrough exit and the forward target marked. Because the loop
visits lower indices first, it hits ret before 1: sc.w and returns false
immediately, allowing a probe inside the LR/SC sequence that livelocks when
the branch to sc is taken.

> diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
> index eb177d0ce8ab0..d97a102621665 100644
> --- a/arch/riscv/kernel/probes/uprobes.c
> +++ b/arch/riscv/kernel/probes/uprobes.c
> @@ -28,6 +28,34 @@ unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
>  	return instruction_pointer(regs);
>  }
>  
> +/*
> + * Is the instruction at addr inside a constrained LR/SC sequence? Only the
> + * probed instruction was copied, so read the text that follows it from the
> + * task. The caller holds mmap_lock, as uprobe_write_opcode() requires too.
> + * Text that cannot be read ends the scan early, which accepts the probe.
> + */
> +static bool uprobe_in_lrsc_body(struct mm_struct *mm, unsigned long addr)
> +{
> +	u16 buf[RISCV_LRSC_SCAN_BYTES / sizeof(u16)];
> +	struct vm_area_struct *vma;
> +	struct page *page;
> +	size_t got = 0;
> +	int len;
> +
> +	while (got < sizeof(buf)) {
> +		len = min_t(size_t, sizeof(buf) - got,
> +			    PAGE_SIZE - offset_in_page(addr + got));
> +		page = get_user_page_vma_remote(mm, addr + got, FOLL_FORCE, &vma);
> +		if (IS_ERR(page))
> +			break;
> +		uprobe_copy_from_page(page, addr + got, (void *)buf + got, len);
> +		put_page(page);
> +		got += len;
> +	}
> +
> +	return riscv_probe_in_lrsc_body(buf, got);
> +}
> +
>  int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
>  			     unsigned long addr)
>  {
> @@ -53,6 +81,9 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
>  		return -EINVAL;
>  	}
>  
> +	if (uprobe_in_lrsc_body(mm, addr))
> +		return -EINVAL;
> +
>  	return 0;
>  }

[Severity: Medium]
What happens if get_user_page_vma_remote() fails on the first page (got == 0)?

Breaking out of the loop passes got == 0 to riscv_probe_in_lrsc_body(), which
returns false because len < 4 without checking even the already-copied
instruction in auprobe->insn. arch_uprobe_analyze_insn() then returns 0 and
prepare_uprobe() permanently sets UPROBE_COPY_INSN on uprobe->flags, so later
installations of this uprobe skip prepare_uprobe() and arm the breakpoint
even if the probed instruction sits inside an LR/SC loop or directly on an sc.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-probes-in-lrsc-v5-0-38a32e53fcc2@mobileye.com?part=2

  reply	other threads:[~2026-11-25 16:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 16:12 [PATCH v5 0/3] riscv: probes: check for LR/SC sequence Vladimir Kondratiev
2026-11-25 16:12 ` Vladimir Kondratiev
2026-11-25 16:12 ` [PATCH v5 1/3] riscv: kprobes: fix out-of-bounds read in riscv_probe_decode_insn Vladimir Kondratiev
2026-11-25 16:12   ` Vladimir Kondratiev
2026-11-25 16:12 ` [PATCH v5 2/3] riscv: probes: reject probes inside an LR/SC sequence Vladimir Kondratiev
2026-11-25 16:12   ` Vladimir Kondratiev
2026-11-25 16:31   ` sashiko-bot [this message]
2026-11-25 16:12 ` [PATCH v5 3/3] riscv: kprobes: add KUnit test for LR/SC sequence rejection Vladimir Kondratiev
2026-11-25 16:12   ` Vladimir Kondratiev

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-233758@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vladimir.kondratiev@mobileye.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 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.