Skip to contentSkip to conversation

powerpc/uprobes: Don't allow probe on suffix of prefixed instruction

PATCH In review Ravi Bangoria 2 participants 3 messages Jan 19, 2021 – Jan 20, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/uprobes: Don't allow probe on suffix of prefixed instruction

Probe on 2nd word of a prefixed instruction is invalid scenario and should be restricted.

There are two ways probed instruction is changed in mapped pages. First, when Uprobe is activated, it searches for all the relevant pages and replace instruction in them. In this case, if we notice that probe is on the 2nd word of prefixed instruction, error out directly. Second, when Uprobe is already active and user maps a relevant page via mmap(), instruction is replaced via mmap() code path. But because Uprobe is invalid, entire mmap() operation can not be stopped. In this case just print an error and continue.

---
 arch/powerpc/kernel/uprobes.c | 28 ++++++++++++++++++++++++++++
 include/linux/uprobes.h       |  1 +
 kernel/events/uprobes.c       |  8 ++++++++
 3 files changed, 37 insertions(+)
arch/powerpc/kernel/uprobes.c +28 -0
index e8a63713e655..c73d5a397164 100644
@@ -7,6 +7,7 @@
  * Adapted from the x86 port by Ananth N Mavinakayanahalli <[email protected]>
  */
 #include <linux/kernel.h>
+#include <linux/highmem.h>
 #include <linux/sched.h>
 #include <linux/ptrace.h>
 #include <linux/uprobes.h>
@@ -44,6 +45,33 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 	return 0;
 }
 
+#ifdef CONFIG_PPC64
+int arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+			      uprobe_opcode_t opcode)
+{
+	uprobe_opcode_t prefix;
+	void *kaddr;
+	struct ppc_inst inst;
+
+	/* Don't check if vaddr is pointing to the beginning of page */
+	if (!(vaddr & ~PAGE_MASK))
+		return 0;
+
+	kaddr = kmap_atomic(page);
+	memcpy(&prefix, kaddr + ((vaddr - 4) & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
+	kunmap_atomic(kaddr);
+
+	inst = ppc_inst_prefix(prefix, opcode);
+
+	if (ppc_inst_prefixed(inst)) {
+		printk_ratelimited("Cannot register a uprobe on the second "
+				   "word of prefixed instruction\n");
+		return -1;
+	}
+	return 0;
+}
+#endif
+
 /*
  * arch_uprobe_pre_xol - prepare to execute out of line.
  * @auprobe: the probepoint information.
include/linux/uprobes.h +1 -0
index f46e0ca0169c..5a3b45878e13 100644
@@ -128,6 +128,7 @@ extern bool uprobe_deny_signal(void);
 extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
 extern void uprobe_clear_state(struct mm_struct *mm);
 extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
+int arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t opcode);
 extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
kernel/events/uprobes.c +8 -0
index bf9edd8d75be..be02e6c26e3f 100644
@@ -255,6 +255,12 @@ static void copy_to_page(struct page *page, unsigned long vaddr, const void *src
 	kunmap_atomic(kaddr);
 }
 
+int __weak arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+				     uprobe_opcode_t opcode)
+{
+	return 0;
+}
+
 static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
 {
 	uprobe_opcode_t old_opcode;
@@ -275,6 +281,8 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
 	if (is_swbp_insn(new_opcode)) {
 		if (is_swbp)		/* register: already installed? */
 			return 0;
+		if (arch_uprobe_verify_opcode(page, vaddr, old_opcode))
+			return -EINVAL;
 	} else {
 		if (!is_swbp)		/* unregister: was it changed by us? */
 			return 0;
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 3 lines · Ravi Bangoria

Probe on 2nd word of a prefixed instruction is invalid scenario and should be restricted.

I don't understand this ppc-specific problem, but...

arch/powerpc/kernel/uprobes.c int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, · line 58

Quoted 11 lines
arch/powerpc/kernel/uprobes.c +11 -0
+#ifdef CONFIG_PPC64
+int arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+			      uprobe_opcode_t opcode)
+{
+	uprobe_opcode_t prefix;
+	void *kaddr;
+	struct ppc_inst inst;
+
+	/* Don't check if vaddr is pointing to the beginning of page */
+	if (!(vaddr & ~PAGE_MASK))
+		return 0;

So the fix is incomplete? Or insn at the start of page can't be prefixed?

Quoted 10 lines
kernel/events/uprobes.c +6 -0
+int __weak arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+				     uprobe_opcode_t opcode)
+{
+	return 0;
+}
+
 static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
 {
 	uprobe_opcode_t old_opcode;

kernel/events/uprobes.c static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t · line 285

Quoted 5 lines
kernel/events/uprobes.c +2 -0
@@ -275,6 +281,8 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
 	if (is_swbp_insn(new_opcode)) {
 		if (is_swbp)		/* register: already installed? */
 			return 0;
+		if (arch_uprobe_verify_opcode(page, vaddr, old_opcode))
+			return -EINVAL;

Well, this doesn't look good...

To me it would be better to change the prepare_uprobe() path to copy the potential prefix into uprobe->arch and check ppc_inst_prefixed() in arch_uprobe_analyze_insn(). What do you think?

Oleg.

#2 lore raw
Quoted 6 lines · Oleg Nesterov
Quoted 3 lines · Ravi Bangoria

Probe on 2nd word of a prefixed instruction is invalid scenario and should be restricted.

I don't understand this ppc-specific problem, but...

So far (upto Power9), instruction size was fixed - 4 bytes. But Power10 introduced a prefixed instruction which consist of 8 bytes, where first 4 bytes is prefix and remaining is suffix.

This patch checks whether the Uprobe is on the 2nd word (suffix) of a prefixed instruction. If so, consider it as invalid Uprobe.

Quoted 14 lines

arch/powerpc/kernel/uprobes.c int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, · line 58

Quoted 12 lines
arch/powerpc/kernel/uprobes.c +11 -0
+#ifdef CONFIG_PPC64
+int arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+			      uprobe_opcode_t opcode)
+{
+	uprobe_opcode_t prefix;
+	void *kaddr;
+	struct ppc_inst inst;
+
+	/* Don't check if vaddr is pointing to the beginning of page */
+	if (!(vaddr & ~PAGE_MASK))
+		return 0;

So the fix is incomplete? Or insn at the start of page can't be prefixed?

Prefixed instruction can not cross 64 byte boundary. If it does, kernel generates SIGBUS. Considering all powerpc supported page sizes to be multiple of 64 bytes, there will never be a scenario where prefix and suffix will be on different pages. i.e. a beginning of the page should never be a suffix.

Quoted 19 lines
Quoted 11 lines
kernel/events/uprobes.c +6 -0
+int __weak arch_uprobe_verify_opcode(struct page *page, unsigned long vaddr,
+				     uprobe_opcode_t opcode)
+{
+	return 0;
+}
+
  static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  {
  	uprobe_opcode_t old_opcode;

kernel/events/uprobes.c static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t · line 285

Quoted 5 lines
kernel/events/uprobes.c +2 -0
@@ -275,6 +281,8 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
  	if (is_swbp_insn(new_opcode)) {
  		if (is_swbp)		/* register: already installed? */
  			return 0;
+		if (arch_uprobe_verify_opcode(page, vaddr, old_opcode))
+			return -EINVAL;

Well, this doesn't look good...

Quoted 3 lines

To me it would be better to change the prepare_uprobe() path to copy the potential prefix into uprobe->arch and check ppc_inst_prefixed() in arch_uprobe_analyze_insn(). What do you think?

Agreed. The only reason I was checking via verify_opcode() is to make the code more simpler. If I need to check via prepare_uprobe(), I'll need to abuse uprobe->offset by setting it to uprobe->offset - 4 to read previous 4 bytes of current instruction. Which, IMHO, is not that straightforward with current implementation of prepare_uprobe().

But while replying here, I'm thinking... I should be able to grab a page using mm and vaddr, which are already available in arch_uprobe_analyze_insn(). With that, I should be able to do all this inside arch_uprobe_analyze_insn() only. I'll try this and send v2 if that works.

Thanks for the review.
Ravi