Skip to contentSkip to conversation

Blacklist powerpc exception vectors from kprobes

RFC PATCH In review Naveen N. Rao 3 participants 13 messages Nov 17, 2016 – Nov 18, 2016 PowerPC

lore
Share & tools
lore
#0 lore raw

cover Blacklist powerpc exception vectors from kprobes

This is the beginning of work to come up with a more relevant kprobe blacklist on powerpc. In this series, we primarily blacklist exception vectors and kernel entry code.

Naveen N. Rao (4):
powerpc: asm: introduce new macros for assembly globals
powerpc: kprobe: add arch specific blacklist
powerpc: mm/slb: convert slb_low.S to use the new macros
powerpc: mm/slb: blacklist symbols from kprobe

 arch/powerpc/include/asm/ppc_asm.h | 19 +++++++++++++++++--
 arch/powerpc/kernel/entry_32.S     |  2 ++
 arch/powerpc/kernel/entry_64.S     |  2 ++
 arch/powerpc/kernel/kprobes.c      | 10 ++++++++++
 arch/powerpc/mm/slb_low.S          | 30 +++++++++++++-----------------
 5 files changed, 44 insertions(+), 19 deletions(-)
Signature · 1 line
2.10.2
#1 lore raw

RFC PATCH 1/4 powerpc: asm: introduce new macros for assembly globals

- Introduce _GLOBAL_SYM() for global symbols in assembly. This helps reduce verbosity of assembly files.
- Introduce NOKPROBE variants of _GLOBAL() and _GLOBAL_SYM(). These are used subsequently to blacklist certain assembly functions and symbols from kprobe.
- Fix a small typo in kprobe comment and re-format it, to make it clearer.

---
 arch/powerpc/include/asm/ppc_asm.h | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/ppc_asm.h +17 -2
index 025833b..2443545 100644
@@ -254,13 +254,19 @@ GLUE(.,name):
 
 #endif
 
+#define _GLOBAL_SYM(name)	\
+	.globl name;		\
+name:
+
 /*
  * __kprobes (the C annotation) puts the symbol into the .kprobes.text
  * section, which gets emitted at the end of regular text.
  *
  * _ASM_NOKPROBE_SYMBOL and NOKPROBE_SYMBOL just adds the symbol to
- * a blacklist. The former is for core kprobe functions/data, the
- * latter is for those that incdentially must be excluded from probing
+ * a blacklist.
+ *
+ * The former (__kprobes) is for core kprobe functions/data, the
+ * latter is for those that incidentally must be excluded from probing
  * and allows them to be linked at more optimal location within text.
  */
 #ifdef CONFIG_KPROBES
@@ -272,6 +278,15 @@ GLUE(.,name):
 #define _ASM_NOKPROBE_SYMBOL(entry)
 #endif
 
+#define _GLOBAL_NOKPROBE(name)				\
+	_GLOBAL(name);					\
+	_ASM_NOKPROBE_SYMBOL(name)
+
+#define _GLOBAL_SYM_NOKPROBE(name)			\
+	_GLOBAL_SYM(name);				\
+	_ASM_NOKPROBE_SYMBOL(name)
+
+
 #define FUNC_START(name)	_GLOBAL(name)
 #define FUNC_END(name)
 
Signature · 1 line
2.10.2
#2 lore raw

Re: [RFC PATCH 1/4] powerpc: asm: introduce new macros for assembly globals

Quoted 2 lines · Naveen N. Rao

- Introduce _GLOBAL_SYM() for global symbols in assembly. This helps reduce verbosity of assembly files.

Unfortunately you've walked into a bit of mine field here :)

In user space they use FUNC_START() to declare the start of a function, and we should do the same. Anton added FUNC_START/END, but didn't quite get around to converting everything, see 151f25112ff7 ("powerpc: define FUNC_START/FUNC_END").

So what I'd like is all uses of _GLOBAL() to become FUNC_START(), and then we can change _GLOBAL() to just define a global symbol.

We can probably decouple that from most of this series though, as I mentioned in my other reply, just by using _ASM_NOKPROBE_SYMBOL().

cheers

#3 lore raw

Re: [RFC PATCH 1/4] powerpc: asm: introduce new macros for assembly globals

Quoted 7 lines · Michael Ellerman
Quoted 2 lines · Naveen N. Rao

- Introduce _GLOBAL_SYM() for global symbols in assembly. This helps reduce verbosity of assembly files.

Unfortunately you've walked into a bit of mine field here :)

Quoted 7 lines

In user space they use FUNC_START() to declare the start of a function, and we should do the same. Anton added FUNC_START/END, but didn't quite get around to converting everything, see 151f25112ff7 ("powerpc: define FUNC_START/FUNC_END").

So what I'd like is all uses of _GLOBAL() to become FUNC_START(), and then we can change _GLOBAL() to just define a global symbol.

Can't say I didn't get tempted to rename _GLOBAL() to _GLOBAL_FUNC() :D I'll convert the files I touch.

Quoted 3 lines

We can probably decouple that from most of this series though, as I mentioned in my other reply, just by using _ASM_NOKPROBE_SYMBOL().

Sure.

Thanks!
- Naveen

#4 lore raw

RFC PATCH 2/4 powerpc: kprobe: add arch specific blacklist

In reply to Naveen N. Rao

Add symbol to mark end of entry_*.S and use the same to blacklist all addresses from kernel start (_stext) to entry code from kprobes. Much of this code is early exception handling where we can't really take a trap.

---
 arch/powerpc/kernel/entry_32.S |  2 ++
 arch/powerpc/kernel/entry_64.S |  2 ++
 arch/powerpc/kernel/kprobes.c  | 10 ++++++++++
 3 files changed, 14 insertions(+)
arch/powerpc/kernel/entry_32.S +2 -0
index 3841d74..de1ed6e 100644
@@ -1410,3 +1410,5 @@ _GLOBAL(return_to_handler)
 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 
 #endif /* CONFIG_FUNCTION_TRACER */
+
+_GLOBAL_SYM(__entry_text_end)
arch/powerpc/kernel/entry_64.S +2 -0
index 6432d4b..f5f99920 100644
@@ -1551,3 +1551,5 @@ _GLOBAL(return_to_handler)
 	blr
 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 #endif /* CONFIG_FUNCTION_TRACER */
+
+_GLOBAL_SYM(__entry_text_end)
arch/powerpc/kernel/kprobes.c +10 -0
index 9479d8e..b5173d6 100644
@@ -36,12 +36,22 @@
 #include <asm/cacheflush.h>
 #include <asm/sstep.h>
 #include <asm/uaccess.h>
+#include <asm/sections.h>
 
 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 
 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
 
+bool arch_within_kprobe_blacklist(unsigned long addr)
+{
+	/* The __kprobes marked functions and entry code must not be probed */
+	return (addr >= (unsigned long)__kprobes_text_start &&
+	        addr < (unsigned long)__kprobes_text_end) ||
+	       (addr >= (unsigned long)_stext &&
+		addr < (unsigned long)__entry_text_end);
+}
+
 int __kprobes arch_prepare_kprobe(struct kprobe *p)
 {
 	int ret = 0;
Signature · 1 line
2.10.2
#5 lore raw

Re: [RFC PATCH 2/4] powerpc: kprobe: add arch specific blacklist

Quoted 3 lines · Naveen N. Rao

Add symbol to mark end of entry_*.S and use the same to blacklist all addresses from kernel start (_stext) to entry code from kprobes. Much of this code is early exception handling where we can't really take a trap.

I'm not sure about this. entry_*.S is actually a bit of jumble, especially the 64bit version. I've been wanting to split it up for a long time.

It doesn't actually contain any early exception handling. It does contain the common syscall handler, and the exception return paths, some of which should be black listed. And lots of other junk.

Also I'm not sure if it's guaranteed that there won't be other code between _stext and the end of entry, it's not handled explicitly in the linker script, it just tends to get linked early because it's in head-y.

So I think it would be better if we had a clearer picture of exactly what in this file we want to blacklist.

cheers

#6 lore raw

Re: [RFC PATCH 2/4] powerpc: kprobe: add arch specific blacklist

Quoted 14 lines · On Fri, 18 Nov 2016 16:48:01 +1100 Michael Ellerman <[email protected]> wrote:
Quoted 3 lines · Naveen N. Rao

Add symbol to mark end of entry_*.S and use the same to blacklist all addresses from kernel start (_stext) to entry code from kprobes. Much of this code is early exception handling where we can't really take a trap.

I'm not sure about this. entry_*.S is actually a bit of jumble, especially the 64bit version. I've been wanting to split it up for a long time.

It doesn't actually contain any early exception handling. It does contain the common syscall handler, and the exception return paths, some of which should be black listed. And lots of other junk.

Quoted 6 lines

Also I'm not sure if it's guaranteed that there won't be other code between _stext and the end of entry, it's not handled explicitly in the linker script, it just tends to get linked early because it's in head-y.

So I think it would be better if we had a clearer picture of exactly what in this file we want to blacklist.

Fair enough.

OK, the purpose of the kprobe blacklist is to avoid crashing kernel by putting kprobes in some critical area (critical for kprobes, not what usually "critical region" means).

Since kprobes is using breakpoint(trap) exception, if there is another kprobes(trap) on the path until kprobe_handler() handle it, the kernel kicks same exception handler and fall into the recursive fault. So the blacklist is used in kprobe to prohibit putting kprobes on such functions for avoiding it.

So, we might be carefully choose the function for the blacklist.

BTW, Naveen, as far as I can see the kprobe implementation on ppc, it still depends on exceptions_notify to handle trap. It is no more recommended becuase notifier_call_chain involves too many unrelated functions. I recommend you to callback kprobe_handler and kprobe_post_handler directly from the trap handler as same as x86.

Unless that, kprobe_blacklist may not work.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#7 lore raw

Re: [RFC PATCH 2/4] powerpc: kprobe: add arch specific blacklist

Hi Masami,

Quoted 30 lines · Masami Hiramatsu
Quoted 14 lines · On Fri, 18 Nov 2016 16:48:01 +1100 Michael Ellerman <[email protected]> wrote:
Quoted 3 lines · Naveen N. Rao

Add symbol to mark end of entry_*.S and use the same to blacklist all addresses from kernel start (_stext) to entry code from kprobes. Much of this code is early exception handling where we can't really take a trap.

I'm not sure about this. entry_*.S is actually a bit of jumble, especially the 64bit version. I've been wanting to split it up for a long time.

It doesn't actually contain any early exception handling. It does contain the common syscall handler, and the exception return paths, some of which should be black listed. And lots of other junk.

Quoted 6 lines

Also I'm not sure if it's guaranteed that there won't be other code between _stext and the end of entry, it's not handled explicitly in the linker script, it just tends to get linked early because it's in head-y.

So I think it would be better if we had a clearer picture of exactly what in this file we want to blacklist.

Fair enough.

OK, the purpose of the kprobe blacklist is to avoid crashing kernel by putting kprobes in some critical area (critical for kprobes, not what usually "critical region" means).

Quoted 7 lines

Since kprobes is using breakpoint(trap) exception, if there is another kprobes(trap) on the path until kprobe_handler() handle it, the kernel kicks same exception handler and fall into the recursive fault. So the blacklist is used in kprobe to prohibit putting kprobes on such functions for avoiding it.

So, we might be carefully choose the function for the blacklist.

Agreed, though in this case, I'm trying to blacklist early exception code to begin with :)

Quoted 6 lines

BTW, Naveen, as far as I can see the kprobe implementation on ppc, it still depends on exceptions_notify to handle trap. It is no more recommended becuase notifier_call_chain involves too many unrelated functions. I recommend you to callback kprobe_handler and kprobe_post_handler directly from the trap handler as same as x86.

Ah, thanks for the tip. Patch to follow.

- Naveen

#8 lore raw

Re: [RFC PATCH 2/4] powerpc: kprobe: add arch specific blacklist

In reply to Michael Ellerman
Quoted 9 lines · Michael Ellerman
Quoted 3 lines · Naveen N. Rao

Add symbol to mark end of entry_*.S and use the same to blacklist all addresses from kernel start (_stext) to entry code from kprobes. Much of this code is early exception handling where we can't really take a trap.

I'm not sure about this. entry_*.S is actually a bit of jumble, especially the 64bit version. I've been wanting to split it up for a long time.

Ok. Let me take a stab at that.

Quoted 8 lines

It doesn't actually contain any early exception handling. It does contain the common syscall handler, and the exception return paths, some of which should be black listed. And lots of other junk.

Also I'm not sure if it's guaranteed that there won't be other code between _stext and the end of entry, it's not handled explicitly in the linker script, it just tends to get linked early because it's in head-y.

I actually considered that. One of the issues in trying to get entry_* linked in early has to do with the exception common handlers - they start at 0x7000 or 0x8000 and are placed in .text *and* I think they need to be within 64k from the exception vectors. As such, placing entry_* in a separate section and linking it after HEAD_TEXT resulted in moving down the common exception handlers.

Regardless of the kprobe blacklist, does it make sense to put the common exception handlers into a separate section so as to separate them out from the rest of the code?

Quoted 3 lines

So I think it would be better if we had a clearer picture of exactly what in this file we want to blacklist.

Agreed. As a first step, I wanted to get a coarser blacklist in place and fine tune it later. But, I can see why entry_* needs a smaller blacklist. I'll get back on this.

- Naveen

#9 lore raw

RFC PATCH 3/4 powerpc: mm/slb: convert slb_low.S to use the new macros

In reply to Naveen N. Rao

Also convert slb_finish_load[_1T] to a local symbol as this doesn't need to be globally visible.

---
 arch/powerpc/mm/slb_low.S | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
arch/powerpc/mm/slb_low.S +12 -16
index e2974fc..c1c7456 100644
@@ -59,8 +59,7 @@ _GLOBAL(slb_allocate_realmode)
 	/* Linear mapping encoding bits, the "li" instruction below will
 	 * be patched by the kernel at boot
 	 */
-.globl slb_miss_kernel_load_linear
-slb_miss_kernel_load_linear:
+_GLOBAL_SYM(slb_miss_kernel_load_linear)
 	li	r11,0
 	/*
 	 * context = (MAX_USER_CONTEXT) + ((ea >> 60) - 0xc) + 1
@@ -71,17 +70,16 @@ slb_miss_kernel_load_linear:
 
 
 BEGIN_FTR_SECTION
-	b	slb_finish_load
+	b	.Lslb_finish_load
 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
-	b	slb_finish_load_1T
+	b	.Lslb_finish_load_1T
 
 1:
 #ifdef CONFIG_SPARSEMEM_VMEMMAP
 	/* Check virtual memmap region. To be patches at kernel boot */
 	cmpldi	cr0,r9,0xf
 	bne	1f
-.globl slb_miss_kernel_load_vmemmap
-slb_miss_kernel_load_vmemmap:
+_GLOBAL_SYM(slb_miss_kernel_load_vmemmap)
 	li	r11,0
 	b	6f
 1:
@@ -97,8 +95,7 @@ slb_miss_kernel_load_vmemmap:
 	b	6f
 5:
 	/* IO mapping */
-.globl slb_miss_kernel_load_io
-slb_miss_kernel_load_io:
+_GLOBAL_SYM(slb_miss_kernel_load_io)
 	li	r11,0
 6:
 	/*
@@ -109,9 +106,9 @@ slb_miss_kernel_load_io:
 	addi	r9,r9,(MAX_USER_CONTEXT - 0xc + 1)@l
 
 BEGIN_FTR_SECTION
-	b	slb_finish_load
+	b	.Lslb_finish_load
 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
-	b	slb_finish_load_1T
+	b	.Lslb_finish_load_1T
 
 0:	/*
 	 * For userspace addresses, make sure this is region 0.
@@ -174,9 +171,9 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
 	ld	r9,PACACONTEXTID(r13)
 BEGIN_FTR_SECTION
 	cmpldi	r10,0x1000
-	bge	slb_finish_load_1T
+	bge	.Lslb_finish_load_1T
 END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
-	b	slb_finish_load
+	b	.Lslb_finish_load
 
 8:	/* invalid EA - return an error indication */
 	crset	4*cr0+eq		/* indicate failure */
@@ -187,7 +184,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  *
  * r3 = EA, r9 = context, r10 = ESID, r11 = flags, clobbers r9, cr7 = <> PAGE_OFFSET
  */
-slb_finish_load:
+.Lslb_finish_load:
 	rldimi  r10,r9,ESID_BITS,0
 	ASM_VSID_SCRAMBLE(r10,r9,256M)
 	/*
@@ -206,8 +203,7 @@ slb_finish_load:
 7:	ld	r10,PACASTABRR(r13)
 	addi	r10,r10,1
 	/* This gets soft patched on boot. */
-.globl slb_compare_rr_to_size
-slb_compare_rr_to_size:
+_GLOBAL_SYM(slb_compare_rr_to_size)
 	cmpldi	r10,0
 
 	blt+	4f
@@ -256,7 +252,7 @@ slb_compare_rr_to_size:
  *
  * r3 = EA, r9 = context, r10 = ESID(256MB), r11 = flags, clobbers r9
  */
-slb_finish_load_1T:
+.Lslb_finish_load_1T:
 	srdi	r10,r10,(SID_SHIFT_1T - SID_SHIFT)	/* get 1T ESID */
 	rldimi  r10,r9,ESID_BITS_1T,0
 	ASM_VSID_SCRAMBLE(r10,r9,1T)
Signature · 1 line
2.10.2
#10 lore raw

RFC PATCH 4/4 powerpc: mm/slb: blacklist symbols from kprobe

In reply to Naveen N. Rao

We can't really take a trap at this point. So, blacklist these symbols.

---
 arch/powerpc/mm/slb_low.S | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/powerpc/mm/slb_low.S +5 -5
index c1c7456..c2bae92 100644
@@ -30,7 +30,7 @@
  *	r9, r10, r11 are clobbered by this function
  * No other registers are examined or changed.
  */
-_GLOBAL(slb_allocate_realmode)
+_GLOBAL_NOKPROBE(slb_allocate_realmode)
 	/*
 	 * check for bad kernel/user address
 	 * (ea & ~REGION_MASK) >= PGTABLE_RANGE
@@ -59,7 +59,7 @@ _GLOBAL(slb_allocate_realmode)
 	/* Linear mapping encoding bits, the "li" instruction below will
 	 * be patched by the kernel at boot
 	 */
-_GLOBAL_SYM(slb_miss_kernel_load_linear)
+_GLOBAL_SYM_NOKPROBE(slb_miss_kernel_load_linear)
 	li	r11,0
 	/*
 	 * context = (MAX_USER_CONTEXT) + ((ea >> 60) - 0xc) + 1
@@ -79,7 +79,7 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
 	/* Check virtual memmap region. To be patches at kernel boot */
 	cmpldi	cr0,r9,0xf
 	bne	1f
-_GLOBAL_SYM(slb_miss_kernel_load_vmemmap)
+_GLOBAL_SYM_NOKPROBE(slb_miss_kernel_load_vmemmap)
 	li	r11,0
 	b	6f
 1:
@@ -95,7 +95,7 @@ _GLOBAL_SYM(slb_miss_kernel_load_vmemmap)
 	b	6f
 5:
 	/* IO mapping */
-_GLOBAL_SYM(slb_miss_kernel_load_io)
+_GLOBAL_SYM_NOKPROBE(slb_miss_kernel_load_io)
 	li	r11,0
 6:
 	/*
@@ -203,7 +203,7 @@ END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
 7:	ld	r10,PACASTABRR(r13)
 	addi	r10,r10,1
 	/* This gets soft patched on boot. */
-_GLOBAL_SYM(slb_compare_rr_to_size)
+_GLOBAL_SYM_NOKPROBE(slb_compare_rr_to_size)
 	cmpldi	r10,0
 
 	blt+	4f
Signature · 1 line
2.10.2
#11 lore raw

Re: [RFC PATCH 4/4] powerpc: mm/slb: blacklist symbols from kprobe

Quoted 13 lines · Naveen N. Rao

We can't really take a trap at this point. So, blacklist these symbols.

---
 arch/powerpc/mm/slb_low.S | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/powerpc/mm/slb_low.S +0 -0
index c1c7456..c2bae92 100644

arch/powerpc/mm/slb_low.S line 33

Quoted 5 lines
arch/powerpc/mm/slb_low.S +1 -1
@@ -30,7 +30,7 @@
  *	r9, r10, r11 are clobbered by this function
  * No other registers are examined or changed.
  */
-_GLOBAL(slb_allocate_realmode)
+_GLOBAL_NOKPROBE(slb_allocate_realmode)

I think I'd prefer we just leave these as is, and add somewhere nearby:

_ASM_NOKPROBE_SYMBOL(slb_allocate_realmode)

It means someone reading the code doesn't need to worry about the nokprobe part, just to understand the function. It also means we need fewer macros :)

cheers

#12 lore raw

Re: [RFC PATCH 4/4] powerpc: mm/slb: blacklist symbols from kprobe

Quoted 23 lines · Michael Ellerman
Quoted 13 lines · Naveen N. Rao

We can't really take a trap at this point. So, blacklist these symbols.

---
 arch/powerpc/mm/slb_low.S | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/powerpc/mm/slb_low.S +0 -0
index c1c7456..c2bae92 100644

arch/powerpc/mm/slb_low.S line 33

Quoted 5 lines
arch/powerpc/mm/slb_low.S +1 -1
@@ -30,7 +30,7 @@
  *	r9, r10, r11 are clobbered by this function
  * No other registers are examined or changed.
  */
-_GLOBAL(slb_allocate_realmode)
+_GLOBAL_NOKPROBE(slb_allocate_realmode)

I think I'd prefer we just leave these as is, and add somewhere nearby:

Quoted 6 lines

_ASM_NOKPROBE_SYMBOL(slb_allocate_realmode)

It means someone reading the code doesn't need to worry about the nokprobe part, just to understand the function. It also means we need fewer macros :)

:)
Ok.

- Naveen