Skip to contentSkip to conversation

Control-flow Enforcement: Branch Tracking, PTRACE

PATCH Superseded Yu-cheng Yu 8 participants 72 messages Jun 6, 2019 – Jun 15, 2019

Subsystems (11)

v7 of 30 Superseded Latest is v30 →

lore
Share & tools
lore
#0 lore raw

v7 cover Control-flow Enforcement: Branch Tracking, PTRACE

The previous version of CET Branch Tracking/PTRACE patches is here:

https://lkml.org/lkml/2018/11/20/203

Summary of changes from v6:

Rebase to v5.2-rc3.

Add Branch Tracking in the signal handling routines.

Fix Branch Tracking (and Shadow Stack) for vsyscall (patch #12):
This patch can be dropped if we expect CET blocking vsyscall.

Include H.J. Lu's patch to discard .note.gnu.property in the kernel.

H.J. Lu (4):
x86/vdso: Insert endbr32/endbr64 to vDSO x86/vdso/32: Add ENDBR32 to __kernel_vsyscall entry point x86/vsyscall/64: Add ENDBR64 to vsyscall entry points
x86: Discard .note.gnu.property sections

Yu-cheng Yu (10):
x86/cet/ibt: Add Kconfig option for user-mode Indirect Branch Tracking x86/cet/ibt: User-mode indirect branch tracking support x86/cet/ibt: Add IBT legacy code bitmap setup function x86/cet/ibt: Handle signals for IBT
mm/mmap: Add IBT bitmap size to address space limit check x86/cet/ibt: ELF header parsing for IBT
x86/cet/ibt: Add arch_prctl functions for IBT
x86/cet/ibt: Add ENDBR to op-code-map
x86/vsyscall/64: Fixup shadow stack and branch tracking for vsyscall x86/cet: Add PTRACE interface for CET

 arch/x86/Kconfig                              | 16 ++++
 arch/x86/Makefile                             |  7 ++
 arch/x86/entry/vdso/Makefile                  | 12 ++-
 arch/x86/entry/vdso/vdso-layout.lds.S         |  1 +
 arch/x86/entry/vdso/vdso32/system_call.S      |  3 +
 arch/x86/entry/vsyscall/vsyscall_64.c         | 28 +++++++
 arch/x86/entry/vsyscall/vsyscall_emu_64.S     |  9 +++
 arch/x86/include/asm/cet.h                    |  8 ++
 arch/x86/include/asm/disabled-features.h      |  8 +-
 arch/x86/include/asm/fpu/regset.h             |  7 +-
 arch/x86/include/asm/mmu_context.h            | 10 +++
 arch/x86/include/uapi/asm/prctl.h             |  2 +
 arch/x86/kernel/cet.c                         | 80 +++++++++++++++++++
 arch/x86/kernel/cet_prctl.c                   | 21 +++++
 arch/x86/kernel/cpu/common.c                  | 17 ++++
 arch/x86/kernel/fpu/regset.c                  | 41 ++++++++++
 arch/x86/kernel/process_64.c                  |  6 ++
 arch/x86/kernel/ptrace.c                      | 16 ++++
 arch/x86/kernel/vmlinux.lds.S                 | 11 ++-
 arch/x86/lib/x86-opcode-map.txt               | 13 ++-
 include/uapi/linux/elf.h                      |  1 +
 mm/mmap.c                                     | 19 ++++-
 .../arch/x86/include/asm/disabled-features.h  |  8 +-
 tools/objtool/arch/x86/lib/x86-opcode-map.txt | 13 ++-
 24 files changed, 345 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v7 1/14 x86/cet/ibt: 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 and feature setup.

---
 arch/x86/Kconfig  | 16 ++++++++++++++++
 arch/x86/Makefile |  7 +++++++
 2 files changed, 23 insertions(+)
arch/x86/Kconfig +16 -0
index df8b57de75b2..47afe47c01eb 100644
@@ -1921,6 +1921,9 @@ config X86_INTEL_CET
 config ARCH_HAS_SHSTK
 	def_bool n
 
+config ARCH_HAS_AS_LIMIT
+	def_bool n
+
 config X86_INTEL_SHADOW_STACK_USER
 	prompt "Intel Shadow Stack for user-mode"
 	def_bool n
@@ -1941,6 +1944,19 @@ 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_AS_LIMIT
+	select ARCH_USE_GNU_PROPERTY
+	---help---
+	  Indirect Branch Tracking provides hardware protection against return-/jmp-
+	  oriented programming attacks.
+
+	  If unsure, say y
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/Makefile +7 -0
index 0b2e9df48907..25372cc4a303 100644
@@ -155,6 +155,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.17.1
#2 lore raw

PATCH v7 2/14 x86/cet/ibt: User-mode indirect branch tracking support

In reply to Yu-cheng Yu

Add user-mode indirect branch tracking enabling/disabling and supporting routines.

---
 arch/x86/include/asm/cet.h                    |  7 ++++
 arch/x86/include/asm/disabled-features.h      |  8 ++++-
 arch/x86/kernel/cet.c                         | 36 +++++++++++++++++++
 arch/x86/kernel/cpu/common.c                  | 17 +++++++++
 .../arch/x86/include/asm/disabled-features.h  |  8 ++++-
 5 files changed, 74 insertions(+), 2 deletions(-)
arch/x86/include/asm/cet.h +7 -0
index 2df357dffd24..89330e4159a9 100644
@@ -14,8 +14,11 @@ struct sc_ext;
 struct cet_status {
 	unsigned long	shstk_base;
 	unsigned long	shstk_size;
+	unsigned long	ibt_bitmap_addr;
+	unsigned long	ibt_bitmap_size;
 	unsigned int	locked:1;
 	unsigned int	shstk_enabled:1;
+	unsigned int	ibt_enabled:1;
 };
 
 #ifdef CONFIG_X86_INTEL_CET
@@ -27,6 +30,8 @@ void cet_disable_shstk(void);
 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_shstk(void) { return -EINVAL; }
@@ -37,6 +42,8 @@ static inline void cet_disable_free_shstk(struct task_struct *p) {}
 static inline int cet_restore_signal(bool ia32, struct sc_ext *sc) { return -EINVAL; }
 static inline int cet_setup_signal(bool ia32, unsigned long rstor,
 				   struct sc_ext *sc) { return -EINVAL; }
+static inline int cet_setup_ibt(void) { return -EINVAL; }
+static inline void cet_disable_ibt(void) {}
 #endif
 
 #define cpu_x86_cet_enabled() \
arch/x86/include/asm/disabled-features.h +7 -1
index 06323ebed643..fc7d3d5a1bf4 100644
@@ -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 +36 -0
index 0004333f8373..14ad25b8ff21 100644
@@ -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>
@@ -325,3 +327,37 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext)
 	modify_fpu_regs_end();
 	return 0;
 }
+
+int cet_setup_ibt(void)
+{
+	u64 r;
+
+	if (!cpu_feature_enabled(X86_FEATURE_IBT))
+		return -EOPNOTSUPP;
+
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_ENDBR_EN | MSR_IA32_CET_NO_TRACK_EN);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();
+
+	current->thread.cet.ibt_enabled = 1;
+	return 0;
+}
+
+void cet_disable_ibt(void)
+{
+	u64 r;
+
+	if (!cpu_feature_enabled(X86_FEATURE_IBT))
+		return;
+
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r &= ~(MSR_IA32_CET_ENDBR_EN | MSR_IA32_CET_LEG_IW_EN |
+	       MSR_IA32_CET_NO_TRACK_EN | MSR_IA32_CET_BITMAP_MASK);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();
+
+	current->thread.cet.ibt_enabled = 0;
+}
arch/x86/kernel/cpu/common.c +17 -0
index b0780fe8717e..7fa38e4a9e82 100644
@@ -441,6 +441,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
index 06323ebed643..fc7d3d5a1bf4 100644
@@ -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.17.1
#3 lore raw

PATCH v7 3/14 x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

---
 arch/x86/include/asm/cet.h |  1 +
 arch/x86/kernel/cet.c      | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
arch/x86/include/asm/cet.h +1 -0
index 89330e4159a9..9e613a6598c9 100644
@@ -31,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_setup_ibt_bitmap(unsigned long bitmap, unsigned long size);
 void cet_disable_ibt(void);
 #else
 static inline int prctl_cet(int option, unsigned long arg2) { return -EINVAL; }
arch/x86/kernel/cet.c +26 -0
index 14ad25b8ff21..e0ef996d3148 100644
@@ -22,6 +22,7 @@
 #include <asm/fpu/types.h>
 #include <asm/cet.h>
 #include <asm/special_insns.h>
+#include <asm/elf.h>
 #include <uapi/asm/sigcontext.h>
 
 static int set_shstk_ptr(unsigned long addr)
@@ -361,3 +362,28 @@ void cet_disable_ibt(void)
 
 	current->thread.cet.ibt_enabled = 0;
 }
+
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+	u64 r;
+
+	if (!current->thread.cet.ibt_enabled)
+		return -EINVAL;
+
+	if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+		return -EINVAL;
+
+	current->thread.cet.ibt_bitmap_addr = bitmap;
+	current->thread.cet.ibt_bitmap_size = size;
+
+	/*
+	 * Turn on IBT legacy bitmap.
+	 */
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();
+
+	return 0;
+}
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 21 lines · Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +14 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+	u64 r;
+
+	if (!current->thread.cet.ibt_enabled)
+		return -EINVAL;
+
+	if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+		return -EINVAL;
+
+	current->thread.cet.ibt_bitmap_addr = bitmap;
+	current->thread.cet.ibt_bitmap_size = size;
+
+	/*

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 10 lines
arch/x86/kernel/cet.c +10 -0
+	 * Turn on IBT legacy bitmap.
+	 */
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();
+
+	return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

#5 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +20 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+	u64 r;
+
+	if (!current->thread.cet.ibt_enabled)
+		return -EINVAL;
+
+	if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+		return -EINVAL;
+
+	current->thread.cet.ibt_bitmap_addr = bitmap;
+	current->thread.cet.ibt_bitmap_size = size;
+
+	/*
+	 * Turn on IBT legacy bitmap.
+	 */
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
Quoted 8 lines

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 4 lines
arch/x86/kernel/cet.c +4 -0
+	modify_fpu_regs_end();
+
+	return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

This function checks the bitmap's alignment and size, and anything else is the app's responsibility. What else do you think the kernel should check?

Yu-cheng

#6 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 41 lines · Yu-cheng Yu
Quoted 29 lines · Peter Zijlstra
Quoted 27 lines · Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +20 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+    u64 r;
+
+    if (!current->thread.cet.ibt_enabled)
+        return -EINVAL;
+
+    if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+        return -EINVAL;
+
+    current->thread.cet.ibt_bitmap_addr = bitmap;
+    current->thread.cet.ibt_bitmap_size = size;
+
+    /*
+     * Turn on IBT legacy bitmap.
+     */
+    modify_fpu_regs_begin();
+    rdmsrl(MSR_IA32_U_CET, r);
+    r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+    wrmsrl(MSR_IA32_U_CET, r);
Quoted 8 lines

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 4 lines
arch/x86/kernel/cet.c +4 -0
+    modify_fpu_regs_end();
+
+    return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

Quoted 3 lines

This function checks the bitmap's alignment and size, and anything else is the app's responsibility. What else do you think the kernel should check?

One might reasonably wonder why this state is privileged in the first place and, given that, why we’re allowing it to be written like this.

Arguably we should have another prctl to lock these values (until exec) as a gardening measure.

#7 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 3 lines · Andy Lutomirski

One might reasonably wonder why this state is privileged in the first place and, given that, why we’re allowing it to be written like this.

I think it's generally a good architectural practice to make things like this privileged. They're infrequent so can survive the cost of a trip in/out of the kernel and are a great choke point to make sure the OS is involved. I wish we had the same for MPX or pkeys per-task "setup".

#8 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Andy Lutomirski
Quoted 44 lines · Andy Lutomirski
Quoted 39 lines · Yu-cheng Yu
Quoted 29 lines · Peter Zijlstra
Quoted 27 lines · Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +20 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+    u64 r;
+
+    if (!current->thread.cet.ibt_enabled)
+        return -EINVAL;
+
+    if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+        return -EINVAL;
+
+    current->thread.cet.ibt_bitmap_addr = bitmap;
+    current->thread.cet.ibt_bitmap_size = size;
+
+    /*
+     * Turn on IBT legacy bitmap.
+     */
+    modify_fpu_regs_begin();
+    rdmsrl(MSR_IA32_U_CET, r);
+    r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+    wrmsrl(MSR_IA32_U_CET, r);
Quoted 8 lines

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 4 lines
arch/x86/kernel/cet.c +4 -0
+    modify_fpu_regs_end();
+
+    return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

Quoted 4 lines

This function checks the bitmap's alignment and size, and anything else is the
app's responsibility. What else do you think the kernel should check?

Quoted 5 lines

One might reasonably wonder why this state is privileged in the first place and, given that, why we’re allowing it to be written like this.

Arguably we should have another prctl to lock these values (until exec) as a gardening measure.

We can prevent the bitmap from being set more than once. I will test it.

Yu-cheng

#9 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 50 lines · Yu-cheng Yu
Quoted 44 lines · Andy Lutomirski
Quoted 43 lines · Yu-cheng Yu
Quoted 29 lines
Quoted 29 lines · Peter Zijlstra

On Thu, Jun 06, 2019 at 01:09:15PM -0700, Yu-cheng Yu wrote:
Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +20 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+    u64 r;
+
+    if (!current->thread.cet.ibt_enabled)
+        return -EINVAL;
+
+    if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+        return -EINVAL;
+
+    current->thread.cet.ibt_bitmap_addr = bitmap;
+    current->thread.cet.ibt_bitmap_size = size;
+
+    /*
+     * Turn on IBT legacy bitmap.
+     */
+    modify_fpu_regs_begin();
+    rdmsrl(MSR_IA32_U_CET, r);
+    r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+    wrmsrl(MSR_IA32_U_CET, r);
Quoted 8 lines

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 4 lines
arch/x86/kernel/cet.c +4 -0
+    modify_fpu_regs_end();
+
+    return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

This function checks the bitmap's alignment and size, and anything else is the
app's responsibility. What else do you think the kernel should check?

Quoted 8 lines
Quoted 5 lines

One might reasonably wonder why this state is privileged in the first place and, given that, why we’re allowing it to be written like this.

Arguably we should have another prctl to lock these values (until exec) as a gardening measure.

We can prevent the bitmap from being set more than once. I will test it.

I think it would be better to make locking an explicit opt-in.

maintainer
#10 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Yu-cheng Yu
Quoted 37 lines · Yu-cheng Yu
Quoted 27 lines · Peter Zijlstra
Quoted 26 lines · Yu-cheng Yu

Indirect Branch Tracking (IBT) provides an optional legacy code bitmap that allows execution of legacy, non-IBT compatible library by an IBT-enabled application. When set, each bit in the bitmap indicates one page of legacy code.

The bitmap is allocated and setup from the application.

arch/x86/kernel/cet.c +20 -0
+int cet_setup_ibt_bitmap(unsigned long bitmap, unsigned long size)
+{
+	u64 r;
+
+	if (!current->thread.cet.ibt_enabled)
+		return -EINVAL;
+
+	if (!PAGE_ALIGNED(bitmap) || (size > TASK_SIZE_MAX))
+		return -EINVAL;
+
+	current->thread.cet.ibt_bitmap_addr = bitmap;
+	current->thread.cet.ibt_bitmap_size = size;
+
+	/*
+	 * Turn on IBT legacy bitmap.
+	 */
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
Quoted 8 lines

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 389

Quoted 4 lines
arch/x86/kernel/cet.c +4 -0
+	modify_fpu_regs_end();
+
+	return 0;
+}

So you just program a random user supplied address into the hardware. What happens if there's not actually anything at that address or the user munmap()s the data after doing this?

Quoted 2 lines

This function checks the bitmap's alignment and size, and anything else is the app's responsibility. What else do you think the kernel should check?

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Does it then fault or what?

#11 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 4 lines · Peter Zijlstra

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

	prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

#12 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 8 lines · Dave Hansen
Quoted 5 lines · Peter Zijlstra

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Quoted 8 lines
Quoted 1 line

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Quoted 5 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Quoted 7 lines

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

   prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Given that the format depends on the VA size, this might be a good idea. I bet we can reuse the special mapping infrastructure for this — the VMA could
be a MAP_PRIVATE special mapping named [cet_legacy_bitmap] or similar, and we can even make special rules to core dump it intelligently if needed. And we can make mremap() on it work correctly if anyone (CRIU?) cares.

Hmm. Can we be creative and skip populating it with zeros? The CPU should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

This also gives us an excellent opportunity to make it read-only as seen from userspace to prevent exploits from just poking it full of ones before redirecting execution.

#13 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

On 6/7/19 11:29 AM, Andy Lutomirski wrote:
...

Quoted 5 lines
Quoted 2 lines

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Good point.

Yu-cheng, can you just confirm that the bitmap can't be referenced in ring-0, no matter what? We should also make sure that no funny business happens if we put an address in the bitmap that faults, or is non-canonical. Do we have any self-tests for that?

Let's say userspace gets a fault on this. Do they have the introspection capability to figure out why they faulted, say in their signal handler?

Quoted 7 lines
Quoted 7 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

Quoted 7 lines
Quoted 4 lines
   prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Given that the format depends on the VA size, this might be a good idea.

Yeah, making userspace know how large the address space is or could be is rather nasty, especially if we ever get any fancy CPU features that eat up address bits (a la ARM top-byte-ignore or SPARC ADI).

Quoted 1 line

Hmm. Can we be creative and skip populating it with zeros? The CPU

should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

Won't reads on a big, contiguous private mapping get the huge zero page anyway?

Quoted 2 lines

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

Yeah, practically, you could only make it shared if you shared the layout of all code in the address space. I'm sure the big database(s) do that cross-process, but I bet nobody else does. User ASLR practically guarantees that nobody can do this.

Quoted 3 lines

This also gives us an excellent opportunity to make it read-only as seen from userspace to prevent exploits from just poking it full of ones before redirecting execution.

That would be fun.

#14 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 8 lines · Dave Hansen

On 6/7/19 11:29 AM, Andy Lutomirski wrote:
...

Quoted 5 lines
Quoted 2 lines

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Quoted 6 lines

Good point.

Yu-cheng, can you just confirm that the bitmap can't be referenced in ring-0, no matter what? We should also make sure that no funny business happens if we put an address in the bitmap that faults, or is non-canonical. Do we have any self-tests for that?

Yes, the bitmap is user memory, but the kernel can still get to it (e.g. copy_from_user()). We can do more check on the address.

Quoted 4 lines

Let's say userspace gets a fault on this. Do they have the introspection capability to figure out why they faulted, say in their signal handler?

The bitmap address is kept by the application; the kernel won't provide it again to user-space. In the signal handler, the app can find out from its own record.

[...]

#15 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen
Quoted 14 lines · Dave Hansen

On 6/7/19 11:29 AM, Andy Lutomirski wrote:
...

Quoted 5 lines
Quoted 2 lines

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Good point.

Quoted 8 lines

Yu-cheng, can you just confirm that the bitmap can't be referenced in ring-0, no matter what? We should also make sure that no funny business happens if we put an address in the bitmap that faults, or is non-canonical. Do we have any self-tests for that?

Let's say userspace gets a fault on this. Do they have the introspection capability to figure out why they faulted, say in their signal handler?

We need to stick the tracker state in the sigcontext somewhere.

Did we end up defining a signal frame shadow stack token?

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

  prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Quoted 6 lines
Quoted 2 lines

Given that the format depends on the VA size, this might be a good idea.

Yeah, making userspace know how large the address space is or could be is rather nasty, especially if we ever get any fancy CPU features that eat up address bits (a la ARM top-byte-ignore or SPARC ADI).

That gets extra bad if we ever grow user code that uses it but is unaware. It could poke the wrong part of the bitmap.

Quoted 10 lines
Quoted 2 lines

Hmm. Can we be creative and skip populating it with zeros? The CPU

should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

Won't reads on a big, contiguous private mapping get the huge zero page anyway?

The zero pages may be free, but the page tables could be decently large. Does the core mm code use huge, immense, etc huge zero pages? Or can it synthesize them by reusing page table pages that map zeros?

Quoted 8 lines
Quoted 3 lines

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

Yeah, practically, you could only make it shared if you shared the layout of all code in the address space. I'm sure the big database(s) do that cross-process, but I bet nobody else does. User ASLR practically guarantees that nobody can do this.

I meant per-mm instead of per-task.

#16 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 8 lines · Andy Lutomirski
Quoted 8 lines
Quoted 2 lines

Hmm. Can we be creative and skip populating it with zeros? The CPU

should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

Quoted 4 lines
Quoted 2 lines

Won't reads on a big, contiguous private mapping get the huge zero page anyway?

The zero pages may be free, but the page tables could be decently

large. Does the core mm code use huge, immense, etc huge zero pages? Or can it synthesize them by reusing page table pages that map zeros?

IIRC, we only ever fill single PMDs, even though we could gang a pmd page up and do it for 1GB areas too.

I guess the page table consumption could really suck if we had code all over the 57-bit address space and that code moved around and the process ran for a long long time. Pathologically, we need a ulong/pmd_t for each 2MB of address space which is 8*2^56-30=512GB per process. Yikes.
Right now, we'd at least detect the memory consumption and OOM-kill the
process(es) eventually. But, that's not really _this_ patch's problem.
It's a general problem, and doesn't even require the zero page to be
mapped all over.

Longer-term, I'd much rather see us add some page table reclaim mechanism that new how to go after things like excessive page tables in MAP_NORESERVE areas.

#17 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Andy Lutomirski
Quoted 43 lines · Andy Lutomirski
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Peter Zijlstra

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Quoted 8 lines
Quoted 1 line

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Quoted 5 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Quoted 7 lines

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

   prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Given that the format depends on the VA size, this might be a good idea. I bet we can reuse the special mapping infrastructure for this — the VMA could be a MAP_PRIVATE special mapping named [cet_legacy_bitmap] or similar, and we can even make special rules to core dump it intelligently if needed. And we can make mremap() on it work correctly if anyone (CRIU?) cares.

Hmm. Can we be creative and skip populating it with zeros? The CPU should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

Quoted 6 lines

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

This also gives us an excellent opportunity to make it read-only as seen from userspace to prevent exploits from just poking it full of ones before redirecting execution.

GLIBC sets bits only for legacy code, and then makes the bitmap read-only. That avoids most issues:

To populate bitmap pages, mprotect() is required.
Reading zero bitmap pages would not waste more physical memory, right?

Yu-cheng

#18 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 9 lines · Yu-cheng Yu
Quoted 4 lines

This also gives us an excellent opportunity to make it read-only as seen from userspace to prevent exploits from just poking it full of ones before redirecting execution.

GLIBC sets bits only for legacy code, and then makes the bitmap read-only. That avoids most issues:

To populate bitmap pages, mprotect() is required.
Reading zero bitmap pages would not waste more physical memory, right?

Huh, how does glibc know about all possible past and future legacy code in the application?

#19 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 11 lines · Dave Hansen
Quoted 10 lines · Yu-cheng Yu
Quoted 5 lines

This also gives us an excellent opportunity to make it read-only as seen from
userspace to prevent exploits from just poking it full of ones before redirecting execution.

GLIBC sets bits only for legacy code, and then makes the bitmap read- only. That
avoids most issues:

Quoted 5 lines
Quoted 2 lines

To populate bitmap pages, mprotect() is required.
Reading zero bitmap pages would not waste more physical memory, right?

Huh, how does glibc know about all possible past and future legacy code in the application?

When dlopen() gets a legacy binary and the policy allows that, it will manage the bitmap:

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

Yu-cheng

#20 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 7 lines · Yu-cheng Yu
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

When dlopen() gets a legacy binary and the policy allows that, it will manage the bitmap:

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

#21 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 6 lines · Dave Hansen
Quoted 2 lines · Yu-cheng Yu
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

Quoted 7 lines
Quoted 5 lines

When dlopen() gets a legacy binary and the policy allows that, it will manage the bitmap:

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

CRIU is another consideration: it would be rather annoying if CET programs can’t migrate between LA57 and normal machines.

#22 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 10 lines · Andy Lutomirski
Quoted 10 lines · Dave Hansen
Quoted 7 lines · Yu-cheng Yu
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

When dlopen() gets a legacy binary and the policy allows that, it will manage
the bitmap:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

CRIU is another consideration: it would be rather annoying if CET programs can’t migrate between LA57 and normal machines.

When a machine migrates, does its applications' addresses change? If no, then the bitmap should still work, right?

Yu-cheng

#23 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen
Quoted 4 lines · Dave Hansen
Quoted 3 lines · Yu-cheng Yu
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

Quoted 8 lines
Quoted 6 lines

When dlopen() gets a legacy binary and the policy allows that, it will manage
the bitmap:

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

If JIT manages the bitmap, it knows where it is.
It can always read the bitmap again, right?

Yu-cheng

#24 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

* Yu-cheng Yu:

Quoted 9 lines · Dave Hansen
Quoted 8 lines · Yu-cheng Yu
Quoted 7 lines
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

When dlopen() gets a legacy binary and the policy allows that, it will manage
the bitmap:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

If JIT manages the bitmap, it knows where it is.
It can always read the bitmap again, right?

The problem are JIT libraries without assembler code which can be marked non-CET, such as liborc. Our builds (e.g., orc-0.4.29-2.fc30.x86_64) currently carries the IBT and SHSTK flag, although the entry points into the generated code do not start with ENDBR, so that a jump to them will fault with the CET enabled.

Thanks,
Florian

#25 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Yu-cheng Yu
Quoted 8 lines · Yu-cheng Yu
Quoted 7 lines · Dave Hansen
Quoted 6 lines · Yu-cheng Yu
Quoted 2 lines

Huh, how does glibc know about all possible past and future legacy code in the application?

When dlopen() gets a legacy binary and the policy allows that, it will manage
the bitmap:

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

If a bitmap has not been created, create one.
Set bits for the legacy code being loaded.

I was thinking about code that doesn't go through GLIBC like JITs.

If JIT manages the bitmap, it knows where it is.
It can always read the bitmap again, right?

Let's just be clear:

The design proposed here is that all code mappers (anybody wanting to get legacy non-CET code into the address space):

1. Know about CET
2. Know where the bitmap is, and identify the part that needs to be changed
3. Be able to mprotect() the bitmap to be writable (undoing glibc's PROT_READ)
4. Set the bits in the bitmap for the legacy code
5. mprotect() the bitmap back to PROT_READ

Do the non-glibc code mappers have glibc interfaces for this? Otherwise, how could a bunch of JITs in a big multi-threaded application possibly coordinate the mprotect()s? Won't they race with each other?

#26 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Yu-cheng Yu
Quoted 51 lines · Yu-cheng Yu
Quoted 46 lines · Andy Lutomirski
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Peter Zijlstra

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Quoted 8 lines
Quoted 1 line

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We should put it into core dumps, though.

Quoted 5 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Quoted 7 lines

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

  prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Given that the format depends on the VA size, this might be a good idea. I bet we can reuse the special mapping infrastructure for this — the VMA could be a MAP_PRIVATE special mapping named [cet_legacy_bitmap] or similar, and we can even make special rules to core dump it intelligently if needed. And we can make mremap() on it work correctly if anyone (CRIU?) cares.

Hmm. Can we be creative and skip populating it with zeros? The CPU should only ever touch a page if we miss an ENDBR on it, so, in normal operation, we don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally wasting lots of memory by forcing it to be fully populated when the read it.

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

Quoted 6 lines
Quoted 3 lines

This also gives us an excellent opportunity to make it read-only as seen from userspace to prevent exploits from just poking it full of ones before redirecting execution.

GLIBC sets bits only for legacy code, and then makes the bitmap read-only. That avoids most issues:

How does glibc know the linear address space size? We don’t want LA64 to break old binaries because the address calculation changed.

#27 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 64 lines · Andy Lutomirski
Quoted 64 lines · Yu-cheng Yu
Quoted 60 lines · Andy Lutomirski
Quoted 6 lines · Dave Hansen
Quoted 5 lines · Peter Zijlstra

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Quoted 8 lines
Quoted 1 line

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

This shouldn’t be able to OOPS because it only happens at CPL 3, right? We
should put it into core dumps, though.

Quoted 6 lines

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Quoted 7 lines

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

  prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

Given that the format depends on the VA size, this might be a good idea. I
bet we can reuse the special mapping infrastructure for this — the VMA could
be a MAP_PRIVATE special mapping named [cet_legacy_bitmap] or similar, and we
can even make special rules to core dump it intelligently if needed. And we
can make mremap() on it work correctly if anyone (CRIU?) cares.

Hmm. Can we be creative and skip populating it with zeros? The CPU should
only ever touch a page if we miss an ENDBR on it, so, in normal operation, we
don’t need anything to be there. We could try to prevent anyone from *reading* it outside of ENDBR tracking if we want to avoid people accidentally
wasting lots of memory by forcing it to be fully populated when the read it.

The one downside is this forces it to be per-mm, but that seems like a generally reasonable model anyway.

This also gives us an excellent opportunity to make it read-only as seen from
userspace to prevent exploits from just poking it full of ones before redirecting execution.

Quoted 6 lines
Quoted 3 lines

GLIBC sets bits only for legacy code, and then makes the bitmap read- only. That
avoids most issues:

How does glibc know the linear address space size? We don’t want LA64 to break old binaries because the address calculation changed.

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Yu-cheng

#28 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 4 lines · Yu-cheng Yu
Quoted 2 lines

How does glibc know the linear address space size? We don’t want LA64 to break old binaries because the address calculation changed.

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Huh, I didn't think we ran code from the stack. ;)

Especially given the way that we implemented the new 5-level-paging address space, I don't think that expecting code to be below the stack is a good universal expectation.

#29 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 4 lines · Dave Hansen
Quoted 3 lines · Yu-cheng Yu
Quoted 2 lines

How does glibc know the linear address space size? We don’t want LA64 to break old binaries because the address calculation changed.

Quoted 8 lines
Quoted 2 lines

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Huh, I didn't think we ran code from the stack. ;)

Especially given the way that we implemented the new 5-level-paging address space, I don't think that expecting code to be below the stack is a good universal expectation.

Yes, you make a good point. However, allowing the application manage the bitmap is the most efficient and flexible. If the loader finds a legacy lib is beyond the bitmap can cover, it can deal with the problem by moving the lib to a lower address; or re-allocate the bitmap. If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

Yu-cheng

#30 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 11 lines · Yu-cheng Yu
Quoted 7 lines
Quoted 2 lines

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Huh, I didn't think we ran code from the stack. ;)

Especially given the way that we implemented the new 5-level-paging address space, I don't think that expecting code to be below the stack is a good universal expectation.

Yes, you make a good point. However, allowing the application manage the bitmap is the most efficient and flexible. If the loader finds a legacy lib is beyond the bitmap can cover, it can deal with the problem by moving the lib to a lower address; or re-allocate the bitmap.

How could the loader reallocate the bitmap and coordinate with other users of the bitmap?

Quoted 3 lines

If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

This is actually an argument to do it in the kernel. The kernel can always allocate the virtual space however it wants, no matter how large.
If we hide the bitmap behind a kernel API then we can put it at high
5-level user addresses because we also don't have to worry about the high bits confusing userspace.

maintainer
#31 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 21 lines · Dave Hansen
Quoted 11 lines · Yu-cheng Yu
Quoted 7 lines
Quoted 2 lines

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Huh, I didn't think we ran code from the stack. ;)

Especially given the way that we implemented the new 5-level-paging address space, I don't think that expecting code to be below the stack is a good universal expectation.

Yes, you make a good point. However, allowing the application manage the bitmap is the most efficient and flexible. If the loader finds a legacy lib is beyond the bitmap can cover, it can deal with the problem by moving the lib to a lower address; or re-allocate the bitmap.

How could the loader reallocate the bitmap and coordinate with other users of the bitmap?

Quoted 3 lines

If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

Quoted 6 lines

This is actually an argument to do it in the kernel. The kernel can always allocate the virtual space however it wants, no matter how large.
If we hide the bitmap behind a kernel API then we can put it at high
5-level user addresses because we also don't have to worry about the high bits confusing userspace.

That's a fairly compelling argument.

The bitmap is one bit per page, right? So it's smaller than the address space by a factor of 8*2^12 == 2^15. This means that, if we ever get full 64-bit linear addresses reserved entirely for userspace (which could happen if my perennial request to Intel to split user and kernel addresses completely happens), then we'll need 2^48 bytes for the bitmap, which simply does not fit in the address space of a legacy application.

#32 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen
Quoted 18 lines · Dave Hansen
Quoted 9 lines · Yu-cheng Yu
Quoted 8 lines
Quoted 2 lines

When an application starts, its highest stack address is determined. It uses that as the maximum the bitmap needs to cover.

Huh, I didn't think we ran code from the stack. ;)

Especially given the way that we implemented the new 5-level-paging address space, I don't think that expecting code to be below the stack is a good universal expectation.

Quoted 7 lines

Yes, you make a good point. However, allowing the application manage the bitmap
is the most efficient and flexible. If the loader finds a legacy lib is beyond
the bitmap can cover, it can deal with the problem by moving the lib to a lower
address; or re-allocate the bitmap.

Quoted 2 lines

How could the loader reallocate the bitmap and coordinate with other users of the bitmap?

Assuming the loader actually chooses to re-allocate, it can copy the old bitmap over to the new before doing the switch. But, I agree, the other choice is easier; the loader can simply put the lib at lower address. AFAIK, the loader does not request high address in mmap().

Quoted 10 lines
Quoted 4 lines

If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

This is actually an argument to do it in the kernel. The kernel can always allocate the virtual space however it wants, no matter how large.
If we hide the bitmap behind a kernel API then we can put it at high
5-level user addresses because we also don't have to worry about the high bits confusing userspace.

We actually tried this. The kernel needs to reserve the bitmap space in the beginning for every CET-enabled app, regardless of actual needs. On each memory request, the kernel then must consider a percentage of allocated space in its calculation, and on systems with less memory this quickly becomes a problem.

#33 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 10 lines · Yu-cheng Yu
Quoted 8 lines
Quoted 3 lines

If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

This is actually an argument to do it in the kernel. The kernel can always allocate the virtual space however it wants, no matter how large.
If we hide the bitmap behind a kernel API then we can put it at high
5-level user addresses because we also don't have to worry about the high bits confusing userspace.

We actually tried this. The kernel needs to reserve the bitmap space in the beginning for every CET-enabled app, regardless of actual needs.

I don't think this is a problem. In fact, I think reserving the space is actually the only sane behavior. If you don't reserve it, you fundamentally limit where future legacy instructions can go.

One idea is that we always size the bitmap for the 48-bit addressing systems. Legacy code probably doesn't _need_ to go in the new address space, and if we do this we don't have to worry about the gigantic 57-bit address space bitmap.

Quoted 3 lines

On each memory request, the kernel then must consider a percentage of allocated space in its calculation, and on systems with less memory this quickly becomes a problem.

I'm not sure what you're referring to here? Are you referring to our overcommit limits?

#34 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 23 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Quoted 4 lines
Quoted 3 lines

If the loader cannot allocate a big bitmap to cover all 5-level address space (the bitmap will be large), it can put all legacy lib's at lower address. We cannot do these easily in the kernel.

Quoted 8 lines
Quoted 5 lines

This is actually an argument to do it in the kernel. The kernel can always allocate the virtual space however it wants, no matter how large.
If we hide the bitmap behind a kernel API then we can put it at high
5-level user addresses because we also don't have to worry about the high bits confusing userspace.

We actually tried this. The kernel needs to reserve the bitmap space in the beginning for every CET-enabled app, regardless of actual needs.

I don't think this is a problem. In fact, I think reserving the space is actually the only sane behavior. If you don't reserve it, you fundamentally limit where future legacy instructions can go.

One idea is that we always size the bitmap for the 48-bit addressing systems. Legacy code probably doesn't _need_ to go in the new address space, and if we do this we don't have to worry about the gigantic 57-bit address space bitmap.

Quoted 6 lines
Quoted 3 lines

On each memory request, the kernel then must consider a percentage of allocated space in its calculation, and on systems with less memory this quickly becomes a problem.

I'm not sure what you're referring to here? Are you referring to our overcommit limits?

Yes.

#35 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 6 lines · Yu-cheng Yu
Quoted 5 lines
Quoted 3 lines

On each memory request, the kernel then must consider a percentage of allocated space in its calculation, and on systems with less memory this quickly becomes a problem.

I'm not sure what you're referring to here? Are you referring to our overcommit limits?

Yes.

My assumption has always been that these large, potentially sparse hardware tables *must* be mmap()'d with MAP_NORESERVE specified. That should keep them from being problematic with respect to overcommit.

#36 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 5 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Quoted 4 lines
Quoted 3 lines

On each memory request, the kernel then must consider a percentage of allocated space in its calculation, and on systems with less memory this quickly becomes a problem.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I'm not sure what you're referring to here? Are you referring to our overcommit limits?

Yes.

My assumption has always been that these large, potentially sparse hardware tables *must* be mmap()'d with MAP_NORESERVE specified. That should keep them from being problematic with respect to overcommit.

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

We then create PR_MARK_CODE_AS_LEGACY. The kernel will set the bitmap, but it is going to be slow.

Perhaps we still let the app fill the bitmap?

Yu-cheng

#37 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 2 lines · Yu-cheng Yu

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

Could you make sure to discuss the downsides of only doing a 48-bit address space?

What are the reasons behind and implications of VM_DONTDUMP?

Quoted 2 lines

We then create PR_MARK_CODE_AS_LEGACY. The kernel will set the bitmap, but it is going to be slow.

Slow compared to what? We're effectively adding one (quick) system call to a path that, today, has at *least* half a dozen syscalls and probably a bunch of page faults. Heck, we can probably avoid the actual page fault to populate the bitmap if we're careful. That alone would put a syscall on equal footing with any other approach. If the bit setting crossed a page boundary it would probably win.

Quoted 1 line

Perhaps we still let the app fill the bitmap?

I think I'd want to see some performance data on it first.

#38 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 11 lines · Dave Hansen
Quoted 3 lines

We then create PR_MARK_CODE_AS_LEGACY. The kernel will set the bitmap, but it is going to be slow.

Slow compared to what? We're effectively adding one (quick) system call to a path that, today, has at *least* half a dozen syscalls and probably a bunch of page faults. Heck, we can probably avoid the actual page fault to populate the bitmap if we're careful. That alone would put a syscall on equal footing with any other approach. If the bit setting crossed a page boundary it would probably win.

Quoted 3 lines
Quoted 1 line

Perhaps we still let the app fill the bitmap?

I think I'd want to see some performance data on it first.

Updating legacy bitmap in user space from kernel requires

long q;

get_user(q, ...);
q |= mask;
put_user(q, ...);

instead of

*p |= mask;

get_user + put_user was quite slow when we tried before.

Signature · 1 line
H.J.
#39 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 10 lines · H.J. Lu
Quoted 2 lines
Quoted 1 line

Perhaps we still let the app fill the bitmap?

I think I'd want to see some performance data on it first.

Updating legacy bitmap in user space from kernel requires

long q;

get_user(q, ...);
q |= mask;
put_user(q, ...);

Quoted 5 lines

instead of

*p |= mask;

get_user + put_user was quite slow when we tried before.

Numbers, please.

There are *lots* of ways to speed something like that up if you have actual issues with it. For instance, you can skip the get_user() for whole bytes. You can write bits with 0's for unallocated address space.
You can do user_access_begin/end() to avoid bunches of STAC/CLACs...

The list goes on and on. :)

#40 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen
Quoted 23 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

Could you make sure to discuss the downsides of only doing a 48-bit address space?

What are the reasons behind and implications of VM_DONTDUMP?

Quoted 2 lines

We then create PR_MARK_CODE_AS_LEGACY. The kernel will set the bitmap, but it is going to be slow.

Slow compared to what? We're effectively adding one (quick) system call to a path that, today, has at *least* half a dozen syscalls and probably a bunch of page faults. Heck, we can probably avoid the actual page fault to populate the bitmap if we're careful. That alone would put a syscall on equal footing with any other approach. If the bit setting crossed a page boundary it would probably win.

Quoted 3 lines
Quoted 1 line

Perhaps we still let the app fill the bitmap?

I think I'd want to see some performance data on it first.

Trying to summarize:

If we manage the whole thing in user space, we are basically committing to only covering 48 bits — otherwise the whole model falls apart in quite a few ways. We gain some simplicity in the kernel.

If we do it in the kernel, we still have to decide how much address space to cover. We get to play games like allocating the bitmap above 2^48, but then we might have CRIU issues if we migrate to a system with fewer BA bits.

I doubt that the performance matters much one way or another. I just don’t expect any of this to be a bottleneck.

Another benefit of kernel management: we could plausibly auto-clear the bits corresponding to munmapped regions. Is this worth it?

And a maybe-silly benefit: if we manage it in the kernel, we could optimize the inevitable case where the bitmap contains pages that are all ones :). If it’s in userspace, KSM could do the, but that will be inefficient at best.

#41 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 2 lines · Andy Lutomirski

Another benefit of kernel management: we could plausibly auto-clear the bits corresponding to munmapped regions. Is this worth it?

I did it for MPX. I think I even went to the trouble of zapping the whole pages that got unused.

But, MPX tables took 80% of the address space, worst-case. This takes 0.003% :) The only case it would really matter would be a task was long-running, used legacy executables/JITs, and was mapping/unmapping text all over the address space. That seems rather unlikely.

#42 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 8 lines · Dave Hansen
Quoted 4 lines · Andy Lutomirski

Another benefit of kernel management: we could plausibly auto-clear the bits corresponding to munmapped regions. Is this worth it?

Quoted 7 lines

I did it for MPX. I think I even went to the trouble of zapping the whole pages that got unused.

But, MPX tables took 80% of the address space, worst-case. This takes 0.003% :) The only case it would really matter would be a task was long-running, used legacy executables/JITs, and was mapping/unmapping text all over the address space. That seems rather unlikely.

Every wasted page still costs 4K plus page table overhead. The worst case is a JIT that doesn’t clean up and leaks legacy bitmap memory all over. We can blame the JIT, but the actual attribution could be complicated.

It also matters when you unmap one thing, map something else, and are sad when the legacy bits are still set.

Admittedly, it’s a bit hard to imagine the exploit that takes advantage of this.

#43 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Yu-cheng Yu

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

Could you make sure to discuss the downsides of only doing a 48-bit address space?

The downside is that we cannot load legacy lib's above 48-bit address space, but currently ld-linux does not do that. Should ld-linux do that in the future, dlopen() fails. Considering CRIU migration, we probably need to do this anyway?

Quoted 1 line

What are the reasons behind and implications of VM_DONTDUMP?

The bitmap is very big.

In GDB, it should be easy to tell why a control-protection fault occurred without the bitmap.

Yu-cheng

#44 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 11 lines · Yu-cheng Yu
Quoted 6 lines · Dave Hansen
Quoted 2 lines · Yu-cheng Yu

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

Could you make sure to discuss the downsides of only doing a 48-bit address space?

The downside is that we cannot load legacy lib's above 48-bit address space, but currently ld-linux does not do that. Should ld-linux do that in the future, dlopen() fails. Considering CRIU migration, we probably need to do this anyway?

Again, I was thinking about JITs. Please remember that not all code in the system is from files on the disk. Please. We need to be really, really sure that we don't addle this implementation by being narrow minded about this.

Please don't forget about JITs.

Quoted 3 lines
Quoted 1 line

What are the reasons behind and implications of VM_DONTDUMP?

The bitmap is very big.

Really? It's actually, what, 8*4096=32k, so 1/32,768th of the size of the libraries legacy libraries you load? Do our crash dumps really not know how to represent or deal with sparse mappings?

Quoted 2 lines

In GDB, it should be easy to tell why a control-protection fault occurred without the bitmap.

How about why one didn't happen?

#45 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 22 lines · Dave Hansen
Quoted 5 lines · Yu-cheng Yu
Quoted 4 lines · Dave Hansen
Quoted 2 lines · Yu-cheng Yu

Ok, we will go back to do_mmap() with MAP_PRIVATE, MAP_NORESERVE and VM_DONTDUMP. The bitmap will cover only 48-bit address space.

Quoted 8 lines
Quoted 2 lines

Could you make sure to discuss the downsides of only doing a 48-bit address space?

The downside is that we cannot load legacy lib's above 48-bit address space, but
currently ld-linux does not do that. Should ld-linux do that in the future, dlopen() fails. Considering CRIU migration, we probably need to do this anyway?

Again, I was thinking about JITs. Please remember that not all code in the system is from files on the disk. Please. We need to be really, really sure that we don't addle this implementation by being narrow minded about this.

Please don't forget about JITs.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What are the reasons behind and implications of VM_DONTDUMP?

The bitmap is very big.

Really? It's actually, what, 8*4096=32k, so 1/32,768th of the size of the libraries legacy libraries you load? Do our crash dumps really not know how to represent or deal with sparse mappings?

Ok, even the core dump is not physically big, its size still looks odd, right? Could this also affect how much time for GDB to load it.
We will only mmap the bitmap when the first time the bitmap prctl is called.

I have a related question:

Do we allow the application to read the bitmap, or any fault from the application on bitmap pages?

We populate a page only when bits are set from a prctl.
Any other fault means either the application tries to find out an address range's status or it executes legacy code that has not been marked in the bitmap.

Quoted 5 lines
Quoted 3 lines

In GDB, it should be easy to tell why a control-protection fault occurred without the bitmap.

How about why one didn't happen?

We'll dump the bitmap if it is allocated.

Yu-cheng

#46 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 9 lines · Yu-cheng Yu
Quoted 6 lines · Dave Hansen
Quoted 1 line · Yu-cheng Yu

The bitmap is very big.

Really? It's actually, what, 8*4096=32k, so 1/32,768th of the size of the libraries legacy libraries you load? Do our crash dumps really not know how to represent or deal with sparse mappings?

Ok, even the core dump is not physically big, its size still looks odd, right?

Hell if I know.

Could you please go try this in practice so that we're designing this thing fixing real actual problems instead of phantoms that we're anticipating?

Quoted 1 line

Could this also affect how much time for GDB to load it.

I don't know. Can you go find out for sure, please?

Quoted 4 lines

I have a related question:

Do we allow the application to read the bitmap, or any fault from the application on bitmap pages?

We have to allow apps to read it. Otherwise they can't execute instructions.

We don't have to allow them to (popuating) fault on it. But, if we don't, we need some kind of kernel interface to avoid the faults.

#47 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 14 lines · Dave Hansen
Quoted 10 lines · Yu-cheng Yu
Quoted 6 lines · Dave Hansen
Quoted 1 line · Yu-cheng Yu

The bitmap is very big.

Really? It's actually, what, 8*4096=32k, so 1/32,768th of the size of the libraries legacy libraries you load? Do our crash dumps really not know how to represent or deal with sparse mappings?

Ok, even the core dump is not physically big, its size still looks odd, right?

Hell if I know.

Quoted 7 lines

Could you please go try this in practice so that we're designing this thing fixing real actual problems instead of phantoms that we're anticipating?

Quoted 1 line

Could this also affect how much time for GDB to load it.

I don't know. Can you go find out for sure, please?

OK!

Quoted 8 lines
Quoted 5 lines

I have a related question:

Do we allow the application to read the bitmap, or any fault from the application on bitmap pages?

We have to allow apps to read it. Otherwise they can't execute instructions.

What I meant was, if an app executes some legacy code that results in bitmap lookup, but the bitmap page is not yet populated, and if we then populate that page with all-zero, a #CP should follow. So do we even populate that zero page at all?

I think we should; a #CP is more obvious to the user at least.

Quoted 3 lines

We don't have to allow them to (popuating) fault on it. But, if we don't, we need some kind of kernel interface to avoid the faults.

The plan is:

* Move STACK_TOP (and vdso) down to give space to the bitmap.

* Reserve the bitmap space from (mm->start_stack + PAGE_SIZE) to cover a code size of TASK_SIZE_LOW, which is (TASK_SIZE_LOW / PAGE_SIZE / 8).

* Mmap the space only when the app issues the first mark-legacy prctl. This avoids the core-dump issue for most apps and the accounting problem that MAP_NORESERVE probably won't solve completely.

* The bitmap is read-only. The kernel sets the bitmap with get_user_pages_fast(FOLL_WRITE) and user_access_begin()/user_addess_end().

I will send out a RFC patch.

Yu-cheng

#48 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 9 lines · Yu-cheng Yu
Quoted 7 lines · Dave Hansen
Quoted 4 lines

I have a related question:

Do we allow the application to read the bitmap, or any fault from the application on bitmap pages?

We have to allow apps to read it. Otherwise they can't execute instructions.

Quoted 6 lines

What I meant was, if an app executes some legacy code that results in bitmap lookup, but the bitmap page is not yet populated, and if we then populate that page with all-zero, a #CP should follow. So do we even populate that zero page at all?

I think we should; a #CP is more obvious to the user at least.

Please make an effort to un-Intel-ificate your messages as much as possible. I'd really prefer that folks say "missing end branch fault" rather than #CP. I had to Google "#CP".

I *think* you are saying that: The *only* lookups to this bitmap are on "missing end branch" conditions. Normal, proper-functioning code execution that has ENDBR instructions in it will never even look at the bitmap. The only case when we reference the bitmap locations is when the processor is about do do a "missing end branch fault" so that it can be suppressed. Any population with the zero page would be done when code had already encountered a "missing end branch" condition, and populating with a zero-filled page will guarantee that a "missing end branch fault" will result. You're arguing that we should just figure this out at fault time and not ever reach the "missing end branch fault" at all.

Is that right?

If so, that's an architecture subtlety that I missed until now and which went entirely unmentioned in the changelog and discussion up to this point. Let's make sure that nobody else has to walk that path by improving our changelog, please.

In any case, I don't think this is worth special-casing our zero-fill code, FWIW. It's not performance critical and not worth the complexity.
If apps want to handle the signals and abuse this to fill space up with
boring page table contents, they're welcome to. There are much easier ways to consume a lot of memory.

Quoted 6 lines
Quoted 2 lines

We don't have to allow them to (popuating) fault on it. But, if we don't, we need some kind of kernel interface to avoid the faults.

The plan is:

* Move STACK_TOP (and vdso) down to give space to the bitmap.

Even for apps with 57-bit address spaces?

Quoted 2 lines

* Reserve the bitmap space from (mm->start_stack + PAGE_SIZE) to cover a code size of TASK_SIZE_LOW, which is (TASK_SIZE_LOW / PAGE_SIZE / 8).

The bitmap size is determined by CR4.LA57, not the app. If you place the bitmap here, won't references to it for high addresses go into the high address space?

Specifically, on a CR4.LA57=0 system, we have 48 bits of address space, so 128TB for apps. You are proposing sticking the bitmap above the stack which is near the top of that 128TB address space. But on a 5-level paging system with CR4.LA57=1, there could be valid data at 129GB. Is there something keeping that data from being mistaken for being part of the bitmap?

Also, if you're limiting it to TASK_SIZE_LOW, please don't forget that this is yet another thing that probably won't work with the vsyscall page. Please make sure you consider it and mention it in your next post.

Quoted 3 lines

* Mmap the space only when the app issues the first mark-legacy prctl. This avoids the core-dump issue for most apps and the accounting problem that MAP_NORESERVE probably won't solve completely.

What is this accounting problem?

#49 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 44 lines · Dave Hansen
Quoted 11 lines · Yu-cheng Yu
Quoted 7 lines · Dave Hansen
Quoted 4 lines

I have a related question:

Do we allow the application to read the bitmap, or any fault from the application on bitmap pages?

We have to allow apps to read it. Otherwise they can't execute instructions.

Quoted 6 lines

What I meant was, if an app executes some legacy code that results in bitmap lookup, but the bitmap page is not yet populated, and if we then populate that page with all-zero, a #CP should follow. So do we even populate that zero page at all?

I think we should; a #CP is more obvious to the user at least.

Please make an effort to un-Intel-ificate your messages as much as possible. I'd really prefer that folks say "missing end branch fault" rather than #CP. I had to Google "#CP".

I *think* you are saying that: The *only* lookups to this bitmap are on "missing end branch" conditions. Normal, proper-functioning code execution that has ENDBR instructions in it will never even look at the bitmap. The only case when we reference the bitmap locations is when the processor is about do do a "missing end branch fault" so that it can be suppressed. Any population with the zero page would be done when code had already encountered a "missing end branch" condition, and populating with a zero-filled page will guarantee that a "missing end branch fault" will result. You're arguing that we should just figure this out at fault time and not ever reach the "missing end branch fault" at all.

Is that right?

If so, that's an architecture subtlety that I missed until now and which went entirely unmentioned in the changelog and discussion up to this point. Let's make sure that nobody else has to walk that path by improving our changelog, please.

Quoted 5 lines

In any case, I don't think this is worth special-casing our zero-fill code, FWIW. It's not performance critical and not worth the complexity. If apps want to handle the signals and abuse this to fill space up with boring page table contents, they're welcome to. There are much easier ways to consume a lot of memory.

Isn’t it a special case either way? Either we look at CR2 and populate a page, or we look at CR2 and the “tracker” state and send a different signal. Admittedly the former is very common in the kernel.

Quoted 17 lines
Quoted 7 lines
Quoted 3 lines

We don't have to allow them to (popuating) fault on it. But, if we don't, we need some kind of kernel interface to avoid the faults.

The plan is:

* Move STACK_TOP (and vdso) down to give space to the bitmap.

Even for apps with 57-bit address spaces?

Quoted 2 lines

* Reserve the bitmap space from (mm->start_stack + PAGE_SIZE) to cover a code size of TASK_SIZE_LOW, which is (TASK_SIZE_LOW / PAGE_SIZE / 8).

The bitmap size is determined by CR4.LA57, not the app. If you place the bitmap here, won't references to it for high addresses go into the high address space?

Quoted 7 lines

Specifically, on a CR4.LA57=0 system, we have 48 bits of address space, so 128TB for apps. You are proposing sticking the bitmap above the stack which is near the top of that 128TB address space. But on a 5-level paging system with CR4.LA57=1, there could be valid data at 129GB. Is there something keeping that data from being mistaken for being part of the bitmap?

I think we need to make the vma be full sized — it should cover the entire range that the CPU might access. If that means it spans the 48-bit boundary, so be it.

Quoted 3 lines

Also, if you're limiting it to TASK_SIZE_LOW, please don't forget that this is yet another thing that probably won't work with the vsyscall page. Please make sure you consider it and mention it in your next post.

Why not? The vsyscall page is at a negative address.

Quoted 4 lines
Quoted 4 lines

* Mmap the space only when the app issues the first mark-legacy prctl. This avoids the core-dump issue for most apps and the accounting problem that MAP_NORESERVE probably won't solve

What happens if there’s another VMA there by the time you map it?

#50 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen

* Dave Hansen:

Quoted 3 lines

My assumption has always been that these large, potentially sparse hardware tables *must* be mmap()'d with MAP_NORESERVE specified. That should keep them from being problematic with respect to overcommit.

MAP_NORESERVE pages still count towards the commit limit. The flag only disables checks at allocation time, for this particular allocation. (At least this was the behavior the last time I looked into this, I believe.)

Not sure if this makes a difference here.

Thanks,
Florian

#51 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Dave Hansen

Hi!

Quoted 16 lines
Quoted 4 lines

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Quoted 7 lines

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

	prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

For the record, that sounds like a better interface than userspace knowing about the bitmap formats...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#52 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 21 lines · Pavel Machek

Hi!

Quoted 19 lines
Quoted 4 lines

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

Quoted 8 lines
Quoted 4 lines
	prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

For the record, that sounds like a better interface than userspace knowing about the bitmap formats...
Pavel

Initially we implemented the bitmap that way. To manage the bitmap, every time the application issues a syscall for a .so it loads, and the kernel does copy_from_user() & copy_to_user() (or similar things). If a system has a few legacy .so files and every application does the same, it can take a long time to boot up.

Yu-cheng

#53 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 31 lines · Yu-cheng Yu
Quoted 21 lines · Pavel Machek

Hi!

Quoted 19 lines
Quoted 4 lines

I've no idea what the kernel should do; since you failed to answer the question what happens when you point this to garbage.

Does it then fault or what?

Yeah, I think you'll fault with a rather mysterious CR2 value since you'll go look at the instruction that faulted and not see any references to the CR2 value.

I think this new MSR probably needs to get included in oops output when CET is enabled.

Why don't we require that a VMA be in place for the entire bitmap? Don't we need a "get" prctl function too in case something like a JIT is running and needs to find the location of this bitmap to set bits itself?

Or, do we just go whole-hog and have the kernel manage the bitmap itself. Our interface here could be:

Quoted 8 lines
Quoted 4 lines
	prctl(PR_MARK_CODE_AS_LEGACY, start, size);

and then have the kernel allocate and set the bitmap for those code locations.

For the record, that sounds like a better interface than userspace knowing about the bitmap formats...
Pavel

Quoted 5 lines

Initially we implemented the bitmap that way. To manage the bitmap, every time the application issues a syscall for a .so it loads, and the kernel does copy_from_user() & copy_to_user() (or similar things). If a system has a few legacy .so files and every application does the same, it can take a long time to boot up.

Loading .so is already many syscalls, I'd not expect measurable performance there. Are you sure?
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#54 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

In reply to Yu-cheng Yu

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 386

Quoted 5 lines · Yu-cheng Yu
arch/x86/kernel/cet.c +5 -0
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();

Isn't there a bunch of other stuff in this MSR? It seems like the bitmap value would allow overwriting lots of bits in the MSR that have nothing to do with the bitmap... in a prctl() that's supposed to only be dealing with the bitmap.

#55 lore raw

Re: [PATCH v7 03/14] x86/cet/ibt: Add IBT legacy code bitmap setup function

Quoted 11 lines · Dave Hansen

arch/x86/kernel/cet.c void cet_disable_ibt(void) · line 386

Quoted 5 lines · Yu-cheng Yu
arch/x86/kernel/cet.c +5 -0
+	modify_fpu_regs_begin();
+	rdmsrl(MSR_IA32_U_CET, r);
+	r |= (MSR_IA32_CET_LEG_IW_EN | bitmap);
+	wrmsrl(MSR_IA32_U_CET, r);
+	modify_fpu_regs_end();

Isn't there a bunch of other stuff in this MSR? It seems like the bitmap value would allow overwriting lots of bits in the MSR that have nothing to do with the bitmap... in a prctl() that's supposed to only be dealing with the bitmap.

Yes, the bitmap address should have been masked, although it is checked for page alignment (which has the same effect). I will fix it.

Yu-cheng

#56 lore raw

PATCH v7 4/14 x86/cet/ibt: Handle signals for IBT

In reply to Yu-cheng Yu

Setup/Restore Indirect Branch Tracking for signals.

---
 arch/x86/kernel/cet.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/x86/kernel/cet.c +18 -0
index e0ef996d3148..e1ab7e722637 100644
@@ -282,6 +282,15 @@ int cet_restore_signal(bool ia32, struct sc_ext *sc_ext)
 		msr_ia32_u_cet |= MSR_IA32_CET_SHSTK_EN;
 	}
 
+	if (current->thread.cet.ibt_enabled) {
+		if (current->thread.cet.ibt_bitmap_addr != 0)
+			msr_ia32_u_cet |= (current->thread.cet.ibt_bitmap_addr |
+					   MSR_IA32_CET_LEG_IW_EN);
+
+		msr_ia32_u_cet |= (MSR_IA32_CET_ENDBR_EN |
+				   MSR_IA32_CET_NO_TRACK_EN);
+	}
+
 	wrmsrl(MSR_IA32_PL3_SSP, new_ssp);
 	wrmsrl(MSR_IA32_U_CET, msr_ia32_u_cet);
 	return 0;
@@ -322,6 +331,15 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext)
 		sc_ext->ssp = new_ssp;
 	}
 
+	if (current->thread.cet.ibt_enabled) {
+		if (current->thread.cet.ibt_bitmap_addr != 0)
+			msr_ia32_u_cet |= (current->thread.cet.ibt_bitmap_addr |
+					   MSR_IA32_CET_LEG_IW_EN);
+
+		msr_ia32_u_cet |= (MSR_IA32_CET_ENDBR_EN |
+				   MSR_IA32_CET_NO_TRACK_EN);
+	}
+
 	modify_fpu_regs_begin();
 	wrmsrl(MSR_IA32_PL3_SSP, ssp);
 	wrmsrl(MSR_IA32_U_CET, msr_ia32_u_cet);
Signature · 1 line
2.17.1
#57 lore raw

PATCH v7 5/14 mm/mmap: Add IBT bitmap size to address space limit check

In reply to 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.

---
 arch/x86/include/asm/mmu_context.h | 10 ++++++++++
 mm/mmap.c                          | 19 ++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
arch/x86/include/asm/mmu_context.h +10 -0
index a9a768529540..2499f6490428 100644
@@ -360,6 +360,16 @@ static inline unsigned long __get_current_cr3_fast(void)
 	return cr3;
 }
 
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+static inline unsigned long arch_as_limit(void)
+{
+	if (current->thread.cet.ibt_enabled)
+		return current->thread.cet.ibt_bitmap_size;
+	else
+		return 0;
+}
+#endif
+
 typedef struct {
 	struct mm_struct *mm;
 } temp_mm_state_t;
mm/mmap.c +18 -1
index 3b643ace2c49..a0d6fb559518 100644
@@ -3283,13 +3283,30 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 	return NULL;
 }
 
+#ifndef CONFIG_ARCH_HAS_AS_LIMIT
+static inline unsigned long arch_as_limit(void)
+{
+	return 0;
+}
+#endif
+
 /*
  * Return true if the calling process may expand its vm space by the passed
  * number of pages
  */
 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 + arch_as_limit();
+
+	/* as_limit_plus overflowed */
+	if (as_limit_plus < as_limit)
+		as_limit_plus = RLIM_INFINITY;
+
+	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.17.1
#58 lore raw

PATCH v7 6/14 x86/cet/ibt: ELF header parsing for IBT

In reply to Yu-cheng Yu

Look in .note.gnu.property of an ELF file and check if Indirect Branch Tracking needs to be enabled for the task.

---
 arch/x86/kernel/process_64.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kernel/process_64.c +6 -0
index 5fa0d9ab18f1..16dae646f633 100644
@@ -856,6 +856,12 @@ int arch_setup_property(void *ehdr, void *phdr, struct file *f, bool inter)
 		if (r < 0)
 			return r;
 	}
+
+	if (cpu_feature_enabled(X86_FEATURE_IBT)) {
+		if (property & GNU_PROPERTY_X86_FEATURE_1_IBT)
+			r = cet_setup_ibt();
+	}
+
 	return r;
 }
 #endif
Signature · 1 line
2.17.1
#59 lore raw

PATCH v7 7/14 x86/cet/ibt: Add arch_prctl functions for IBT

In reply to Yu-cheng Yu

Update ARCH_X86_CET_STATUS and ARCH_X86_CET_DISABLE to include Indirect Branch Tracking features.

Introduce:

arch_prctl(ARCH_X86_CET_SET_LEGACY_BITMAP, unsigned long *addr)
Enable the Indirect Branch Tracking legacy code bitmap.

The parameter 'addr' is a pointer to a user buffer that has:

*addr = IBT bitmap base address
*(addr + 1) = IBT bitmap size

---
 arch/x86/include/uapi/asm/prctl.h |  2 ++
 arch/x86/kernel/cet_prctl.c       | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+)
arch/x86/include/uapi/asm/prctl.h +2 -0
index d962f0ec9ccf..5eb9aeb5c662 100644
@@ -18,5 +18,7 @@
 #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
 
 #endif /* _ASM_X86_PRCTL_H */
arch/x86/kernel/cet_prctl.c +21 -0
index 9c9d4262b07e..b7f37bbc0dd3 100644
@@ -20,6 +20,8 @@ static int handle_get_status(unsigned long arg2)
 
 	if (current->thread.cet.shstk_enabled)
 		features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
+	if (current->thread.cet.ibt_enabled)
+		features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
 
 	shstk_base = current->thread.cet.shstk_base;
 	shstk_size = current->thread.cet.shstk_size;
@@ -55,6 +57,17 @@ static int handle_alloc_shstk(unsigned long arg2)
 	return 0;
 }
 
+static int handle_bitmap(unsigned long arg2)
+{
+	unsigned long addr, size;
+
+	if (get_user(addr, (unsigned long __user *)arg2) ||
+	    get_user(size, (unsigned long __user *)arg2 + 1))
+		return -EFAULT;
+
+	return cet_setup_ibt_bitmap(addr, size);
+}
+
 int prctl_cet(int option, unsigned long arg2)
 {
 	if (!cpu_x86_cet_enabled())
@@ -69,6 +82,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;
 
@@ -79,6 +94,12 @@ int prctl_cet(int option, unsigned long arg2)
 	case ARCH_X86_CET_ALLOC_SHSTK:
 		return handle_alloc_shstk(arg2);
 
+	/*
+	 * Allocate legacy bitmap and return address & size to user.
+	 */
+	case ARCH_X86_CET_SET_LEGACY_BITMAP:
+		return handle_bitmap(arg2);
+
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.17.1
maintainer
#60 lore raw

Re: [PATCH v7 07/14] x86/cet/ibt: Add arch_prctl functions for IBT

Quoted 7 lines · Yu-cheng Yu
arch/x86/kernel/cet_prctl.c +7 -0
+static int handle_bitmap(unsigned long arg2)
+{
+	unsigned long addr, size;
+
+	if (get_user(addr, (unsigned long __user *)arg2) ||
+	    get_user(size, (unsigned long __user *)arg2 + 1))
+		return -EFAULT;

arch/x86/kernel/cet_prctl.c static int handle_alloc_shstk(unsigned long arg2)

Quoted 10 lines
arch/x86/kernel/cet_prctl.c +8 -0
+
+	return cet_setup_ibt_bitmap(addr, size);
+}
 
 
+	/*
+	 * Allocate legacy bitmap and return address & size to user.
+	 */
+	case ARCH_X86_CET_SET_LEGACY_BITMAP:
+		return handle_bitmap(arg2);

AFAICT it does exactly the opposite of that comment; it gets the address and size from userspace and doesn't allocate anything at all.

#61 lore raw

PATCH v7 8/14 x86/cet/ibt: Add ENDBR to op-code-map

In reply to Yu-cheng Yu

Add control transfer terminating instructions:

ENDBR64/ENDBR32:
Mark a valid 64/32-bit control transfer endpoint.

---
 arch/x86/lib/x86-opcode-map.txt               | 13 +++++++++++--
 tools/objtool/arch/x86/lib/x86-opcode-map.txt | 13 +++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)
arch/x86/lib/x86-opcode-map.txt +11 -2
index c5e825d44766..fbc53481bc59 100644
@@ -620,7 +620,16 @@ ea: SAVEPREVSSP (f3)
 # Skip 0xeb-0xff
 EndTable
 
-Table: 3-byte opcode 2 (0x0f 0x38)
+Table: 3-byte opcode 2 (0x0f 0x1e)
+Referrer:
+AVXcode:
+# Skip 0x00-0xf9
+fa: ENDBR64 (f3)
+fb: ENDBR32 (f3)
+#skip 0xfc-0xff
+EndTable
+
+Table: 3-byte opcode 3 (0x0f 0x38)
 Referrer: 3-byte escape 1
 AVXcode: 2
 # 0x0f 0x38 0x00-0x0f
@@ -804,7 +813,7 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq
 f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v)
 EndTable
 
-Table: 3-byte opcode 3 (0x0f 0x3a)
+Table: 3-byte opcode 4 (0x0f 0x3a)
 Referrer: 3-byte escape 2
 AVXcode: 3
 # 0x0f 0x3a 0x00-0xff
tools/objtool/arch/x86/lib/x86-opcode-map.txt +11 -2
index c5e825d44766..fbc53481bc59 100644
@@ -620,7 +620,16 @@ ea: SAVEPREVSSP (f3)
 # Skip 0xeb-0xff
 EndTable
 
-Table: 3-byte opcode 2 (0x0f 0x38)
+Table: 3-byte opcode 2 (0x0f 0x1e)
+Referrer:
+AVXcode:
+# Skip 0x00-0xf9
+fa: ENDBR64 (f3)
+fb: ENDBR32 (f3)
+#skip 0xfc-0xff
+EndTable
+
+Table: 3-byte opcode 3 (0x0f 0x38)
 Referrer: 3-byte escape 1
 AVXcode: 2
 # 0x0f 0x38 0x00-0x0f
@@ -804,7 +813,7 @@ f6: ADCX Gy,Ey (66) | ADOX Gy,Ey (F3) | MULX By,Gy,rDX,Ey (F2),(v) | WRSS Pq,Qq
 f7: BEXTR Gy,Ey,By (v) | SHLX Gy,Ey,By (66),(v) | SARX Gy,Ey,By (F3),(v) | SHRX Gy,Ey,By (F2),(v)
 EndTable
 
-Table: 3-byte opcode 3 (0x0f 0x3a)
+Table: 3-byte opcode 4 (0x0f 0x3a)
 Referrer: 3-byte escape 2
 AVXcode: 3
 # 0x0f 0x3a 0x00-0xff
Signature · 1 line
2.17.1
#62 lore raw

PATCH v7 9/14 x86/vdso: Insert endbr32/endbr64 to vDSO

In reply to Yu-cheng Yu

From: "H.J. Lu" <[email protected]>

When Intel indirect branch tracking is enabled, functions in vDSO which may be called indirectly must have endbr32 or endbr64 as the first instruction. Compiler must support -fcf-protection=branch so that it can be used to compile vDSO.

---
 arch/x86/entry/vdso/Makefile          | 12 +++++++++++-
 arch/x86/entry/vdso/vdso-layout.lds.S |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/Makefile +11 -1
index 42fe42e82baf..718fc17b0d67 100644
@@ -108,13 +108,17 @@ vobjx32s := $(foreach F,$(vobjx32s-y),$(obj)/$F)
 
 # Convert 64bit object file to x32 for x32 vDSO.
 quiet_cmd_x32 = X32     $@
-      cmd_x32 = $(OBJCOPY) -O elf32-x86-64 $< $@
+      cmd_x32 = $(OBJCOPY) -R .note.gnu.property -O elf32-x86-64 $< $@
 
 $(obj)/%-x32.o: $(obj)/%.o FORCE
 	$(call if_changed,x32)
 
 targets += vdsox32.lds $(vobjx32s-y)
 
+ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+    $(obj)/vclock_gettime.o $(obj)/vgetcpu.o $(obj)/vdso32/vclock_gettime.o: KBUILD_CFLAGS += -fcf-protection=branch
+endif
+
 $(obj)/%.so: OBJCOPYFLAGS := -S
 $(obj)/%.so: $(obj)/%.so.dbg FORCE
 	$(call if_changed,objcopy)
@@ -173,6 +177,12 @@ quiet_cmd_vdso = VDSO    $@
 VDSO_LDFLAGS = -shared $(call ld-option, --hash-style=both) \
 	$(call ld-option, --build-id) $(call ld-option, --eh-frame-hdr) \
 	-Bsymbolic
+ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+  VDSO_LDFLAGS += $(call ldoption, -z$(comma)ibt)
+endif
+ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER
+  VDSO_LDFLAGS += $(call ldoption, -z$(comma)shstk)
+endif
 GCOV_PROFILE := n
 
 #
arch/x86/entry/vdso/vdso-layout.lds.S +1 -0
index 93c6dc7812d0..3fea2ce318bc 100644
@@ -52,6 +52,7 @@ SECTIONS
 		*(.gnu.linkonce.b.*)
 	}						:text
 
+	.note.gnu.property : { *(.note.gnu.property) }	:text	:note
 	.note		: { *(.note.*) }		:text	:note
 
 	.eh_frame_hdr	: { *(.eh_frame_hdr) }		:text	:eh_frame_hdr
Signature · 1 line
2.17.1
maintainer Acked-by on v7 9/14
#63 lore raw

Re: [PATCH v7 09/14] x86/vdso: Insert endbr32/endbr64 to vDSO

Quoted 7 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 must have endbr32 or endbr64 as the first instruction. Compiler must support -fcf-protection=branch so that it can be used to compile vDSO.

Quoted 2 lines

You're still missing your Signed-off-by.

#64 lore raw

PATCH v7 10/14 x86/vdso/32: Add ENDBR32 to __kernel_vsyscall entry point

In reply to Yu-cheng Yu

From: "H.J. Lu" <[email protected]>

Add ENDBR32 to __kernel_vsyscall entry point.

---
 arch/x86/entry/vdso/vdso32/system_call.S | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/entry/vdso/vdso32/system_call.S +3 -0
index 263d7433dea8..2fc8141fff4e 100644
@@ -14,6 +14,9 @@
 	ALIGN
 __kernel_vsyscall:
 	CFI_STARTPROC
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+	endbr32
+#endif
 	/*
 	 * Reshuffle regs so that all of any of the entry instructions
 	 * will preserve enough state.
Signature · 1 line
2.17.1
#66 lore raw

PATCH v7 11/14 x86/vsyscall/64: Add ENDBR64 to vsyscall entry points

In reply to Yu-cheng Yu

From: "H.J. Lu" <[email protected]>

Add ENDBR64 to vsyscall entry points.

---
 arch/x86/entry/vsyscall/vsyscall_emu_64.S | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/entry/vsyscall/vsyscall_emu_64.S +9 -0
index 2e203f3a25a7..040696333457 100644
@@ -17,16 +17,25 @@ __PAGE_ALIGNED_DATA
 	.type __vsyscall_page, @object
 __vsyscall_page:
 
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+	endbr64
+#endif
 	mov $__NR_gettimeofday, %rax
 	syscall
 	ret
 
 	.balign 1024, 0xcc
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+	endbr64
+#endif
 	mov $__NR_time, %rax
 	syscall
 	ret
 
 	.balign 1024, 0xcc
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+	endbr64
+#endif
 	mov $__NR_getcpu, %rax
 	syscall
 	ret
Signature · 1 line
2.17.1
maintainer
#67 lore raw

Re: [PATCH v7 11/14] x86/vsyscall/64: Add ENDBR64 to vsyscall entry points

Quoted 4 lines · Yu-cheng Yu

From: "H.J. Lu" <[email protected]>

Add ENDBR64 to vsyscall entry points.

I'm still okay with this patch, but this is rather silly. If anyone actually executes this code, they're doing it wrong.

--Andy

#68 lore raw

PATCH v7 12/14 x86/vsyscall/64: Fixup shadow stack and branch tracking for vsyscall

In reply to Yu-cheng Yu

When emulating a RET, also unwind the task's shadow stack and cancel the current branch tracking status.

---
 arch/x86/entry/vsyscall/vsyscall_64.c | 28 +++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/x86/entry/vsyscall/vsyscall_64.c +28 -0
index d9d81ad7a400..6869ef9d1e8b 100644
@@ -38,6 +38,8 @@
 #include <asm/fixmap.h>
 #include <asm/traps.h>
 #include <asm/paravirt.h>
+#include <asm/fpu/xstate.h>
+#include <asm/fpu/types.h>
 
 #define CREATE_TRACE_POINTS
 #include "vsyscall_trace.h"
@@ -92,6 +94,30 @@ static int addr_to_vsyscall_nr(unsigned long addr)
 	return nr;
 }
 
+void fixup_shstk(void)
+{
+#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER
+	u64 r;
+
+	if (current->thread.cet.shstk_enabled) {
+		rdmsrl(MSR_IA32_PL3_SSP, r);
+		wrmsrl(MSR_IA32_PL3_SSP, r + 8);
+	}
+#endif
+}
+
+void fixup_ibt(void)
+{
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+	u64 r;
+
+	if (current->thread.cet.ibt_enabled) {
+		rdmsrl(MSR_IA32_U_CET, r);
+		wrmsrl(MSR_IA32_U_CET, r & ~MSR_IA32_CET_WAIT_ENDBR);
+	}
+#endif
+}
+
 static bool write_ok_or_segv(unsigned long ptr, size_t size)
 {
 	/*
@@ -265,6 +291,8 @@ bool emulate_vsyscall(struct pt_regs *regs, unsigned long address)
 	/* Emulate a ret instruction. */
 	regs->ip = caller;
 	regs->sp += 8;
+	fixup_shstk();
+	fixup_ibt();
 	return true;
 
 sigsegv:
Signature · 1 line
2.17.1
maintainer
#69 lore raw

Re: [PATCH v7 12/14] x86/vsyscall/64: Fixup shadow stack and branch tracking for vsyscall

Quoted 39 lines · Yu-cheng Yu

When emulating a RET, also unwind the task's shadow stack and cancel the current branch tracking status.

---
 arch/x86/entry/vsyscall/vsyscall_64.c | 28 +++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/x86/entry/vsyscall/vsyscall_64.c +15 -0
index d9d81ad7a400..6869ef9d1e8b 100644
@@ -38,6 +38,8 @@
 #include <asm/fixmap.h>
 #include <asm/traps.h>
 #include <asm/paravirt.h>
+#include <asm/fpu/xstate.h>
+#include <asm/fpu/types.h>
 
 #define CREATE_TRACE_POINTS
 #include "vsyscall_trace.h"
@@ -92,6 +94,30 @@ static int addr_to_vsyscall_nr(unsigned long addr)
        return nr;
 }
 
+void fixup_shstk(void)
+{
+#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER
+       u64 r;
+
+       if (current->thread.cet.shstk_enabled) {
+               rdmsrl(MSR_IA32_PL3_SSP, r);
+               wrmsrl(MSR_IA32_PL3_SSP, r + 8);
+       }
+#endif
+}
+
+void fixup_ibt(void)

arch/x86/entry/vsyscall/vsyscall_64.c static int addr_to_vsyscall_nr(unsigned long addr) · line 119

Quoted 10 lines
arch/x86/entry/vsyscall/vsyscall_64.c +10 -0
+{
+#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER
+       u64 r;
+
+       if (current->thread.cet.ibt_enabled) {
+               rdmsrl(MSR_IA32_U_CET, r);
+               wrmsrl(MSR_IA32_U_CET, r & ~MSR_IA32_CET_WAIT_ENDBR);
+       }
+#endif
+}

These should be static.

But please just inline them directly in their one call site. The code will be a lot easier to understand.

--Andy

#70 lore raw

PATCH v7 13/14 x86/cet: Add PTRACE interface for CET

In reply to Yu-cheng Yu

Add REGSET_CET64/REGSET_CET32 to get/set CET MSRs:

IA32_U_CET (user-mode CET settings) and
IA32_PL3_SSP (user-mode shadow stack)

---
 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
index d5bdffb9d27f..edad0d889084 100644
@@ -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
index a166c960bc9e..db902ed9b353 100644
@@ -51,7 +51,9 @@ enum x86_regset {
 	REGSET_IOPERM64 = REGSET_XFP,
 	REGSET_XSTATE,
 	REGSET_TLS,
+	REGSET_CET64 = REGSET_TLS,
 	REGSET_IOPERM32,
+	REGSET_CET32,
 };
 
 struct pt_regs_offset {
@@ -1268,6 +1270,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 = {
@@ -1323,6 +1332,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
index 316177ce9e76..4f320d96d538 100644
@@ -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.17.1
#71 lore raw

PATCH v7 14/14 x86: Discard .note.gnu.property sections

In reply to Yu-cheng Yu

From: "H.J. Lu" <[email protected]>

With the command-line option, -mx86-used-note=yes, the x86 assembler in binutils 2.32 and above generates a program property note in a note section, .note.gnu.property, to encode used x86 ISAs and features. To exclude .note.gnu.property sections from NOTE segment in x86 kernel linker script:

PHDRS {
text PT_LOAD FLAGS(5);
data PT_LOAD FLAGS(6);
percpu PT_LOAD FLAGS(6);
init PT_LOAD FLAGS(7);
note PT_NOTE FLAGS(0);
}
SECTIONS
{
...
.notes : AT(ADDR(.notes) - 0xffffffff80000000) { __start_notes = .; KEEP(*(.not
e.*)) __stop_notes = .; } :text :note
...
}

this patch discards .note.gnu.property sections in kernel linker script by adding

/DISCARD/ : {
*(.note.gnu.property)
}

before .notes sections. Since .exit.text and .exit.data sections are discarded at runtime, it undefines EXIT_TEXT and EXIT_DATA to exclude .exit.text and .exit.data sections from default discarded sections.

---
 arch/x86/kernel/vmlinux.lds.S | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kernel/vmlinux.lds.S +10 -1
index 0850b5149345..d2594b482c09 100644
@@ -146,6 +146,10 @@ SECTIONS
 	/* End of text section */
 	_etext = .;
 
+	/* .note.gnu.property sections should be discarded */
+	/DISCARD/ : {
+		*(.note.gnu.property)
+	}
 	NOTES :text :note
 
 	EXCEPTION_TABLE(16) :text = 0x9090
@@ -382,7 +386,12 @@ SECTIONS
 	STABS_DEBUG
 	DWARF_DEBUG
 
-	/* Sections to be discarded */
+	/* Sections to be discarded.  EXIT_TEXT and EXIT_DATA discard at runtime.
+	 * not link time.  */
+#undef EXIT_TEXT
+#define EXIT_TEXT
+#undef EXIT_DATA
+#define EXIT_DATA
 	DISCARDS
 	/DISCARD/ : {
 		*(.eh_frame)
Signature · 1 line
2.17.1