x86/ibt: FineIBT-BHI
Subsystems (5)
v4 cover x86/ibt: FineIBT-BHI
Hi all!
Hopefully final version of these here patches.
As reported, these patches include the cfi=paranoid flag for dealing with the FineIBT SYSCALL pivot and cfi=bhi for further hardering FineIBT.
Biggest difference since last time is the reworking (vastly simplifying) of cfi=warn, and getting rid of the ud_type propagation in favour of using is_cfi_trap().
As reported earlier, available at:
git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git x86/fineibt-bhi2
Previous version at:
https://lkml.kernel.org/r/[email protected]
--- Makefile | 3 + arch/x86/Kconfig | 8 + arch/x86/include/asm/bug.h | 3 + arch/x86/include/asm/cfi.h | 10 ++ arch/x86/include/asm/ibt.h | 4 + arch/x86/kernel/alternative.c | 370 +++++++++++++++++++++++++++++++++++++----- arch/x86/kernel/cfi.c | 8 +- arch/x86/kernel/traps.c | 54 +++++- arch/x86/lib/Makefile | 3 +- arch/x86/lib/bhi.S | 146 +++++++++++++++++ arch/x86/net/bpf_jit_comp.c | 30 ++-- include/linux/cfi.h | 2 + kernel/cfi.c | 4 +- 13 files changed, 585 insertions(+), 60 deletions(-)
PATCH v4 1/10 x86/cfi: Add warn option
Rebuilding with CFI_PERMISSIVE toggled is such a pain, esp. since clang is so slow.
--- arch/x86/kernel/alternative.c | 3 +++ arch/x86/kernel/cfi.c | 8 ++++---- include/linux/cfi.h | 2 ++ kernel/cfi.c | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-)
arch/x86/kernel/alternative.c +3 -0
@@ -1022,6 +1022,9 @@ static __init int cfi_parse_cmdline(char cfi_mode =CFI_FINEIBT ; }else if (!strcmp (str,"norand" )) { cfi_rand = false; + }else if (!strcmp (str,"warn" )) { +pr_alert ("CFI mismatch non-fatal!\n" ); + cfi_warn = true; }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
include/linux/cfi.h +2 -0
@@ -11,6 +11,8 @@#include <linux/module.h> #include <asm/cfi.h> +extern bool cfi_warn; +#ifndef cfi_get_offsetstatic inline int cfi_get_offset (void ) {
kernel/cfi.c +3 -1
@@ -7,6 +7,8 @@#include <linux/cfi.h> +bool cfi_warn__ro_after_init =IS_ENABLED (CONFIG_CFI_PERMISSIVE ); +enum bug_trap_type report_cfi_failure (struct pt_regs *regs,unsigned long addr,unsigned long *target,u32 type) {
@@ -17,7 +19,7 @@ enum bug_trap_type report_cfi_failure(stpr_err ("CFI failure at %pS (no target information)\n" , (void *)addr); -if (IS_ENABLED (CONFIG_CFI_PERMISSIVE )) { +if (cfi_warn) {__warn (NULL ,0 , (void *)addr,0 , regs,NULL );return BUG_TRAP_TYPE_WARN ; }
Re: [PATCH v4 01/10] x86/cfi: Add warn option
Quoted 4 lines · Peter Zijlstra
Rebuilding with
CFI_PERMISSIVEtoggled is such a pain, esp. since clang is so slow.Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Lovely :)
Signature · 1 line
Kees Cook
[tip: x86/core] x86/cfi: Add warn option
In reply to Peter ZijlstraThe 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: f003da5d29a179bd1beb4ef87ac93a3ca6ddf734 Gitweb: https://git.kernel.org/tip/f003da5d29a179bd1beb4ef87ac93a3ca6ddf734 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:04 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:53 +01:00
x86/cfi: Add warn option
Rebuilding with CFI_PERMISSIVE toggled is such a pain, esp. since clang is so slow.
--- arch/x86/kernel/alternative.c | 3 +++ include/linux/cfi.h | 2 ++ kernel/cfi.c | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +3 -0
@@ -1022,6 +1022,9 @@ static __init int cfi_parse_cmdline(char *str) cfi_mode =CFI_FINEIBT ; }else if (!strcmp (str,"norand" )) { cfi_rand = false; + }else if (!strcmp (str,"warn" )) { +pr_alert ("CFI mismatch non-fatal!\n" ); + cfi_warn = true; }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
include/linux/cfi.h +2 -0
@@ -11,6 +11,8 @@#include <linux/module.h> #include <asm/cfi.h> +extern bool cfi_warn; +#ifndef cfi_get_offsetstatic inline int cfi_get_offset (void ) {
kernel/cfi.c +3 -1
@@ -7,6 +7,8 @@#include <linux/cfi.h> +bool cfi_warn__ro_after_init =IS_ENABLED (CONFIG_CFI_PERMISSIVE ); +enum bug_trap_type report_cfi_failure (struct pt_regs *regs,unsigned long addr,unsigned long *target,u32 type) {
@@ -17,7 +19,7 @@ enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,pr_err ("CFI failure at %pS (no target information)\n" , (void *)addr); -if (IS_ENABLED (CONFIG_CFI_PERMISSIVE )) { +if (cfi_warn) {__warn (NULL ,0 , (void *)addr,0 , regs,NULL );return BUG_TRAP_TYPE_WARN ; }
[tip: x86/core] x86/cfi: Add 'cfi=warn' boot option
In reply to Peter ZijlstraThe 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: 9a54fb31343362f93680543e37afc14484c185d9 Gitweb: https://git.kernel.org/tip/9a54fb31343362f93680543e37afc14484c185d9 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:04 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:10:48 +01:00
x86/cfi: Add 'cfi=warn' boot option
Rebuilding with CONFIG_CFI_PERMISSIVE=y enabled is such a pain, esp. since clang is so slow.
--- arch/x86/kernel/alternative.c | 3 +++ include/linux/cfi.h | 2 ++ kernel/cfi.c | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kernel/alternative.c +3 -0
@@ -1022,6 +1022,9 @@ static __init int cfi_parse_cmdline(char *str) cfi_mode =CFI_FINEIBT ; }else if (!strcmp (str,"norand" )) { cfi_rand = false; + }else if (!strcmp (str,"warn" )) { +pr_alert ("CFI mismatch non-fatal!\n" ); + cfi_warn = true; }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
include/linux/cfi.h +2 -0
@@ -11,6 +11,8 @@#include <linux/module.h> #include <asm/cfi.h> +extern bool cfi_warn; +#ifndef cfi_get_offsetstatic inline int cfi_get_offset (void ) {
kernel/cfi.c +3 -1
@@ -7,6 +7,8 @@#include <linux/cfi.h> +bool cfi_warn__ro_after_init =IS_ENABLED (CONFIG_CFI_PERMISSIVE ); +enum bug_trap_type report_cfi_failure (struct pt_regs *regs,unsigned long addr,unsigned long *target,u32 type) {
@@ -17,7 +19,7 @@ enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,pr_err ("CFI failure at %pS (no target information)\n" , (void *)addr); -if (IS_ENABLED (CONFIG_CFI_PERMISSIVE )) { +if (cfi_warn) {__warn (NULL ,0 , (void *)addr,0 , regs,NULL );return BUG_TRAP_TYPE_WARN ; }
PATCH v4 2/10 x86/ibt: Add exact_endbr() helper
In reply to Peter ZijlstraFor when we want to exactly match ENDBR, and not everything that we can scribble it with.
--- arch/x86/kernel/alternative.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +13 -3
@@ -863,6 +863,17 @@ __noendbr bool is_endbr(u32 *val)return false; } +static __noendbr bool exact_endbr(u32 *val) +{ +u32 endbr; + +__get_kernel_nofault (&endbr, val, u32, Efault); +return endbr ==gen_endbr (); + +Efault: +return false; +} +static void poison_cfi (void *addr); static void __init_or_module poison_endbr(void *addr)
@@ -1427,10 +1438,9 @@ static void poison_cfi(void *addr)bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud2; -u32 endbr, hash; +u32 hash; -__get_kernel_nofault (&endbr, addr, u32, Efault); -if (endbr !=gen_endbr ()) +if (!exact_endbr ((void *)addr))return false; *target = addr + fineibt_preamble_size;
[tip: x86/core] x86/ibt: Add exact_endbr() helper
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: 1d60b295042d20f312de17d74076a74a0d13a32d Gitweb: https://git.kernel.org/tip/1d60b295042d20f312de17d74076a74a0d13a32d Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:05 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:53 +01:00
x86/ibt: Add exact_endbr() helper
For when we want to exactly match ENDBR, and not everything that we can scribble it with.
--- arch/x86/kernel/alternative.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +13 -3
@@ -863,6 +863,17 @@ Efault:return false; } +static __noendbr bool exact_endbr(u32 *val) +{ +u32 endbr; + +__get_kernel_nofault (&endbr, val, u32, Efault); +return endbr ==gen_endbr (); + +Efault: +return false; +} +static void poison_cfi (void *addr); static void __init_or_module poison_endbr(void *addr)
@@ -1426,10 +1437,9 @@ static void poison_cfi(void *addr)bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud2; -u32 endbr, hash; +u32 hash; -__get_kernel_nofault (&endbr, addr, u32, Efault); -if (endbr !=gen_endbr ()) +if (!exact_endbr ((void *)addr))return false; *target = addr + fineibt_preamble_size;
[tip: x86/core] x86/ibt: Add exact_endbr() helper
In reply to Peter ZijlstraThe 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: 500a41acb05a973cb6826ee56df082a97e210a95 Gitweb: https://git.kernel.org/tip/500a41acb05a973cb6826ee56df082a97e210a95 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:05 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:11:18 +01:00
x86/ibt: Add exact_endbr() helper
For when we want to exactly match ENDBR, and not everything that we can scribble it with.
--- arch/x86/kernel/alternative.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +17 -3
@@ -863,6 +863,21 @@ Efault:return false; } +#ifdef CONFIG_FINEIBT + +static __noendbr bool exact_endbr(u32 *val) +{ +u32 endbr; + +__get_kernel_nofault (&endbr, val, u32, Efault); +return endbr ==gen_endbr (); + +Efault: +return false; +} + +#endif +static void poison_cfi (void *addr); static void __init_or_module poison_endbr(void *addr)
@@ -1426,10 +1441,9 @@ static void poison_cfi(void *addr)bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud2; -u32 endbr, hash; +u32 hash; -__get_kernel_nofault (&endbr, addr, u32, Efault); -if (endbr !=gen_endbr ()) +if (!exact_endbr ((void *)addr))return false; *target = addr + fineibt_preamble_size;
PATCH v4 3/10 x86/traps: Decode 0xEA #UD
In reply to Peter ZijlstraFineIBT will start using 0xEA as #UD
--- arch/x86/include/asm/bug.h | 1 + arch/x86/kernel/traps.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +1 -0
@@ -25,6 +25,7 @@#define BUG_UD2 0xfffe#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc +#define BUG_EA 0xffea#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +17 -3
@@ -96,6 +96,7 @@ __always_inline int is_valid_bugaddr(uns* Check for UD1 or UD2, accounting for Address Size Override Prefixes. * If it's a UD1, further decode to determine its use: * +* FineIBT: ea (bad) * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -113,6 +114,10 @@ __always_inline int decode_bug(unsigned v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); +if (v ==0xea ) { + *len = addr - start; +return BUG_EA ; + }if (v !=OPCODE_ESCAPE )return BUG_NONE ;
@@ -309,10 +314,16 @@ static noinstr bool handle_bug(struct ptswitch (ud_type) {case BUG_UD2 : -if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN || -handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { - regs->ip += ud_len; +if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN ) { + handled = true; +break ; + } + fallthrough; + +case BUG_EA : +if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true; +break ; }break ;
@@ -328,6 +339,9 @@ static noinstr bool handle_bug(struct ptbreak ; } +if (handled) + regs->ip += ud_len; +if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();instrumentation_end ();
Re: [PATCH v4 03/10] x86/traps: Decode 0xEA #UD
Quoted 3 lines · Peter Zijlstra
FineIBT will start using
0xEAas #UDSigned-off-by Peter Zijlstra (Intel) <[email protected]>
Yup, looks good.
Signature · 1 line
Kees Cook
[tip: x86/core] x86/traps: Decode 0xEA #UD
In reply to Peter ZijlstraThe 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: dbf3638d81bf009568de02bde9dc5fda9769e672 Gitweb: https://git.kernel.org/tip/dbf3638d81bf009568de02bde9dc5fda9769e672 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:06 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:54 +01:00
x86/traps: Decode 0xEA #UD
FineIBT will start using 0xEA as #UD
--- arch/x86/include/asm/bug.h | 1 + arch/x86/kernel/traps.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +1 -0
@@ -25,6 +25,7 @@#define BUG_UD2 0xfffe#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc +#define BUG_EA 0xffea#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +17 -3
@@ -96,6 +96,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr)* Check for UD1 or UD2, accounting for Address Size Override Prefixes. * If it's a UD1, further decode to determine its use: * +* FineIBT: ea (bad) * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -113,6 +114,10 @@ __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); +if (v ==0xea ) { + *len = addr - start; +return BUG_EA ; + }if (v !=OPCODE_ESCAPE )return BUG_NONE ;
@@ -309,10 +314,16 @@ static noinstr bool handle_bug(struct pt_regs *regs)switch (ud_type) {case BUG_UD2 : -if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN || -handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { - regs->ip += ud_len; +if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN ) { + handled = true; +break ; + } + fallthrough; + +case BUG_EA : +if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true; +break ; }break ;
@@ -328,6 +339,9 @@ static noinstr bool handle_bug(struct pt_regs *regs)break ; } +if (handled) + regs->ip += ud_len; +if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();instrumentation_end ();
[tip: x86/core] x86/traps: Decode 0xEA instructions as #UD
In reply to Peter ZijlstraThe 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: 2e044911be75ce3321c5b3d10205ac0b54f8cb92 Gitweb: https://git.kernel.org/tip/2e044911be75ce3321c5b3d10205ac0b54f8cb92 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:06 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:22:10 +01:00
x86/traps: Decode 0xEA instructions as #UD
FineIBT will start using 0xEA as #UD. Normally '0xEA' is a 'bad', invalid instruction for the CPU.
--- arch/x86/include/asm/bug.h | 1 + arch/x86/kernel/traps.c | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +1 -0
@@ -25,6 +25,7 @@#define BUG_UD2 0xfffe#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc +#define BUG_EA 0xffea#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +17 -3
@@ -96,6 +96,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr)* Check for UD1 or UD2, accounting for Address Size Override Prefixes. * If it's a UD1, further decode to determine its use: * +* FineIBT: ea (bad) * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -113,6 +114,10 @@ __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); +if (v ==0xea ) { + *len = addr - start; +return BUG_EA ; + }if (v !=OPCODE_ESCAPE )return BUG_NONE ;
@@ -309,10 +314,16 @@ static noinstr bool handle_bug(struct pt_regs *regs)switch (ud_type) {case BUG_UD2 : -if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN || -handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { - regs->ip += ud_len; +if (report_bug (regs->ip, regs) ==BUG_TRAP_TYPE_WARN ) { + handled = true; +break ; + } + fallthrough; + +case BUG_EA : +if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true; +break ; }break ;
@@ -328,6 +339,9 @@ static noinstr bool handle_bug(struct pt_regs *regs)break ; } +if (handled) + regs->ip += ud_len; +if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();instrumentation_end ();
PATCH v4 4/10 x86/traps: Allow custom fixups in handle_bug()
In reply to Peter ZijlstraThe normal fixup in handle_bug() is simply continuing at the next instruction. However upcomming patches make this the wrong thing, so allow handlers (specifically handle_cfi_failure()) to over-ride regs->ip.
The callchain is such that the fixup needs to be done before it is determined if the exception is fatal, as such, revert any changes in that case.
Additinoally, have handle_cfi_failure() remember the regs->ip value it starts with for reporting.
--- arch/x86/kernel/cfi.c | 8 ++++---- arch/x86/kernel/traps.c | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kernel/cfi.c +4 -4
@@ -67,16 +67,16 @@ static bool decode_cfi_insn(struct pt_re*/ enum bug_trap_type handle_cfi_failure (struct pt_regs *regs) { -unsigned long target; +unsigned long target, addr = regs->ip;u32 type;switch (cfi_mode) {case CFI_KCFI : -if (!is_cfi_trap (regs->ip)) +if (!is_cfi_trap (addr))return BUG_TRAP_TYPE_NONE ;if (!decode_cfi_insn (regs, &target, &type)) -return report_cfi_failure_noaddr (regs, regs->ip); +return report_cfi_failure_noaddr (regs, addr);break ;
@@ -90,7 +90,7 @@ enum bug_trap_type handle_cfi_failure(streturn BUG_TRAP_TYPE_NONE ; } -return report_cfi_failure (regs, regs->ip, &target, type); +return report_cfi_failure (regs, addr, &target, type); }/*
arch/x86/kernel/traps.c +13 -3
@@ -287,11 +287,12 @@ static inline void handle_invalid_op(str static noinstr bool handle_bug(struct pt_regs *regs) { +unsigned long addr = regs->ip;bool handled = false;int ud_type, ud_len;s32 ud_imm; - ud_type =decode_bug (regs->ip, &ud_imm, &ud_len); + ud_type =decode_bug (addr, &ud_imm, &ud_len);if (ud_type ==BUG_NONE )return handled;
@@ -339,8 +340,17 @@ static noinstr bool handle_bug(struct ptbreak ; } -if (handled) - regs->ip += ud_len; +/* +* When continuing, and regs->ip hasn't changed, move it to the next +* instruction. When not continuing execution, restore the instruction +* pointer. +*/ +if (handled) { +if (regs->ip == addr) + regs->ip += ud_len; + }else { + regs->ip = addr; + }if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();
Re: [PATCH v4 04/10] x86/traps: Allow custom fixups in handle_bug()
Quoted 10 lines · Peter Zijlstra
The normal fixup in
handle_bug()is simply continuing at the next instruction. However upcomming patches make this the wrong thing, so allow handlers (specificallyhandle_cfi_failure()) to over-rideregs->ip.The callchain is such that the fixup needs to be done before it is determined if the exception is fatal, as such, revert any changes in that case.
Additinoally, have
handle_cfi_failure()remember theregs->ipvalue it
typo above, if you want to tweak that in your tree.
Quoted 3 lines
starts with for reporting.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Thanks for the added comment, my future brain will appreciate it. :)
Signature · 1 line
Kees Cook
Re: [PATCH v4 04/10] x86/traps: Allow custom fixups in handle_bug()
Quoted 6 lines · Kees Cook
Quoted 5 lines · Peter Zijlstra
The normal fixup in
handle_bug()is simply continuing at the next instruction. However upcomming patches make this the wrong thing, so allow handlers (specificallyhandle_cfi_failure()) to over-rideregs->ip.
Quoted 7 lines
Quoted 5 lines
The callchain is such that the fixup needs to be done before it is determined if the exception is fatal, as such, revert any changes in that case.
Additinoally, have
handle_cfi_failure()remember theregs->ipvalue ittypo above, if you want to tweak that in your tree.
Typing is forever hard :-)
Quoted 7 lines
Quoted 3 lines
starts with for reporting.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Thanks for the added comment, my future brain will appreciate it. :)
Reviewed-by Kees Cook <[email protected]>
Thanks!
[tip: x86/core] x86/traps: Allow custom fixups in handle_bug()
In reply to Peter ZijlstraThe 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: 5d28fddb66e672e3183716a156cae04597599d3b Gitweb: https://git.kernel.org/tip/5d28fddb66e672e3183716a156cae04597599d3b Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:07 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:54 +01:00
x86/traps: Allow custom fixups in handle_bug()
The normal fixup in handle_bug() is simply continuing at the next instruction. However upcomming patches make this the wrong thing, so allow handlers (specifically handle_cfi_failure()) to over-ride regs->ip.
The callchain is such that the fixup needs to be done before it is determined if the exception is fatal, as such, revert any changes in that case.
Additionally, have handle_cfi_failure() remember the regs->ip value it starts with for reporting.
--- arch/x86/kernel/cfi.c | 8 ++++---- arch/x86/kernel/traps.c | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kernel/cfi.c +4 -4
@@ -67,16 +67,16 @@ static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,*/ enum bug_trap_type handle_cfi_failure (struct pt_regs *regs) { -unsigned long target; +unsigned long target, addr = regs->ip;u32 type;switch (cfi_mode) {case CFI_KCFI : -if (!is_cfi_trap (regs->ip)) +if (!is_cfi_trap (addr))return BUG_TRAP_TYPE_NONE ;if (!decode_cfi_insn (regs, &target, &type)) -return report_cfi_failure_noaddr (regs, regs->ip); +return report_cfi_failure_noaddr (regs, addr);break ;
@@ -90,7 +90,7 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)return BUG_TRAP_TYPE_NONE ; } -return report_cfi_failure (regs, regs->ip, &target, type); +return report_cfi_failure (regs, addr, &target, type); }/*
arch/x86/kernel/traps.c +13 -3
@@ -287,11 +287,12 @@ static inline void handle_invalid_op(struct pt_regs *regs) static noinstr bool handle_bug(struct pt_regs *regs) { +unsigned long addr = regs->ip;bool handled = false;int ud_type, ud_len;s32 ud_imm; - ud_type =decode_bug (regs->ip, &ud_imm, &ud_len); + ud_type =decode_bug (addr, &ud_imm, &ud_len);if (ud_type ==BUG_NONE )return handled;
@@ -339,8 +340,17 @@ static noinstr bool handle_bug(struct pt_regs *regs)break ; } -if (handled) - regs->ip += ud_len; +/* +* When continuing, and regs->ip hasn't changed, move it to the next +* instruction. When not continuing execution, restore the instruction +* pointer. +*/ +if (handled) { +if (regs->ip == addr) + regs->ip += ud_len; + }else { + regs->ip = addr; + }if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();
[tip: x86/core] x86/traps: Allow custom fixups in handle_bug()
In reply to Peter ZijlstraThe 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: e33d805a1005bf7b8a5a53559c81a8e17f0b981b Gitweb: https://git.kernel.org/tip/e33d805a1005bf7b8a5a53559c81a8e17f0b981b Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:07 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:22:39 +01:00
x86/traps: Allow custom fixups in handle_bug()
The normal fixup in handle_bug() is simply continuing at the next instruction. However upcoming patches make this the wrong thing, so allow handlers (specifically handle_cfi_failure()) to over-ride regs->ip.
The callchain is such that the fixup needs to be done before it is determined if the exception is fatal, as such, revert any changes in that case.
Additionally, have handle_cfi_failure() remember the regs->ip value it starts with for reporting.
--- arch/x86/kernel/cfi.c | 8 ++++---- arch/x86/kernel/traps.c | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kernel/cfi.c +4 -4
@@ -67,16 +67,16 @@ static bool decode_cfi_insn(struct pt_regs *regs, unsigned long *target,*/ enum bug_trap_type handle_cfi_failure (struct pt_regs *regs) { -unsigned long target; +unsigned long target, addr = regs->ip;u32 type;switch (cfi_mode) {case CFI_KCFI : -if (!is_cfi_trap (regs->ip)) +if (!is_cfi_trap (addr))return BUG_TRAP_TYPE_NONE ;if (!decode_cfi_insn (regs, &target, &type)) -return report_cfi_failure_noaddr (regs, regs->ip); +return report_cfi_failure_noaddr (regs, addr);break ;
@@ -90,7 +90,7 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *regs)return BUG_TRAP_TYPE_NONE ; } -return report_cfi_failure (regs, regs->ip, &target, type); +return report_cfi_failure (regs, addr, &target, type); }/*
arch/x86/kernel/traps.c +13 -3
@@ -287,11 +287,12 @@ static inline void handle_invalid_op(struct pt_regs *regs) static noinstr bool handle_bug(struct pt_regs *regs) { +unsigned long addr = regs->ip;bool handled = false;int ud_type, ud_len;s32 ud_imm; - ud_type =decode_bug (regs->ip, &ud_imm, &ud_len); + ud_type =decode_bug (addr, &ud_imm, &ud_len);if (ud_type ==BUG_NONE )return handled;
@@ -339,8 +340,17 @@ static noinstr bool handle_bug(struct pt_regs *regs)break ; } -if (handled) - regs->ip += ud_len; +/* +* When continuing, and regs->ip hasn't changed, move it to the next +* instruction. When not continuing execution, restore the instruction +* pointer. +*/ +if (handled) { +if (regs->ip == addr) + regs->ip += ud_len; + }else { + regs->ip = addr; + }if (regs->flags &X86_EFLAGS_IF )raw_local_irq_disable ();
PATCH v4 5/10 x86/ibt: Optimize FineIBT sequence
In reply to Peter ZijlstraScott notes that non-taken branches are faster. Abuse overlapping code that traps instead of explicit UD2 instructions.
And LEA does not modify flags and will have less dependencies.
--- arch/x86/kernel/alternative.c | 61 +++++++++++++++++++++++++++--------------- arch/x86/net/bpf_jit_comp.c | 5 +-- 2 files changed, 42 insertions(+), 24 deletions(-)
arch/x86/kernel/alternative.c +40 -21
@@ -1053,9 +1053,9 @@ early_param("cfi", cfi_parse_cmdline);* __cfi_\func: __cfi_\func: * movl $0x12345678,%eax // 5 endbr64 // 4 * nop subl $0x12345678,%r10d // 7 -* nop jz 1f // 2 -* nop ud2 // 2 -* nop 1: nop // 1 +* nop jne __cfi_\func+6 // 2 +* nop nop3 // 3 +* nop * nop * nop * nop
@@ -1067,37 +1067,50 @@ early_param("cfi", cfi_parse_cmdline);* * caller: caller: * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6 -* addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4 +* addl $-15(%r11),%r10d // 4 lea -0x10(%r11),%r11 // 4 * je 1f // 2 nop4 // 4 * ud2 // 2 -* 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5 +* 1: cs call __x86_indirect_thunk_r11 // 6 call *%r11; nop3; // 6 * */ -asm(".pushsection .rodata \n" -"fineibt_preamble_start: \n" -" endbr64 \n" -" subl $0x12345678, %r10d \n" -" je fineibt_preamble_end \n" -"fineibt_preamble_ud2: \n" -" ud2 \n" -" nop \n" -"fineibt_preamble_end: \n" +/* +* <fineibt_preamble_start>: +* 0: f3 0f 1e fa endbr64 +* 4: 41 81 <ea> 78 56 34 12 sub $0x12345678, %r10d +* b: 75 f9 jne 6 <fineibt_preamble_start+0x6> +* d: 0f 1f 00 nopl (%rax) +* +* Note that the JNE target is the 0xEA byte inside the SUB, this decodes as +* (bad) on x86_64 and raises #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_preamble_start: \n" +" endbr64 \n" +" subl $0x12345678, %r10d \n" +" jne fineibt_preamble_start+6 \n" +ASM_NOP3 +"fineibt_preamble_end: \n" ".popsection\n" );extern u8 fineibt_preamble_start[]; -extern u8 fineibt_preamble_ud2[];extern u8 fineibt_preamble_end[];#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start) -#define fineibt_preamble_ud2 (fineibt_preamble_ud2 - fineibt_preamble_start) +#define fineibt_preamble_ud 6#define fineibt_preamble_hash 7 +/* +* <fineibt_caller_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 4d 8d 5b f0 lea -0x10(%r11), %r11 +* a: 0f 1f 40 00 nopl 0x0(%rax) +*/ asm(".pushsection .rodata \n" "fineibt_caller_start: \n" " movl $0x12345678, %r10d \n" -" sub $16, %r11 \n" +" lea -0x10(%r11), %r11 \n" ASM_NOP4 "fineibt_caller_end: \n" ".popsection \n"
@@ -1428,15 +1441,15 @@ static void poison_cfi(void *addr) }/* -* regs->ip points to a UD2 instruction, return true and fill out target and -* type when this UD2 is from a FineIBT preamble. +* When regs->ip points to a 0xEA byte in the FineIBT preamble, +* return true and fill out target and type. * * We check the preamble by checking for the ENDBR instruction relative to the -* UD2 instruction. +* 0xEA instruction. */ bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) { -unsigned long addr = regs->ip - fineibt_preamble_ud2; +unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;if (!exact_endbr ((void *)addr))
@@ -1447,6 +1460,12 @@ bool decode_fineibt_insn(struct pt_regs__get_kernel_nofault (&hash, addr + fineibt_preamble_hash, u32, Efault); *type = (u32 )regs->r10 + hash; +/* +* Since regs->ip points to the middle of an instruction; it cannot +* continue with the normal fixup. +*/ + regs->ip = *target; +return true; Efault:
arch/x86/net/bpf_jit_comp.c +2 -3
@@ -417,9 +417,8 @@ static void emit_fineibt(u8 **pprog, u32EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x74 ,0x07 );/* jz.d8 +7 */ -EMIT2 (0x0f ,0x0b );/* ud2 */ -EMIT1 (0x90 );/* nop */ +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ EMIT_ENDBR_POISON (); *pprog = prog;
[tip: x86/core] x86/ibt: Optimize FineIBT sequence
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: 7359ea8e1b7fbd5b98cb72a9f596ddf67f2dc33b Gitweb: https://git.kernel.org/tip/7359ea8e1b7fbd5b98cb72a9f596ddf67f2dc33b Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:08 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:54 +01:00
x86/ibt: Optimize FineIBT sequence
Scott notes that non-taken branches are faster. Abuse overlapping code that traps instead of explicit UD2 instructions.
And LEA does not modify flags and will have less dependencies.
--- arch/x86/kernel/alternative.c | 61 ++++++++++++++++++++++------------ arch/x86/net/bpf_jit_comp.c | 5 +-- 2 files changed, 42 insertions(+), 24 deletions(-)
arch/x86/kernel/alternative.c +40 -21
@@ -1053,9 +1053,9 @@ early_param("cfi", cfi_parse_cmdline);* __cfi_\func: __cfi_\func: * movl $0x12345678,%eax // 5 endbr64 // 4 * nop subl $0x12345678,%r10d // 7 -* nop jz 1f // 2 -* nop ud2 // 2 -* nop 1: nop // 1 +* nop jne __cfi_\func+6 // 2 +* nop nop3 // 3 +* nop * nop * nop * nop
@@ -1067,37 +1067,50 @@ early_param("cfi", cfi_parse_cmdline);* * caller: caller: * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6 -* addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4 +* addl $-15(%r11),%r10d // 4 lea -0x10(%r11),%r11 // 4 * je 1f // 2 nop4 // 4 * ud2 // 2 -* 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5 +* 1: cs call __x86_indirect_thunk_r11 // 6 call *%r11; nop3; // 6 * */ -asm(".pushsection .rodata \n" -"fineibt_preamble_start: \n" -" endbr64 \n" -" subl $0x12345678, %r10d \n" -" je fineibt_preamble_end \n" -"fineibt_preamble_ud2: \n" -" ud2 \n" -" nop \n" -"fineibt_preamble_end: \n" +/* +* <fineibt_preamble_start>: +* 0: f3 0f 1e fa endbr64 +* 4: 41 81 <ea> 78 56 34 12 sub $0x12345678, %r10d +* b: 75 f9 jne 6 <fineibt_preamble_start+0x6> +* d: 0f 1f 00 nopl (%rax) +* +* Note that the JNE target is the 0xEA byte inside the SUB, this decodes as +* (bad) on x86_64 and raises #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_preamble_start: \n" +" endbr64 \n" +" subl $0x12345678, %r10d \n" +" jne fineibt_preamble_start+6 \n" +ASM_NOP3 +"fineibt_preamble_end: \n" ".popsection\n" );extern u8 fineibt_preamble_start[]; -extern u8 fineibt_preamble_ud2[];extern u8 fineibt_preamble_end[];#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start) -#define fineibt_preamble_ud2 (fineibt_preamble_ud2 - fineibt_preamble_start) +#define fineibt_preamble_ud 6#define fineibt_preamble_hash 7 +/* +* <fineibt_caller_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 4d 8d 5b f0 lea -0x10(%r11), %r11 +* a: 0f 1f 40 00 nopl 0x0(%rax) +*/ asm(".pushsection .rodata \n" "fineibt_caller_start: \n" " movl $0x12345678, %r10d \n" -" sub $16, %r11 \n" +" lea -0x10(%r11), %r11 \n" ASM_NOP4 "fineibt_caller_end: \n" ".popsection \n"
@@ -1428,15 +1441,15 @@ static void poison_cfi(void *addr) }/* -* regs->ip points to a UD2 instruction, return true and fill out target and -* type when this UD2 is from a FineIBT preamble. +* When regs->ip points to a 0xEA byte in the FineIBT preamble, +* return true and fill out target and type. * * We check the preamble by checking for the ENDBR instruction relative to the -* UD2 instruction. +* 0xEA instruction. */ bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) { -unsigned long addr = regs->ip - fineibt_preamble_ud2; +unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;if (!exact_endbr ((void *)addr))
@@ -1447,6 +1460,12 @@ bool decode_fineibt_insn(struct pt_regs *regs, unsigned long *target, u32 *type)__get_kernel_nofault (&hash, addr + fineibt_preamble_hash, u32, Efault); *type = (u32 )regs->r10 + hash; +/* +* Since regs->ip points to the middle of an instruction; it cannot +* continue with the normal fixup. +*/ + regs->ip = *target; +return true; Efault:
arch/x86/net/bpf_jit_comp.c +2 -3
@@ -417,9 +417,8 @@ static void emit_fineibt(u8 **pprog, u32 hash)EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x74 ,0x07 );/* jz.d8 +7 */ -EMIT2 (0x0f ,0x0b );/* ud2 */ -EMIT1 (0x90 );/* nop */ +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ EMIT_ENDBR_POISON (); *pprog = prog;
[tip: x86/core] x86/ibt: Optimize the FineIBT instruction sequence
In reply to Peter ZijlstraThe 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: 06926c6cdb955bf24521d4e13af95b4d062d02d0 Gitweb: https://git.kernel.org/tip/06926c6cdb955bf24521d4e13af95b4d062d02d0 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:08 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:24:09 +01:00
x86/ibt: Optimize the FineIBT instruction sequence
Scott notes that non-taken branches are faster. Abuse overlapping code that traps instead of explicit UD2 instructions.
And LEA does not modify flags and will have less dependencies.
--- arch/x86/kernel/alternative.c | 61 ++++++++++++++++++++++------------ arch/x86/net/bpf_jit_comp.c | 5 +-- 2 files changed, 42 insertions(+), 24 deletions(-)
arch/x86/kernel/alternative.c +40 -21
@@ -1057,9 +1057,9 @@ early_param("cfi", cfi_parse_cmdline);* __cfi_\func: __cfi_\func: * movl $0x12345678,%eax // 5 endbr64 // 4 * nop subl $0x12345678,%r10d // 7 -* nop jz 1f // 2 -* nop ud2 // 2 -* nop 1: nop // 1 +* nop jne __cfi_\func+6 // 2 +* nop nop3 // 3 +* nop * nop * nop * nop
@@ -1071,37 +1071,50 @@ early_param("cfi", cfi_parse_cmdline);* * caller: caller: * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6 -* addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4 +* addl $-15(%r11),%r10d // 4 lea -0x10(%r11),%r11 // 4 * je 1f // 2 nop4 // 4 * ud2 // 2 -* 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5 +* 1: cs call __x86_indirect_thunk_r11 // 6 call *%r11; nop3; // 6 * */ -asm(".pushsection .rodata \n" -"fineibt_preamble_start: \n" -" endbr64 \n" -" subl $0x12345678, %r10d \n" -" je fineibt_preamble_end \n" -"fineibt_preamble_ud2: \n" -" ud2 \n" -" nop \n" -"fineibt_preamble_end: \n" +/* +* <fineibt_preamble_start>: +* 0: f3 0f 1e fa endbr64 +* 4: 41 81 <ea> 78 56 34 12 sub $0x12345678, %r10d +* b: 75 f9 jne 6 <fineibt_preamble_start+0x6> +* d: 0f 1f 00 nopl (%rax) +* +* Note that the JNE target is the 0xEA byte inside the SUB, this decodes as +* (bad) on x86_64 and raises #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_preamble_start: \n" +" endbr64 \n" +" subl $0x12345678, %r10d \n" +" jne fineibt_preamble_start+6 \n" +ASM_NOP3 +"fineibt_preamble_end: \n" ".popsection\n" );extern u8 fineibt_preamble_start[]; -extern u8 fineibt_preamble_ud2[];extern u8 fineibt_preamble_end[];#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start) -#define fineibt_preamble_ud2 (fineibt_preamble_ud2 - fineibt_preamble_start) +#define fineibt_preamble_ud 6#define fineibt_preamble_hash 7 +/* +* <fineibt_caller_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 4d 8d 5b f0 lea -0x10(%r11), %r11 +* a: 0f 1f 40 00 nopl 0x0(%rax) +*/ asm(".pushsection .rodata \n" "fineibt_caller_start: \n" " movl $0x12345678, %r10d \n" -" sub $16, %r11 \n" +" lea -0x10(%r11), %r11 \n" ASM_NOP4 "fineibt_caller_end: \n" ".popsection \n"
@@ -1432,15 +1445,15 @@ static void poison_cfi(void *addr) }/* -* regs->ip points to a UD2 instruction, return true and fill out target and -* type when this UD2 is from a FineIBT preamble. +* When regs->ip points to a 0xEA byte in the FineIBT preamble, +* return true and fill out target and type. * * We check the preamble by checking for the ENDBR instruction relative to the -* UD2 instruction. +* 0xEA instruction. */ bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) { -unsigned long addr = regs->ip - fineibt_preamble_ud2; +unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;if (!exact_endbr ((void *)addr))
@@ -1451,6 +1464,12 @@ bool decode_fineibt_insn(struct pt_regs *regs, unsigned long *target, u32 *type)__get_kernel_nofault (&hash, addr + fineibt_preamble_hash, u32, Efault); *type = (u32 )regs->r10 + hash; +/* +* Since regs->ip points to the middle of an instruction; it cannot +* continue with the normal fixup. +*/ + regs->ip = *target; +return true; Efault:
arch/x86/net/bpf_jit_comp.c +2 -3
@@ -417,9 +417,8 @@ static void emit_fineibt(u8 **pprog, u32 hash)EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x74 ,0x07 );/* jz.d8 +7 */ -EMIT2 (0x0f ,0x0b );/* ud2 */ -EMIT1 (0x90 );/* nop */ +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ EMIT_ENDBR_POISON (); *pprog = prog;
PATCH v4 6/10 x86/traps: Decode LOCK Jcc.d8 #UD
In reply to Peter ZijlstraBecause overlapping code sequences are all the rage.
--- arch/x86/include/asm/bug.h | 2 ++ arch/x86/kernel/traps.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +2 -0
@@ -17,6 +17,7 @@* In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. */ #define INSN_ASOP 0x67 +#define INSN_LOCK 0xf0#define OPCODE_ESCAPE 0x0f#define SECOND_BYTE_OPCODE_UD1 0xb9#define SECOND_BYTE_OPCODE_UD2 0x0b
@@ -26,6 +27,7 @@#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc#define BUG_EA 0xffea +#define BUG_LOCK 0xfff0#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +23 -3
@@ -97,6 +97,7 @@ __always_inline int is_valid_bugaddr(uns* If it's a UD1, further decode to determine its use: * * FineIBT: ea (bad) +* FineIBT: 0f 75 f9 lock jne . - 6 * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -106,6 +107,7 @@ __always_inline int is_valid_bugaddr(uns __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) {unsigned long start = addr; +bool lock = false;u8 v;if (addr <TASK_SIZE_MAX )
@@ -114,12 +116,29 @@ __always_inline int decode_bug(unsigned v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); -if (v ==0xea ) { + +if (v ==INSN_LOCK ) { + lock = true; + v = *(u8 *)(addr++); + } + +switch (v) { + case 0x70 ... 0x7f: /* Jcc.d8 */ + addr +=1 ;/* d8 */ + *len = addr - start; +WARN_ON_ONCE (!lock); +return BUG_LOCK ; + +case 0xea : *len = addr - start;return BUG_EA ; - } -if (v !=OPCODE_ESCAPE ) + +case OPCODE_ESCAPE : +break ; + +default :return BUG_NONE ; + } v = *(u8 *)(addr++);if (v ==SECOND_BYTE_OPCODE_UD2 ) {
@@ -322,6 +341,7 @@ static noinstr bool handle_bug(struct pt fallthrough;case BUG_EA : +case BUG_LOCK :if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true;break ;
Re: [PATCH v4 06/10] x86/traps: Decode LOCK Jcc.d8 #UD
Quoted 30 lines · On Mon, 24 Feb 2025 13:37:09 +0100 Peter Zijlstra <[email protected]> wrote:
Because overlapping code sequences are all the rage.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Reviewed-by Kees Cook <[email protected]>--- arch/x86/include/asm/bug.h | 2 ++ arch/x86/kernel/traps.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)arch/x86/include/asm/bug.h +2
-0@@ -17,6 +17,7 @@* In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. */ #define INSN_ASOP 0x67 +#define INSN_LOCK 0xf0#define OPCODE_ESCAPE 0x0f#define SECOND_BYTE_OPCODE_UD1 0xb9#define SECOND_BYTE_OPCODE_UD2 0x0b@@ -26,6 +27,7 @@#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc#define BUG_EA 0xffea +#define BUG_LOCK 0xfff0#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c __always_inline int is_valid_bugaddr(uns · line 100
Quoted 4 lines
arch/x86/kernel/traps.c +1
-0@@ -97,6 +97,7 @@ __always_inline int is_valid_bugaddr(uns* If it's a UD1, further decode to determine its use: * * FineIBT: ea (bad) +* FineIBT: 0f 75 f9 lock jne . - 6
^^ nibble swapped
David
Quoted 55 lines
arch/x86/kernel/traps.c +22
-3* UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx @@ -106,6 +107,7 @@ __always_inline int is_valid_bugaddr(uns __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) {unsigned long start = addr; +bool lock = false;u8 v;if (addr <TASK_SIZE_MAX )@@ -114,12 +116,29 @@ __always_inline int decode_bug(unsigned v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); -if (v ==0xea ) { + +if (v ==INSN_LOCK ) { + lock = true; + v = *(u8 *)(addr++); + } + +switch (v) { + case 0x70 ... 0x7f: /* Jcc.d8 */ + addr +=1 ;/* d8 */ + *len = addr - start; +WARN_ON_ONCE (!lock); +return BUG_LOCK ; + +case 0xea : *len = addr - start;return BUG_EA ; - } -if (v !=OPCODE_ESCAPE ) + +case OPCODE_ESCAPE : +break ; + +default :return BUG_NONE ; + } v = *(u8 *)(addr++);if (v ==SECOND_BYTE_OPCODE_UD2 ) {@@ -322,6 +341,7 @@ static noinstr bool handle_bug(struct pt fallthrough;case BUG_EA : +case BUG_LOCK :if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true;break ;
Kees Cook
maintainerRe: [PATCH v4 06/10] x86/traps: Decode LOCK Jcc.d8 #UD
Quoted 30 lines · David Laight
Quoted 27 lines · On Mon, 24 Feb 2025 13:37:09 +0100 Peter Zijlstra <[email protected]> wrote:
Because overlapping code sequences are all the rage.
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Reviewed-by Kees Cook <[email protected]>--- arch/x86/include/asm/bug.h | 2 ++ arch/x86/kernel/traps.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)arch/x86/include/asm/bug.h +2
-0@@ -17,6 +17,7 @@* In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. */ #define INSN_ASOP 0x67 +#define INSN_LOCK 0xf0#define OPCODE_ESCAPE 0x0f#define SECOND_BYTE_OPCODE_UD1 0xb9#define SECOND_BYTE_OPCODE_UD2 0x0b@@ -26,6 +27,7 @@#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc#define BUG_EA 0xffea +#define BUG_LOCK 0xfff0#ifdef CONFIG_GENERIC_BUG
Quoted 8 lines
arch/x86/kernel/traps.c __always_inline int is_valid_bugaddr(uns · line 100
Quoted 7 lines
arch/x86/kernel/traps.c +1
-0@@ -97,6 +97,7 @@ __always_inline int is_valid_bugaddr(uns* If it's a UD1, further decode to determine its use: * * FineIBT: ea (bad) +* FineIBT: 0f 75 f9 lock jne . - 6 ^^ nibble swapped
Oh, good catch!
Signature · 1 line
Kees Cook
[tip: x86/core] x86/traps: Decode LOCK Jcc.d8 #UD
In reply to Peter ZijlstraThe 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: e874854d308841f2383ee3d04f0438f56ddddaaf Gitweb: https://git.kernel.org/tip/e874854d308841f2383ee3d04f0438f56ddddaaf Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:09 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:55 +01:00
x86/traps: Decode LOCK Jcc.d8 #UD
Because overlapping code sequences are all the rage.
--- arch/x86/include/asm/bug.h | 2 ++ arch/x86/kernel/traps.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +2 -0
@@ -17,6 +17,7 @@* In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. */ #define INSN_ASOP 0x67 +#define INSN_LOCK 0xf0#define OPCODE_ESCAPE 0x0f#define SECOND_BYTE_OPCODE_UD1 0xb9#define SECOND_BYTE_OPCODE_UD2 0x0b
@@ -26,6 +27,7 @@#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc#define BUG_EA 0xffea +#define BUG_LOCK 0xfff0#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +23 -3
@@ -97,6 +97,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr)* If it's a UD1, further decode to determine its use: * * FineIBT: ea (bad) +* FineIBT: f0 75 f9 lock jne . - 6 * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -106,6 +107,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr) __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) {unsigned long start = addr; +bool lock = false;u8 v;if (addr <TASK_SIZE_MAX )
@@ -114,12 +116,29 @@ __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); -if (v ==0xea ) { + +if (v ==INSN_LOCK ) { + lock = true; + v = *(u8 *)(addr++); + } + +switch (v) { + case 0x70 ... 0x7f: /* Jcc.d8 */ + addr +=1 ;/* d8 */ + *len = addr - start; +WARN_ON_ONCE (!lock); +return BUG_LOCK ; + +case 0xea : *len = addr - start;return BUG_EA ; - } -if (v !=OPCODE_ESCAPE ) + +case OPCODE_ESCAPE : +break ; + +default :return BUG_NONE ; + } v = *(u8 *)(addr++);if (v ==SECOND_BYTE_OPCODE_UD2 ) {
@@ -322,6 +341,7 @@ static noinstr bool handle_bug(struct pt_regs *regs) fallthrough;case BUG_EA : +case BUG_LOCK :if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true;break ;
[tip: x86/core] x86/traps: Decode LOCK Jcc.d8 as #UD
In reply to Peter ZijlstraThe 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: 029f718fedd72872f7475604fe71b2a841108834 Gitweb: https://git.kernel.org/tip/029f718fedd72872f7475604fe71b2a841108834 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:09 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:24:17 +01:00
x86/traps: Decode LOCK Jcc.d8 as #UD
Because overlapping code sequences are all the rage.
--- arch/x86/include/asm/bug.h | 2 ++ arch/x86/kernel/traps.c | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)
arch/x86/include/asm/bug.h +2 -0
@@ -17,6 +17,7 @@* In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit. */ #define INSN_ASOP 0x67 +#define INSN_LOCK 0xf0#define OPCODE_ESCAPE 0x0f#define SECOND_BYTE_OPCODE_UD1 0xb9#define SECOND_BYTE_OPCODE_UD2 0x0b
@@ -26,6 +27,7 @@#define BUG_UD1 0xfffd#define BUG_UD1_UBSAN 0xfffc#define BUG_EA 0xffea +#define BUG_LOCK 0xfff0#ifdef CONFIG_GENERIC_BUG
arch/x86/kernel/traps.c +23 -3
@@ -97,6 +97,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr)* If it's a UD1, further decode to determine its use: * * FineIBT: ea (bad) +* FineIBT: f0 75 f9 lock jne . - 6 * UBSan{0}: 67 0f b9 00 ud1 (%eax),%eax * UBSan{10}: 67 0f b9 40 10 ud1 0x10(%eax),%eax * static_call: 0f b9 cc ud1 %esp,%ecx
@@ -106,6 +107,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr) __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) {unsigned long start = addr; +bool lock = false;u8 v;if (addr <TASK_SIZE_MAX )
@@ -114,12 +116,29 @@ __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len) v = *(u8 *)(addr++);if (v ==INSN_ASOP ) v = *(u8 *)(addr++); -if (v ==0xea ) { + +if (v ==INSN_LOCK ) { + lock = true; + v = *(u8 *)(addr++); + } + +switch (v) { + case 0x70 ... 0x7f: /* Jcc.d8 */ + addr +=1 ;/* d8 */ + *len = addr - start; +WARN_ON_ONCE (!lock); +return BUG_LOCK ; + +case 0xea : *len = addr - start;return BUG_EA ; - } -if (v !=OPCODE_ESCAPE ) + +case OPCODE_ESCAPE : +break ; + +default :return BUG_NONE ; + } v = *(u8 *)(addr++);if (v ==SECOND_BYTE_OPCODE_UD2 ) {
@@ -322,6 +341,7 @@ static noinstr bool handle_bug(struct pt_regs *regs) fallthrough;case BUG_EA : +case BUG_LOCK :if (handle_cfi_failure (regs) ==BUG_TRAP_TYPE_WARN ) { handled = true;break ;
PATCH v4 7/10 x86/ibt: Add paranoid FineIBT mode
In reply to Peter ZijlstraDue to concerns about circumvention attacks against FineIBT on 'naked' ENDBR, add an additional caller side hash check to FineIBT. This should make it impossible to pivot over such a 'naked' ENDBR instruction at the cost of an additional load.
The specific pivot reported was against the SYSCALL entry site and FRED will have all those holes fixed up.
https://lore.kernel.org/linux-hardening/Z60NwR4w%2F28Z7XUa@ubun/
This specific fineibt_paranoid_start[] sequence was concocted by Scott.
--- arch/x86/kernel/alternative.c | 144 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 6 deletions(-)
arch/x86/kernel/alternative.c +138 -6
@@ -741,6 +741,11 @@ void __init_or_module noinline apply_ret op2 = insn.opcode.bytes[1 ];switch (op1) { + case 0x70 ... 0x7f: /* Jcc.d8 */ +/* See cfi_paranoid. */ +WARN_ON_ONCE (cfi_mode !=CFI_FINEIBT ); +continue ; +case CALL_INSN_OPCODE :case JMP32_INSN_OPCODE :break ;
@@ -994,6 +999,8 @@ u32 cfi_get_func_hash(void *func)static bool cfi_rand__ro_after_init = true;static u32 cfi_seed__ro_after_init ; +static bool cfi_paranoid__ro_after_init = false; +/* * Re-hash the CFI hash with a boot-time seed while making sure the result is * not a valid ENDBR instruction.
@@ -1036,6 +1043,12 @@ static __init int cfi_parse_cmdline(char }else if (!strcmp (str,"warn" )) {pr_alert ("CFI mismatch non-fatal!\n" ); cfi_warn = true; + }else if (!strcmp (str,"paranoid" )) { +if (cfi_mode ==CFI_FINEIBT ) { + cfi_paranoid = true; + }else { +pr_err ("Ignoring paranoid; depends on fineibt.\n" ); + } }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
@@ -1124,6 +1137,52 @@ extern u8 fineibt_caller_end[];#define fineibt_caller_jmp (fineibt_caller_size - 2) +/* +* Since FineIBT does hash validation on the callee side it is prone to +* circumvention attacks where a 'naked' ENDBR instruction exists that +* is not part of the fineibt_preamble sequence. +* +* Notably the x86 entry points must be ENDBR and equally cannot be +* fineibt_preamble. +* +* The fineibt_paranoid caller sequence adds additional caller side +* hash validation. This stops such circumvention attacks dead, but at the cost +* of adding a load. +* +* <fineibt_paranoid_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d +* a: 4d 8d 5b <f0> lea -0x10(%r11), %r11 +* e: 75 fd jne d <fineibt_paranoid_start+0xd> +* 10: 41 ff d3 call *%r11 +* 13: 90 nop +* +* Notably LEA does not modify flags and can be reordered with the CMP, +* avoiding a dependency. Again, using a non-taken (backwards) branch +* for the failure case, abusing LEA's immediate 0xf0 as LOCK prefix for the +* Jcc.d8, causing #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_paranoid_start: \n" +" movl $0x12345678, %r10d \n" +" cmpl -9(%r11), %r10d \n" +" lea -0x10(%r11), %r11 \n" +" jne fineibt_paranoid_start+0xd \n" +"fineibt_paranoid_ind: \n" +" call *%r11 \n" +" nop \n" +"fineibt_paranoid_end: \n" +".popsection \n" +); + +extern u8 fineibt_paranoid_start[]; +extern u8 fineibt_paranoid_ind[]; +extern u8 fineibt_paranoid_end[]; + +#define fineibt_paranoid_size (fineibt_paranoid_end - fineibt_paranoid_start) +#define fineibt_paranoid_ind (fineibt_paranoid_ind - fineibt_paranoid_start) +#define fineibt_paranoid_ud 0xd +static u32 decode_preamble_hash (void *addr) {u8 *p = addr;
@@ -1287,18 +1346,48 @@ static int cfi_rewrite_callers(s32 *star {s32 *s; +BUG_ON (fineibt_paranoid_size !=20 ); +for (s = start; s < end; s++) {void *addr = (void *)s + *s; +struct insn insn; +u8 bytes[20 ];u32 hash; +int ret; +u8 op; addr -= fineibt_caller_size; hash =decode_caller_hash (addr); -if (hash) { +if (!hash) +continue ; + +if (!cfi_paranoid) {text_poke_early (addr, fineibt_caller_start, fineibt_caller_size);WARN_ON (*(u32 *)(addr + fineibt_caller_hash) !=0x12345678 );text_poke_early (addr + fineibt_caller_hash, &hash,4 ); +/* rely on apply_retpolines() */ +continue ; + } + +/* cfi_paranoid */ + ret =insn_decode_kernel (&insn, addr + fineibt_caller_size); +if (WARN_ON_ONCE (ret <0 )) +continue ; + + op = insn.opcode.bytes[0 ]; +if (op !=CALL_INSN_OPCODE && op !=JMP32_INSN_OPCODE ) { +WARN_ON_ONCE (1 ); +continue ; } -/* rely on apply_retpolines() */ + +memcpy (bytes, fineibt_paranoid_start, fineibt_paranoid_size); +memcpy (bytes + fineibt_caller_hash, &hash,4 ); + + ret =emit_indirect (op,11 , bytes + fineibt_paranoid_ind); +if (WARN_ON_ONCE (ret !=3 )) +continue ; + +text_poke_early (addr, bytes, fineibt_paranoid_size); }return 0 ;
@@ -1315,8 +1404,15 @@ static void __apply_fineibt(s32 *start_rif (cfi_mode ==CFI_AUTO ) { cfi_mode =CFI_KCFI ; -if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) +if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) { +/* +* FRED has much saner context on exception entry and +* is less easy to take advantage of. +*/ +if (!cpu_feature_enabled (X86_FEATURE_FRED )) + cfi_paranoid = true; cfi_mode =CFI_FINEIBT ; + } }/*
@@ -1373,8 +1469,10 @@ static void __apply_fineibt(s32 *start_r/* now that nobody targets func()+0, remove ENDBR there */ cfi_rewrite_endbr (start_cfi, end_cfi); -if (builtin) -pr_info ("Using FineIBT CFI\n" ); +if (builtin) { +pr_info ("Using %sFineIBT CFI\n" , + cfi_paranoid ?"paranoid " :"" ); + }return ;default :
@@ -1447,7 +1545,7 @@ static void poison_cfi(void *addr)* We check the preamble by checking for the ENDBR instruction relative to the * 0xEA instruction. */ -bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +static bool decode_fineibt_preamble (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;
@@ -1472,6 +1570,40 @@ bool decode_fineibt_insn(struct pt_regs return false; } +/* +* regs->ip points to a LOCK Jcc.d8 instruction from the fineibt_paranoid_start[] +* sequence. +*/ +static bool decode_fineibt_paranoid (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +unsigned long addr = regs->ip - fineibt_paranoid_ud; +u32 hash; + +if (!cfi_paranoid || !is_cfi_trap (addr + fineibt_caller_size -LEN_UD2 )) +return false; + +__get_kernel_nofault (&hash, addr + fineibt_caller_hash, u32, Efault); + *target = regs->r11 + fineibt_preamble_size; + *type = regs->r10; + +/* +* Since the trapping instruction is the exact, but LOCK prefixed, +* Jcc.d8 that got us here, the normal fixup will work. +*/ +return true; + +Efault: +return false; +} + +bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +if (decode_fineibt_paranoid (regs, target, type)) +return true; + +return decode_fineibt_preamble (regs, target, type); +} +#else static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
Re: [PATCH v4 07/10] x86/ibt: Add paranoid FineIBT mode
Quoted 8 lines · Peter Zijlstra
Due to concerns about circumvention attacks against FineIBT on 'naked' ENDBR, add an additional caller side hash check to FineIBT. This should make it impossible to pivot over such a 'naked' ENDBR instruction at the cost of an additional load.
The specific pivot reported was against the SYSCALL entry site and FRED will have all those holes fixed up.
Quoted 7 lines
https://lore.kernel.org/linux-hardening/Z60NwR4w%2F28Z7XUa@ubun/
This specific fineibt_paranoid_start[] sequence was concocted by Scott.
Reported-by Jennifer Miller <[email protected]>Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Great! I'm happy to see the pre-call checking. :)
Signature · 1 line
Kees Cook
[tip: x86/core] x86/ibt: Add paranoid FineIBT mode
In reply to Peter ZijlstraThe 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: 3b920ee30ac68e6b040d37b9205f0acc074a0d9b Gitweb: https://git.kernel.org/tip/3b920ee30ac68e6b040d37b9205f0acc074a0d9b Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:10 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:55 +01:00
x86/ibt: Add paranoid FineIBT mode
Due to concerns about circumvention attacks against FineIBT on 'naked' ENDBR, add an additional caller side hash check to FineIBT. This should make it impossible to pivot over such a 'naked' ENDBR instruction at the cost of an additional load.
The specific pivot reported was against the SYSCALL entry site and FRED will have all those holes fixed up.
https://lore.kernel.org/linux-hardening/Z60NwR4w%2F28Z7XUa@ubun/
This specific fineibt_paranoid_start[] sequence was concocted by Scott.
--- arch/x86/kernel/alternative.c | 144 +++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 6 deletions(-)
arch/x86/kernel/alternative.c +138 -6
@@ -741,6 +741,11 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) op2 = insn.opcode.bytes[1 ];switch (op1) { + case 0x70 ... 0x7f: /* Jcc.d8 */ +/* See cfi_paranoid. */ +WARN_ON_ONCE (cfi_mode !=CFI_FINEIBT ); +continue ; +case CALL_INSN_OPCODE :case JMP32_INSN_OPCODE :break ;
@@ -994,6 +999,8 @@ u32 cfi_get_func_hash(void *func)static bool cfi_rand__ro_after_init = true;static u32 cfi_seed__ro_after_init ; +static bool cfi_paranoid__ro_after_init = false; +/* * Re-hash the CFI hash with a boot-time seed while making sure the result is * not a valid ENDBR instruction.
@@ -1036,6 +1043,12 @@ static __init int cfi_parse_cmdline(char *str) }else if (!strcmp (str,"warn" )) {pr_alert ("CFI mismatch non-fatal!\n" ); cfi_warn = true; + }else if (!strcmp (str,"paranoid" )) { +if (cfi_mode ==CFI_FINEIBT ) { + cfi_paranoid = true; + }else { +pr_err ("Ignoring paranoid; depends on fineibt.\n" ); + } }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
@@ -1124,6 +1137,52 @@ extern u8 fineibt_caller_end[];#define fineibt_caller_jmp (fineibt_caller_size - 2) +/* +* Since FineIBT does hash validation on the callee side it is prone to +* circumvention attacks where a 'naked' ENDBR instruction exists that +* is not part of the fineibt_preamble sequence. +* +* Notably the x86 entry points must be ENDBR and equally cannot be +* fineibt_preamble. +* +* The fineibt_paranoid caller sequence adds additional caller side +* hash validation. This stops such circumvention attacks dead, but at the cost +* of adding a load. +* +* <fineibt_paranoid_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d +* a: 4d 8d 5b <f0> lea -0x10(%r11), %r11 +* e: 75 fd jne d <fineibt_paranoid_start+0xd> +* 10: 41 ff d3 call *%r11 +* 13: 90 nop +* +* Notably LEA does not modify flags and can be reordered with the CMP, +* avoiding a dependency. Again, using a non-taken (backwards) branch +* for the failure case, abusing LEA's immediate 0xf0 as LOCK prefix for the +* Jcc.d8, causing #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_paranoid_start: \n" +" movl $0x12345678, %r10d \n" +" cmpl -9(%r11), %r10d \n" +" lea -0x10(%r11), %r11 \n" +" jne fineibt_paranoid_start+0xd \n" +"fineibt_paranoid_ind: \n" +" call *%r11 \n" +" nop \n" +"fineibt_paranoid_end: \n" +".popsection \n" +); + +extern u8 fineibt_paranoid_start[]; +extern u8 fineibt_paranoid_ind[]; +extern u8 fineibt_paranoid_end[]; + +#define fineibt_paranoid_size (fineibt_paranoid_end - fineibt_paranoid_start) +#define fineibt_paranoid_ind (fineibt_paranoid_ind - fineibt_paranoid_start) +#define fineibt_paranoid_ud 0xd +static u32 decode_preamble_hash (void *addr) {u8 *p = addr;
@@ -1287,18 +1346,48 @@ static int cfi_rewrite_callers(s32 *start, s32 *end) {s32 *s; +BUG_ON (fineibt_paranoid_size !=20 ); +for (s = start; s < end; s++) {void *addr = (void *)s + *s; +struct insn insn; +u8 bytes[20 ];u32 hash; +int ret; +u8 op; addr -= fineibt_caller_size; hash =decode_caller_hash (addr); -if (hash) { +if (!hash) +continue ; + +if (!cfi_paranoid) {text_poke_early (addr, fineibt_caller_start, fineibt_caller_size);WARN_ON (*(u32 *)(addr + fineibt_caller_hash) !=0x12345678 );text_poke_early (addr + fineibt_caller_hash, &hash,4 ); +/* rely on apply_retpolines() */ +continue ; + } + +/* cfi_paranoid */ + ret =insn_decode_kernel (&insn, addr + fineibt_caller_size); +if (WARN_ON_ONCE (ret <0 )) +continue ; + + op = insn.opcode.bytes[0 ]; +if (op !=CALL_INSN_OPCODE && op !=JMP32_INSN_OPCODE ) { +WARN_ON_ONCE (1 ); +continue ; } -/* rely on apply_retpolines() */ + +memcpy (bytes, fineibt_paranoid_start, fineibt_paranoid_size); +memcpy (bytes + fineibt_caller_hash, &hash,4 ); + + ret =emit_indirect (op,11 , bytes + fineibt_paranoid_ind); +if (WARN_ON_ONCE (ret !=3 )) +continue ; + +text_poke_early (addr, bytes, fineibt_paranoid_size); }return 0 ;
@@ -1315,8 +1404,15 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,if (cfi_mode ==CFI_AUTO ) { cfi_mode =CFI_KCFI ; -if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) +if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) { +/* +* FRED has much saner context on exception entry and +* is less easy to take advantage of. +*/ +if (!cpu_feature_enabled (X86_FEATURE_FRED )) + cfi_paranoid = true; cfi_mode =CFI_FINEIBT ; + } }/*
@@ -1373,8 +1469,10 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,/* now that nobody targets func()+0, remove ENDBR there */ cfi_rewrite_endbr (start_cfi, end_cfi); -if (builtin) -pr_info ("Using FineIBT CFI\n" ); +if (builtin) { +pr_info ("Using %sFineIBT CFI\n" , + cfi_paranoid ?"paranoid " :"" ); + }return ;default :
@@ -1447,7 +1545,7 @@ static void poison_cfi(void *addr)* We check the preamble by checking for the ENDBR instruction relative to the * 0xEA instruction. */ -bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +static bool decode_fineibt_preamble (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;
@@ -1472,6 +1570,40 @@ Efault: return false; } +/* +* regs->ip points to a LOCK Jcc.d8 instruction from the fineibt_paranoid_start[] +* sequence. +*/ +static bool decode_fineibt_paranoid (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +unsigned long addr = regs->ip - fineibt_paranoid_ud; +u32 hash; + +if (!cfi_paranoid || !is_cfi_trap (addr + fineibt_caller_size -LEN_UD2 )) +return false; + +__get_kernel_nofault (&hash, addr + fineibt_caller_hash, u32, Efault); + *target = regs->r11 + fineibt_preamble_size; + *type = regs->r10; + +/* +* Since the trapping instruction is the exact, but LOCK prefixed, +* Jcc.d8 that got us here, the normal fixup will work. +*/ +return true; + +Efault: +return false; +} + +bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +if (decode_fineibt_paranoid (regs, target, type)) +return true; + +return decode_fineibt_preamble (regs, target, type); +} +#else static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
[tip: x86/core] x86/ibt: Add paranoid FineIBT mode
In reply to Peter ZijlstraThe 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: 97e59672a9d2aec0c27f6cd6a6b0edfdd6e5a85c Gitweb: https://git.kernel.org/tip/97e59672a9d2aec0c27f6cd6a6b0edfdd6e5a85c Author: Peter Zijlstra <[email protected]> AuthorDate: Wed, 26 Feb 2025 12:25:17 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:27:45 +01:00
x86/ibt: Add paranoid FineIBT mode
Due to concerns about circumvention attacks against FineIBT on 'naked' ENDBR, add an additional caller side hash check to FineIBT. This should make it impossible to pivot over such a 'naked' ENDBR instruction at the cost of an additional load.
The specific pivot reported was against the SYSCALL entry site and FRED will have all those holes fixed up.
https://lore.kernel.org/linux-hardening/Z60NwR4w%2F28Z7XUa@ubun/
This specific fineibt_paranoid_start[] sequence was concocted by Scott.
--- arch/x86/kernel/alternative.c | 144 +++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 6 deletions(-)
arch/x86/kernel/alternative.c +138 -6
@@ -741,6 +741,11 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) op2 = insn.opcode.bytes[1 ];switch (op1) { + case 0x70 ... 0x7f: /* Jcc.d8 */ +/* See cfi_paranoid. */ +WARN_ON_ONCE (cfi_mode !=CFI_FINEIBT ); +continue ; +case CALL_INSN_OPCODE :case JMP32_INSN_OPCODE :break ;
@@ -998,6 +1003,8 @@ u32 cfi_get_func_hash(void *func)static bool cfi_rand__ro_after_init = true;static u32 cfi_seed__ro_after_init ; +static bool cfi_paranoid__ro_after_init = false; +/* * Re-hash the CFI hash with a boot-time seed while making sure the result is * not a valid ENDBR instruction.
@@ -1040,6 +1047,12 @@ static __init int cfi_parse_cmdline(char *str) }else if (!strcmp (str,"warn" )) {pr_alert ("CFI mismatch non-fatal!\n" ); cfi_warn = true; + }else if (!strcmp (str,"paranoid" )) { +if (cfi_mode ==CFI_FINEIBT ) { + cfi_paranoid = true; + }else { +pr_err ("Ignoring paranoid; depends on fineibt.\n" ); + } }else {pr_err ("Ignoring unknown cfi option (%s)." , str); }
@@ -1128,6 +1141,52 @@ extern u8 fineibt_caller_end[];#define fineibt_caller_jmp (fineibt_caller_size - 2) +/* +* Since FineIBT does hash validation on the callee side it is prone to +* circumvention attacks where a 'naked' ENDBR instruction exists that +* is not part of the fineibt_preamble sequence. +* +* Notably the x86 entry points must be ENDBR and equally cannot be +* fineibt_preamble. +* +* The fineibt_paranoid caller sequence adds additional caller side +* hash validation. This stops such circumvention attacks dead, but at the cost +* of adding a load. +* +* <fineibt_paranoid_start>: +* 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d +* 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d +* a: 4d 8d 5b <f0> lea -0x10(%r11), %r11 +* e: 75 fd jne d <fineibt_paranoid_start+0xd> +* 10: 41 ff d3 call *%r11 +* 13: 90 nop +* +* Notably LEA does not modify flags and can be reordered with the CMP, +* avoiding a dependency. Again, using a non-taken (backwards) branch +* for the failure case, abusing LEA's immediate 0xf0 as LOCK prefix for the +* Jcc.d8, causing #UD. +*/ +asm(".pushsection .rodata \n" +"fineibt_paranoid_start: \n" +" movl $0x12345678, %r10d \n" +" cmpl -9(%r11), %r10d \n" +" lea -0x10(%r11), %r11 \n" +" jne fineibt_paranoid_start+0xd \n" +"fineibt_paranoid_ind: \n" +" call *%r11 \n" +" nop \n" +"fineibt_paranoid_end: \n" +".popsection \n" +); + +extern u8 fineibt_paranoid_start[]; +extern u8 fineibt_paranoid_ind[]; +extern u8 fineibt_paranoid_end[]; + +#define fineibt_paranoid_size (fineibt_paranoid_end - fineibt_paranoid_start) +#define fineibt_paranoid_ind (fineibt_paranoid_ind - fineibt_paranoid_start) +#define fineibt_paranoid_ud 0xd +static u32 decode_preamble_hash (void *addr) {u8 *p = addr;
@@ -1291,18 +1350,48 @@ static int cfi_rewrite_callers(s32 *start, s32 *end) {s32 *s; +BUG_ON (fineibt_paranoid_size !=20 ); +for (s = start; s < end; s++) {void *addr = (void *)s + *s; +struct insn insn; +u8 bytes[20 ];u32 hash; +int ret; +u8 op; addr -= fineibt_caller_size; hash =decode_caller_hash (addr); -if (hash) { +if (!hash) +continue ; + +if (!cfi_paranoid) {text_poke_early (addr, fineibt_caller_start, fineibt_caller_size);WARN_ON (*(u32 *)(addr + fineibt_caller_hash) !=0x12345678 );text_poke_early (addr + fineibt_caller_hash, &hash,4 ); +/* rely on apply_retpolines() */ +continue ; + } + +/* cfi_paranoid */ + ret =insn_decode_kernel (&insn, addr + fineibt_caller_size); +if (WARN_ON_ONCE (ret <0 )) +continue ; + + op = insn.opcode.bytes[0 ]; +if (op !=CALL_INSN_OPCODE && op !=JMP32_INSN_OPCODE ) { +WARN_ON_ONCE (1 ); +continue ; } -/* rely on apply_retpolines() */ + +memcpy (bytes, fineibt_paranoid_start, fineibt_paranoid_size); +memcpy (bytes + fineibt_caller_hash, &hash,4 ); + + ret =emit_indirect (op,11 , bytes + fineibt_paranoid_ind); +if (WARN_ON_ONCE (ret !=3 )) +continue ; + +text_poke_early (addr, bytes, fineibt_paranoid_size); }return 0 ;
@@ -1319,8 +1408,15 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,if (cfi_mode ==CFI_AUTO ) { cfi_mode =CFI_KCFI ; -if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) +if (HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT )) { +/* +* FRED has much saner context on exception entry and +* is less easy to take advantage of. +*/ +if (!cpu_feature_enabled (X86_FEATURE_FRED )) + cfi_paranoid = true; cfi_mode =CFI_FINEIBT ; + } }/*
@@ -1377,8 +1473,10 @@ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,/* now that nobody targets func()+0, remove ENDBR there */ cfi_rewrite_endbr (start_cfi, end_cfi); -if (builtin) -pr_info ("Using FineIBT CFI\n" ); +if (builtin) { +pr_info ("Using %sFineIBT CFI\n" , + cfi_paranoid ?"paranoid " :"" ); + }return ;default :
@@ -1451,7 +1549,7 @@ static void poison_cfi(void *addr)* We check the preamble by checking for the ENDBR instruction relative to the * 0xEA instruction. */ -bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +static bool decode_fineibt_preamble (struct pt_regs *regs,unsigned long *target,u32 *type) {unsigned long addr = regs->ip - fineibt_preamble_ud;u32 hash;
@@ -1476,6 +1574,40 @@ Efault: return false; } +/* +* regs->ip points to a LOCK Jcc.d8 instruction from the fineibt_paranoid_start[] +* sequence. +*/ +static bool decode_fineibt_paranoid (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +unsigned long addr = regs->ip - fineibt_paranoid_ud; +u32 hash; + +if (!cfi_paranoid || !is_cfi_trap (addr + fineibt_caller_size -LEN_UD2 )) +return false; + +__get_kernel_nofault (&hash, addr + fineibt_caller_hash, u32, Efault); + *target = regs->r11 + fineibt_preamble_size; + *type = regs->r10; + +/* +* Since the trapping instruction is the exact, but LOCK prefixed, +* Jcc.d8 that got us here, the normal fixup will work. +*/ +return true; + +Efault: +return false; +} + +bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type) +{ +if (decode_fineibt_paranoid (regs, target, type)) +return true; + +return decode_fineibt_preamble (regs, target, type); +} +#else /* !CONFIG_FINEIBT: */ static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
PATCH v4 8/10 x86: BHI stubs
In reply to Peter Zijlstra--- arch/x86/include/asm/cfi.h | 4 + arch/x86/lib/Makefile | 3 arch/x86/lib/bhi.S | 146 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-)
arch/x86/include/asm/cfi.h +4 -0
@@ -101,6 +101,10 @@ enum cfi_mode {extern enum cfi_mode cfi_mode; +typedef u8 bhi_thunk [32 ]; +extern bhi_thunk __bhi_args[]; +extern bhi_thunk __bhi_args_end[]; +struct pt_regs ;#ifdef CONFIG_CFI_CLANG
arch/x86/lib/Makefile +2 -1
@@ -66,5 +66,6 @@ endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o copy_user_uncached_64.o - lib-y += cmpxchg16b_emu.o + lib-y += cmpxchg16b_emu.o + lib-y += bhi.o endif
arch/x86/lib/bhi.S new file +146 -0
146 lines not on this page · raw message
Kees Cook
maintainerRe: [PATCH v4 08/10] x86: BHI stubs
Quoted 2 lines · Peter Zijlstra
Signed-off-by Peter Zijlstra (Intel) <[email protected]>
Did this commit log go missing?
Signature · 1 line
Kees Cook
Re: [PATCH v4 08/10] x86: BHI stubs
Quoted 5 lines · Kees Cook
Quoted 2 lines · Peter Zijlstra
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Did this commit log go missing?
Nah, it never had one.
How does this sound?
---
Add an array of code thunks, to be called from the FineIBT preamble, clobbering the first 'n' argument registers for speculative execution.
Notably the 0th entry will clobber no argument registers and will never be used, it exists so the array can be naturally indexed, while the 7th entry will clobber all the 6 argument registers and also RSP in order to mess up stack based arguments.
Re: [PATCH v4 08/10] x86: BHI stubs
Quoted 14 lines · Peter Zijlstra
Quoted 5 lines · Kees Cook
Quoted 2 lines · Peter Zijlstra
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Did this commit log go missing?
Nah, it never had one.
How does this sound?
---
Quoted 8 lines
Add an array of code thunks, to be called from the FineIBT preamble, clobbering the first 'n' argument registers for speculative execution.
Notably the 0th entry will clobber no argument registers and will never be used, it exists so the array can be naturally indexed, while the 7th entry will clobber all the 6 argument registers and also RSP in order to mess up stack based arguments.
Yes, this reads well. Thanks!
Signature · 1 line
Kees Cook
[tip: x86/core] x86: BHI stubs
In reply to Peter ZijlstraThe 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: 53f12687ab00414965a96768bbf09a9c8485a669 Gitweb: https://git.kernel.org/tip/53f12687ab00414965a96768bbf09a9c8485a669 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:11 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:55 +01:00
x86: BHI stubs
Add an array of code thunks, to be called from the FineIBT preamble, clobbering the first 'n' argument registers for speculative execution.
Notably the 0th entry will clobber no argument registers and will never be used, it exists so the array can be naturally indexed, while the 7th entry will clobber all the 6 argument registers and also RSP in order to mess up stack based arguments.
--- arch/x86/include/asm/cfi.h | 4 +- arch/x86/lib/Makefile | 3 +- arch/x86/lib/bhi.S | 146 ++++++++++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 arch/x86/lib/bhi.S
arch/x86/include/asm/cfi.h +4 -0
@@ -101,6 +101,10 @@ enum cfi_mode {extern enum cfi_mode cfi_mode; +typedef u8 bhi_thunk [32 ]; +extern bhi_thunk __bhi_args[]; +extern bhi_thunk __bhi_args_end[]; +struct pt_regs ;#ifdef CONFIG_CFI_CLANG
arch/x86/lib/Makefile +2 -1
@@ -66,5 +66,6 @@ endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o copy_user_uncached_64.o - lib-y += cmpxchg16b_emu.o + lib-y += cmpxchg16b_emu.o + lib-y += bhi.o endif
arch/x86/lib/bhi.S new file +146 -0
146 lines not on this page · raw message
[tip: x86/core] x86/bhi: Add BHI stubs
In reply to Peter ZijlstraThe 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: a18e6715ff4e6e71c2b5dc8ec78f2ddc3fe4b0a8 Gitweb: https://git.kernel.org/tip/a18e6715ff4e6e71c2b5dc8ec78f2ddc3fe4b0a8 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:28:20 +01:00
x86/bhi: Add BHI stubs
Add an array of code thunks, to be called from the FineIBT preamble, clobbering the first 'n' argument registers for speculative execution.
Notably the 0th entry will clobber no argument registers and will never be used, it exists so the array can be naturally indexed, while the 7th entry will clobber all the 6 argument registers and also RSP in order to mess up stack based arguments.
--- arch/x86/include/asm/cfi.h | 4 +- arch/x86/lib/Makefile | 3 +- arch/x86/lib/bhi.S | 146 ++++++++++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 arch/x86/lib/bhi.S
arch/x86/include/asm/cfi.h +4 -0
@@ -101,6 +101,10 @@ enum cfi_mode {extern enum cfi_mode cfi_mode; +typedef u8 bhi_thunk [32 ]; +extern bhi_thunk __bhi_args[]; +extern bhi_thunk __bhi_args_end[]; +struct pt_regs ;#ifdef CONFIG_CFI_CLANG
arch/x86/lib/Makefile +2 -1
@@ -66,5 +66,6 @@ endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o copy_user_uncached_64.o - lib-y += cmpxchg16b_emu.o + lib-y += cmpxchg16b_emu.o + lib-y += bhi.o endif
arch/x86/lib/bhi.S new file +146 -0
146 lines not on this page · raw message
[tip: x86/core] x86/bhi: Add BHI stubs
In reply to Peter ZijlstraThe 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: b815f6877d8063482fe745f098eeef632679aa8a Gitweb: https://git.kernel.org/tip/b815f6877d8063482fe745f098eeef632679aa8a Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:48:52 +01:00
x86/bhi: Add BHI stubs
Add an array of code thunks, to be called from the FineIBT preamble, clobbering the first 'n' argument registers for speculative execution.
Notably the 0th entry will clobber no argument registers and will never be used, it exists so the array can be naturally indexed, while the 7th entry will clobber all the 6 argument registers and also RSP in order to mess up stack based arguments.
--- arch/x86/include/asm/cfi.h | 4 +- arch/x86/lib/Makefile | 3 +- arch/x86/lib/bhi.S | 147 ++++++++++++++++++++++++++++++++++++- 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 arch/x86/lib/bhi.S
arch/x86/include/asm/cfi.h +4 -0
@@ -101,6 +101,10 @@ enum cfi_mode {extern enum cfi_mode cfi_mode; +typedef u8 bhi_thunk [32 ]; +extern bhi_thunk __bhi_args[]; +extern bhi_thunk __bhi_args_end[]; +struct pt_regs ;#ifdef CONFIG_CFI_CLANG
arch/x86/lib/Makefile +2 -1
@@ -66,5 +66,6 @@ endif lib-y += clear_page_64.o copy_page_64.o lib-y += memmove_64.o memset_64.o lib-y += copy_user_64.o copy_user_uncached_64.o - lib-y += cmpxchg16b_emu.o + lib-y += cmpxchg16b_emu.o + lib-y += bhi.o endif
arch/x86/lib/bhi.S new file +147 -0
147 lines not on this page · raw message
PATCH v4 9/10 x86/ibt: Implement FineIBT-BHI mitigation
In reply to Peter ZijlstraWhile WAIT_FOR_ENDBR is specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.
To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb6a200153eee245
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
(This is where Scott goes and gives more details...)
--- Makefile | 3 + arch/x86/Kconfig | 8 +++ arch/x86/include/asm/cfi.h | 6 ++ arch/x86/kernel/alternative.c | 107 ++++++++++++++++++++++++++++++++++++++---- arch/x86/net/bpf_jit_comp.c | 29 +++++++---- 5 files changed, 134 insertions(+), 19 deletions(-)
Makefile +3 -0
@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
arch/x86/Kconfig +8 -0
@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST + config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B
@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
arch/x86/include/asm/cfi.h +6 -0
@@ -100,6 +100,7 @@ enum cfi_mode { };extern enum cfi_mode cfi_mode; +extern bool cfi_bhi;typedef u8 bhi_thunk [32 ];extern bhi_thunk __bhi_args[];
@@ -129,6 +130,7 @@ static inline int cfi_get_offset(void)#define cfi_get_offset cfi_get_offsetextern u32 cfi_get_func_hash (void *func); +extern int cfi_get_func_arity (void *func);#ifdef CONFIG_FINEIBT extern bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type);
@@ -152,6 +154,10 @@ static inline u32 cfi_get_func_hash(void {return 0 ; } +static inline int cfi_get_func_arity (void *func) +{ +return 0 ; +}#endif /* CONFIG_CFI_CLANG */ #if HAS_KERNEL_IBT ==1
arch/x86/kernel/alternative.c +97 -10
207 lines not on this page · raw message
arch/x86/net/bpf_jit_comp.c +20 -9
@@ -410,15 +410,20 @@ static void emit_nops(u8 **pprog, int le* Emit the various CFI preambles, see asm/cfi.h and the comments about FineIBT * in arch/x86/kernel/alternative.c */ +static int emit_call (u8 **prog,void *func,void *ip); -static void emit_fineibt (u8 **pprog,u32 hash) +static void emit_fineibt (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ -EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ +if (cfi_bhi) { +emit_call (&prog, __bhi_args[arity], ip +11 ); + }else { +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ + }EMIT_ENDBR_POISON (); *pprog = prog;
@@ -447,13 +452,13 @@ static void emit_kcfi(u8 **pprog, u32 ha *pprog = prog; } -static void emit_cfi (u8 **pprog,u32 hash) +static void emit_cfi (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;switch (cfi_mode) {case CFI_FINEIBT : -emit_fineibt (&prog, hash); +emit_fineibt (&prog, ip, hash, arity);break ;case CFI_KCFI :
@@ -504,13 +509,17 @@ static void emit_prologue_tail_call(u8 ** bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes * while jumping to another program */ -static void emit_prologue (u8 **pprog,u32 stack_depth,bool ebpf_from_cbpf, +static void emit_prologue (u8 **pprog,u8 *ip,u32 stack_depth,bool ebpf_from_cbpf,bool tail_call_reachable,bool is_subprog,bool is_exception_cb) {u8 *prog = *pprog; -emit_cfi (&prog, is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash); +if (is_subprog) { +emit_cfi (&prog, ip, cfi_bpf_subprog_hash,5 ); + }else { +emit_cfi (&prog, ip, cfi_bpf_hash,1 ); + }/* BPF trampoline can be made to work without these nops, * but let's waste 5 bytes for now and optimize later */
@@ -1479,7 +1488,7 @@ static int do_jit(struct bpf_prog *bpf_pdetect_reg_usage (insn, insn_cnt, callee_regs_used); -emit_prologue (&prog, stack_depth, +emit_prologue (&prog, image, stack_depth,bpf_prog_was_classic (bpf_prog), tail_call_reachable,bpf_is_subprog (bpf_prog), bpf_prog->aux->exception_cb);/* Exception callback will clobber callee regs for its own use, and
@@ -3046,7 +3055,9 @@ static int __arch_prepare_bpf_trampoline/* * Indirect call for bpf_struct_ops */ -emit_cfi (&prog,cfi_get_func_hash (func_addr)); +emit_cfi (&prog, image, +cfi_get_func_hash (func_addr), +cfi_get_func_arity (func_addr)); }else {/* * Direct-call fentry stub, as such it needs accounting for the
Re: [PATCH v4 09/10] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 10 lines · Peter Zijlstra
While
WAIT_FOR_ENDBRis specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb6a200153eee245
Quoted 7 lines
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
(This is where Scott goes and gives more details...)
Scott, could you give a blurb here? Would the below cover things?
The basic setup, for 1 argument, is something like:
__bhi_args_1:
jne .Lud
cmovne %r10, %rdi
ret
int3
__cfi_foo:
endbr
subl $hash, %r10d
call __bhi_args_1
foo:
osp nop3
...
such that when speculation of an indirect call is maliciously steered here from a non-matching site, the hash check (__cfi_foo's SUB) will not match, resulting in !ZF and non-zero R10. Subsequently the __bhi_args stub will either hit #UD exception, which kills speculation, or when steered wrong, hit the cmovne, which will then clobber the argument register RDI with whatever non-zero garbage sits in R10. Making it much harder to control whatever foo normally does with its input argument.
Additionally, CFI hashes are randomized at boot, making it much harder still to predict/control the non-zero value.
RE: [PATCH v4 09/10] x86/ibt: Implement FineIBT-BHI mitigation
Peter's example is perfectly valid. I can elaborate on the broader goals:
Microarchitectural attacks such as Branch History Injection (BHI) and Intra-mode Branch Target Injection (IMBTI) [1] can cause an indirect call to mispredict to an adversary-influenced target within the same hardware domain (e.g., within the kernel). Instructions at the mispredicted target may execute speculatively and potentially expose kernel data (e.g., to a user-mode adversary) through a microarchitectural covert channel such as CPU cache state.
CET-IBT [2] is a coarse-grained control-flow integrity (CFI) ISA extension that enforces that each indirect call (or indirect jump) must land on an ENDBR (end branch) instruction, even speculatively*. FineIBT is a software technique that refines CET-IBT by associating each function type with a 32-bit hash and enforcing (at the callee) that the hash of the caller's function pointer type matches the hash of the callee's function type. However, recent research [3] has demonstrated that the conditional branch that enforces FineIBT's hash check can be coerced to mispredict, potentially allowing an adversary to speculatively bypass the hash check:
fineibt_target:
ENDBR64
SUB R10d, 0x01234567
JZ .fineibt_target_body # Even if the hash check fails and ZF=0, this branch could still mispredict as taken
UD2
.fineibt_target_body:
...
The techniques demonstrated in [3] require the attacker to be able to control the contents of at least one live register at the mispredicted target. Therefore, this patch set introduces a sequence of CMOV instructions at each indirect-callable target that poisons every live register with data that the attacker cannot control whenever the FineIBT hash check fails, thus mitigating any potential attack.
The security provided by this scheme has been discussed in detail on an earlier thread [4].
[1] https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html
[2] Intel Software Developer's Manual, Volume 1, Chapter 18
[3] https://www.vusec.net/projects/native-bhi/
[4] https://lore.kernel.org/lkml/[email protected]/
*There are some caveats for certain processors, see [1] for more info
Regards,
Scott Constable
-----Original Message-----
From: Peter Zijlstra <[email protected]>
Sent: Tuesday, February 25, 2025 1:12 AM
To: [email protected]
Cc: [email protected]; Milburn, Alyssa <[email protected]>; Constable, Scott D <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v4 09/10] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 12 lines · Peter Zijlstra
While
WAIT_FOR_ENDBRis specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb
6a200153eee245
Quoted 7 lines
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
(This is where Scott goes and gives more details...)
Scott, could you give a blurb here? Would the below cover things?
The basic setup, for 1 argument, is something like:
__bhi_args_1:
jne .Lud
cmovne %r10, %rdi
ret
int3
__cfi_foo:
endbr
subl $hash, %r10d
call __bhi_args_1
foo:
osp nop3
...
such that when speculation of an indirect call is maliciously steered here from a non-matching site, the hash check (__cfi_foo's SUB) will not match, resulting in !ZF and non-zero R10. Subsequently the __bhi_args stub will either hit #UD exception, which kills speculation, or when steered wrong, hit the cmovne, which will then clobber the argument register RDI with whatever non-zero garbage sits in R10. Making it much harder to control whatever foo normally does with its input argument.
Additionally, CFI hashes are randomized at boot, making it much harder still to predict/control the non-zero value.
[tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
In reply to Peter ZijlstraThe 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: 106a97384748b3cc7a42ea1598bd8193c6e2a2a0 Gitweb: https://git.kernel.org/tip/106a97384748b3cc7a42ea1598bd8193c6e2a2a0 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:12 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:55 +01:00
x86/ibt: Implement FineIBT-BHI mitigation
While WAIT_FOR_ENDBR is specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.
To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb6a200153eee245
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
Scott sayeth thusly:
Microarchitectural attacks such as Branch History Injection (BHI) and Intra-mode Branch Target Injection (IMBTI) [1] can cause an indirect call to mispredict to an adversary-influenced target within the same hardware domain (e.g., within the kernel). Instructions at the mispredicted target may execute speculatively and potentially expose kernel data (e.g., to a user-mode adversary) through a microarchitectural covert channel such as CPU cache state.
CET-IBT [2] is a coarse-grained control-flow integrity (CFI) ISA extension that enforces that each indirect call (or indirect jump) must land on an ENDBR (end branch) instruction, even speculatively*. FineIBT is a software technique that refines CET-IBT by associating each function type with a 32-bit hash and enforcing (at the callee) that the hash of the caller's function pointer type matches the hash of the callee's function type. However, recent research [3] has demonstrated that the conditional branch that enforces FineIBT's hash check can be coerced to mispredict, potentially allowing an adversary to speculatively bypass the hash check:
__cfi_foo:
ENDBR64
SUB R10d, 0x01234567
JZ foo # Even if the hash check fails and ZF=0, this branch could still mispredict as taken
UD2
foo:
...
The techniques demonstrated in [3] require the attacker to be able to control the contents of at least one live register at the mispredicted target. Therefore, this patch set introduces a sequence of CMOV instructions at each indirect-callable target that poisons every live register with data that the attacker cannot control whenever the FineIBT hash check fails, thus mitigating any potential attack.
The security provided by this scheme has been discussed in detail on an earlier thread [4].
[1] https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html
[2] Intel Software Developer's Manual, Volume 1, Chapter 18
[3] https://www.vusec.net/projects/native-bhi/
[4] https://lore.kernel.org/lkml/[email protected]/
*There are some caveats for certain processors, see [1] for more info
--- Makefile | 3 +- arch/x86/Kconfig | 8 ++- arch/x86/include/asm/cfi.h | 6 ++- arch/x86/kernel/alternative.c | 107 +++++++++++++++++++++++++++++---- arch/x86/net/bpf_jit_comp.c | 29 ++++++--- 5 files changed, 134 insertions(+), 19 deletions(-)
Makefile +3 -0
@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
arch/x86/Kconfig +8 -0
@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST + config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B
@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
arch/x86/include/asm/cfi.h +6 -0
@@ -100,6 +100,7 @@ enum cfi_mode { };extern enum cfi_mode cfi_mode; +extern bool cfi_bhi;typedef u8 bhi_thunk [32 ];extern bhi_thunk __bhi_args[];
@@ -129,6 +130,7 @@ static inline int cfi_get_offset(void)#define cfi_get_offset cfi_get_offsetextern u32 cfi_get_func_hash (void *func); +extern int cfi_get_func_arity (void *func);#ifdef CONFIG_FINEIBT extern bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type);
@@ -152,6 +154,10 @@ static inline u32 cfi_get_func_hash(void *func) {return 0 ; } +static inline int cfi_get_func_arity (void *func) +{ +return 0 ; +}#endif /* CONFIG_CFI_CLANG */ #if HAS_KERNEL_IBT ==1
arch/x86/kernel/alternative.c +97 -10
207 lines not on this page · raw message
arch/x86/net/bpf_jit_comp.c +20 -9
@@ -410,15 +410,20 @@ static void emit_nops(u8 **pprog, int len)* Emit the various CFI preambles, see asm/cfi.h and the comments about FineIBT * in arch/x86/kernel/alternative.c */ +static int emit_call (u8 **prog,void *func,void *ip); -static void emit_fineibt (u8 **pprog,u32 hash) +static void emit_fineibt (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ -EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ +if (cfi_bhi) { +emit_call (&prog, __bhi_args[arity], ip +11 ); + }else { +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ + }EMIT_ENDBR_POISON (); *pprog = prog;
@@ -447,13 +452,13 @@ static void emit_kcfi(u8 **pprog, u32 hash) *pprog = prog; } -static void emit_cfi (u8 **pprog,u32 hash) +static void emit_cfi (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;switch (cfi_mode) {case CFI_FINEIBT : -emit_fineibt (&prog, hash); +emit_fineibt (&prog, ip, hash, arity);break ;case CFI_KCFI :
@@ -504,13 +509,17 @@ static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)* bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes * while jumping to another program */ -static void emit_prologue (u8 **pprog,u32 stack_depth,bool ebpf_from_cbpf, +static void emit_prologue (u8 **pprog,u8 *ip,u32 stack_depth,bool ebpf_from_cbpf,bool tail_call_reachable,bool is_subprog,bool is_exception_cb) {u8 *prog = *pprog; -emit_cfi (&prog, is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash); +if (is_subprog) { +emit_cfi (&prog, ip, cfi_bpf_subprog_hash,5 ); + }else { +emit_cfi (&prog, ip, cfi_bpf_hash,1 ); + }/* BPF trampoline can be made to work without these nops, * but let's waste 5 bytes for now and optimize later */
@@ -1479,7 +1488,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_imagedetect_reg_usage (insn, insn_cnt, callee_regs_used); -emit_prologue (&prog, stack_depth, +emit_prologue (&prog, image, stack_depth,bpf_prog_was_classic (bpf_prog), tail_call_reachable,bpf_is_subprog (bpf_prog), bpf_prog->aux->exception_cb);/* Exception callback will clobber callee regs for its own use, and
@@ -3046,7 +3055,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im/* * Indirect call for bpf_struct_ops */ -emit_cfi (&prog,cfi_get_func_hash (func_addr)); +emit_cfi (&prog, image, +cfi_get_func_hash (func_addr), +cfi_get_func_arity (func_addr)); }else {/* * Direct-call fentry stub, as such it needs accounting for the
[tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
In reply to Peter ZijlstraThe 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: d8122c428076ee5281baa045edaa502b316e4157 Gitweb: https://git.kernel.org/tip/d8122c428076ee5281baa045edaa502b316e4157 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:12 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:28:57 +01:00
x86/ibt: Implement FineIBT-BHI mitigation
While WAIT_FOR_ENDBR is specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.
To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb6a200153eee245
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
Scott sayeth thusly:
Microarchitectural attacks such as Branch History Injection (BHI) and Intra-mode Branch Target Injection (IMBTI) [1] can cause an indirect call to mispredict to an adversary-influenced target within the same hardware domain (e.g., within the kernel). Instructions at the mispredicted target may execute speculatively and potentially expose kernel data (e.g., to a user-mode adversary) through a microarchitectural covert channel such as CPU cache state.
CET-IBT [2] is a coarse-grained control-flow integrity (CFI) ISA extension that enforces that each indirect call (or indirect jump) must land on an ENDBR (end branch) instruction, even speculatively*. FineIBT is a software technique that refines CET-IBT by associating each function type with a 32-bit hash and enforcing (at the callee) that the hash of the caller's function pointer type matches the hash of the callee's function type. However, recent research [3] has demonstrated that the conditional branch that enforces FineIBT's hash check can be coerced to mispredict, potentially allowing an adversary to speculatively bypass the hash check:
__cfi_foo:
ENDBR64
SUB R10d, 0x01234567
JZ foo # Even if the hash check fails and ZF=0, this branch could still mispredict as taken
UD2
foo:
...
The techniques demonstrated in [3] require the attacker to be able to control the contents of at least one live register at the mispredicted target. Therefore, this patch set introduces a sequence of CMOV instructions at each indirect-callable target that poisons every live register with data that the attacker cannot control whenever the FineIBT hash check fails, thus mitigating any potential attack.
The security provided by this scheme has been discussed in detail on an earlier thread [4].
[1] https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html
[2] Intel Software Developer's Manual, Volume 1, Chapter 18
[3] https://www.vusec.net/projects/native-bhi/
[4] https://lore.kernel.org/lkml/[email protected]/
*There are some caveats for certain processors, see [1] for more info
--- Makefile | 3 +- arch/x86/Kconfig | 8 ++- arch/x86/include/asm/cfi.h | 6 ++- arch/x86/kernel/alternative.c | 107 +++++++++++++++++++++++++++++---- arch/x86/net/bpf_jit_comp.c | 29 ++++++--- 5 files changed, 134 insertions(+), 19 deletions(-)
Makefile +3 -0
@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
arch/x86/Kconfig +8 -0
@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST + config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B
@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
arch/x86/include/asm/cfi.h +6 -0
@@ -100,6 +100,7 @@ enum cfi_mode { };extern enum cfi_mode cfi_mode; +extern bool cfi_bhi;typedef u8 bhi_thunk [32 ];extern bhi_thunk __bhi_args[];
@@ -129,6 +130,7 @@ static inline int cfi_get_offset(void)#define cfi_get_offset cfi_get_offsetextern u32 cfi_get_func_hash (void *func); +extern int cfi_get_func_arity (void *func);#ifdef CONFIG_FINEIBT extern bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type);
@@ -152,6 +154,10 @@ static inline u32 cfi_get_func_hash(void *func) {return 0 ; } +static inline int cfi_get_func_arity (void *func) +{ +return 0 ; +}#endif /* CONFIG_CFI_CLANG */ #if HAS_KERNEL_IBT ==1
arch/x86/kernel/alternative.c +97 -10
207 lines not on this page · raw message
arch/x86/net/bpf_jit_comp.c +20 -9
@@ -410,15 +410,20 @@ static void emit_nops(u8 **pprog, int len)* Emit the various CFI preambles, see asm/cfi.h and the comments about FineIBT * in arch/x86/kernel/alternative.c */ +static int emit_call (u8 **prog,void *func,void *ip); -static void emit_fineibt (u8 **pprog,u32 hash) +static void emit_fineibt (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ -EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ +if (cfi_bhi) { +emit_call (&prog, __bhi_args[arity], ip +11 ); + }else { +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ + }EMIT_ENDBR_POISON (); *pprog = prog;
@@ -447,13 +452,13 @@ static void emit_kcfi(u8 **pprog, u32 hash) *pprog = prog; } -static void emit_cfi (u8 **pprog,u32 hash) +static void emit_cfi (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;switch (cfi_mode) {case CFI_FINEIBT : -emit_fineibt (&prog, hash); +emit_fineibt (&prog, ip, hash, arity);break ;case CFI_KCFI :
@@ -504,13 +509,17 @@ static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)* bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes * while jumping to another program */ -static void emit_prologue (u8 **pprog,u32 stack_depth,bool ebpf_from_cbpf, +static void emit_prologue (u8 **pprog,u8 *ip,u32 stack_depth,bool ebpf_from_cbpf,bool tail_call_reachable,bool is_subprog,bool is_exception_cb) {u8 *prog = *pprog; -emit_cfi (&prog, is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash); +if (is_subprog) { +emit_cfi (&prog, ip, cfi_bpf_subprog_hash,5 ); + }else { +emit_cfi (&prog, ip, cfi_bpf_hash,1 ); + }/* BPF trampoline can be made to work without these nops, * but let's waste 5 bytes for now and optimize later */
@@ -1479,7 +1488,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_imagedetect_reg_usage (insn, insn_cnt, callee_regs_used); -emit_prologue (&prog, stack_depth, +emit_prologue (&prog, image, stack_depth,bpf_prog_was_classic (bpf_prog), tail_call_reachable,bpf_is_subprog (bpf_prog), bpf_prog->aux->exception_cb);/* Exception callback will clobber callee regs for its own use, and
@@ -3046,7 +3055,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im/* * Indirect call for bpf_struct_ops */ -emit_cfi (&prog,cfi_get_func_hash (func_addr)); +emit_cfi (&prog, image, +cfi_get_func_hash (func_addr), +cfi_get_func_arity (func_addr)); }else {/* * Direct-call fentry stub, as such it needs accounting for the
[tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
In reply to Peter ZijlstraThe 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: 0c92385dc05ee9637c04372ea95a11bbf6e010ff Gitweb: https://git.kernel.org/tip/0c92385dc05ee9637c04372ea95a11bbf6e010ff Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:12 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:49:11 +01:00
x86/ibt: Implement FineIBT-BHI mitigation
While WAIT_FOR_ENDBR is specified to be a full speculation stop; it has been shown that some implementations are 'leaky' to such an extend that speculation can escape even the FineIBT preamble.
To deal with this, add additional hardening to the FineIBT preamble.
Notably, using a new LLVM feature:
https://github.com/llvm/llvm-project/commit/e223485c9b38a5579991b8cebb6a200153eee245
which encodes the number of arguments in the kCFI preamble's register.
Using this register<->arity mapping, have the FineIBT preamble CALL into a stub clobbering the relevant argument registers in the speculative case.
Scott sayeth thusly:
Microarchitectural attacks such as Branch History Injection (BHI) and Intra-mode Branch Target Injection (IMBTI) [1] can cause an indirect call to mispredict to an adversary-influenced target within the same hardware domain (e.g., within the kernel). Instructions at the mispredicted target may execute speculatively and potentially expose kernel data (e.g., to a user-mode adversary) through a microarchitectural covert channel such as CPU cache state.
CET-IBT [2] is a coarse-grained control-flow integrity (CFI) ISA extension that enforces that each indirect call (or indirect jump) must land on an ENDBR (end branch) instruction, even speculatively*. FineIBT is a software technique that refines CET-IBT by associating each function type with a 32-bit hash and enforcing (at the callee) that the hash of the caller's function pointer type matches the hash of the callee's function type. However, recent research [3] has demonstrated that the conditional branch that enforces FineIBT's hash check can be coerced to mispredict, potentially allowing an adversary to speculatively bypass the hash check:
__cfi_foo:
ENDBR64
SUB R10d, 0x01234567
JZ foo # Even if the hash check fails and ZF=0, this branch could still mispredict as taken
UD2
foo:
...
The techniques demonstrated in [3] require the attacker to be able to control the contents of at least one live register at the mispredicted target. Therefore, this patch set introduces a sequence of CMOV instructions at each indirect-callable target that poisons every live register with data that the attacker cannot control whenever the FineIBT hash check fails, thus mitigating any potential attack.
The security provided by this scheme has been discussed in detail on an earlier thread [4].
[1] https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html
[2] Intel Software Developer's Manual, Volume 1, Chapter 18
[3] https://www.vusec.net/projects/native-bhi/
[4] https://lore.kernel.org/lkml/[email protected]/
*There are some caveats for certain processors, see [1] for more info
--- Makefile | 3 +- arch/x86/Kconfig | 8 ++- arch/x86/include/asm/cfi.h | 6 ++- arch/x86/kernel/alternative.c | 107 +++++++++++++++++++++++++++++---- arch/x86/net/bpf_jit_comp.c | 29 ++++++--- 5 files changed, 134 insertions(+), 19 deletions(-)
Makefile +3 -0
@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
arch/x86/Kconfig +8 -0
@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST + config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B
@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
arch/x86/include/asm/cfi.h +6 -0
@@ -100,6 +100,7 @@ enum cfi_mode { };extern enum cfi_mode cfi_mode; +extern bool cfi_bhi;typedef u8 bhi_thunk [32 ];extern bhi_thunk __bhi_args[];
@@ -129,6 +130,7 @@ static inline int cfi_get_offset(void)#define cfi_get_offset cfi_get_offsetextern u32 cfi_get_func_hash (void *func); +extern int cfi_get_func_arity (void *func);#ifdef CONFIG_FINEIBT extern bool decode_fineibt_insn (struct pt_regs *regs,unsigned long *target,u32 *type);
@@ -152,6 +154,10 @@ static inline u32 cfi_get_func_hash(void *func) {return 0 ; } +static inline int cfi_get_func_arity (void *func) +{ +return 0 ; +}#endif /* CONFIG_CFI_CLANG */ #if HAS_KERNEL_IBT ==1
arch/x86/kernel/alternative.c +97 -10
207 lines not on this page · raw message
arch/x86/net/bpf_jit_comp.c +20 -9
@@ -410,15 +410,20 @@ static void emit_nops(u8 **pprog, int len)* Emit the various CFI preambles, see asm/cfi.h and the comments about FineIBT * in arch/x86/kernel/alternative.c */ +static int emit_call (u8 **prog,void *func,void *ip); -static void emit_fineibt (u8 **pprog,u32 hash) +static void emit_fineibt (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;EMIT_ENDBR ();EMIT3_off32 (0x41 ,0x81 ,0xea , hash);/* subl $hash, %r10d */ -EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ -EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ +if (cfi_bhi) { +emit_call (&prog, __bhi_args[arity], ip +11 ); + }else { +EMIT2 (0x75 ,0xf9 );/* jne.d8 .-7 */ +EMIT3 (0x0f ,0x1f ,0x00 );/* nop3 */ + }EMIT_ENDBR_POISON (); *pprog = prog;
@@ -447,13 +452,13 @@ static void emit_kcfi(u8 **pprog, u32 hash) *pprog = prog; } -static void emit_cfi (u8 **pprog,u32 hash) +static void emit_cfi (u8 **pprog,u8 *ip,u32 hash,int arity) {u8 *prog = *pprog;switch (cfi_mode) {case CFI_FINEIBT : -emit_fineibt (&prog, hash); +emit_fineibt (&prog, ip, hash, arity);break ;case CFI_KCFI :
@@ -504,13 +509,17 @@ static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)* bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes * while jumping to another program */ -static void emit_prologue (u8 **pprog,u32 stack_depth,bool ebpf_from_cbpf, +static void emit_prologue (u8 **pprog,u8 *ip,u32 stack_depth,bool ebpf_from_cbpf,bool tail_call_reachable,bool is_subprog,bool is_exception_cb) {u8 *prog = *pprog; -emit_cfi (&prog, is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash); +if (is_subprog) { +emit_cfi (&prog, ip, cfi_bpf_subprog_hash,5 ); + }else { +emit_cfi (&prog, ip, cfi_bpf_hash,1 ); + }/* BPF trampoline can be made to work without these nops, * but let's waste 5 bytes for now and optimize later */
@@ -1479,7 +1488,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_imagedetect_reg_usage (insn, insn_cnt, callee_regs_used); -emit_prologue (&prog, stack_depth, +emit_prologue (&prog, image, stack_depth,bpf_prog_was_classic (bpf_prog), tail_call_reachable,bpf_is_subprog (bpf_prog), bpf_prog->aux->exception_cb);/* Exception callback will clobber callee regs for its own use, and
@@ -3046,7 +3055,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im/* * Indirect call for bpf_struct_ops */ -emit_cfi (&prog,cfi_get_func_hash (func_addr)); +emit_cfi (&prog, image, +cfi_get_func_hash (func_addr), +cfi_get_func_arity (func_addr)); }else {/* * Direct-call fentry stub, as such it needs accounting for the
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 19 lines · On Wed, Feb 26, 2025 at 12:54:35PM -0000, tip-bot2 for Peter Zijlstra wrote:
Makefile +3
-0@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.arch/x86/Kconfig +0
-0
arch/x86/Kconfig config CC_HAS_RETURN_THUNK · line 2479
Quoted 7 lines
arch/x86/Kconfig +4
-0@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST +
Miguel, can we work on fixing that !RUST dep?
Quoted 14 lines
arch/x86/Kconfig +4
-0config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Ping -- anything I can do the help?
Quoted 45 lines · Peter Zijlstra
Quoted 19 lines · On Wed, Feb 26, 2025 at 12:54:35PM -0000, tip-bot2 for Peter Zijlstra wrote:
Makefile +3
-0@@ -1014,6 +1014,9 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_FINEIBT_BHI + CC_FLAGS_CFI += -fsanitize-kcfi-arity +endif ifdef CONFIG_RUST # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.arch/x86/Kconfig +0
-0arch/x86/Kconfig config CC_HAS_RETURN_THUNK · line 2479
Quoted 7 lines
arch/x86/Kconfig +4
-0@@ -2473,6 +2473,10 @@ config CC_HAS_RETURN_THUNK config CC_HAS_ENTRY_PADDING def_bool $(cc-option,-fpatchable-function-entry=16,16) +config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST +Miguel, can we work on fixing that !RUST dep?
Quoted 14 lines
arch/x86/Kconfig +4
-0config FUNCTION_PADDING_CFI int default 59 if FUNCTION_ALIGNMENT_64B@@ -2498,6 +2502,10 @@ config FINEIBT depends on X86_KERNEL_IBT && CFI_CLANG && MITIGATION_RETPOLINE select CALL_PADDING +config FINEIBT_BHI + def_bool y + depends on FINEIBT && CC_HAS_KCFI_ARITY + config HAVE_CALL_THUNKS def_bool y depends on CC_HAS_ENTRY_PADDING && MITIGATION_RETHUNK && OBJTOOL
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
In reply to Peter ZijlstraQuoted 9 lines · Peter Zijlstra
arch/x86/Kconfig config CC_HAS_RETURN_THUNK · line 2479
Quoted 4 lines · On Wed, Feb 26, 2025 at 12:54:35PM -0000, tip-bot2 for Peter Zijlstra wrote:
arch/x86/Kconfig +4
-0+config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST +Miguel, can we work on fixing that !RUST dep?
Thanks for the ping Peter -- we discussed rustc in the couple PRs that added it to LLVM back then, and I think the conclusion was that it shouldn't be a fundamental problem to add it to rustc.
From a quick look, the Clang flag that eventually landed just emits one more llvm.module.flags and LLVM takes care of the rest. So it should be straightforward to add a -Csanitize-kcfi-arity in rustc and then pass it at the same time to both Clang and rustc in the kernel.
But I may be missing something -- Cc'ing Ramon and Matthew, since they are the ones behind sanitizers and kCFI in upstream Rust.
I added it to our list, and created an issue in upstream Rust for it:
https://github.com/rust-lang/rust/issues/138311 https://github.com/Rust-for-Linux/linux/issues/355
I will also mention it in the meeting with upstream Rust in a couple days.
Cheers,
Miguel
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 24 lines · Miguel Ojeda
Quoted 9 lines · Peter Zijlstra
arch/x86/Kconfig config CC_HAS_RETURN_THUNK · line 2479
Quoted 4 lines · On Wed, Feb 26, 2025 at 12:54:35PM -0000, tip-bot2 for Peter Zijlstra wrote:
arch/x86/Kconfig +4
-0+config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST +Miguel, can we work on fixing that !RUST dep?
Thanks for the ping Peter -- we discussed
rustcin the couple PRs that added it to LLVM back then, and I think the conclusion was that it shouldn't be a fundamental problem to add it torustc.From a quick look, the Clang flag that eventually landed just emits one more
llvm.module.flagsand LLVM takes care of the rest. So it should be straightforward to add a-Csanitize-kcfi-arityinrustcand then pass it at the same time to both Clang andrustcin the kernel.But I may be missing something -- Cc'ing Ramon and Matthew, since they are the ones behind sanitizers and kCFI in upstream Rust.
Quoted 6 lines
I added it to our list, and created an issue in upstream Rust for it:
https://github.com/rust-lang/rust/issues/138311 https://github.com/Rust-for-Linux/linux/issues/355I will also mention it in the meeting with upstream Rust in a couple days.
Thanks!, let me know if there's anything I can do. I'm happy to test patches.
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 28 lines · Peter Zijlstra
Quoted 26 lines · Miguel Ojeda
Quoted 9 lines · Peter Zijlstra
arch/x86/Kconfig config CC_HAS_RETURN_THUNK · line 2479
Quoted 4 lines · On Wed, Feb 26, 2025 at 12:54:35PM -0000, tip-bot2 for Peter Zijlstra wrote:
arch/x86/Kconfig +4
-0+config CC_HAS_KCFI_ARITY + def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity) + depends on CC_IS_CLANG && !RUST +Miguel, can we work on fixing that !RUST dep?
Thanks for the ping Peter -- we discussed
rustcin the couple PRs that added it to LLVM back then, and I think the conclusion was that it shouldn't be a fundamental problem to add it torustc.From a quick look, the Clang flag that eventually landed just emits one more
llvm.module.flagsand LLVM takes care of the rest. So it should be straightforward to add a-Csanitize-kcfi-arityinrustcand then pass it at the same time to both Clang andrustcin the kernel.But I may be missing something -- Cc'ing Ramon and Matthew, since they are the ones behind sanitizers and kCFI in upstream Rust.
I added it to our list, and created an issue in upstream Rust for it:
Quoted 7 lines
Quoted 4 lines
https://github.com/rust-lang/rust/issues/138311 https://github.com/Rust-for-Linux/linux/issues/355I will also mention it in the meeting with upstream Rust in a couple days.
Thanks!, let me know if there's anything I can do. I'm happy to test patches.
I've submitted a PR for it:
https://github.com/rust-lang/rust/pull/138368. Let me know if you're able to give it a try.
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 4 lines · Ramon de C Valle
I've submitted a PR for it:
https://github.com/rust-lang/rust/pull/138368. Let me know if you're able to give it a try.
Thanks Ramon, that was quick!
Left a comment there and linked it -- if you meant to ask Peter, then I guess it would be easiest for him to use the first nightly that has it as a -Z flag.
i.e. I think it is simple enough to land it as-is, especially if we add the quick check I suggested.
Cheers,
Miguel
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 7 lines · Miguel Ojeda
Quoted 4 lines · Ramon de C Valle
I've submitted a PR for it:
https://github.com/rust-lang/rust/pull/138368. Let me know if you're able to give it a try.
Quoted 8 lines
Thanks Ramon, that was quick!
Left a comment there and linked it -- if you meant to ask Peter, then I guess it would be easiest for him to use the first nightly that has it as a
-Zflag.i.e. I think it is simple enough to land it as-is, especially if we add the quick check I suggested.
Left a note there. Let me know what you think. Yes, I guess we could just check the next nightly that has those changes.
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 15 lines · Ramon de C Valle
Quoted 13 lines · Miguel Ojeda
Quoted 4 lines · Ramon de C Valle
I've submitted a PR for it:
https://github.com/rust-lang/rust/pull/138368. Let me know if you're able to give it a try.Thanks Ramon, that was quick!
Left a comment there and linked it -- if you meant to ask Peter, then I guess it would be easiest for him to use the first nightly that has it as a
-Zflag.
Quoted 5 lines
Quoted 2 lines
i.e. I think it is simple enough to land it as-is, especially if we add the quick check I suggested.
Left a note there. Let me know what you think. Yes, I guess we could just check the next nightly that has those changes.
The rust-log-analyzer seems to suggest the nightly build failed?
Suppose it didn't fail, where do I find it?
I normally build llvm toolchains using tc-build, but it seems rust is not actually part of llvm?
Re: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 4 lines · Peter Zijlstra
The rust-log-analyzer seems to suggest the nightly build failed?
Suppose it didn't fail, where do I find it?
Ah, sorry for the confusion -- by "nightly" here I meant the toolchain that gets built and distributed by the Rust project.
To get one, we need the PR to land first, and then we can use the new flag. It is easy to land such a PR/feature because it lands as an "unstable feature", i.e. meant for testing and so on only. So we can get quickly "proper" toolchains (i.e. tested and built like the full/normal releases are), and if everything checks out, then upstream Rust can commit to stabilize the feature later on.
That is why I suggested to wait for that, since the PR seemed straightforward to land to me, and thus it could land soon.
I don't know if there may be a way to pick up the toolchains that their CI built for testing PRs, though. It is not too hard to build from scratch anyway if you want to do so -- I can also build it for you if you want to test right away before it lands. Otherwise, I can ping you when the nightly toolchain is ready.
Quoted 2 lines
I normally build llvm toolchains using tc-build, but it seems rust is not actually part of llvm?
Yeah, Rust uses LLVM but is not part of the LLVM project.
But I think we could have support to build Rust there easily -- I discussed it with Nathan (Cc'd) in the past. Currently, for the LLVM+Rust toolchains he provides in kernel.org, he just bundles the upstream Rust binaries AFAIR -- and IIRC he uses tc-build to drive that build, so if we do that we could also "easily" get the full chain in kernel.org too ("easily" if we ignore doing the PGO dance for the Rust side etc. and assuming the building time/resources makes it doable, which I don't know about).
If that is correct, I could take a look into adding a simple Rust build to tc-build (i.e. without PGO etc.).
Cheers,
Miguel
Nathan Chancellor
maintainerRe: [tip: x86/core] x86/ibt: Implement FineIBT-BHI mitigation
Quoted 15 lines · Miguel Ojeda
Quoted 2 lines · Peter Zijlstra
I normally build llvm toolchains using tc-build, but it seems rust is not actually part of llvm?
Yeah, Rust uses LLVM but is not part of the LLVM project.
But I think we could have support to build Rust there easily -- I discussed it with Nathan (Cc'd) in the past. Currently, for the LLVM+Rust toolchains he provides in kernel.org, he just bundles the upstream Rust binaries AFAIR -- and IIRC he uses tc-build to drive that build, so if we do that we could also "easily" get the full chain in kernel.org too ("easily" if we ignore doing the PGO dance for the Rust side etc. and assuming the building time/resources makes it doable, which I don't know about).
Quoted 2 lines
If that is correct, I could take a look into adding a simple Rust build to tc-build (i.e. without PGO etc.).
Right, tc-build is used to build the toolchain but I have another build wrapper around that to build the toolchain in a Debian bullseye userspace for compatibility with glibc 2.28 and newer:
That is where I had initially considered wiring it up when we last talked but wiring it up in tc-build would probably be better/cleaner, especially with the rewrite I did two years ago. I could envision tc_build/rust.py for example, then either integrating it into build-llvm.py or have a separate standalone build-rust.py, like binutils.
Cheers,
Nathan
PATCH v4 10/10 x86/ibt: Optimize fineibt-bhi arity 1 case
In reply to Peter ZijlstraSaves a CALL to an out-of-line thunk for the common case of 1 argument.
--- arch/x86/include/asm/ibt.h | 4 ++ arch/x86/kernel/alternative.c | 59 +++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 9 deletions(-)
arch/x86/include/asm/ibt.h +4 -0
@@ -70,6 +70,10 @@ static inline bool __is_endbr(u32 val)if (val ==gen_endbr_poison ())return true; +/* See cfi_fineibt_bhi_preamble() */ +if (IS_ENABLED (CONFIG_FINEIBT_BHI ) && val ==0x001f0ff5 ) +return true; + val &= ~0x01000000U ;/* ENDBR32 -> ENDBR64 */ return val ==gen_endbr (); }
arch/x86/kernel/alternative.c +50 -9
77 lines not on this page · raw message
[tip: x86/core] x86/ibt: Optimize fineibt-bhi arity 1 case
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: af908171aed7b0a7f6376f3ee6c53f40c84ae494 Gitweb: https://git.kernel.org/tip/af908171aed7b0a7f6376f3ee6c53f40c84ae494 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:13 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Wed, 26 Feb 2025 11:41:56 +01:00
x86/ibt: Optimize fineibt-bhi arity 1 case
Saves a CALL to an out-of-line thunk for the common case of 1 argument.
--- arch/x86/include/asm/ibt.h | 4 ++- arch/x86/kernel/alternative.c | 59 ++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-)
arch/x86/include/asm/ibt.h +4 -0
@@ -70,6 +70,10 @@ static inline bool __is_endbr(u32 val)if (val ==gen_endbr_poison ())return true; +/* See cfi_fineibt_bhi_preamble() */ +if (IS_ENABLED (CONFIG_FINEIBT_BHI ) && val ==0x001f0ff5 ) +return true; + val &= ~0x01000000U ;/* ENDBR32 -> ENDBR64 */ return val ==gen_endbr (); }
arch/x86/kernel/alternative.c +50 -9
77 lines not on this page · raw message
[tip: x86/core] x86/ibt: Optimize the fineibt-bhi arity 1 case
In reply to Peter ZijlstraThe 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: 496ce4741ccbc91838df4bf9e137eda99c61f500 Gitweb: https://git.kernel.org/tip/496ce4741ccbc91838df4bf9e137eda99c61f500 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:13 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 12:30:52 +01:00
x86/ibt: Optimize the fineibt-bhi arity 1 case
Saves a CALL to an out-of-line thunk for the common case of 1 argument.
--- arch/x86/include/asm/ibt.h | 4 ++- arch/x86/kernel/alternative.c | 59 ++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-)
arch/x86/include/asm/ibt.h +4 -0
@@ -70,6 +70,10 @@ static inline bool __is_endbr(u32 val)if (val ==gen_endbr_poison ())return true; +/* See cfi_fineibt_bhi_preamble() */ +if (IS_ENABLED (CONFIG_FINEIBT_BHI ) && val ==0x001f0ff5 ) +return true; + val &= ~0x01000000U ;/* ENDBR32 -> ENDBR64 */ return val ==gen_endbr (); }
arch/x86/kernel/alternative.c +50 -9
77 lines not on this page · raw message
[tip: x86/core] x86/ibt: Optimize the fineibt-bhi arity 1 case
In reply to Peter ZijlstraThe 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: dfebe7362f6f461d771cdb9ac2c5172a4721f064 Gitweb: https://git.kernel.org/tip/dfebe7362f6f461d771cdb9ac2c5172a4721f064 Author: Peter Zijlstra <[email protected]> AuthorDate: Mon, 24 Feb 2025 13:37:13 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 26 Feb 2025 13:49:11 +01:00
x86/ibt: Optimize the fineibt-bhi arity 1 case
Saves a CALL to an out-of-line thunk for the common case of 1 argument.
--- arch/x86/include/asm/ibt.h | 4 ++- arch/x86/kernel/alternative.c | 59 ++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-)
arch/x86/include/asm/ibt.h +4 -0
@@ -70,6 +70,10 @@ static inline bool __is_endbr(u32 val)if (val ==gen_endbr_poison ())return true; +/* See cfi_fineibt_bhi_preamble() */ +if (IS_ENABLED (CONFIG_FINEIBT_BHI ) && val ==0x001f0ff5 ) +return true; + val &= ~0x01000000U ;/* ENDBR32 -> ENDBR64 */ return val ==gen_endbr (); }
arch/x86/kernel/alternative.c +50 -9
77 lines not on this page · raw message