[Devel] [PATCH RHEL7 COMMIT] ms/x86/kprobes: Fix kprobes instruction boudary check with CONFIG_RETHUNK

Konstantin Khorenko khorenko at virtuozzo.com
Thu Dec 14 20:33:05 MSK 2023


The commit is pushed to "branch-rh7-3.10.0-1160.99.1.vz7.211.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-1160.99.1.vz7.211.3
------>
commit 612bc1eefa11fb6b2f5469c4062b70ea068eedd5
Author: Masami Hiramatsu (Google) <mhiramat at kernel.org>
Date:   Thu Dec 7 17:14:10 2023 +0800

    ms/x86/kprobes: Fix kprobes instruction boudary check with CONFIG_RETHUNK
    
    Since the CONFIG_RETHUNK and CONFIG_SLS will use INT3 for stopping
    speculative execution after RET instruction, kprobes always failes to
    check the probed instruction boundary by decoding the function body if
    the probed address is after such sequence. (Note that some conditional
    code blocks will be placed after function return, if compiler decides
    it is not on the hot path.)
    
    This is because kprobes expects kgdb puts the INT3 as a software
    breakpoint and it will replace the original instruction.
    But these INT3 are not such purpose, it doesn't need to recover the
    original instruction.
    
    To avoid this issue, kprobes checks whether the INT3 is owned by
    kgdb or not, and if so, stop decoding and make it fail. The other
    INT3 will come from CONFIG_RETHUNK/CONFIG_SLS and those can be
    treated as a one-byte instruction.
    
    mFixes: e463a09af2f0 ("x86: Add straight-line-speculation mitigation")
    Suggested-by: Peter Zijlstra <peterz at infradead.org>
    Signed-off-by: Masami Hiramatsu (Google) <mhiramat at kernel.org>
    Signed-off-by: Peter Zijlstra (Intel) <peterz at infradead.org>
    Cc: stable at vger.kernel.org
    Link: https://lore.kernel.org/r/167146051026.1374301.392728975473572291.stgit@devnote3
    
    Changes:
     - s/INT3_INSN_OPCODE/BREAKPOINT_INSTRUCTION/
     - include kgdb_has_hit_break implementation
    
    If function return (int3 instruction) got compiled in assembly code into
    the middle of the function, then we failed to insert kprobe at any point
    after that. To be able to insert kprobes everythere in the function code
    we need this patch.
    
    (cherry picked from commit 1993bf97992df2d560287f3c4120eda57426843d)
    Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 arch/x86/kernel/kprobes/core.c | 10 +++++++---
 include/linux/kgdb.h           |  1 +
 kernel/debug/debug_core.c      | 12 ++++++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 2be0dde130f1..9dc7f48e84ea 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -48,6 +48,7 @@
 #include <linux/module.h>
 #include <linux/kdebug.h>
 #include <linux/kallsyms.h>
+#include <linux/kgdb.h>
 #include <linux/ftrace.h>
 #include <linux/frame.h>
 #include <linux/kasan.h>
@@ -300,12 +301,15 @@ static int __kprobes can_probe(unsigned long paddr)
 		kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
 		insn_get_length(&insn);
 
+#ifdef CONFIG_KGDB
 		/*
-		 * Another debugging subsystem might insert this breakpoint.
-		 * In that case, we can't recover it.
+		 * If there is a dynamically installed kgdb sw breakpoint,
+		 * this function should not be probed.
 		 */
-		if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
+		if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION &&
+		    kgdb_has_hit_break(addr))
 			return 0;
+#endif
 		addr += insn.length;
 	}
 
diff --git a/include/linux/kgdb.h b/include/linux/kgdb.h
index 6b06d378f3df..47a8fd917b3b 100644
--- a/include/linux/kgdb.h
+++ b/include/linux/kgdb.h
@@ -305,6 +305,7 @@ extern int kgdb_hex2mem(char *buf, char *mem, int count);
 
 extern int kgdb_isremovedbreak(unsigned long addr);
 extern void kgdb_schedule_breakpoint(void);
+extern int kgdb_has_hit_break(unsigned long addr);
 
 extern int
 kgdb_handle_exception(int ex_vector, int signo, int err_code,
diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c
index 8865caec45fb..e5872e38fae5 100644
--- a/kernel/debug/debug_core.c
+++ b/kernel/debug/debug_core.c
@@ -356,6 +356,18 @@ int kgdb_isremovedbreak(unsigned long addr)
 	return 0;
 }
 
+int kgdb_has_hit_break(unsigned long addr)
+{
+	int i;
+
+	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+		if (kgdb_break[i].state == BP_ACTIVE &&
+		    kgdb_break[i].bpt_addr == addr)
+			return 1;
+	}
+	return 0;
+}
+
 int dbg_remove_all_break(void)
 {
 	int error;


More information about the Devel mailing list