[PATCH v2 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set

From: Masami Hiramatsu (Google)

Date: Fri Dec 04 2026 - 01:02:14 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

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@xxxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Acked-by: Oleg Nesterov <oleg@xxxxxxxxxx>
---
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);
}