LKML Archive on lore.kernel.org
 help / Atom feed
* [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline
@ 2018-01-18 16:13 Masami Hiramatsu
  2018-01-18 16:14 ` [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk Masami Hiramatsu
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Masami Hiramatsu @ 2018-01-18 16:13 UTC (permalink / raw)
  To: Ingo Molnar, David Woodhouse
  Cc: Masami Hiramatsu, linux-kernel, Andi Kleen, Greg Kroah-Hartman,
	Arjan van de Ven, Peter Zijlstra, Ananth N Mavinakayanahalli,
	Thomas Gleixner, H . Peter Anvin

Hi,

This is the 2nd version of the series to fix kprobes issues
on the kernel with CONFIG_RETPOLINE=y.

- [1/3]: This introduces __x86_indirect_thunk_* boundary
	 symbols so that kprobes easily identify those functions.
- [2/3]: Mark __x86_indirect_thunk_* as blacklisted function
	 for kprobes, since it can be called from other
	 blacklisted functions.
- [3/3]: Check jmp instructions in the probe target function
	 whether it jumps into the __x86_indirect_thunk_*,
	 because it is equal to an indirect jump instruction.

In this version, just [1/3] has been changed according to
David Woodhouse's comment.

 [1/3]: Consolidate .text.__x86.indirect_thunk.* sections
       to .text.__x86.indirect_thunk section.

Thank you,

---

Masami Hiramatsu (3):
      retpoline: Introduce start/end markers of indirect thunk
      kprobes/x86: Blacklist indirect thunk functions for kprobes
      kprobes/x86: Disable optimizing on the function jumps to indirect thunk


 arch/x86/include/asm/nospec-branch.h |    3 +++
 arch/x86/kernel/kprobes/opt.c        |   23 ++++++++++++++++++++++-
 arch/x86/kernel/vmlinux.lds.S        |    6 ++++++
 arch/x86/lib/retpoline.S             |    5 +++--
 4 files changed, 34 insertions(+), 3 deletions(-)

--
Masami Hiramatsu (Linaro) <mhiramat@kernel.org>

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

* [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk
  2018-01-18 16:13 [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline Masami Hiramatsu
@ 2018-01-18 16:14 ` Masami Hiramatsu
  2018-01-19 15:47   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
  2018-01-18 16:14 ` [PATCH v2 tip/master 2/3] kprobes/x86: Blacklist indirect thunk functions for kprobes Masami Hiramatsu
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Masami Hiramatsu @ 2018-01-18 16:14 UTC (permalink / raw)
  To: Ingo Molnar, David Woodhouse
  Cc: Masami Hiramatsu, linux-kernel, Andi Kleen, Greg Kroah-Hartman,
	Arjan van de Ven, Peter Zijlstra, Ananth N Mavinakayanahalli,
	Thomas Gleixner, H . Peter Anvin

Introduce start/end markers of __x86_indirect_thunk_* functions.
To make it easy, consolidate .text.__x86.indirect_thunk.* sections
to one .text.__x86.indirect_thunk section and put it in the
end of kernel text section and adds __indirect_thunk_start/end
so that other subsystem (e.g. kprobes) can identify it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
  Changes in v2:
   - Consolidate .text.__x86.indirect_thunk.* sections to
     one .text.__x86.indirect_thunk section. (Thanks David!)
---
 arch/x86/include/asm/nospec-branch.h |    3 +++
 arch/x86/kernel/vmlinux.lds.S        |    6 ++++++
 arch/x86/lib/retpoline.S             |    2 +-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 7b45d8424150..19ba5ad19c65 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -194,6 +194,9 @@ enum spectre_v2_mitigation {
 	SPECTRE_V2_IBRS,
 };
 
+extern char __indirect_thunk_start[];
+extern char __indirect_thunk_end[];
+
 /*
  * On VMEXIT we must ensure that no RSB predictions learned in the guest
  * can be followed in the host, by overwriting the RSB completely. Both
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 1e413a9326aa..9b138a06c1a4 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -124,6 +124,12 @@ SECTIONS
 		ASSERT(. - _entry_trampoline == PAGE_SIZE, "entry trampoline is too big");
 #endif
 
+#ifdef CONFIG_RETPOLINE
+		__indirect_thunk_start = .;
+		*(.text.__x86.indirect_thunk)
+		__indirect_thunk_end = .;
+#endif
+
 		/* End of text section */
 		_etext = .;
 	} :text = 0x9090
diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index cb45c6cb465f..d3415dc30f82 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -9,7 +9,7 @@
 #include <asm/nospec-branch.h>
 
 .macro THUNK reg
-	.section .text.__x86.indirect_thunk.\reg
+	.section .text.__x86.indirect_thunk
 
 ENTRY(__x86_indirect_thunk_\reg)
 	CFI_STARTPROC

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

* [PATCH v2 tip/master 2/3] kprobes/x86: Blacklist indirect thunk functions for kprobes
  2018-01-18 16:13 [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline Masami Hiramatsu
  2018-01-18 16:14 ` [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk Masami Hiramatsu
@ 2018-01-18 16:14 ` Masami Hiramatsu
  2018-01-19 15:47   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
  2018-01-18 16:15 ` [PATCH v2 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Masami Hiramatsu
  2018-01-19  9:03 ` [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline David Woodhouse
  3 siblings, 1 reply; 8+ messages in thread
From: Masami Hiramatsu @ 2018-01-18 16:14 UTC (permalink / raw)
  To: Ingo Molnar, David Woodhouse
  Cc: Masami Hiramatsu, linux-kernel, Andi Kleen, Greg Kroah-Hartman,
	Arjan van de Ven, Peter Zijlstra, Ananth N Mavinakayanahalli,
	Thomas Gleixner, H . Peter Anvin

Mark __x86_indirect_thunk_* functions as blacklist for kprobes
because those functions can be called from anywhere in the kernel
including blacklist functions of kprobes.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/lib/retpoline.S |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index d3415dc30f82..dfb2ba91b670 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -25,7 +25,8 @@ ENDPROC(__x86_indirect_thunk_\reg)
  * than one per register with the correct names. So we do it
  * the simple and nasty way...
  */
-#define EXPORT_THUNK(reg) EXPORT_SYMBOL(__x86_indirect_thunk_ ## reg)
+#define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
+#define EXPORT_THUNK(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
 #define GENERATE_THUNK(reg) THUNK reg ; EXPORT_THUNK(reg)
 
 GENERATE_THUNK(_ASM_AX)

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

* [PATCH v2 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk
  2018-01-18 16:13 [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline Masami Hiramatsu
  2018-01-18 16:14 ` [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk Masami Hiramatsu
  2018-01-18 16:14 ` [PATCH v2 tip/master 2/3] kprobes/x86: Blacklist indirect thunk functions for kprobes Masami Hiramatsu
@ 2018-01-18 16:15 ` Masami Hiramatsu
  2018-01-19 15:48   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
  2018-01-19  9:03 ` [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline David Woodhouse
  3 siblings, 1 reply; 8+ messages in thread
From: Masami Hiramatsu @ 2018-01-18 16:15 UTC (permalink / raw)
  To: Ingo Molnar, David Woodhouse
  Cc: Masami Hiramatsu, linux-kernel, Andi Kleen, Greg Kroah-Hartman,
	Arjan van de Ven, Peter Zijlstra, Ananth N Mavinakayanahalli,
	Thomas Gleixner, H . Peter Anvin

Since indirect jump instructions will be replaced by jump
to __x86_indirect_thunk_*, those jmp instruction must be
treated as an indirect jump. Since optprobe prohibits to
optimize probes in the function which uses an indirect jump,
it also needs to find out the function which jump to
__x86_indirect_thunk_* and disable optimization.

This adds a check that the jump target address is between
the __indirect_thunk_start/end when optimizing kprobe.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/kernel/kprobes/opt.c |   23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index e941136e24d8..203d398802a3 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -40,6 +40,7 @@
 #include <asm/debugreg.h>
 #include <asm/set_memory.h>
 #include <asm/sections.h>
+#include <asm/nospec-branch.h>
 
 #include "common.h"
 
@@ -203,7 +204,7 @@ static int copy_optimized_instructions(u8 *dest, u8 *src, u8 *real)
 }
 
 /* Check whether insn is indirect jump */
-static int insn_is_indirect_jump(struct insn *insn)
+static int __insn_is_indirect_jump(struct insn *insn)
 {
 	return ((insn->opcode.bytes[0] == 0xff &&
 		(X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
@@ -237,6 +238,26 @@ static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
 	return (start <= target && target <= start + len);
 }
 
+static int insn_is_indirect_jump(struct insn *insn)
+{
+	int ret = __insn_is_indirect_jump(insn);
+
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * Jump to x86_indirect_thunk_* is treated as an indirect jump.
+	 * Note that even with CONFIG_RETPOLINE=y, the kernel compiled with
+	 * older gcc may use indirect jump. So we add this check instead of
+	 * replace indirect-jump check.
+	 */
+	if (!ret)
+		ret = insn_jump_into_range(insn,
+				(unsigned long)__indirect_thunk_start,
+				(unsigned long)__indirect_thunk_end -
+				(unsigned long)__indirect_thunk_start);
+#endif
+	return ret;
+}
+
 /* Decode whole function to ensure any instructions don't jump into target */
 static int can_optimize(unsigned long paddr)
 {

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

* Re: [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline
  2018-01-18 16:13 [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline Masami Hiramatsu
                   ` (2 preceding siblings ...)
  2018-01-18 16:15 ` [PATCH v2 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Masami Hiramatsu
@ 2018-01-19  9:03 ` David Woodhouse
  3 siblings, 0 replies; 8+ messages in thread
From: David Woodhouse @ 2018-01-19  9:03 UTC (permalink / raw)
  To: Masami Hiramatsu, Ingo Molnar
  Cc: linux-kernel, Andi Kleen, Greg Kroah-Hartman, Arjan van de Ven,
	Peter Zijlstra, Ananth N Mavinakayanahalli, Thomas Gleixner,
	H . Peter Anvin, stable, Ghitulete, Razvan-alin

[-- Attachment #1: Type: text/plain, Size: 426 bytes --]

On Fri, 2018-01-19 at 01:13 +0900, Masami Hiramatsu wrote:
> Masami Hiramatsu (3):
>       retpoline: Introduce start/end markers of indirect thunk
>       kprobes/x86: Blacklist indirect thunk functions for kprobes
>       kprobes/x86: Disable optimizing on the function jumps to indirect thunk

For all three:

Acked-by: David Woodhouse <dwmw@amazon.co.uk>
Cc: stable@vger.kernel.org

Thank you.

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 5213 bytes --]

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

* [tip:x86/pti] retpoline: Introduce start/end markers of indirect thunk
  2018-01-18 16:14 ` [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk Masami Hiramatsu
@ 2018-01-19 15:47   ` " tip-bot for Masami Hiramatsu
  0 siblings, 0 replies; 8+ messages in thread
From: tip-bot for Masami Hiramatsu @ 2018-01-19 15:47 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, peterz, ak, ananth, tglx, gregkh, mingo, hpa, dwmw,
	mhiramat, arjan

Commit-ID:  736e80a4213e9bbce40a7c050337047128b472ac
Gitweb:     https://git.kernel.org/tip/736e80a4213e9bbce40a7c050337047128b472ac
Author:     Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate: Fri, 19 Jan 2018 01:14:21 +0900
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Fri, 19 Jan 2018 16:31:28 +0100

retpoline: Introduce start/end markers of indirect thunk

Introduce start/end markers of __x86_indirect_thunk_* functions.
To make it easy, consolidate .text.__x86.indirect_thunk.* sections
to one .text.__x86.indirect_thunk section and put it in the
end of kernel text section and adds __indirect_thunk_start/end
so that other subsystem (e.g. kprobes) can identify it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: David Woodhouse <dwmw@amazon.co.uk>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/151629206178.10241.6828804696410044771.stgit@devbox

---
 arch/x86/include/asm/nospec-branch.h | 3 +++
 arch/x86/kernel/vmlinux.lds.S        | 6 ++++++
 arch/x86/lib/retpoline.S             | 2 +-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 7b45d84..19ba5ad 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -194,6 +194,9 @@ enum spectre_v2_mitigation {
 	SPECTRE_V2_IBRS,
 };
 
+extern char __indirect_thunk_start[];
+extern char __indirect_thunk_end[];
+
 /*
  * On VMEXIT we must ensure that no RSB predictions learned in the guest
  * can be followed in the host, by overwriting the RSB completely. Both
diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 1e413a93..9b138a0 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -124,6 +124,12 @@ SECTIONS
 		ASSERT(. - _entry_trampoline == PAGE_SIZE, "entry trampoline is too big");
 #endif
 
+#ifdef CONFIG_RETPOLINE
+		__indirect_thunk_start = .;
+		*(.text.__x86.indirect_thunk)
+		__indirect_thunk_end = .;
+#endif
+
 		/* End of text section */
 		_etext = .;
 	} :text = 0x9090
diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index cb45c6c..d3415dc 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -9,7 +9,7 @@
 #include <asm/nospec-branch.h>
 
 .macro THUNK reg
-	.section .text.__x86.indirect_thunk.\reg
+	.section .text.__x86.indirect_thunk
 
 ENTRY(__x86_indirect_thunk_\reg)
 	CFI_STARTPROC

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

* [tip:x86/pti] kprobes/x86: Blacklist indirect thunk functions for kprobes
  2018-01-18 16:14 ` [PATCH v2 tip/master 2/3] kprobes/x86: Blacklist indirect thunk functions for kprobes Masami Hiramatsu
@ 2018-01-19 15:47   ` " tip-bot for Masami Hiramatsu
  0 siblings, 0 replies; 8+ messages in thread
From: tip-bot for Masami Hiramatsu @ 2018-01-19 15:47 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: tglx, peterz, linux-kernel, ak, ananth, mhiramat, hpa, mingo,
	gregkh, arjan, dwmw

Commit-ID:  c1804a236894ecc942da7dc6c5abe209e56cba93
Gitweb:     https://git.kernel.org/tip/c1804a236894ecc942da7dc6c5abe209e56cba93
Author:     Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate: Fri, 19 Jan 2018 01:14:51 +0900
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Fri, 19 Jan 2018 16:31:28 +0100

kprobes/x86: Blacklist indirect thunk functions for kprobes

Mark __x86_indirect_thunk_* functions as blacklist for kprobes
because those functions can be called from anywhere in the kernel
including blacklist functions of kprobes.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: David Woodhouse <dwmw@amazon.co.uk>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/151629209111.10241.5444852823378068683.stgit@devbox

---
 arch/x86/lib/retpoline.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
index d3415dc..dfb2ba9 100644
--- a/arch/x86/lib/retpoline.S
+++ b/arch/x86/lib/retpoline.S
@@ -25,7 +25,8 @@ ENDPROC(__x86_indirect_thunk_\reg)
  * than one per register with the correct names. So we do it
  * the simple and nasty way...
  */
-#define EXPORT_THUNK(reg) EXPORT_SYMBOL(__x86_indirect_thunk_ ## reg)
+#define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
+#define EXPORT_THUNK(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
 #define GENERATE_THUNK(reg) THUNK reg ; EXPORT_THUNK(reg)
 
 GENERATE_THUNK(_ASM_AX)

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

* [tip:x86/pti] kprobes/x86: Disable optimizing on the function jumps to indirect thunk
  2018-01-18 16:15 ` [PATCH v2 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Masami Hiramatsu
@ 2018-01-19 15:48   ` " tip-bot for Masami Hiramatsu
  0 siblings, 0 replies; 8+ messages in thread
From: tip-bot for Masami Hiramatsu @ 2018-01-19 15:48 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: mhiramat, arjan, peterz, ak, gregkh, mingo, dwmw, hpa,
	linux-kernel, tglx, ananth

Commit-ID:  c86a32c09f8ced67971a2310e3b0dda4d1749007
Gitweb:     https://git.kernel.org/tip/c86a32c09f8ced67971a2310e3b0dda4d1749007
Author:     Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate: Fri, 19 Jan 2018 01:15:20 +0900
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Fri, 19 Jan 2018 16:31:29 +0100

kprobes/x86: Disable optimizing on the function jumps to indirect thunk

Since indirect jump instructions will be replaced by jump
to __x86_indirect_thunk_*, those jmp instruction must be
treated as an indirect jump. Since optprobe prohibits to
optimize probes in the function which uses an indirect jump,
it also needs to find out the function which jump to
__x86_indirect_thunk_* and disable optimization.

Add a check that the jump target address is between the
__indirect_thunk_start/end when optimizing kprobe.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: David Woodhouse <dwmw@amazon.co.uk>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Arjan van de Ven <arjan@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/151629212062.10241.6991266100233002273.stgit@devbox

---
 arch/x86/kernel/kprobes/opt.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 4f98aad..3668f28 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -40,6 +40,7 @@
 #include <asm/debugreg.h>
 #include <asm/set_memory.h>
 #include <asm/sections.h>
+#include <asm/nospec-branch.h>
 
 #include "common.h"
 
@@ -205,7 +206,7 @@ static int copy_optimized_instructions(u8 *dest, u8 *src)
 }
 
 /* Check whether insn is indirect jump */
-static int insn_is_indirect_jump(struct insn *insn)
+static int __insn_is_indirect_jump(struct insn *insn)
 {
 	return ((insn->opcode.bytes[0] == 0xff &&
 		(X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
@@ -239,6 +240,26 @@ static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
 	return (start <= target && target <= start + len);
 }
 
+static int insn_is_indirect_jump(struct insn *insn)
+{
+	int ret = __insn_is_indirect_jump(insn);
+
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * Jump to x86_indirect_thunk_* is treated as an indirect jump.
+	 * Note that even with CONFIG_RETPOLINE=y, the kernel compiled with
+	 * older gcc may use indirect jump. So we add this check instead of
+	 * replace indirect-jump check.
+	 */
+	if (!ret)
+		ret = insn_jump_into_range(insn,
+				(unsigned long)__indirect_thunk_start,
+				(unsigned long)__indirect_thunk_end -
+				(unsigned long)__indirect_thunk_start);
+#endif
+	return ret;
+}
+
 /* Decode whole function to ensure any instructions don't jump into target */
 static int can_optimize(unsigned long paddr)
 {

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

end of thread, back to index

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-18 16:13 [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline Masami Hiramatsu
2018-01-18 16:14 ` [PATCH v2 tip/master 1/3] retpoline: Introduce start/end markers of indirect thunk Masami Hiramatsu
2018-01-19 15:47   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
2018-01-18 16:14 ` [PATCH v2 tip/master 2/3] kprobes/x86: Blacklist indirect thunk functions for kprobes Masami Hiramatsu
2018-01-19 15:47   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
2018-01-18 16:15 ` [PATCH v2 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Masami Hiramatsu
2018-01-19 15:48   ` [tip:x86/pti] " tip-bot for Masami Hiramatsu
2018-01-19  9:03 ` [PATCH v2 tip/master 0/3] kprobes/x86: retpoline: Fix kprobes for retpoline David Woodhouse

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org linux-kernel@archiver.kernel.org
	public-inbox-index lkml


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox