Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal
@ 2026-12-03  9:58 Masami Hiramatsu (Google)
  2026-12-03  9:58 ` [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03  9:58 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Masami Hiramatsu, Oleg Nesterov
  Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-kernel, linux-trace-kernel,
	linux-perf-users

Hi,

This series fixes two bugs around UPROBE_COPY_INSN handling in
kernel/events/uprobes.c (both reported by Sashiko):

1. prepare_uprobe() uses double-checked locking around UPROBE_COPY_INSN,
   with smp_wmb() before set_bit(UPROBE_COPY_INSN, &uprobe->flags), but
   the lockless fast path returns 0 without a paired smp_rmb(). On
   weakly-ordered architectures, a concurrent caller can observe
   UPROBE_COPY_INSN set while reading stale uprobe->arch fields (such as
   auprobe->bpinsn in ARM's set_swbp()). Patch 1 adds the missing
   smp_rmb().

2. remove_breakpoint() unconditionally calls set_orig_insn(&uprobe->arch,
   ...) without checking UPROBE_COPY_INSN. If prepare_uprobe() previously
   failed or never ran, uprobe->arch.insn remains zero-initialized or
   unvalidated, and error cleanup or unregistration can overwrite an
   existing third-party software breakpoint (such as GDB) with zeroes.
   Patch 2 returns early if UPROBE_COPY_INSN is not set, paired with
   smp_rmb() before reading uprobe->arch.

Both patches are based on v7.4-rc5.

Thank you,

Masami Hiramatsu (Google) (2):
  uprobes: Add missing read memory barrier in prepare_uprobe()
  uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set

 kernel/events/uprobes.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe()
  2026-12-03  9:58 [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
@ 2026-12-03  9:58 ` Masami Hiramatsu (Google)
  2026-12-03 11:19   ` Oleg Nesterov
  2026-12-03  9:59 ` [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
  2026-12-03 10:17 ` [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal sashiko-bot
  2 siblings, 1 reply; 7+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03  9:58 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Masami Hiramatsu, Oleg Nesterov
  Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-kernel, linux-trace-kernel,
	linux-perf-users

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

prepare_uprobe() uses double-checked locking around UPROBE_COPY_INSN to
serialize concurrent callers (for example, register_for_each_vma() and
uprobe_mmap()). The writer populates uprobe->arch, executes smp_wmb(),
and sets UPROBE_COPY_INSN in uprobe->flags.

However, the lockless fast path at the start of prepare_uprobe() tests
UPROBE_COPY_INSN and returns 0 without a paired read memory barrier. On
weakly-ordered architectures, a concurrent caller taking the fast path
can observe UPROBE_COPY_INSN set while subsequently reading stale or
uninitialized fields from uprobe->arch (for instance, auprobe->bpinsn in
ARM's set_swbp()).

Add the missing smp_rmb() on the fast path of prepare_uprobe() to pair
with the smp_wmb() before set_bit(UPROBE_COPY_INSN, &uprobe->flags).

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-68084e34-bdf9-4e4a-8740-e4208f044e15
Fixes: 4710f05fd146 ("uprobes: Fix prepare_uprobe() race with itself")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 kernel/events/uprobes.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index c47b1ee266d5..e850865045b2 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1101,8 +1101,17 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 {
 	int ret = 0;
 
-	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
+	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) {
+		/*
+		 * Pairs with the smp_wmb() below.
+		 *
+		 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
+		 * we must also see the stores to &uprobe->arch performed by
+		 * prepare_uprobe().
+		 */
+		smp_rmb();
 		return ret;
+	}
 
 	/* TODO: move this into _register, until then we abuse this sem. */
 	down_write(&uprobe->consumer_rwsem);
@@ -1121,7 +1130,8 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 	if (ret)
 		goto out;
 
-	smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
+	/* Pairs with the smp_rmb() in prepare_uprobe() and handle_swbp() */
+	smp_wmb();
 	set_bit(UPROBE_COPY_INSN, &uprobe->flags);
 
  out:


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set
  2026-12-03  9:58 [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
  2026-12-03  9:58 ` [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
@ 2026-12-03  9:59 ` Masami Hiramatsu (Google)
  2026-12-03 10:17   ` sashiko-bot
  2026-12-03 11:58   ` Oleg Nesterov
  2026-12-03 10:17 ` [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal sashiko-bot
  2 siblings, 2 replies; 7+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-03  9:59 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Masami Hiramatsu, Oleg Nesterov
  Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-kernel, linux-trace-kernel,
	linux-perf-users

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

install_breakpoint() only inserts a breakpoint via set_swbp() after
prepare_uprobe() populates uprobe->arch and sets UPROBE_COPY_INSN in
uprobe->flags.

If prepare_uprobe() fails (for example in copy_insn() or
arch_uprobe_analyze_insn()) or was never called (for example when no
matching VMA was present at registration time), UPROBE_COPY_INSN stays
unset and uprobe->arch.insn remains zero-initialized or unvalidated.
During uprobe_register() error cleanup or unregistration,
register_for_each_vma() still calls remove_breakpoint() for any mm with
MMF_HAS_UPROBES set (such as when other uprobes are active in the same
process).

Because remove_breakpoint() calls set_orig_insn() without checking
UPROBE_COPY_INSN, if a third-party software breakpoint (such as GDB) is
present in an anonymous COW page at that address, verify_opcode() allows
the write and overwrites the process text with zeroes from
uprobe->arch.insn (and decrements ref_ctr if configured).

Check UPROBE_COPY_INSN in remove_breakpoint() and return 0 early if it
is not set. Pair the check with smp_rmb() so stores to uprobe->arch from
prepare_uprobe() are visible before set_orig_insn() reads them.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-fd7eba34-a37f-4278-b0b4-f3d7e399dfbe
Fixes: 2b1444983508 ("uprobes, mm, x86: Add the ability to install and remove uprobes breakpoints")
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 kernel/events/uprobes.c |   18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index e850865045b2..7f0515c9dc32 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1130,7 +1130,10 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 	if (ret)
 		goto out;
 
-	/* Pairs with the smp_rmb() in prepare_uprobe() and handle_swbp() */
+	/*
+	 * Pairs with the smp_rmb() in prepare_uprobe(), remove_breakpoint(),
+	 * and handle_swbp().
+	 */
 	smp_wmb();
 	set_bit(UPROBE_COPY_INSN, &uprobe->flags);
 
@@ -1194,6 +1197,19 @@ static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma,
 {
 	struct mm_struct *mm = vma->vm_mm;
 
+	/* No breakpoint was ever installed if prepare_uprobe() never succeeded */
+	if (!test_bit(UPROBE_COPY_INSN, &uprobe->flags))
+		return 0;
+
+	/*
+	 * Pairs with the smp_wmb() in prepare_uprobe().
+	 *
+	 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
+	 * we must also see the stores to &uprobe->arch before
+	 * set_orig_insn() reads them.
+	 */
+	smp_rmb();
+
 	mm_flags_set(MMF_RECALC_UPROBES, mm);
 	return set_orig_insn(&uprobe->arch, vma, vaddr);
 }


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set
  2026-12-03  9:59 ` [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
@ 2026-12-03 10:17   ` sashiko-bot
  2026-12-03 11:58   ` Oleg Nesterov
  1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 10:17 UTC (permalink / raw)
  To: Masami Hiramatsu (Google); +Cc: linux-trace-kernel, linux-perf-users

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] uprobes: early return in remove_breakpoint() skips
  MMF_RECALC_UPROBES
--

commit 7922d016707ed1ec398765711ff02f36bfd6af90
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>

uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set

This commit makes remove_breakpoint() return early if UPROBE_COPY_INSN is
not set in uprobe->flags, and adds an smp_rmb() pairing with
prepare_uprobe() before set_orig_insn() reads uprobe->arch.

> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index e850865045b21..7f0515c9dc327 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c

[ ... ]

> @@ -1194,6 +1197,19 @@ static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma,
>  {
>  	struct mm_struct *mm = vma->vm_mm;
>  
> +	/* No breakpoint was ever installed if prepare_uprobe() never succeeded */
> +	if (!test_bit(UPROBE_COPY_INSN, &uprobe->flags))
> +		return 0;
> +
> +	/*
> +	 * Pairs with the smp_wmb() in prepare_uprobe().
> +	 *
> +	 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
> +	 * we must also see the stores to &uprobe->arch before
> +	 * set_orig_insn() reads them.
> +	 */
> +	smp_rmb();
> +
>  	mm_flags_set(MMF_RECALC_UPROBES, mm);
>  	return set_orig_insn(&uprobe->arch, vma, vaddr);
>  }

[Severity: Medium]
Can returning early here leave MMF_HAS_UPROBES permanently set on mm?

Suppose a process has two uprobes on the same mapped file: U1 is installed
(setting MMF_HAS_UPROBES), and U2 has a consumer filter that rejects this
process so U2 sits in uprobes_tree with UPROBE_COPY_INSN unset.

When U1 is unregistered, remove_breakpoint() sets MMF_RECALC_UPROBES.  If
the process then hits a non-uprobe trap, find_active_uprobe_rcu() clears
MMF_RECALC_UPROBES and calls mmf_recalc_uprobes(), which finds U2 via
vma_has_uprobes() and leaves MMF_HAS_UPROBES set.

When U2 is later unregistered, remove_breakpoint() now returns 0 without
setting MMF_RECALC_UPROBES.  Once U2 is gone from uprobes_tree, even
uprobe_munmap() will not set it, leaving MMF_HAS_UPROBES stuck so later
non-uprobe traps keep taking the find_active_uprobe_rcu() slow path.

Calling mm_flags_set(MMF_RECALC_UPROBES, mm) before checking
UPROBE_COPY_INSN would avoid this regression while still skipping
set_orig_insn().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179629192497.2709886.2379669650835098449.stgit@mhiramat.c.googlers.com?part=2

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal
  2026-12-03  9:58 [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
  2026-12-03  9:58 ` [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
  2026-12-03  9:59 ` [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
@ 2026-12-03 10:17 ` sashiko-bot
  2 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 10:17 UTC (permalink / raw)
  To: Masami Hiramatsu (Google); +Cc: linux-perf-users

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("uprobes: Skip remove_breakpoint() if UPROBE_COPY_INS..."): 1
  potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179629192497.2709886.2379669650835098449.stgit@mhiramat.c.googlers.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe()
  2026-12-03  9:58 ` [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
@ 2026-12-03 11:19   ` Oleg Nesterov
  0 siblings, 0 replies; 7+ messages in thread
From: Oleg Nesterov @ 2026-12-03 11:19 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-kernel,
	linux-trace-kernel, linux-perf-users

On 12/03, Masami Hiramatsu (Google) wrote:
>
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -1101,8 +1101,17 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
>  {
>  	int ret = 0;
>  
> -	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
> +	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) {
> +		/*
> +		 * Pairs with the smp_wmb() below.
> +		 *
> +		 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
> +		 * we must also see the stores to &uprobe->arch performed by
> +		 * prepare_uprobe().
> +		 */
> +		smp_rmb();
>  		return ret;
> +	}
>  
>  	/* TODO: move this into _register, until then we abuse this sem. */
>  	down_write(&uprobe->consumer_rwsem);
> @@ -1121,7 +1130,8 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
>  	if (ret)
>  		goto out;
>  
> -	smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
> +	/* Pairs with the smp_rmb() in prepare_uprobe() and handle_swbp() */
> +	smp_wmb();
>  	set_bit(UPROBE_COPY_INSN, &uprobe->flags);

Acked-by: Oleg Nesterov <oleg@redhat.com>

But I guess we could use test_bit_acquire(UPROBE_COPY_INSN) and
smp_mb__before_atomic() before set_bit(UPROBE_COPY_INSN) ?

Oleg.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set
  2026-12-03  9:59 ` [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
  2026-12-03 10:17   ` sashiko-bot
@ 2026-12-03 11:58   ` Oleg Nesterov
  1 sibling, 0 replies; 7+ messages in thread
From: Oleg Nesterov @ 2026-12-03 11:58 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, James Clark, linux-kernel,
	linux-trace-kernel, linux-perf-users

On 12/03, Masami Hiramatsu (Google) wrote:
>
> @@ -1194,6 +1197,19 @@ static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma,
>  {
>  	struct mm_struct *mm = vma->vm_mm;
>  
> +	/* No breakpoint was ever installed if prepare_uprobe() never succeeded */
> +	if (!test_bit(UPROBE_COPY_INSN, &uprobe->flags))
> +		return 0;
> +
> +	/*
> +	 * Pairs with the smp_wmb() in prepare_uprobe().
> +	 *
> +	 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
> +	 * we must also see the stores to &uprobe->arch before
> +	 * set_orig_insn() reads them.
> +	 */
> +	smp_rmb();

Acked-by: Oleg Nesterov <oleg@redhat.com>

But again, it can use test_bit_acquire() unless I am confused.

And perhaps it would be better to change uprobe_unregister_nosync()
to check UPROBE_COPY_INSN once?

In any case UPROBE_COPY_INSN must die ;) see the TODO comment in
prepare_uprobe().

Oleg.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-03 11:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  9:58 [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
2026-12-03  9:58 ` [PATCH 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
2026-12-03 11:19   ` Oleg Nesterov
2026-12-03  9:59 ` [PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
2026-12-03 10:17   ` sashiko-bot
2026-12-03 11:58   ` Oleg Nesterov
2026-12-03 10:17 ` [PATCH 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox