Control Flow Enforcement - Part (4)
Subsystems (9)
cover Control Flow Enforcement - Part (4)
This series introduces CET - indirect branch tracking
The major task of indirect branch tracking is for the compiler to insert the ENDBR instructions at all valid branch targets.
The kernel provides:
CPUID enumeration and feature setup; Legacy bitmap allocation; Some basic supporting routines.
In this patch, there are also a CET command-line utility and PTRACE support.
H.J. Lu (2):
x86: Insert endbr32/endbr64 to vDSO
tools: Add cetcmd
Yu-cheng Yu (5):
x86/cet: Add Kconfig option for user-mode Indirect Branch Tracking x86/cet: User-mode indirect branch tracking support mm/mmap: Add IBT bitmap size to address space limit check x86/cet: add arcp_prctl functions for indirect branch tracking x86/cet: Add PTRACE interface for CET
arch/x86/Kconfig | 12 +++ arch/x86/entry/vdso/.gitignore | 4 + arch/x86/entry/vdso/Makefile | 34 +++++++ arch/x86/entry/vdso/endbr.sh | 32 ++++++ arch/x86/include/asm/cet.h | 9 ++ arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/fpu/regset.h | 7 +- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/include/uapi/asm/resource.h | 5 + arch/x86/kernel/cet.c | 73 ++++++++++++++ arch/x86/kernel/cet_prctl.c | 54 +++++++++- arch/x86/kernel/cpu/common.c | 20 +++- arch/x86/kernel/elf.c | 19 +++- arch/x86/kernel/fpu/regset.c | 41 ++++++++ arch/x86/kernel/process.c | 2 + arch/x86/kernel/ptrace.c | 16 +++ include/uapi/asm-generic/resource.h | 3 + include/uapi/linux/elf.h | 1 + mm/mmap.c | 8 +- tools/Makefile | 13 +-- tools/arch/x86/include/uapi/asm/elf_property.h | 16 +++ tools/arch/x86/include/uapi/asm/prctl.h | 33 ++++++ tools/cet/.gitignore | 1 + tools/cet/Makefile | 11 ++ tools/cet/cetcmd.c | 134 +++++++++++++++++++++++++ tools/include/uapi/asm/elf_property.h | 4 + tools/include/uapi/asm/prctl.h | 4 + 27 files changed, 549 insertions(+), 16 deletions(-) create mode 100644 arch/x86/entry/vdso/endbr.sh create mode 100644 tools/arch/x86/include/uapi/asm/elf_property.h create mode 100644 tools/arch/x86/include/uapi/asm/prctl.h create mode 100644 tools/cet/.gitignore create mode 100644 tools/cet/Makefile create mode 100644 tools/cet/cetcmd.c create mode 100644 tools/include/uapi/asm/elf_property.h create mode 100644 tools/include/uapi/asm/prctl.h
Signature · 1 line
2.15.1
PATCH 1/7 x86/cet: Add Kconfig option for user-mode Indirect Branch Tracking
The user-mode indirect branch tracking support is done mostly by GCC to insert ENDBR64/ENDBR32 instructions at branch targets. The kernel provides CPUID enumeration, feature MSR setup and the allocation of legacy bitmap.
--- arch/x86/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/Kconfig +12 -0
@@ -1953,6 +1953,18 @@ 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 + select ARCH_HAS_PROGRAM_PROPERTIES + ---help--- + Indirect Branch Tracking provides hardware protection against return-/jmp- + oriented programing attacks. + + If unsure, say y + config EFI bool "EFI runtime service support" depends on ACPI
Signature · 1 line
2.15.1
PATCH 2/7 x86/cet: User-mode indirect branch tracking support
In reply to Yu-cheng YuAdd user-mode indirect branch tracking enabling/disabling and supporting routines.
--- arch/x86/include/asm/cet.h | 8 ++++ arch/x86/include/asm/disabled-features.h | 8 +++- arch/x86/kernel/cet.c | 73 ++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/common.c | 20 ++++++++- arch/x86/kernel/elf.c | 15 ++++++- arch/x86/kernel/process.c | 1 + 6 files changed, 122 insertions(+), 3 deletions(-)
arch/x86/include/asm/cet.h +8 -0
@@ -13,7 +13,10 @@ struct cet_stat {unsigned long shstk_base;unsigned long shstk_size;unsigned long exec_shstk_size; +unsigned long ibt_bitmap_addr; +unsigned long ibt_bitmap_size; unsigned int shstk_enabled:1; + unsigned int ibt_enabled:1; unsigned int locked:1; unsigned int exec_shstk:2; };
@@ -29,6 +32,9 @@ void cet_disable_shstk(void);void cet_disable_free_shstk (struct task_struct *p);int cet_restore_signal (unsigned long ssp);int cet_setup_signal (int ia32,unsigned long addr); +int cet_setup_ibt (void ); +int cet_setup_ibt_bitmap (void ); +void cet_disable_ibt (void );#else static inline int prctl_cet (int option,unsigned long arg2) {return 0 ; }static inline unsigned long cet_get_shstk_ptr (void ) {return 0 ; }
@@ -41,6 +47,8 @@ static inline void cet_disable_shstk(void) {}static inline void cet_disable_free_shstk (struct task_struct *p) {}static inline int cet_restore_signal (unsigned long ssp) {return 0 ; }static inline int cet_setup_signal (int ia32,unsigned long addr) {return 0 ; } +static inline int cet_setup_ibt (void ) {return 0 ; } +static inline void cet_disable_ibt (void ) {}#endif #endif /* __ASSEMBLY__ */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -62,6 +62,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 */
@@ -72,7 +78,7 @@#define DISABLED_MASK4 (DISABLE_PCID)#define DISABLED_MASK5 0#define DISABLED_MASK6 0 -#define DISABLED_MASK7 (DISABLE_PTI) +#define DISABLED_MASK7 (DISABLE_PTI|DISABLE_IBT)#define DISABLED_MASK8 0#define DISABLED_MASK9 (DISABLE_MPX)#define DISABLED_MASK10 0
arch/x86/kernel/cet.c +73 -0
82 lines not on this page · raw message
arch/x86/kernel/cpu/common.c +19 -1
@@ -403,7 +403,8 @@ __setup("nopku", setup_disable_pku); static __always_inline void setup_cet(struct cpuinfo_x86 *c) { -if (cpu_feature_enabled (X86_FEATURE_SHSTK )) +if (cpu_feature_enabled (X86_FEATURE_SHSTK ) || +cpu_feature_enabled (X86_FEATURE_IBT ))cr4_set_bits (X86_CR4_CET ); }
@@ -424,6 +425,23 @@ static __init int setup_disable_shstk(char *s)__setup ("noshstk" , 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 (strlen (s)) +return 0 ; + +if (!boot_cpu_has (X86_FEATURE_IBT )) +return 1 ; + +setup_clear_cpu_cap (X86_FEATURE_IBT ); +pr_info ("x86: 'noibt' specified, disabling Branch Tracking\n" ); +return 1 ; +} +__setup ("noibt" , 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
arch/x86/kernel/elf.c +14 -1
@@ -18,6 +18,7 @@#include <linux/fs.h> #include <linux/uaccess.h> #include <linux/string.h> +#include <linux/compat.h> #define ELF_NOTE_DESC_OFFSET (n, align) \ round_up(sizeof(*n) + n->n_namesz, (align))
@@ -183,7 +184,8 @@ int arch_setup_features(void *ehdr_p, void *phdr_p,struct elf64_hdr *ehdr64 = ehdr_p; -if (!cpu_feature_enabled (X86_FEATURE_SHSTK )) +if (!cpu_feature_enabled (X86_FEATURE_SHSTK ) && + !cpu_feature_enabled (X86_FEATURE_IBT ))return 0 ;if (ehdr64->e_ident[EI_CLASS ] ==ELFCLASS64 ) {
@@ -211,6 +213,9 @@ int arch_setup_features(void *ehdr_p, void *phdr_p, current->thread.cet.shstk_enabled =0 ; current->thread.cet.shstk_base =0 ; current->thread.cet.shstk_size =0 ; + current->thread.cet.ibt_enabled =0 ; + current->thread.cet.ibt_bitmap_addr =0 ; + current->thread.cet.ibt_bitmap_size =0 ; current->thread.cet.locked =0 ;if (cpu_feature_enabled (X86_FEATURE_SHSTK )) {int exec = current->thread.cet.exec_shstk;
@@ -224,6 +229,14 @@ int arch_setup_features(void *ehdr_p, void *phdr_p, } } +if (cpu_feature_enabled (X86_FEATURE_IBT )) { +if (ibt) { + err =cet_setup_ibt (); +if (err <0 ) + goto out; + } + } +/* * Lockout CET features if no interpreter */
arch/x86/kernel/process.c +1 -0
@@ -139,6 +139,7 @@ void flush_thread(void)memset (tsk->thread.tls_array,0 ,sizeof (tsk->thread.tls_array));cet_disable_shstk (); +cet_disable_ibt ();fpu__clear (&tsk->thread.fpu); }
Signature · 1 line
2.15.1
PATCH 3/7 mm/mmap: Add IBT bitmap size to address space limit check
In reply to Yu-cheng YuThe indirect branch tracking legacy bitmap takes a large address space. This causes may_expand_vm() failure on the address limit check. For a IBT-enabled task, add the bitmap size to the address limit.
--- arch/x86/include/uapi/asm/resource.h | 5 +++++ include/uapi/asm-generic/resource.h | 3 +++ mm/mmap.c | 8 +++++++- 3 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/resource.h +5 -0
@@ -1 +1,6 @@ +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */ +#ifdef CONFIG_X86_INTEL_CET +#define rlimit_as_extra () current->thread.cet.ibt_bitmap_size +#endif +#include <asm-generic/resource.h>
include/uapi/asm-generic/resource.h +3 -0
@@ -58,5 +58,8 @@# define RLIM_INFINITY (~0UL)#endif +#ifndef rlimit_as_extra +#define rlimit_as_extra () 0 +#endif #endif /* _UAPI_ASM_GENERIC_RESOURCE_H */
mm/mmap.c +7 -1
@@ -3255,7 +3255,13 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,*/ bool may_expand_vm (struct mm_struct *mm,vm_flags_t flags,unsigned long npages) { -if (mm->total_vm + npages >rlimit (RLIMIT_AS ) >>PAGE_SHIFT ) +unsigned long as_limit =rlimit (RLIMIT_AS ); +unsigned long as_limit_plus = as_limit +rlimit_as_extra (); + +if (as_limit_plus > as_limit) + as_limit = as_limit_plus; + +if (mm->total_vm + npages > as_limit >>PAGE_SHIFT )return false; if (is_data_mapping(flags) &&
Signature · 1 line
2.15.1
PATCH 4/7 x86/cet: add arcp_prctl functions for indirect branch tracking
In reply to Yu-cheng Yu--- arch/x86/include/asm/cet.h | 1 + arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/cet_prctl.c | 54 ++++++++++++++++++++++++++++++++++++--- arch/x86/kernel/elf.c | 12 ++++++--- arch/x86/kernel/process.c | 1 + 5 files changed, 62 insertions(+), 7 deletions(-)
arch/x86/include/asm/cet.h +1 -0
@@ -19,6 +19,7 @@ struct cet_stat { unsigned int ibt_enabled:1; unsigned int locked:1; unsigned int exec_shstk:2; + unsigned int exec_ibt:2; };#ifdef CONFIG_X86_INTEL_CET
arch/x86/include/uapi/asm/prctl.h +1 -0
@@ -20,6 +20,7 @@#define ARCH_CET_EXEC 0x3004#define ARCH_CET_ALLOC_SHSTK 0x3005#define ARCH_CET_PUSH_SHSTK 0x3006 +#define ARCH_CET_LEGACY_BITMAP 0x3007/* * Settings for ARCH_CET_EXEC
arch/x86/kernel/cet_prctl.c +51 -3
96 lines not on this page · raw message
arch/x86/kernel/elf.c +8 -4
@@ -230,10 +230,14 @@ int arch_setup_features(void *ehdr_p, void *phdr_p, }if (cpu_feature_enabled (X86_FEATURE_IBT )) { -if (ibt) { - err =cet_setup_ibt (); -if (err <0 ) - goto out; +int exec = current->thread.cet.exec_ibt; + +if (exec !=CET_EXEC_ALWAYS_OFF ) { +if (ibt || (exec ==CET_EXEC_ALWAYS_ON )) { + err =cet_setup_ibt (); +if (err <0 ) + goto out; + } } }
arch/x86/kernel/process.c +1 -0
@@ -801,6 +801,7 @@ long do_arch_prctl_common(struct task_struct *task, int option,case ARCH_CET_EXEC :case ARCH_CET_ALLOC_SHSTK :case ARCH_CET_PUSH_SHSTK : +case ARCH_CET_LEGACY_BITMAP :return prctl_cet (option, cpuid_enabled); }
Signature · 1 line
2.15.1
PATCH 5/7 x86: Insert endbr32/endbr64 to vDSO
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
--- arch/x86/entry/vdso/.gitignore | 4 ++++ arch/x86/entry/vdso/Makefile | 34 ++++++++++++++++++++++++++++++++++ arch/x86/entry/vdso/endbr.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 arch/x86/entry/vdso/endbr.sh
arch/x86/entry/vdso/.gitignore +4 -0
@@ -5,3 +5,7 @@ vdso32-sysenter-syms.lds vdso32-int80-syms.lds vdso-image-*.c vdso2c +vclock_gettime.S +vgetcpu.S +vclock_gettime.asm +vgetcpu.asm
arch/x86/entry/vdso/Makefile +34 -0
@@ -118,6 +118,40 @@ $(obj)/%-x32.o: $(obj)/%.o FORCE targets += vdsox32.lds $(vobjx32s-y) +ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER + ifeq ($(call cc-option-yn, -fcf-protection=branch -mibt), y) + $(obj)/vclock_gettime.o $(obj)/vgetcpu.o $(obj)/vdso32/vclock_gettime.o: KBUILD_CFLAGS += -fcf-protection=branch -mibt + else + endbr := $(srctree)/$(src)/endbr.sh + quiet_cmd_endbr = ENDBR $@ + cmd_endbr = $(CONFIG_SHELL) '$(endbr)' $< $@ + + quiet_cmd_cc_asm_c = CC $(quiet_modtag) $@ + cmd_cc_asm_c = $(CC) $(c_flags) $(DISABLE_LTO) -S -o $@ $< + +$(obj)/%.asm: $(src)/%.c + $(call if_changed_dep,cc_asm_c) + +$(obj)/vclock_gettime.S: $(obj)/vclock_gettime.asm + $(call if_changed,endbr) + +$(obj)/vgetcpu.S: $(obj)/vgetcpu.asm + $(call if_changed,endbr) + +$(obj)/vclock_gettime.o: $(obj)/vclock_gettime.S + $(call if_changed_rule,as_o_S) + +$(obj)/vgetcpu.o: $(obj)/vgetcpu.S + $(call if_changed_rule,as_o_S) + +$(obj)/vdso32/vclock_gettime.S: $(obj)/vdso32/vclock_gettime.asm + $(call if_changed,endbr) + +$(obj)/vdso32/vclock_gettime.o: $(obj)/vdso32/vclock_gettime.S + $(call if_changed_rule,as_o_S) + endif +endif + $(obj)/%.so: OBJCOPYFLAGS := -S $(obj)/%.so: $(obj)/%.so.dbg $(call if_changed,objcopy)
arch/x86/entry/vdso/endbr.sh new file +32 -0
@@ -0,0 +1,32 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +in="$1" +out="$2" + +case "$in" in +*/vdso32/*) + endbr=".byte 0xf3,0x0f,0x1e,0xfb" + ;; +*) + endbr=".byte 0xf3,0x0f,0x1e,0xfa" + ;; +esac + +need_endbr=no +while IFS= read line ; do + case "$line" in + __vdso_clock_gettime:|__vdso_getcpu:|__vdso_gettimeofday:|__vdso_time:) + need_endbr=yes + ;; + " ."*) + ;; + " "*) + if [ "$need_endbr" = "yes" ]; then + need_endbr=no + echo " $endbr" + fi + ;; + esac + echo "$line" +done < "$in" > "$out"
Signature · 1 line
2.15.1
PATCH 6/7 tools: Add cetcmd
In reply to Yu-cheng YuFrom: "H.J. Lu" <[email protected]>
Introduce CET command-line utility. This utility allows system admin to enable/disable CET features and set default shadow stack size.
--- tools/Makefile | 13 +-- tools/arch/x86/include/uapi/asm/elf_property.h | 16 +++ tools/arch/x86/include/uapi/asm/prctl.h | 33 ++++++ tools/cet/.gitignore | 1 + tools/cet/Makefile | 11 ++ tools/cet/cetcmd.c | 134 +++++++++++++++++++++++++ tools/include/uapi/asm/elf_property.h | 4 + tools/include/uapi/asm/prctl.h | 4 + 8 files changed, 210 insertions(+), 6 deletions(-) create mode 100644 tools/arch/x86/include/uapi/asm/elf_property.h create mode 100644 tools/arch/x86/include/uapi/asm/prctl.h create mode 100644 tools/cet/.gitignore create mode 100644 tools/cet/Makefile create mode 100644 tools/cet/cetcmd.c create mode 100644 tools/include/uapi/asm/elf_property.h create mode 100644 tools/include/uapi/asm/prctl.h
tools/Makefile +7 -6
@@ -10,6 +10,7 @@ help: @echo 'Possible targets:' @echo '' @echo ' acpi - ACPI tools' + @echo ' cet - Intel CET tools' @echo ' cgroup - cgroup tools' @echo ' cpupower - a tool for all things x86 CPU power' @echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer'
@@ -59,7 +60,7 @@ acpi: FORCE cpupower: FORCE $(call descend,power/$@) -cgroup firewire hv guest spi usb virtio vm bpf iio gpio objtool leds wmi: FORCE +cet cgroup firewire hv guest spi usb virtio vm bpf iio gpio objtool leds wmi: FORCE $(call descend,$@) liblockdep: FORCE
@@ -91,7 +92,7 @@ freefall: FORCE kvm_stat: FORCE $(call descend,kvm/$@) -all: acpi cgroup cpupower gpio hv firewire liblockdep \ +all: acpi cet cgroup cpupower gpio hv firewire liblockdep \ perf selftests spi turbostat usb \ virtio vm bpf x86_energy_perf_policy \ tmon freefall iio objtool kvm_stat wmi
@@ -102,7 +103,7 @@ acpi_install: cpupower_install: $(call descend,power/$(@:_install=),install) -cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install bpf_install objtool_install wmi_install: +cet_install cgroup_install firewire_install gpio_install hv_install iio_install perf_install spi_install usb_install virtio_install vm_install bpf_install objtool_install wmi_install: $(call descend,$(@:_install=),install) liblockdep_install:
@@ -123,7 +124,7 @@ freefall_install: kvm_stat_install: $(call descend,kvm/$(@:_install=),install) -install: acpi_install cgroup_install cpupower_install gpio_install \ +install: acpi_install cet_install cgroup_install cpupower_install gpio_install \ hv_install firewire_install iio_install liblockdep_install \ perf_install selftests_install turbostat_install usb_install \ virtio_install vm_install bpf_install x86_energy_perf_policy_install \
@@ -136,7 +137,7 @@ acpi_clean: cpupower_clean: $(call descend,power/cpupower,clean) -cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean: +cet_clean cgroup_clean hv_clean firewire_clean spi_clean usb_clean virtio_clean vm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean: $(call descend,$(@:_clean=),clean) liblockdep_clean:
@@ -170,7 +171,7 @@ freefall_clean: build_clean: $(call descend,build,clean) -clean: acpi_clean cgroup_clean cpupower_clean hv_clean firewire_clean \ +clean: acpi_clean cet_clean cgroup_clean cpupower_clean hv_clean firewire_clean \ perf_clean selftests_clean turbostat_clean spi_clean usb_clean virtio_clean \ vm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \ freefall_clean build_clean libbpf_clean libsubcmd_clean liblockdep_clean \
tools/arch/x86/include/uapi/asm/elf_property.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _UAPI_ASM_X86_ELF_PROPERTY_H +#define _UAPI_ASM_X86_ELF_PROPERTY_H + +/* +* pr_type +*/ +#define GNU_PROPERTY_X86_FEATURE_1_AND (0xc0000002) + +/* +* Bits for GNU_PROPERTY_X86_FEATURE_1_AND +*/ +#define GNU_PROPERTY_X86_FEATURE_1_SHSTK (0x00000002) +#define GNU_PROPERTY_X86_FEATURE_1_IBT (0x00000001) + +#endif /* _UAPI_ASM_X86_ELF_PROPERTY_H */
tools/arch/x86/include/uapi/asm/prctl.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _ASM_X86_PRCTL_H +#define _ASM_X86_PRCTL_H + +#define ARCH_SET_GS 0x1001 +#define ARCH_SET_FS 0x1002 +#define ARCH_GET_FS 0x1003 +#define ARCH_GET_GS 0x1004 + +#define ARCH_GET_CPUID 0x1011 +#define ARCH_SET_CPUID 0x1012 + +#define ARCH_MAP_VDSO_X32 0x2001 +#define ARCH_MAP_VDSO_32 0x2002 +#define ARCH_MAP_VDSO_64 0x2003 + +#define ARCH_CET_STATUS 0x3001 +#define ARCH_CET_DISABLE 0x3002 +#define ARCH_CET_LOCK 0x3003 +#define ARCH_CET_EXEC 0x3004 +#define ARCH_CET_ALLOC_SHSTK 0x3005 +#define ARCH_CET_PUSH_SHSTK 0x3006 +#define ARCH_CET_LEGACY_BITMAP 0x3007 + +/* +* Settings for ARCH_CET_EXEC +*/ +#define CET_EXEC_ELF_PROPERTY 0 +#define CET_EXEC_ALWAYS_OFF 1 +#define CET_EXEC_ALWAYS_ON 2 +#define CET_EXEC_MAX CET_EXEC_ALWAYS_ON + +#endif /* _ASM_X86_PRCTL_H */
tools/cet/.gitignore new file +1 -0
@@ -0,0 +1 @@ +cetcmd
tools/cet/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for CET tools + +CFLAGS = -O2 -g -Wall -Wextra -I../include/uapi + +all: cetcmd +%: %.c + $(CC) $(CFLAGS) -o $@ $^ + +clean: + $(RM) cetcmd
tools/cet/cetcmd.c new file +134 -0
134 lines not on this page · raw message
tools/include/uapi/asm/elf_property.h new file +4 -0
@@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#if defined(__i386__) || defined(__x86_64__) +#include "../../arch/x86/include/uapi/asm/elf_property.h" +#endif
tools/include/uapi/asm/prctl.h new file +4 -0
@@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#if defined(__i386__) || defined(__x86_64__) +#include "../../arch/x86/include/uapi/asm/prctl.h" +#endif
Signature · 1 line
2.15.1
PATCH 7/7 x86/cet: Add PTRACE interface for CET
In reply to Yu-cheng YuAdd PTRACE interface for CET MSRs.
--- arch/x86/include/asm/fpu/regset.h | 7 ++++--- arch/x86/kernel/fpu/regset.c | 41 +++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 16 +++++++++++++++ include/uapi/linux/elf.h | 1 + 4 files changed, 62 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/regset.h +4 -3
@@ -7,11 +7,12 @@#include <linux/regset.h> -extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active; +extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active, + cetregs_active;extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, - xstateregs_get; + xstateregs_get, cetregs_get;extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, - xstateregs_set; + xstateregs_set, cetregs_set;/* * xstateregs_active == regset_fpregs_active. Please refer to the comment
arch/x86/kernel/fpu/regset.c +41 -0
47 lines not on this page · raw message
arch/x86/kernel/ptrace.c +16 -0
@@ -49,7 +49,9 @@ enum x86_regset {REGSET_IOPERM64 =REGSET_XFP ,REGSET_XSTATE ,REGSET_TLS , +REGSET_CET64 =REGSET_TLS ,REGSET_IOPERM32 , + REGSET_CET32, }; struct pt_regs_offset {
@@ -1276,6 +1278,13 @@ static struct user_regset x86_64_regsets[] __ro_after_init = { .size = sizeof(long), .align = sizeof(long), .active = ioperm_active, .get = ioperm_get }, + [REGSET_CET64] = { + .core_note_type = NT_X86_CET, + .n = sizeof(struct cet_user_state) / sizeof(u64), + .size = sizeof(u64), .align = sizeof(u64), + .active = cetregs_active, .get = cetregs_get, + .set = cetregs_set + }, };static const struct user_regset_view user_x86_64_view = {
@@ -1331,6 +1340,13 @@ static struct user_regset x86_32_regsets[] __ro_after_init = { .size = sizeof(u32), .align = sizeof(u32), .active = ioperm_active, .get = ioperm_get }, + [REGSET_CET32] = { + .core_note_type = NT_X86_CET, + .n = sizeof(struct cet_user_state) / sizeof(u64), + .size = sizeof(u64), .align = sizeof(u64), + .active = cetregs_active, .get = cetregs_get, + .set = cetregs_set + }, };static const struct user_regset_view user_x86_32_view = {
include/uapi/linux/elf.h +1 -0
@@ -401,6 +401,7 @@ typedef struct elf64_shdr {#define NT_386_TLS 0x200/* i386 TLS slots (struct user_desc) */ #define NT_386_IOPERM 0x201/* x86 io permission bitmap (1=deny) */ #define NT_X86_XSTATE 0x202/* x86 extended state using xsave */ +#define NT_X86_CET 0x203/* x86 cet state */ #define NT_S390_HIGH_GPRS 0x300/* s390 upper register halves */ #define NT_S390_TIMER 0x301/* s390 timer register */ #define NT_S390_TODCMP 0x302/* s390 TOD clock comparator register */
Signature · 1 line
2.15.1
Randy Dunlap
reviewerRe: [PATCH 1/7] x86/cet: Add Kconfig option for user-mode Indirect Branch Tracking
In reply to Yu-cheng YuQuoted 17 lines · Yu-cheng Yu
The user-mode indirect branch tracking support is done mostly by GCC to insert ENDBR64/ENDBR32 instructions at branch targets. The kernel provides CPUID enumeration, feature MSR setup and the allocation of legacy bitmap.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+)arch/x86/Kconfig +0
-0@@ -1953,6 +1953,18 @@ config X86_INTEL_SHADOW_STACK_USER If unsure, say y.
arch/x86/Kconfig config X86_INTEL_SHADOW_STACK_USER · line 1964
Quoted 10 lines
arch/x86/Kconfig +9
-0+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 + select ARCH_HAS_PROGRAM_PROPERTIES + ---help--- + Indirect Branch Tracking provides hardware protection against + oriented programing attacks.
programming
and please just move the return/jmp parts to the next line also:
protection against
return-/jmp-oriented programming attacks.
Quoted 7 lines
arch/x86/Kconfig +3
-0+ + If unsure, say y + config EFI bool "EFI runtime service support" depends on ACPI
Signature · 1 line
~Randy
Re: [PATCH 7/7] x86/cet: Add PTRACE interface for CET
In reply to Yu-cheng YuQuoted 47 lines · Yu-cheng Yu
Add PTRACE interface for CET MSRs.
Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/include/asm/fpu/regset.h | 7 ++++--- arch/x86/kernel/fpu/regset.c | 41 +++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 16 +++++++++++++++ include/uapi/linux/elf.h | 1 + 4 files changed, 62 insertions(+), 3 deletions(-)arch/x86/include/asm/fpu/regset.h +4
-3@@ -7,11 +7,12 @@#include <linux/regset.h> -extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active; +extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active, + cetregs_active;extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get, - xstateregs_get; + xstateregs_get, cetregs_get;extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, - xstateregs_set; + xstateregs_set, cetregs_set;/* * xstateregs_active == regset_fpregs_active. Please refer to the comment arch/x86/kernel/fpu/regset.c +8
-0@@ -160,6 +160,47 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,return ret; } +int cetregs_active (struct task_struct *target,const struct user_regset *regset) +{ +#ifdef CONFIG_X86_INTEL_CET +if (target->thread.cet.shstk_enabled || target->thread.cet.ibt_enabled) +return regset->n; +#endif +return 0 ; +}
arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *target, const struct user_regset *regset, · line 180
Quoted 10 lines
arch/x86/kernel/fpu/regset.c +10
-0+ +int cetregs_get (struct task_struct *target,const struct user_regset *regset, +unsigned int pos,unsigned int count, +void *kbuf,void __user *ubuf) +{ +struct fpu *fpu = &target->thread.fpu; +struct cet_user_state *cetregs; + +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return -ENODEV ;
This whole series has a boot_cpu_has, static_cpu_has, and cpu_feature_enabled all over. Please settle on just one, preferably static_cpu_has.
Quoted 11 lines
arch/x86/kernel/fpu/regset.c +11
-0+ + cetregs =get_xsave_addr (&fpu->state.xsave,XFEATURE_MASK_SHSTK_USER ); + +fpu__prepare_read (fpu); +return user_regset_copyout (&pos, &count, &kbuf, &ubuf, cetregs,0 ,-1 ); +} + +int cetregs_set (struct task_struct *target,const struct user_regset *regset, +unsigned int pos,unsigned int count, +const void *kbuf,const void __user *ubuf) +{
arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *target, const struct user_regset *regset, · line 201
Quoted 10 lines
arch/x86/kernel/fpu/regset.c +10
-0+struct fpu *fpu = &target->thread.fpu; +struct cet_user_state *cetregs; + +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return -ENODEV ; + + cetregs =get_xsave_addr (&fpu->state.xsave,XFEATURE_MASK_SHSTK_USER ); + +fpu__prepare_write (fpu); +return user_regset_copyin (&pos, &count, &kbuf, &ubuf, cetregs,0 ,-1 );
Is this called for core dumping on current? If so, please make sure it's correct. (I think it is for get but maybe not for set.)
Andy Lutomirski
maintainerRe: [PATCH 3/7] mm/mmap: Add IBT bitmap size to address space limit check
In reply to Yu-cheng YuQuoted 42 lines · Yu-cheng Yu
The indirect branch tracking legacy bitmap takes a large address space. This causes
may_expand_vm()failure on the address limit check. For a IBT-enabled task, add the bitmap size to the address limit.Signed-off-by Yu-cheng Yu <[email protected]>--- arch/x86/include/uapi/asm/resource.h | 5 +++++ include/uapi/asm-generic/resource.h | 3 +++ mm/mmap.c | 8 +++++++- 3 files changed, 15 insertions(+), 1 deletion(-)arch/x86/include/uapi/asm/resource.h +5
-0@@ -1 +1,6 @@ +/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */ +#ifdef CONFIG_X86_INTEL_CET +#define rlimit_as_extra () current->thread.cet.ibt_bitmap_size +#endif +#include <asm-generic/resource.h> include/uapi/asm-generic/resource.h +3
-0@@ -58,5 +58,8 @@# define RLIM_INFINITY (~0UL)#endif +#ifndef rlimit_as_extra +#define rlimit_as_extra () 0 +#endif #endif /* _UAPI_ASM_GENERIC_RESOURCE_H */ mm/mmap.c +0
-0
mm/mmap.c struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, · line 3263
Quoted 10 lines
mm/mmap.c +6
-1@@ -3255,7 +3255,13 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,*/ bool may_expand_vm (struct mm_struct *mm,vm_flags_t flags,unsigned long npages) { -if (mm->total_vm + npages >rlimit (RLIMIT_AS ) >>PAGE_SHIFT ) +unsigned long as_limit =rlimit (RLIMIT_AS ); +unsigned long as_limit_plus = as_limit +rlimit_as_extra (); + +if (as_limit_plus > as_limit) + as_limit = as_limit_plus; +
What happens if as_limit_plus overflows? I guess it's probably okay here.
Andy Lutomirski
maintainerRe: [PATCH 5/7] x86: Insert endbr32/endbr64 to vDSO
In reply to Yu-cheng YuQuoted 8 lines · Yu-cheng Yu
From: "H.J. Lu" <[email protected]>
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
Wow, that's... a genuine abomination. Do we really need to support CET on kernels built with old toolchains?
--Andy
Re: [PATCH 5/7] x86: Insert endbr32/endbr64 to vDSO
Quoted 4 lines · Andy Lutomirski
Quoted 3 lines · Yu-cheng Yu
From: "H.J. Lu" <[email protected]>
Quoted 9 lines
Quoted 5 lines
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
Wow, that's... a genuine abomination. Do we really need to support CET on kernels built with old toolchains?
Yes. GCC 7 should be able to build CET kernel.
Signature · 1 line
H.J.
Andy Lutomirski
maintainerRe: [PATCH 5/7] x86: Insert endbr32/endbr64 to vDSO
Quoted 12 lines · H.J. Lu
Quoted 10 lines · Andy Lutomirski
Quoted 8 lines · Yu-cheng Yu
From: "H.J. Lu" <[email protected]>
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
Quoted 6 lines
Quoted 3 lines
Wow, that's... a genuine abomination. Do we really need to support CET on kernels built with old toolchains?
Yes. GCC 7 should be able to build CET kernel.
Why? Presumably people running distros that use CET are going to have kernels build with a CET-supporting compiler.
If we really really need this patch, then I want some kind of assurance that selftests will catch the failure if something breaks it or a new vDSO entry point is added. But my inclination is to NAK this patch and let the distros carry it if they really really want it. As it stands, this sucks for maintainability.
Re: [PATCH 5/7] x86: Insert endbr32/endbr64 to vDSO
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · H.J. Lu
Quoted 13 lines · Andy Lutomirski
Quoted 8 lines · Yu-cheng Yu
From: "H.J. Lu" <[email protected]>
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
Wow, that's... a genuine abomination. Do we really need to support CET on kernels built with old toolchains?
Quoted 6 lines
Quoted 2 lines
Yes. GCC 7 should be able to build CET kernel.
Why? Presumably people running distros that use CET are going to have kernels build with a CET-supporting compiler.
Good point. It was needed before GCC 8 was released. We can droparch/x86/entry/vdso/endbr.sh now.
Signature · 1 line
H.J.
Andy Lutomirski
maintainerRe: [PATCH 5/7] x86: Insert endbr32/endbr64 to vDSO
Quoted 22 lines · H.J. Lu
Quoted 20 lines · Andy Lutomirski
Quoted 18 lines · H.J. Lu
Quoted 4 lines · Andy Lutomirski
Quoted 3 lines · Yu-cheng Yu
From: "H.J. Lu" <[email protected]>
Quoted 9 lines
Quoted 5 lines
When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly should have endbr32 or endbr64 as the first instruction. We try to compile vDSO with -fcf-protection=branch -mibt if possible. Otherwise, we insert endbr32 or endbr64 by hand to assembly codes generated by the compiler.
Wow, that's... a genuine abomination. Do we really need to support CET on kernels built with old toolchains?
Yes. GCC 7 should be able to build CET kernel.
Quoted 7 lines
Quoted 3 lines
Why? Presumably people running distros that use CET are going to have kernels build with a CET-supporting compiler.
Good point. It was needed before GCC 8 was released. We can drop
arch/x86/entry/vdso/endbr.shnow.
Phew! We'll still need a patch to prevent configuring CET on a compiler that can't support it.