Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal
@ 2026-12-04  6:01 Masami Hiramatsu (Google)
  2026-12-04  6:01 ` [PATCH v2 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04  6:01 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 is v2 of the series fixing two bugs around UPROBE_COPY_INSN handling
in kernel/events/uprobes.c (both reported by Sashiko).

The previous version is here:
 https://lore.kernel.org/all/179629192497.2709886.2379669650835098449.stgit@mhiramat.c.googlers.com/

Changes in v2:
 - [1/2] Switch from test_bit() + smp_rmb() / smp_wmb() to
   test_bit_acquire() paired with smp_mb__before_atomic() + set_bit()
   in prepare_uprobe() and handle_swbp(), as suggested by Oleg.
 - [2/2] Keep mm_flags_set(MMF_RECALC_UPROBES, mm) before checking
   UPROBE_COPY_INSN so filtered-out uprobes do not leave MMF_HAS_UPROBES
   stuck on mm (reported by Sashiko AI review), and use
   test_bit_acquire() as suggested by Oleg.

1. prepare_uprobe() uses double-checked locking around UPROBE_COPY_INSN,
   with a memory barrier before set_bit(UPROBE_COPY_INSN, &uprobe->flags),
   but the lockless fast path returns 0 without a paired acquire barrier.
   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 uses test_bit_acquire()
   paired with smp_mb__before_atomic() + set_bit() in prepare_uprobe()
   and converts handle_swbp() for consistency.

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, unregistration, or
   uprobe_apply(..., false) can overwrite an existing third-party
   software breakpoint (such as GDB) with zeroes. Patch 2 sets
   MMF_RECALC_UPROBES first and then returns early if
   test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags) is false.

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 | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

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

* [PATCH v2 1/2] uprobes: Add missing read memory barrier in prepare_uprobe()
  2026-12-04  6:01 [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
@ 2026-12-04  6:01 ` Masami Hiramatsu (Google)
  2026-12-04  6:02 ` [PATCH v2 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
  2026-12-04  6:17 ` [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04  6:01 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, issues a memory
barrier, 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/acquire 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()).

Use test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags) on the fast path
of prepare_uprobe() and switch the writer side from smp_wmb() to
smp_mb__before_atomic() before set_bit(UPROBE_COPY_INSN, &uprobe->flags)
to form a standard release/acquire pair. Also convert handle_swbp() from
test_bit() + smp_rmb() to test_bit_acquire() for consistency.

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>
Acked-by: Oleg Nesterov <oleg@redhat.com>
---
Changes in v2:
 - Use test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags) on the fast
   path of prepare_uprobe() and smp_mb__before_atomic() before
   set_bit(UPROBE_COPY_INSN, &uprobe->flags), as suggested by Oleg.
 - Convert handle_swbp() from test_bit() + smp_rmb() to
   test_bit_acquire() for consistency.
---
 kernel/events/uprobes.c |   24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index c47b1ee266d5..2c04fa96fcd0 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1101,7 +1101,11 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 {
 	int ret = 0;
 
-	if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
+	/*
+	 * Pairs with smp_mb__before_atomic() + set_bit(UPROBE_COPY_INSN, ...)
+	 * below so stores to &uprobe->arch are visible to callers.
+	 */
+	if (test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags))
 		return ret;
 
 	/* TODO: move this into _register, until then we abuse this sem. */
@@ -1121,7 +1125,11 @@ 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 test_bit_acquire(UPROBE_COPY_INSN, ...) in
+	 * prepare_uprobe() and handle_swbp().
+	 */
+	smp_mb__before_atomic();
 	set_bit(UPROBE_COPY_INSN, &uprobe->flags);
 
  out:
@@ -2754,18 +2762,14 @@ static void handle_swbp(struct pt_regs *regs)
 	 * TODO: move copy_insn/etc into _register and remove this hack.
 	 * After we hit the bp, _unregister + _register can install the
 	 * new and not-yet-analyzed uprobe at the same address, restart.
-	 */
-	if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
-		goto out;
-
-	/*
-	 * Pairs with the smp_wmb() in prepare_uprobe().
 	 *
-	 * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
+	 * Pairs with smp_mb__before_atomic() + set_bit(UPROBE_COPY_INSN, ...)
+	 * in prepare_uprobe(). Guarantees that if we see UPROBE_COPY_INSN set,
 	 * we must also see the stores to &uprobe->arch performed by the
 	 * prepare_uprobe() call.
 	 */
-	smp_rmb();
+	if (unlikely(!test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags)))
+		goto out;
 
 	/* Tracing handlers use ->utask to communicate with fetch methods */
 	if (!get_utask())


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

* [PATCH v2 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set
  2026-12-04  6:01 [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
  2026-12-04  6:01 ` [PATCH v2 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
@ 2026-12-04  6:02 ` Masami Hiramatsu (Google)
  2026-12-04  6:17 ` [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-12-04  6:02 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 or consumers were filtered
out), UPROBE_COPY_INSN stays unset and uprobe->arch.insn remains
zero-initialized or unvalidated. During uprobe_register() error cleanup,
unregistration, or uprobe_apply(..., false), 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 with test_bit_acquire() in remove_breakpoint()
and return 0 early if it is not set, so stores to uprobe->arch from
prepare_uprobe() are visible before set_orig_insn() reads them. Keep
mm_flags_set(MMF_RECALC_UPROBES, mm) before the early return because a
filtered-out uprobe in uprobes_tree can keep MMF_HAS_UPROBES set across
an earlier mmf_recalc_uprobes(), and still needs to trigger
recalculation when unregistered. Keep the check in remove_breakpoint()
under mmap_write_lock(mm) so it serializes against concurrent
uprobe_mmap() and covers uprobe_apply(..., false).

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>
Acked-by: Oleg Nesterov <oleg@redhat.com>
---
Changes in v2:
 - Keep mm_flags_set(MMF_RECALC_UPROBES, mm) before checking
   UPROBE_COPY_INSN so filtered-out uprobes do not leave
   MMF_HAS_UPROBES stuck on mm (reported by Sashiko AI review).
 - Switch from test_bit() + smp_rmb() to test_bit_acquire(), as
   suggested by Oleg.
---
 kernel/events/uprobes.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 2c04fa96fcd0..afbd21a8bc39 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1127,7 +1127,7 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
 
 	/*
 	 * Pairs with test_bit_acquire(UPROBE_COPY_INSN, ...) in
-	 * prepare_uprobe() and handle_swbp().
+	 * prepare_uprobe(), remove_breakpoint(), and handle_swbp().
 	 */
 	smp_mb__before_atomic();
 	set_bit(UPROBE_COPY_INSN, &uprobe->flags);
@@ -1193,6 +1193,16 @@ static int remove_breakpoint(struct uprobe *uprobe, struct vm_area_struct *vma,
 	struct mm_struct *mm = vma->vm_mm;
 
 	mm_flags_set(MMF_RECALC_UPROBES, mm);
+
+	/*
+	 * No breakpoint was ever installed if prepare_uprobe() never succeeded.
+	 * Pairs with smp_mb__before_atomic() + set_bit(UPROBE_COPY_INSN, ...)
+	 * in prepare_uprobe() so stores to &uprobe->arch are visible before
+	 * set_orig_insn() reads them.
+	 */
+	if (!test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags))
+		return 0;
+
 	return set_orig_insn(&uprobe->arch, vma, vaddr);
 }
 


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

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

Sashiko has reviewed this 2-patch series and found no issues.

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

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

end of thread, other threads:[~2026-12-04  6:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  6:01 [PATCH v2 0/2] uprobes: Fix UPROBE_COPY_INSN ordering and breakpoint removal Masami Hiramatsu (Google)
2026-12-04  6:01 ` [PATCH v2 1/2] uprobes: Add missing read memory barrier in prepare_uprobe() Masami Hiramatsu (Google)
2026-12-04  6:02 ` [PATCH v2 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set Masami Hiramatsu (Google)
2026-12-04  6:17 ` [PATCH v2 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