Control-flow Enforcement: Indirect Branch Tracking
Subsystems (6)
v9 cover Control-flow Enforcement: Indirect Branch Tracking
Control-flow Enforcement (CET) is a new Intel processor feature that blocks return/jump-oriented programming attacks. Details can be found in "Intel 64 and IA-32 Architectures Software Developer's Manual" [1].
This is the second half of CET and enables Indirect Branch Tracking (IBT).
Changes from v8:
- Remove a patch that adds the legacy bitmap size to memory accounting, since the bitmap is now dynamically allocated.
- Change the legacy bitmap from a pre-defined address to get_unmapped_area().
- Fix mis-handling of WAIT_ENDBR in signals
- Split out PTRACE, VDSO, opcode map, and Makefile changes and submit separately.
[1] Intel 64 and IA-32 Architectures Software Developer's Manual:
https://software.intel.com/en-us/download/intel-64-and-ia-32- architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4
[2] CET patches v8:
https://lkml.kernel.org/r/20190813205225.12032-1-yu-cheng.yu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/ https://lkml.kernel.org/r/20190813205359.12196-1-yu-cheng.yu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org/
H.J. Lu (1):
x86/cet/ibt: Add arch_prctl functions for Indirect Branch Tracking
Yu-cheng Yu (6):
x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking x86/cet/ibt: User-mode Indirect Branch Tracking support x86/cet/ibt: Handle signals for Indirect Branch Tracking x86/cet/ibt: ELF header parsing for Indirect Branch Tracking
mm: Update alloc_set_pte() for zero page
x86/cet/ibt: Introduce arch_prctl(ARCH_X86_CET_MARK_LEGACY_CODE)
arch/x86/Kconfig | 17 ++ arch/x86/Makefile | 7 + arch/x86/include/asm/cet.h | 7 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/uapi/asm/prctl.h | 3 + arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/cet.c | 58 ++++- arch/x86/kernel/cet_bitmap.c | 226 ++++++++++++++++++ arch/x86/kernel/cet_prctl.c | 19 ++ arch/x86/kernel/cpu/common.c | 17 ++ arch/x86/kernel/fpu/signal.c | 8 +- arch/x86/kernel/process_64.c | 5 + mm/memory.c | 8 + .../arch/x86/include/asm/disabled-features.h | 8 +- 14 files changed, 385 insertions(+), 8 deletions(-) create mode 100644 arch/x86/kernel/cet_bitmap.c
Signature · 1 line
2.21.0
RFC PATCH v9 1/7 x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking
Introduce Kconfig option X86_INTEL_BRANCH_TRACKING_USER.
Indirect Branch Tracking (IBT) provides protection against CALL-/JMP- oriented programming attacks. It is active when the kernel has this feature enabled, and the processor and the application support it. When this feature is enabled, legacy non-IBT applications continue to work, but without IBT protection.
--- arch/x86/Kconfig | 15 +++++++++++++++ arch/x86/Makefile | 7 +++++++ 2 files changed, 22 insertions(+)
arch/x86/Kconfig +15 -0
@@ -1998,6 +1998,21 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y. +config X86_INTEL_BRANCH_TRACKING_USER + prompt "Intel Indirect Branch Tracking for user-mode" + def_bool n + depends on CPU_SUP_INTEL && X86_64 + select X86_INTEL_CET + ---help--- + Indirect Branch Tracking (IBT) provides protection against + CALL-/JMP-oriented programming attacks. It is active when + the kernel has this feature enabled, and the processor and + the application support it. When this feature is enabled, + legacy non-IBT applications continue to work, but without + IBT protection. + + If unsure, say y + config EFI bool "EFI runtime service support" depends on ACPI
arch/x86/Makefile +7 -0
@@ -156,6 +156,13 @@ ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER endif endif +# Check compiler ibt support +ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + ifeq ($(call cc-option-yn, -fcf-protection=branch), n) + $(error CONFIG_X86_INTEL_BRANCH_TRACKING_USER not supported by compiler) + endif +endif + # # If the function graph tracer is used with mcount instead of fentry, # '-maccumulate-outgoing-args' is needed to prevent a GCC bug
Signature · 1 line
2.21.0
RFC PATCH v9 2/7 x86/cet/ibt: User-mode Indirect Branch Tracking support
In reply to Yu-cheng YuIntroduce user-mode Indirect Branch Tracking (IBT) support. Update setup routines to include IBT.
v9:
- Change cpu_feature_enabled() to static_cpu_has().
v2:
- Change noibt to no_cet_ibt.
--- arch/x86/include/asm/cet.h | 5 +++ arch/x86/include/asm/disabled-features.h | 8 ++++- arch/x86/kernel/cet.c | 34 +++++++++++++++++++ arch/x86/kernel/cpu/common.c | 17 ++++++++++ .../arch/x86/include/asm/disabled-features.h | 8 ++++- 5 files changed, 70 insertions(+), 2 deletions(-)
arch/x86/include/asm/cet.h +5 -0
@@ -16,6 +16,9 @@ struct cet_status {unsigned long shstk_size; unsigned int locked:1; unsigned int shstk_enabled:1; + unsigned int ibt_enabled:1; + unsigned int ibt_bitmap_used:1; +unsigned long ibt_bitmap_base; };#ifdef CONFIG_X86_INTEL_CET
@@ -26,6 +29,8 @@ int cet_alloc_shstk(unsigned long *arg);void cet_disable_free_shstk (struct task_struct *p);int cet_restore_signal (bool ia32,struct sc_ext *sc);int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc); +int cet_setup_ibt (void ); +void cet_disable_ibt (void );#else static inline int prctl_cet (int option,unsigned long arg2) {return -EINVAL ; }static inline int cet_setup_thread_shstk (struct task_struct *p) {return 0 ; }
arch/x86/include/asm/disabled-features.h +7 -1
@@ -68,6 +68,12 @@#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31))#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -89,7 +95,7 @@#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)#endif /* _ASM_X86_DISABLED_FEATURES_H */
arch/x86/kernel/cet.c +34 -0
@@ -13,6 +13,8 @@#include <linux/uaccess.h> #include <linux/sched/signal.h> #include <linux/compat.h> +#include <linux/vmalloc.h> +#include <linux/bitops.h> #include <asm/msr.h> #include <asm/user.h> #include <asm/fpu/internal.h>
@@ -342,3 +344,35 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext)end_update_msrs ();return 0 ; +} + +int cet_setup_ibt (void ) +{ +u64 msr_val; + +if (!static_cpu_has (X86_FEATURE_IBT )) +return -EOPNOTSUPP ; + +start_update_msrs (); +rdmsrl (MSR_IA32_U_CET , msr_val); + msr_val |= (MSR_IA32_CET_ENDBR_EN |MSR_IA32_CET_NO_TRACK_EN ); +wrmsrl (MSR_IA32_U_CET , msr_val); +end_update_msrs (); + current->thread.cet.ibt_enabled =1 ; +return 0 ; +} + +void cet_disable_ibt (void ) +{ +u64 msr_val; + +if (!static_cpu_has (X86_FEATURE_IBT )) +return ; + +start_update_msrs (); +rdmsrl (MSR_IA32_U_CET , msr_val); + msr_val &=MSR_IA32_CET_SHSTK_EN ; +wrmsrl (MSR_IA32_U_CET , msr_val); +end_update_msrs (); + current->thread.cet.ibt_enabled =0 ; +}
arch/x86/kernel/cpu/common.c +17 -0
@@ -510,6 +510,23 @@ static __init int setup_disable_shstk(char *s)__setup ("no_cet_shstk" , setup_disable_shstk);#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +static __init int setup_disable_ibt (char *s) +{ +/* require an exact match without trailing characters */ +if (s[0 ] !='\0' ) +return 0 ; + +if (!boot_cpu_has (X86_FEATURE_IBT )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_IBT ); +pr_info ("x86: 'no_cet_ibt' specified, disabling Branch Tracking\n" ); +return 1 ; +} +__setup ("no_cet_ibt" , setup_disable_ibt); +#endif +/* * Some CPU features depend on higher CPUID levels, which may not always * be available due to CPUID level capping or broken virtualization
tools/arch/x86/include/asm/disabled-features.h +7 -1
@@ -68,6 +68,12 @@#define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31))#endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -89,7 +95,7 @@#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK)#define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19)#endif /* _ASM_X86_DISABLED_FEATURES_H */
Signature · 1 line
2.21.0
RFC PATCH v9 3/7 x86/cet/ibt: Handle signals for Indirect Branch Tracking
In reply to Yu-cheng YuIndirect Branch Tracking setting does not change in signal delivering or sigreturn; except the WAIT_ENDBR status. In general, a task is in WAIT_ENDBR after an indirect CALL/JMP and before the next instruction starts.
WAIT_ENDBR status can be read from MSR_IA32_U_CET. It is reset for signal delivering, but preserved on a task's stack and restored for sigreturn.
v9:
- Fix missing WAIT_ENDBR in signal handling.
--- arch/x86/kernel/cet.c | 24 ++++++++++++++++++++++-- arch/x86/kernel/fpu/signal.c | 8 +++++--- 2 files changed, 27 insertions(+), 5 deletions(-)
arch/x86/kernel/cet.c +22 -2
@@ -280,7 +280,7 @@ int cet_restore_signal(bool ia32, struct sc_ext *sc_ext)u64 msr_val =0 ;int err; -if (!cet->shstk_enabled) +if (!cet->shstk_enabled && !cet->ibt_enabled)return 0 ; cet_user_state = get_xsave_addr(¤t->thread.fpu.state.xsave,
@@ -297,6 +297,16 @@ int cet_restore_signal(bool ia32, struct sc_ext *sc_ext) msr_val |=MSR_IA32_CET_SHSTK_EN ; } +if (cet->ibt_enabled) { + msr_val |= (MSR_IA32_CET_ENDBR_EN |MSR_IA32_CET_NO_TRACK_EN ); + +if (cet->ibt_bitmap_used) + msr_val |= (cet->ibt_bitmap_base |MSR_IA32_CET_LEG_IW_EN ); + +if (sc_ext->wait_endbr) + msr_val |=MSR_IA32_CET_WAIT_ENDBR ; + } + cet_user_state->user_cet = msr_val;return 0 ; }
@@ -312,7 +322,7 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext)unsigned long ssp =0 , new_ssp =0 ;int err; -if (!cet->shstk_enabled) +if (!cet->shstk_enabled && !cet->ibt_enabled)return 0 ;if (cet->shstk_enabled) {
@@ -339,6 +349,16 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext) }start_update_msrs (); +if (cet->ibt_enabled) { +u64 r; + +rdmsrl (MSR_IA32_U_CET , r); +if (r &MSR_IA32_CET_WAIT_ENDBR ) { + sc_ext->wait_endbr =1 ; +wrmsrl (MSR_IA32_U_CET , r & ~MSR_IA32_CET_WAIT_ENDBR ); + } + } +if (cet->shstk_enabled)wrmsrl (MSR_IA32_PL3_SSP , ssp);end_update_msrs ();
arch/x86/kernel/fpu/signal.c +5 -3
@@ -57,7 +57,8 @@ int save_cet_to_sigframe(void __user *fp, unsigned long restorer, int is_ia32)int err =0 ;#ifdef CONFIG_X86_INTEL_CET -if (!current->thread.cet.shstk_enabled) +if (!current->thread.cet.shstk_enabled && + !current->thread.cet.ibt_enabled)return 0 ;if (fp) {
@@ -89,7 +90,8 @@ static int restore_cet_from_sigframe(int is_ia32, void __user *fp)int err =0 ;#ifdef CONFIG_X86_INTEL_CET -if (!current->thread.cet.shstk_enabled) +if (!current->thread.cet.shstk_enabled && + !current->thread.cet.ibt_enabled)return 0 ;if (fp) {
@@ -548,7 +550,7 @@ static unsigned long fpu__alloc_sigcontext_ext(unsigned long sp)if (cpu_x86_cet_enabled ()) {struct cet_status *cet = ¤t->thread.cet; -if (cet->shstk_enabled) +if (cet->shstk_enabled || cet->ibt_enabled) sp -= (sizeof (struct sc_ext ) +8 ); }
Signature · 1 line
2.21.0
RFC PATCH v9 4/7 x86/cet/ibt: ELF header parsing for Indirect Branch Tracking
In reply to Yu-cheng YuUpdate arch_setup_elf_property() for Indirect Branch Tracking.
v9:
- Change cpu_feature_enabled() to static_cpu_has().
--- arch/x86/Kconfig | 2 ++ arch/x86/kernel/process_64.c | 5 +++++ 2 files changed, 7 insertions(+)
arch/x86/Kconfig +2 -0
@@ -2003,6 +2003,8 @@ config X86_INTEL_BRANCH_TRACKING_USER def_bool n depends on CPU_SUP_INTEL && X86_64 select X86_INTEL_CET + select ARCH_USE_GNU_PROPERTY + select ARCH_BINFMT_ELF_STATE ---help--- Indirect Branch Tracking (IBT) provides protection against CALL-/JMP-oriented programming attacks. It is active when
arch/x86/kernel/process_64.c +5 -0
@@ -760,6 +760,11 @@ int arch_setup_elf_property(struct arch_elf_state *state) return r; } +if (static_cpu_has (X86_FEATURE_IBT )) { +if (state->gnu_property &GNU_PROPERTY_X86_FEATURE_1_IBT ) + r =cet_setup_ibt (); + } +return r; }#endif
Signature · 1 line
2.21.0
RFC PATCH v9 5/7 x86/cet/ibt: Add arch_prctl functions for Indirect Branch Tracking
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
Update ARCH_X86_CET_STATUS and ARCH_X86_CET_DISABLE for Indirect Branch Tracking.
--- arch/x86/kernel/cet_prctl.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kernel/cet_prctl.c +4 -0
@@ -20,6 +20,8 @@ static int handle_get_status(unsigned long arg2)if (cet->shstk_enabled) features |=GNU_PROPERTY_X86_FEATURE_1_SHSTK ; +if (cet->ibt_enabled) + features |=GNU_PROPERTY_X86_FEATURE_1_IBT ; buf[0 ] = (unsigned long )features; buf[1 ] = cet->shstk_base;
@@ -68,6 +70,8 @@ int prctl_cet(int option, unsigned long arg2)return -EPERM ;if (arg2 &GNU_PROPERTY_X86_FEATURE_1_SHSTK )cet_disable_free_shstk (current); +if (arg2 &GNU_PROPERTY_X86_FEATURE_1_IBT ) +cet_disable_ibt ();return 0 ;
Signature · 1 line
2.21.0
RFC PATCH v9 6/7 mm: Update alloc_set_pte() for zero page
In reply to Yu-cheng YuUpdate alloc_set_pte() for the zero-page case. A special mapping can also use a zero page for read. One use case is the Indirect Branch Tracking legacy code page (introduced next).
--- mm/memory.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/memory.c +8 -0
@@ -3447,6 +3447,12 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,flush_icache_page (vma, page); entry =mk_pte (page, vma->vm_page_prot); + +if (is_zero_pfn (pte_pfn (entry))) { + entry =pte_mkspecial (entry); + goto alloc_set_pte_out; + } +if (write) entry =maybe_mkwrite (pte_mkdirty (entry), vma);/* copy-on-write page */
@@ -3459,6 +3465,8 @@ vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,inc_mm_counter_fast (vma->vm_mm,mm_counter_file (page));page_add_file_rmap (page, false); } + +alloc_set_pte_out:set_pte_at (vma->vm_mm, vmf->address, vmf->pte, entry);/* no need to invalidate: a not-present page won't be cached */
Signature · 1 line
2.21.0
RFC PATCH v9 7/7 x86/cet/ibt: Introduce arch_prctl(ARCH_X86_CET_MARK_LEGACY_CODE)
In reply to Yu-cheng YuWhen Indirect Branch Tracking (IBT) is active, non-IBT-compatible legacy code can be executed if its address range is specified in the legacy code bitmap. Each bit in the bitmap indicates a 4-KB legacy code page.
The bitmap is allocated when the first time an application calls arch_prctl(ARCH_X86_MARK_LEGACY_CODE). It is setup as a special mapping. The application can read the bitmap but not write to; it manages the bitmap with the arch_prctl() being introduced:
arch_prctl(ARCH_X86_CET_MARK_LEGACY_CODE, unsigned long *buf)
Mark an address range as IBT legacy code.
*buf: starting linear address
*(buf + 1): size of the legacy code
*(buf + 2): set (1); clear (0)
v9:
- Split out special mapping zero page handling to the previous patch.
- Change the bitmap from a pre-defined address to get_unmapped_area().
v8:
- Change legacy bitmap to a special mapping.
--- arch/x86/include/asm/cet.h | 2 + arch/x86/include/uapi/asm/prctl.h | 3 + arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/cet_bitmap.c | 226 ++++++++++++++++++++++++++++++ arch/x86/kernel/cet_prctl.c | 15 ++ 5 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/cet_bitmap.c
arch/x86/include/asm/cet.h +2 -0
@@ -4,6 +4,7 @@#ifndef __ASSEMBLY__#include <linux/types.h> +#include <asm/processor.h> struct task_struct ;struct sc_ext ;
@@ -30,6 +31,7 @@ void cet_disable_free_shstk(struct task_struct *p);int cet_restore_signal (bool ia32,struct sc_ext *sc);int cet_setup_signal (bool ia32,unsigned long rstor,struct sc_ext *sc);int cet_setup_ibt (void ); +int cet_mark_legacy_code (unsigned long addr,unsigned long size,unsigned long set);void cet_disable_ibt (void );#else static inline int prctl_cet (int option,unsigned long arg2) {return -EINVAL ; }
arch/x86/include/uapi/asm/prctl.h +3 -0
@@ -18,5 +18,8 @@#define ARCH_X86_CET_DISABLE 0x3002#define ARCH_X86_CET_LOCK 0x3003#define ARCH_X86_CET_ALLOC_SHSTK 0x3004 +#define ARCH_X86_CET_GET_LEGACY_BITMAP 0x3005/* deprecated */ +#define ARCH_X86_CET_SET_LEGACY_BITMAP 0x3006/* deprecated */ +#define ARCH_X86_CET_MARK_LEGACY_CODE 0x3007#endif /* _ASM_X86_PRCTL_H */
arch/x86/kernel/Makefile +1 -1
@@ -142,7 +142,7 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o -obj-$(CONFIG_X86_INTEL_CET) += cet.o cet_prctl.o +obj-$(CONFIG_X86_INTEL_CET) += cet.o cet_prctl.o cet_bitmap.o ### # 64 bit specific files
arch/x86/kernel/cet_bitmap.c new file +226 -0
226 lines not on this page · raw message
arch/x86/kernel/cet_prctl.c +15 -0
@@ -54,6 +54,18 @@ static int handle_alloc_shstk(unsigned long arg2)return 0 ; } +static int handle_mark_legacy_code (unsigned long arg2) +{ +unsigned long addr, size, set; + +if (get_user (addr, (unsigned long __user *)arg2) || +get_user (size, (unsigned long __user *)arg2 +1 ) || +get_user (set, (unsigned long __user *)arg2 +2 )) +return -EFAULT ; + +return cet_mark_legacy_code (addr, size, set); +} +int prctl_cet (int option,unsigned long arg2) {struct cet_status *cet = ¤t->thread.cet;
@@ -82,6 +94,9 @@ int prctl_cet(int option, unsigned long arg2)case ARCH_X86_CET_ALLOC_SHSTK :return handle_alloc_shstk (arg2); +case ARCH_X86_CET_MARK_LEGACY_CODE : +return handle_mark_legacy_code (arg2); +default :return -EINVAL ; }
Signature · 1 line
2.21.0