[PATCH 2/2] uprobes: Skip remove_breakpoint() if UPROBE_COPY_INSN is not set
From: Masami Hiramatsu (Google)
Date: Thu Dec 03 2026 - 04:59:13 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), 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@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>
---
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);
}