x86: kprobes: Fix CFI_CLANG related issues
Subsystems (4)
Masami Hiramatsu (Google)
maintainerv2 cover x86: kprobes: Fix CFI_CLANG related issues
Hi,
Here is the 2nd version of the kprobe patches for kernel CFI. Previous version is here;
https://lore.kernel.org/all/168899125356.80889.17967397360941194229.stgit@devnote2/
In this version "__pfx_" prefix symbols also are prohibited and that check is done unconditionally [1/2].
- Prohibit probing on __cfi_* and __pfx_* preamble symbols, which have CFI info.
- Prohibit probing on compiler generated movl/addl which is used for detecting typeid on x86.
Thank you,
---
Masami Hiramatsu (Google) (2):
kprobes: Prohibit probing on CFI preamble symbol x86/kprobes: Prohibit probing on compiler generated CFI checking code
arch/x86/kernel/kprobes/core.c | 34 ++++++++++++++++++++++++++++++++++ kernel/kprobes.c | 14 +++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v2 1/2 kprobes: Prohibit probing on CFI preamble symbol
From: Masami Hiramatsu (Google) <[email protected]>
Do not allow to probe on "__cfi_" or "__pfx_" started symbol, because those are used for CFI and not executed. Probing it will break the CFI.
---
Changes in v2:
- Check "__pfx_" prefix functions too.
- Make the check unconditional.
--- kernel/kprobes.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/kprobes.c +13 -1
@@ -1545,6 +1545,17 @@ static int check_ftrace_location(struct kprobe *p)return 0 ; } +static bool is_cfi_preamble_symbol (unsigned long addr) +{ +char symbuf[KSYM_NAME_LEN ]; + +if (lookup_symbol_name (addr, symbuf)) +return false; + +return str_has_prefix ("__cfi_" , symbuf) || +str_has_prefix ("__pfx_" , symbuf); +} +static int check_kprobe_address_safe (struct kprobe *p,struct module **probed_mod) {
@@ -1563,7 +1574,8 @@ static int check_kprobe_address_safe(struct kprobe *p,within_kprobe_blacklist ((unsigned long ) p->addr) ||jump_label_text_reserved (p->addr, p->addr) ||static_call_text_reserved (p->addr, p->addr) || - find_bug((unsigned long)p->addr)) { +find_bug ((unsigned long )p->addr) || + is_cfi_preamble_symbol((unsigned long)p->addr)) { ret = -EINVAL; goto out; }
Masami Hiramatsu (Google)
maintainerPATCH v2 2/2 x86/kprobes: Prohibit probing on compiler generated CFI checking code
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Prohibit probing on the compiler generated CFI typeid checking code because it is used for decoding typeid when CFI error happens.
The compiler generates the following instruction sequence for indirect call checks on x86;
movl -<id>, %r10d ; 6 bytes
addl -4(%reg), %r10d ; 4 bytes je .Ltmp1 ; 2 bytes ud2 ; <- regs->ip
And handle_cfi_failure() decodes these instructions (movl and addl) for the typeid and the target address. Thus if we put a kprobe on those instructions, the decode will fail and report a wrong typeid and target address.
--- arch/x86/kernel/kprobes/core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
arch/x86/kernel/kprobes/core.c +34 -0
@@ -54,6 +54,7 @@#include <asm/insn.h> #include <asm/debugreg.h> #include <asm/ibt.h> +#include <asm/cfi.h> #include "common.h"
@@ -293,7 +294,40 @@ static int can_probe(unsigned long paddr)#endif addr += insn.length; } +if (IS_ENABLED (CONFIG_CFI_CLANG )) { +/* +* The compiler generates the following instruction sequence +* for indirect call checks and cfi.c decodes this; +* +* movl -<id>, %r10d ; 6 bytes +* addl -4(%reg), %r10d ; 4 bytes +* je .Ltmp1 ; 2 bytes +* ud2 ; <- regs->ip +* .Ltmp1: +* +* Also, these movl and addl are used for showing expected +* type. So those must not be touched. +*/ + __addr =recover_probed_instruction (buf, addr); +if (!__addr) +return 0 ; + +if (insn_decode_kernel (&insn, (void *)__addr) <0 ) +return 0 ; + +if (insn.opcode.value ==0xBA ) + offset =12 ; +else if (insn.opcode.value ==0x3 ) + offset =6 ; +else + goto out; + +/* This movl/addl is used for decoding CFI. */ +if (is_cfi_trap (addr + offset)) +return 0 ; + } +out:return (addr == paddr); }
Masami Hiramatsu (Google)
maintainerRe: [PATCH v2 2/2] x86/kprobes: Prohibit probing on compiler generated CFI checking code
Quoted 9 lines · On Tue, 11 Jul 2023 10:50:58 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Prohibit probing on the compiler generated CFI typeid checking code because it is used for decoding typeid when CFI error happens.
The compiler generates the following instruction sequence for indirect call checks on x86;
movl -<id>, %r10d ; 6 bytes
Quoted 10 lines
addl -4(%reg), %r10d ; 4 bytes je .Ltmp1 ; 2 bytes ud2 ; <- regs->ipAnd
handle_cfi_failure()decodes these instructions (movl and addl) for the typeid and the target address. Thus if we put a kprobe on those instructions, the decode will fail and report a wrong typeid and target address.
Hi Peter,
Can I pick this to probes/fixes branch ?
Thank you,
Quoted 60 lines
Signed-off-by Masami Hiramatsu (Google) <[email protected]>--- arch/x86/kernel/kprobes/core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)arch/x86/kernel/kprobes/core.c +34
-0@@ -54,6 +54,7 @@#include <asm/insn.h> #include <asm/debugreg.h> #include <asm/ibt.h> +#include <asm/cfi.h> #include "common.h" @@ -293,7 +294,40 @@ static int can_probe(unsigned long paddr)#endif addr += insn.length; } +if (IS_ENABLED (CONFIG_CFI_CLANG )) { +/* +* The compiler generates the following instruction sequence +* for indirect call checks and cfi.c decodes this; +* +* movl -<id>, %r10d ; 6 bytes +* addl -4(%reg), %r10d ; 4 bytes +* je .Ltmp1 ; 2 bytes +* ud2 ; <- regs->ip +* .Ltmp1: +* +* Also, these movl and addl are used for showing expected +* type. So those must not be touched. +*/ + __addr =recover_probed_instruction (buf, addr); +if (!__addr) +return 0 ; + +if (insn_decode_kernel (&insn, (void *)__addr) <0 ) +return 0 ; + +if (insn.opcode.value ==0xBA ) + offset =12 ; +else if (insn.opcode.value ==0x3 ) + offset =6 ; +else + goto out; + +/* This movl/addl is used for decoding CFI. */ +if (is_cfi_trap (addr + offset)) +return 0 ; + } +out:return (addr == paddr); }
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v2 2/2] x86/kprobes: Prohibit probing on compiler generated CFI checking code
Quoted 23 lines · Masami Hiramatsu
Quoted 9 lines · On Tue, 11 Jul 2023 10:50:58 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Prohibit probing on the compiler generated CFI typeid checking code because it is used for decoding typeid when CFI error happens.
The compiler generates the following instruction sequence for indirect call checks on x86;
movl -<id>, %r10d ; 6 bytes
Quoted 10 lines
addl -4(%reg), %r10d ; 4 bytes je .Ltmp1 ; 2 bytes ud2 ; <- regs->ipAnd
handle_cfi_failure()decodes these instructions (movl and addl) for the typeid and the target address. Thus if we put a kprobe on those instructions, the decode will fail and report a wrong typeid and target address.
Quoted 3 lines
Hi Peter,
Can I pick this to probes/fixes branch ?
I'll stick them in tip/x86/core, that ok?
Masami Hiramatsu (Google)
maintainerRe: [PATCH v2 2/2] x86/kprobes: Prohibit probing on compiler generated CFI checking code
Quoted 24 lines · On Wed, 26 Jul 2023 11:29:17 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 23 lines · Masami Hiramatsu
Quoted 19 lines · On Tue, 11 Jul 2023 10:50:58 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Prohibit probing on the compiler generated CFI typeid checking code because it is used for decoding typeid when CFI error happens.
The compiler generates the following instruction sequence for indirect call checks on x86;
movl -<id>, %r10d ; 6 bytes
addl -4(%reg), %r10d ; 4 bytes je .Ltmp1 ; 2 bytes ud2 ; <- regs->ipAnd
handle_cfi_failure()decodes these instructions (movl and addl) for the typeid and the target address. Thus if we put a kprobe on those instructions, the decode will fail and report a wrong typeid and target address.
Quoted 5 lines
Quoted 3 lines
Hi Peter,
Can I pick this to probes/fixes branch ?
I'll stick them in tip/x86/core, that ok?
Yes, since it is for CFI change.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v2 1/2] kprobes: Prohibit probing on CFI preamble symbol
In reply to Masami Hiramatsu (Google)Quoted 6 lines · On Tue, 11 Jul 2023 10:50:47 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Do not allow to probe on "
__cfi_" or "__pfx_" started symbol, because those are used for CFI and not executed. Probing it will break the CFI.Signed-off-by Masami Hiramatsu (Google) <[email protected]>
-- Steve
Quoted 8 lines
---
Changes in v2:
- Check "__pfx_" prefix functions too.
- Make the check unconditional.--- kernel/kprobes.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
Masami Hiramatsu (Google)
maintainerRe: [PATCH v2 1/2] kprobes: Prohibit probing on CFI preamble symbol
Quoted 11 lines · On Fri, 28 Jul 2023 18:49:13 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Tue, 11 Jul 2023 10:50:47 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Do not allow to probe on "
__cfi_" or "__pfx_" started symbol, because those are used for CFI and not executed. Probing it will break the CFI.Signed-off-by Masami Hiramatsu (Google) <[email protected]>Reviewed-by Steven Rostedt (Google) <[email protected]>
Thanks! I will pick this for probes/fixes.
Quoted 11 lines
-- Steve
Quoted 8 lines
---
Changes in v2:
- Check "__pfx_" prefix functions too.
- Make the check unconditional.--- kernel/kprobes.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
[tip: x86/core] x86/kprobes: Prohibit probing on compiler generated CFI checking code
In reply to Masami Hiramatsu (Google)The following commit has been merged into the x86/core branch of tip:
The following commit has been merged into the x86/core branch of tip:
Commit-ID: b65413768abd27a55af74945aec58127a52b30a8 Gitweb: https://git.kernel.org/tip/b65413768abd27a55af74945aec58127a52b30a8 Author: Masami Hiramatsu <[email protected]> AuthorDate: Tue, 11 Jul 2023 10:50:58 +09:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 02 Aug 2023 16:27:07 +02:00
x86/kprobes: Prohibit probing on compiler generated CFI checking code
Prohibit probing on the compiler generated CFI typeid checking code because it is used for decoding typeid when CFI error happens.
The compiler generates the following instruction sequence for indirect call checks on x86;
movl -<id>, %r10d ; 6 bytes
addl -4(%reg), %r10d ; 4 bytes je .Ltmp1 ; 2 bytes ud2 ; <- regs->ip
And handle_cfi_failure() decodes these instructions (movl and addl) for the typeid and the target address. Thus if we put a kprobe on those instructions, the decode will fail and report a wrong typeid and target address.
--- arch/x86/kernel/kprobes/core.c | 34 +++++++++++++++++++++++++++++++++- include/linux/cfi.h | 4 +++- 2 files changed, 37 insertions(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +34 -0
@@ -45,6 +45,7 @@#include <linux/vmalloc.h> #include <linux/pgtable.h> #include <linux/set_memory.h> +#include <linux/cfi.h> #include <asm/text-patching.h> #include <asm/cacheflush.h>
@@ -293,7 +294,40 @@ static int can_probe(unsigned long paddr)#endif addr += insn.length; } +if (IS_ENABLED (CONFIG_CFI_CLANG )) { +/* +* The compiler generates the following instruction sequence +* for indirect call checks and cfi.c decodes this; +* +* movl -<id>, %r10d ; 6 bytes +* addl -4(%reg), %r10d ; 4 bytes +* je .Ltmp1 ; 2 bytes +* ud2 ; <- regs->ip +* .Ltmp1: +* +* Also, these movl and addl are used for showing expected +* type. So those must not be touched. +*/ + __addr =recover_probed_instruction (buf, addr); +if (!__addr) +return 0 ; + +if (insn_decode_kernel (&insn, (void *)__addr) <0 ) +return 0 ; + +if (insn.opcode.value ==0xBA ) + offset =12 ; +else if (insn.opcode.value ==0x3 ) + offset =6 ; +else + goto out; + +/* This movl/addl is used for decoding CFI. */ +if (is_cfi_trap (addr + offset)) +return 0 ; + } +out:return (addr == paddr); }
include/linux/cfi.h +3 -1
@@ -19,11 +19,13 @@ static inline enum bug_trap_type report_cfi_failure_noaddr(struct pt_regs *regs, {return report_cfi_failure (regs, addr,NULL ,0 ); } +#endif /* CONFIG_CFI_CLANG */ #ifdef CONFIG_ARCH_USES_CFI_TRAPS bool is_cfi_trap (unsigned long addr); +#else +static inline bool is_cfi_trap (unsigned long addr) {return false; }#endif -#endif /* CONFIG_CFI_CLANG */ #ifdef CONFIG_MODULES #ifdef CONFIG_ARCH_USES_CFI_TRAPS