Skip to contentSkip to conversation

Usermode Indirect Branch Tracking

PATCH Superseded Richard Patel 4 participants 22 messages May 17 – May 25

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Usermode Indirect Branch Tracking

I was quite surprised that the Linux kernel still does not allow userspace to enable x86 IBT (indirect jmp/call integrity).

Compilers and linkers have been emitting 'endbr64' IBT markers and ELF support notes for a while now.

The hard work was done years ago by Intel:
https://lore.kernel.org/all/[email protected]/

In summary, usermode IBT requires 3 things:
1. Set the CET_ENDBR_EN bit in MSR_IA32_U_CET for each IBT-enabled thread (PATCH 2,5)
2. Back up the WAIT_FOR_ENDBR bit across signal handling (PATCH 3,4)
3. Provide a way for usermode to enable it (PATCH 5)

This builds on top of Yu Cheng's work, with some adaptations:
- FRED support
- Implemented the existing prctl(PR_CFI_*) API
- Removed ELF parsing (can be added later)

Unresolved questions:
- Is there a cleaner way to do the WAIT_FOR_ENDBR XSAVE fallback?
- What to do about 'notrack jmp *rax'?
I leave CET_NO_TRACK_EN enabled, which weakens IBT, by enabling a jump prefix that skips the ENDBR check. GCC emits it for jump tables (-mcet-switch). We could introduce a PR_CFI_IBT_STRICT bit.
- There's some obvious overlap with arch_prctl(ARCH_SHSTK_*). Happy to use that API instead.

Richard Patel (7):
x86: add userspace IBT config option
x86: shstk: don't clobber IBT bits in U_CET MSR
x86: signal handler support for IBT
x86: ban 32-bit sigreturn when user IBT enabled
x86: expose user IBT via PR_CFI_BRANCH_LANDING_PADS x86/entry/vdso: build with IBT support selftests/x86: test usermode IBT

 arch/x86/Kconfig                            |  17 ++
 arch/x86/entry/vdso/common/Makefile.include |   3 +-
 arch/x86/include/asm/cpufeatures.h          |   1 +
 arch/x86/include/asm/ibt.h                  |  16 ++
 arch/x86/include/asm/processor.h            |   5 +
 arch/x86/include/uapi/asm/ucontext.h        |   5 +
 arch/x86/kernel/Makefile                    |   1 +
 arch/x86/kernel/cet.c                       |   3 +-
 arch/x86/kernel/cpu/common.c                |  14 +-
 arch/x86/kernel/ibt.c                       | 175 ++++++++++++++++++++
 arch/x86/kernel/process_64.c                |   2 +
 arch/x86/kernel/shstk.c                     |  12 +-
 arch/x86/kernel/signal_32.c                 |   5 +
 arch/x86/kernel/signal_64.c                 |   6 +
 tools/arch/x86/include/asm/cpufeatures.h    |   1 +
 tools/testing/selftests/x86/Makefile        |   5 +-
 tools/testing/selftests/x86/user_ibt.c      | 157 ++++++++++++++++++
 17 files changed, 420 insertions(+), 8 deletions(-)
 create mode 100644 arch/x86/kernel/ibt.c
 create mode 100644 tools/testing/selftests/x86/user_ibt.c

--
2.47.3

#1 lore raw

PATCH 1/7 x86: add userspace IBT config option

Adds a X86_USER_IBT Kconfig option and "nouseribt" command-line option. Default disabled for now.

These prepare for userspace support for IBT (forward-edge control flow integrity protection).

User IBT works even if kernel IBT is disabled. However, ibt=off also disables user IBT.

---
 arch/x86/Kconfig                         | 17 +++++++++++++++++
 arch/x86/include/asm/cpufeatures.h       |  1 +
 arch/x86/kernel/cet.c                    |  3 ++-
 arch/x86/kernel/cpu/common.c             | 14 ++++++++++++--
 tools/arch/x86/include/asm/cpufeatures.h |  1 +
 5 files changed, 33 insertions(+), 3 deletions(-)
arch/x86/Kconfig +17 -0
index f3f7cb01d69d..12cc944b63c7 100644
@@ -1901,6 +1901,23 @@ config X86_USER_SHADOW_STACK
 
 	  If unsure, say N.
 
+config X86_USER_IBT
+	bool "X86 userspace indirect branch tracking"
+	depends on X86_64
+	select X86_CET
+	help
+	  Support Indirect Branch Tracking protection for userspace
+	  applications. IBT is a hardware-supported coarse-grained
+	  forward-edge Control Flow Integrity protection feature.
+	  It enforces that all indirect calls must land on an ENDBR
+	  instruction. Applications must be enabled to use it, and old
+	  userspace does not get protection "for free". Enables the
+	  PR_CFI_BRANCH_LANDING_PADS prctl CFI option.
+
+	  CPUs supporting IBT were first released in 2021.
+
+	  If unsure, say N.
+
 config INTEL_TDX_HOST
 	bool "Intel Trust Domain Extensions (TDX) host support"
 	depends on CPU_SUP_INTEL
arch/x86/include/asm/cpufeatures.h +1 -0
index 1d506e5d6f46..1825cbf864c0 100644
@@ -516,6 +516,7 @@
 						      * and purposes if CLEAR_CPU_BUF_VM is set).
 						      */
 #define X86_FEATURE_X2AVIC_EXT		(21*32+20) /* AMD SVM x2AVIC support for 4k vCPUs */
+#define X86_FEATURE_USER_IBT		(21*32+21) /* Indirect Branch Tracking for user mode applications */
 
 /*
  * BUG word(s)
arch/x86/kernel/cet.c +2 -1
index 99444409c026..3ccf47e82da1 100644
@@ -149,7 +149,8 @@ __setup("ibt=", ibt_setup);
 DEFINE_IDTENTRY_ERRORCODE(exc_control_protection)
 {
 	if (user_mode(regs)) {
-		if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK))
+		if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK) ||
+		    cpu_feature_enabled(X86_FEATURE_USER_IBT))
 			do_user_cp_fault(regs, error_code);
 		else
 			do_unexpected_cp(regs, error_code);
arch/x86/kernel/cpu/common.c +12 -2
index a4268c47f2bc..2839edd92331 100644
@@ -634,7 +634,7 @@ __noendbr void ibt_restore(u64 save)
 
 static __always_inline void setup_cet(struct cpuinfo_x86 *c)
 {
-	bool user_shstk, kernel_ibt;
+	bool user_shstk, kernel_ibt, user_ibt;
 
 	if (!IS_ENABLED(CONFIG_X86_CET))
 		return;
@@ -642,13 +642,19 @@ static __always_inline void setup_cet(struct cpuinfo_x86 *c)
 	kernel_ibt = HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT);
 	user_shstk = cpu_feature_enabled(X86_FEATURE_SHSTK) &&
 		     IS_ENABLED(CONFIG_X86_USER_SHADOW_STACK);
+	/* User IBT only needs hardware IBT, not kernel-enabled IBT. */
+	user_ibt = cpu_has(c, X86_FEATURE_IBT) &&
+		   IS_ENABLED(CONFIG_X86_USER_IBT);
 
-	if (!kernel_ibt && !user_shstk)
+	if (!kernel_ibt && !user_shstk && !user_ibt)
 		return;
 
 	if (user_shstk)
 		set_cpu_cap(c, X86_FEATURE_USER_SHSTK);
 
+	if (user_ibt)
+		set_cpu_cap(c, X86_FEATURE_USER_IBT);
+
 	if (kernel_ibt)
 		wrmsrq(MSR_IA32_S_CET, CET_ENDBR_EN);
 	else
@@ -666,6 +672,7 @@ static __always_inline void setup_cet(struct cpuinfo_x86 *c)
 __noendbr void cet_disable(void)
 {
 	if (!(cpu_feature_enabled(X86_FEATURE_IBT) ||
+	      cpu_feature_enabled(X86_FEATURE_USER_IBT) ||
 	      cpu_feature_enabled(X86_FEATURE_SHSTK)))
 		return;
 
@@ -1760,6 +1767,9 @@ static void __init cpu_parse_early_param(void)
 	if (cmdline_find_option_bool(boot_command_line, "nousershstk"))
 		setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK);
 
+	if (cmdline_find_option_bool(boot_command_line, "nouseribt"))
+		setup_clear_cpu_cap(X86_FEATURE_USER_IBT);
+
 	/* Minimize the gap between FRED is available and available but disabled. */
 	arglen = cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg));
 	if (arglen == 3 && !strncmp(arg, "off", 3))
tools/arch/x86/include/asm/cpufeatures.h +1 -0
index 86d17b195e79..1cf22d27c7a1 100644
@@ -515,6 +515,7 @@
 						      * and purposes if CLEAR_CPU_BUF_VM is set).
 						      */
 #define X86_FEATURE_X2AVIC_EXT		(21*32+20) /* AMD SVM x2AVIC support for 4k vCPUs */
+#define X86_FEATURE_USER_IBT		(21*32+21) /* Indirect Branch Tracking for user mode applications */
 
 /*
  * BUG word(s)
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/7 x86: shstk: don't clobber IBT bits in U_CET MSR

In reply to Richard Patel

Updates usermode shadow stack code to not set IBT-related bits in the U_CET MSR.

---
 arch/x86/kernel/shstk.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kernel/shstk.c +10 -2
index 0ca64900192f..ff4106dcfec4 100644
@@ -150,6 +150,7 @@ static int shstk_setup(void)
 {
 	struct thread_shstk *shstk = &current->thread.shstk;
 	unsigned long addr, size;
+	u64 msrval;
 
 	/* Already enabled */
 	if (features_enabled(ARCH_SHSTK_SHSTK))
@@ -166,7 +167,10 @@ static int shstk_setup(void)
 
 	fpregs_lock_and_load();
 	wrmsrq(MSR_IA32_PL3_SSP, addr + size);
-	wrmsrq(MSR_IA32_U_CET, CET_SHSTK_EN);
+	rdmsrq(MSR_IA32_U_CET, msrval);
+	msrval &= ~CET_WRSS_EN;
+	msrval |= CET_SHSTK_EN;
+	wrmsrq(MSR_IA32_U_CET, msrval);
 	fpregs_unlock();
 
 	shstk->base = addr;
@@ -520,6 +524,8 @@ static int wrss_control(bool enable)
 
 static int shstk_disable(void)
 {
+	u64 msrval;
+
 	if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK))
 		return -EOPNOTSUPP;
 
@@ -528,8 +534,10 @@ static int shstk_disable(void)
 		return 0;
 
 	fpregs_lock_and_load();
+	rdmsrq(MSR_IA32_U_CET, msrval);
 	/* Disable WRSS too when disabling shadow stack */
-	wrmsrq(MSR_IA32_U_CET, 0);
+	msrval &= ~(CET_SHSTK_EN | CET_WRSS_EN);
+	wrmsrq(MSR_IA32_U_CET, msrval);
 	wrmsrq(MSR_IA32_PL3_SSP, 0);
 	fpregs_unlock();
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/7 x86: signal handler support for IBT

In reply to Richard Patel

Addresses an edge case in usermode IBT with signal handlers. When entering and exiting signals, the WAIT_FOR_ENDBR CPU state must be restored. WAIT_FOR_ENDBR is a flag that raises a CET violation if the next instruction is not endbr64.

If a thread enters a signal handler immediately after executing an indirect jump (before reaching an endbr64), the WAIT_FOR_ENDBR would otherwise mistakenly cause a CET violation on the signal handler's first instruction.

Worse, an attacker could circumvent IBT by triggering a signal before a vulnerable jump, call rt_sigreturn in that signal, and then return to the original indirect jump target without endbr64 checking.

Unless FRED is enabled, the WAIT_FOR_ENDBR flag is backed up from the U_CET MSR. Due to XSAVE, this MSR might be stale in kernel-mode.

A gap in 32-bit signal handling is resolved in a follow-up commit.

---
 arch/x86/include/asm/ibt.h           | 14 +++++
 arch/x86/include/asm/processor.h     |  5 ++
 arch/x86/include/uapi/asm/ucontext.h |  5 ++
 arch/x86/kernel/Makefile             |  1 +
 arch/x86/kernel/ibt.c                | 88 ++++++++++++++++++++++++++++
 arch/x86/kernel/signal_64.c          |  6 ++
 6 files changed, 119 insertions(+)
 create mode 100644 arch/x86/kernel/ibt.c
arch/x86/include/asm/ibt.h +14 -0
index 5e45d6424722..3fe464bf83e7 100644
@@ -114,4 +114,18 @@ static inline void ibt_restore(u64 save) { }
 
 #define ENDBR_INSN_SIZE		(4*HAS_KERNEL_IBT)
 
+#ifndef __ASSEMBLER__
+
+struct pt_regs;
+
+#ifdef CONFIG_X86_USER_IBT
+bool user_ibt_pop_wait_endbr(struct pt_regs *regs);
+void user_ibt_restore_wait_endbr(struct pt_regs *regs, bool wait_endbr);
+#else
+static inline bool user_ibt_pop_wait_endbr(struct pt_regs *regs) { return false; }
+static inline void user_ibt_restore_wait_endbr(struct pt_regs *regs, bool wait_endbr) {}
+#endif /* CONFIG_X86_USER_IBT */
+
+#endif /* __ASSEMBLER__ */
+
 #endif /* _ASM_X86_IBT_H */
arch/x86/include/asm/processor.h +5 -0
index 10b5355b323e..6ce8f7b6607c 100644
@@ -504,6 +504,11 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_USER_IBT
+	unsigned int		ibt:1;
+	unsigned int		ibt_locked:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/include/uapi/asm/ucontext.h +5 -0
index 5657b7a49f03..0271f5e8aa14 100644
@@ -51,6 +51,11 @@
 #define UC_STRICT_RESTORE_SS	0x4
 #endif
 
+/*
+ * Indicates IBT status WAIT_FOR_ENDBR.
+ */
+#define UC_WAIT_ENDBR		0x8
+
 #include <asm-generic/ucontext.h>
 
 #endif /* _ASM_X86_UCONTEXT_H */
arch/x86/kernel/Makefile +1 -0
index 47a32f583930..05c87f014552 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_CALL_THUNKS)		+= callthunks.o
 obj-$(CONFIG_X86_CET)			+= cet.o
 
 obj-$(CONFIG_X86_USER_SHADOW_STACK)	+= shstk.o
+obj-$(CONFIG_X86_USER_IBT)		+= ibt.o
 
 ###
 # 64 bit specific files
arch/x86/kernel/ibt.c new file +88 -0

88 lines not on this page · raw message

arch/x86/kernel/signal_64.c +6 -0
index d483b585c6c6..9f1861540d27 100644
@@ -14,6 +14,7 @@
 
 #include <asm/ucontext.h>
 #include <asm/fpu/signal.h>
+#include <asm/ibt.h>
 #include <asm/sighandling.h>
 
 #include <asm/syscall.h>
@@ -92,6 +93,8 @@ static bool restore_sigcontext(struct pt_regs *regs,
 	if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
 		force_valid_ss(regs);
 
+	user_ibt_restore_wait_endbr(regs, uc_flags & UC_WAIT_ENDBR);
+
 	return fpu__restore_sig((void __user *)sc.fpstate, 0);
 }
 
@@ -158,6 +161,9 @@ static unsigned long frame_uc_flags(struct pt_regs *regs)
 	if (likely(user_64bit_mode(regs)))
 		flags |= UC_STRICT_RESTORE_SS;
 
+	if (user_ibt_pop_wait_endbr(regs))
+		flags |= UC_WAIT_ENDBR;
+
 	return flags;
 }
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/7 x86: ban 32-bit sigreturn when user IBT enabled

In reply to Richard Patel

IBT enforces that indirect branch targets land on an endbr instruction. The CPU enforces this by setting the 'WAIT_FOR_ENDBR' bit after executing an indirect branch/jump.

The only relevant edge case with user IBT is signal handling:
When entering/leaving a signal handler, the WAIT_FOR_ENDBR bit must be backed up/restored.

IBT is not implemented for 32-bit and cannot be enabled using a 32-bit syscall. However, a 64-bit thread could far jump into 32-bit. Therefore, 32-bit sigreturn must be banned until IBT supports that environment.

---
 arch/x86/kernel/signal_32.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kernel/signal_32.c +5 -0
index e55cf19e68fe..7cb76d794366 100644
@@ -143,6 +143,11 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,
 	regs->ds = fixup_rpl(sc.ds);
 #endif
 
+#ifdef CONFIG_X86_USER_IBT
+	if (current->thread.ibt)
+		return false;
+#endif
+
 	return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
 }
 
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH 4/7] x86: ban 32-bit sigreturn when user IBT enabled

Quoted 26 lines · Richard Patel

IBT enforces that indirect branch targets land on an endbr instruction. The CPU enforces this by setting the 'WAIT_FOR_ENDBR' bit after executing an indirect branch/jump.

The only relevant edge case with user IBT is signal handling:
When entering/leaving a signal handler, the WAIT_FOR_ENDBR bit must be backed up/restored.

IBT is not implemented for 32-bit and cannot be enabled using a 32-bit syscall. However, a 64-bit thread could far jump into 32-bit. Therefore, 32-bit sigreturn must be banned until IBT supports that environment.

---
arch/x86/kernel/signal_32.c | 5 +++++

1 file changed, 5 insertions(+)
arch/x86/kernel/signal_32.c +0 -0
index e55cf19e68fe..7cb76d794366 100644
@@ -143,6 +143,11 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,
 	regs->ds = fixup_rpl(sc.ds);

arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 153

Quoted 10 lines
arch/x86/kernel/signal_32.c +5 -0
 #endif
 
+#ifdef CONFIG_X86_USER_IBT
+	if (current->thread.ibt)
+		return false;
+#endif
+
 	return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
 }
 

Dumb question: is there any reason not to just enable it for 32 bits? It doesn't seem that it would be that big of a delta to Just Do It.™

That being said, I suspect the number of users will be very small if any.

#6 lore raw

Re: [PATCH 4/7] x86: ban 32-bit sigreturn when user IBT enabled

Quoted 8 lines · H. Peter Anvin
Quoted 4 lines · Richard Patel

IBT is not implemented for 32-bit and cannot be enabled using a 32-bit syscall. However, a 64-bit thread could far jump into 32-bit. Therefore, 32-bit sigreturn must be banned until IBT supports that environment.

Dumb question: is there any reason not to just enable it for 32 bits? It doesn't seem that it would be that big of a delta to Just Do It.™

That being said, I suspect the number of users will be very small if any.

Yes, partially, good call. It'd be tricky for legacy IA32 signals. sigframe_ia32 would need to be modified I think. For rt_sigframe_ia32 (rt_sigreturn) we can put it in ucontext_ia32::uc_flags.

I'll fix it in v2 later this week and add tests.

#7 lore raw

Re: [PATCH 4/7] x86: ban 32-bit sigreturn when user IBT enabled

In reply to H. Peter Anvin
Quoted 20 lines · H. Peter Anvin
Quoted 6 lines · Richard Patel
arch/x86/kernel/signal_32.c +0 -0
index e55cf19e68fe..7cb76d794366 100644
@@ -143,6 +143,11 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,
 	regs->ds = fixup_rpl(sc.ds);

arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 153

Quoted 10 lines
arch/x86/kernel/signal_32.c +5 -0
 #endif
 
+#ifdef CONFIG_X86_USER_IBT
+	if (current->thread.ibt)
+		return false;
+#endif
+
 	return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
 }
 

Dumb question: is there any reason not to just enable it for 32 bits? It doesn't seem that it would be that big of a delta to Just Do It.™

Quoted 1 line

That being said, I suspect the number of users will be very small if any.

Hello Peter,

I researched 32-bit user IBT support a bit more.

Intel's original patches used uc_flags, which is not available in the legacy 32-bit frame (breaks sigreturn(2)).

But you could also store Intel CET state via XSAVE into sigframe fpstate, like for Arm64 BTI.

Unfortunately though, this includes both CET control flags ("is IBT enabled?") and user state (WAIT_FOR_ENDBR). Since fpstate is writable, XFEATURE_USER_CET is in XFEATURE_MASK_SUPERVISOR_ALL.

So, we have 3 options:

1. Include CET in both XSAVE and XRSTOR, but revert user changes to control bits before restoring.

2. Include CET in XSAVE, exclude CET from XRSTOR. Parse XSAVE and restore IBT state "by hand".
* Breaking XSAVE/XRSTOR symmetry seems like a bad idea? But the user can already remove xfeatures bits, I think.

3. No CET in XSAVE, instead abuse uc_flags to save this state bit (this patch series).
* uc_flags does not exist in sigframe_ia32, which hasn't been touched in 10 years

IMO: Option 1 seems crazy. Option 2 worth a sketch. Option 3 is ugly.

Really curious what you think. I'm going to send out v2 today with option 2 (CET XSAVE, software restore), and if anyone hates it, I will revert to option 3 (CET software backup and restore), and at least add rt_sigreturn ia32 support.

Btw, OpenBSD doesn't do any of these and discards IBT state. So, if you spam signals on OpenBSD, you can bypass their IBT. That is, uh, option 4, I guess.

Thanks,
-Richard

#8 lore raw

Re: [PATCH 4/7] x86: ban 32-bit sigreturn when user IBT enabled

Quoted 54 lines · On Sun, 24 May 2026 21:53:09 +0000 Richard Patel <[email protected]> wrote:
Quoted 20 lines · H. Peter Anvin
Quoted 6 lines · Richard Patel
arch/x86/kernel/signal_32.c +0 -0
index e55cf19e68fe..7cb76d794366 100644
@@ -143,6 +143,11 @@ static bool ia32_restore_sigcontext(struct pt_regs *regs,
 	regs->ds = fixup_rpl(sc.ds);

arch/x86/kernel/signal_32.c static bool ia32_restore_sigcontext(struct pt_regs *regs, · line 153

Quoted 10 lines
arch/x86/kernel/signal_32.c +5 -0
 #endif
 
+#ifdef CONFIG_X86_USER_IBT
+	if (current->thread.ibt)
+		return false;
+#endif
+
 	return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
 }
 

Dumb question: is there any reason not to just enable it for 32 bits? It doesn't seem that it would be that big of a delta to Just Do It.™

Quoted 1 line

That being said, I suspect the number of users will be very small if any.

Hello Peter,

I researched 32-bit user IBT support a bit more.

Intel's original patches used uc_flags, which is not available in the legacy 32-bit frame (breaks sigreturn(2)).

But you could also store Intel CET state via XSAVE into sigframe fpstate, like for Arm64 BTI.

Unfortunately though, this includes both CET control flags ("is IBT enabled?") and user state (WAIT_FOR_ENDBR). Since fpstate is writable, XFEATURE_USER_CET is in XFEATURE_MASK_SUPERVISOR_ALL.

So, we have 3 options:

1. Include CET in both XSAVE and XRSTOR, but revert user changes to control bits before restoring.

2. Include CET in XSAVE, exclude CET from XRSTOR. Parse XSAVE and restore IBT state "by hand".
* Breaking XSAVE/XRSTOR symmetry seems like a bad idea? But the user can already remove xfeatures bits, I think.

3. No CET in XSAVE, instead abuse uc_flags to save this state bit (this patch series).
* uc_flags does not exist in sigframe_ia32, which hasn't been touched in 10 years

IMO: Option 1 seems crazy. Option 2 worth a sketch. Option 3 is ugly.

Quoted 8 lines

Really curious what you think. I'm going to send out v2 today with option 2 (CET XSAVE, software restore), and if anyone hates it, I will revert to option 3 (CET software backup and restore), and at least add rt_sigreturn ia32 support.

Btw, OpenBSD doesn't do any of these and discards IBT state. So, if you spam signals on OpenBSD, you can bypass their IBT. That is, uh, option 4, I guess.

That is still better than IBT disabled. Wouldn't you crash the program when the signal 'missed'. So there are probably easier ways to break things.

I think that would have the side effect that code would run when single-stepped by gdb, but fail otherwise.
If true that would confuse things.

-- David

Quoted 4 lines

Thanks,
-Richard

#9 lore raw

PATCH 5/7 x86: expose user IBT via PR_CFI_BRANCH_LANDING_PADS

In reply to Richard Patel

Allows userspace applications to enable IBT (forward-edge control flow integrity protection) using the portable PR_CFI prctl API.

The name 'branch landing pads' is RISC-V specific, but the mechanism is nearly identical in x86.

This setting enables the following MSR_IA32_U_CET bits:
- CET_ENDBR_EN (enforce endbr as indirect branch target)
- CET_NOTRACK_EN (jump modifier to opt-out of IBT checking)

Kernel-mode IBT (as part of CFI) bans notrack. A future prctl flag could be introduced to ban notrack in usermode too.

---
 arch/x86/include/asm/ibt.h   |  2 +
 arch/x86/kernel/ibt.c        | 87 ++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/process_64.c |  2 +
 3 files changed, 91 insertions(+)
arch/x86/include/asm/ibt.h +2 -0
index 3fe464bf83e7..a67c9ceaaf9e 100644
@@ -121,9 +121,11 @@ struct pt_regs;
 #ifdef CONFIG_X86_USER_IBT
 bool user_ibt_pop_wait_endbr(struct pt_regs *regs);
 void user_ibt_restore_wait_endbr(struct pt_regs *regs, bool wait_endbr);
+void reset_thread_ibt(void);
 #else
 static inline bool user_ibt_pop_wait_endbr(struct pt_regs *regs) { return false; }
 static inline void user_ibt_restore_wait_endbr(struct pt_regs *regs, bool wait_endbr) {}
+static inline void reset_thread_ibt(void) {}
 #endif /* CONFIG_X86_USER_IBT */
 
 #endif /* __ASSEMBLER__ */
arch/x86/kernel/ibt.c +87 -0

102 lines not on this page · raw message

arch/x86/kernel/process_64.c +2 -0
index b85e715ebb30..4b727cc7bccb 100644
@@ -59,6 +59,7 @@
 #include <asm/fsgsbase.h>
 #include <asm/fred.h>
 #include <asm/msr.h>
+#include <asm/ibt.h>
 #ifdef CONFIG_IA32_EMULATION
 /* Not included via unistd.h */
 #include <asm/unistd_32_ia32.h>
@@ -540,6 +541,7 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip,
 	}
 
 	reset_thread_features();
+	reset_thread_ibt();
 
 	loadsegment(fs, 0);
 	loadsegment(es, _ds);
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 5/7] x86: expose user IBT via PR_CFI_BRANCH_LANDING_PADS

Thinking out loud ... sorry for the noise

Quoted 6 lines · Richard Patel
arch/x86/kernel/ibt.c +6 -0
+int arch_prctl_set_branch_landing_pad_state(struct task_struct *t, unsigned long state)
+{
+	if (!cpu_feature_enabled(X86_FEATURE_USER_IBT) || in_ia32_syscall())
+		return -EINVAL;
+
+	if (t != current)

arch/x86/kernel/ibt.c static bool user_ibt_enabled(struct task_struct *task) · line 74

Quoted 10 lines
arch/x86/kernel/ibt.c +10 -0
+		return -EINVAL;
+
+	if (user_ibt_locked(t))
+		return -EINVAL;
+
+	if (!(state & (PR_CFI_ENABLE | PR_CFI_DISABLE)))
+		return -EINVAL;
+
+	if (state & PR_CFI_ENABLE && state & PR_CFI_DISABLE)
+		return -EINVAL;

I noticed the caller doesn't filter input arguments, so uapi could pass garbage flags, and we need to filter here. Will fix and add a selftest for this in v2.

I think the RISC-V code from which I ported this has the same problem.

arch/x86/kernel/ibt.c user_ibt_restore_wait_endbr(struct pt_regs *regs, bool wait_endbr) · line 175

Quoted 5 lines
arch/x86/kernel/ibt.c +5 -0
+void reset_thread_ibt(void)
+{
+	current->thread.ibt = false;
+	current->thread.ibt_locked = false;
+}

Should I add a comment here explaining why setting U_CET MSR is not necessary here?

arch/x86/kernel/process_64.c start_thread_common(struct pt_regs *regs, unsigned long new_ip, · line 544

Quoted 2 lines
arch/x86/kernel/process_64.c +1 -0
 	reset_thread_features();
+	reset_thread_ibt();

There is quite obvious overlap with ARCH_SHSTK here. The problem is that 'features' is gated by USER_SHADOW_STACK, and I didn't want to cause a mess there before asking. x86 maintainers, what is your preference?

#11 lore raw

PATCH 6/7 x86/entry/vdso: build with IBT support

In reply to Richard Patel

VDSO should expose ENDBR instructions now that usermode IBT is available.

---
 arch/x86/entry/vdso/common/Makefile.include | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/entry/vdso/common/Makefile.include +1 -2
index 687b3d89b40d..a0dc69b7a330 100644
@@ -46,8 +46,7 @@ flags-y += -fasynchronous-unwind-tables
 # Reset cf protections enabled by compiler default
 flags-y += $(call cc-option, -fcf-protection=none)
 flags-$(X86_USER_SHADOW_STACK) += $(call cc-option, -fcf-protection=return)
-# When user space IBT is supported, enable this.
-# flags-$(CONFIG_USER_IBT) += $(call cc-option, -fcf-protection=branch)
+flags-$(CONFIG_X86_USER_IBT) += $(call cc-option, -fcf-protection=branch)
 
 flags-$(CONFIG_MITIGATION_RETPOLINE) += $(RETPOLINE_VDSO_CFLAGS)
 
Signature · 1 line
2.47.3
#12 lore raw

PATCH 7/7 selftests/x86: test usermode IBT

In reply to Richard Patel

Adds a basic selftest exercising a usermode IBT violation.

---
 tools/testing/selftests/x86/Makefile   |   5 +-
 tools/testing/selftests/x86/user_ibt.c | 157 +++++++++++++++++++++++++
 2 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/user_ibt.c
tools/testing/selftests/x86/Makefile +4 -1
index 434065215d12..203c3d9073f2 100644
@@ -19,7 +19,8 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
 			test_FCMOV test_FCOMI test_FISTTP \
 			vdso_restorer
 TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
-			corrupt_xstate_header amx lam test_shadow_stack avx apx
+			corrupt_xstate_header amx lam test_shadow_stack avx apx \
+			user_ibt
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
@@ -138,3 +139,5 @@ $(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f
 $(OUTPUT)/amx_64: EXTRA_FILES += xstate.c
 $(OUTPUT)/avx_64: EXTRA_FILES += xstate.c
 $(OUTPUT)/apx_64: EXTRA_FILES += xstate.c
+
+$(OUTPUT)/user_ibt_64: CFLAGS += -fcf-protection=branch
tools/testing/selftests/x86/user_ibt.c new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.47.3
#13 lore raw
In reply to Richard Patel
Quoted 20 lines · Richard Patel

I was quite surprised that the Linux kernel still does not allow userspace to enable x86 IBT (indirect jmp/call integrity).

Compilers and linkers have been emitting 'endbr64' IBT markers and ELF support notes for a while now.

The hard work was done years ago by Intel:
https://lore.kernel.org/all/[email protected]/

In summary, usermode IBT requires 3 things:
1. Set the CET_ENDBR_EN bit in MSR_IA32_U_CET for each IBT-enabled thread (PATCH 2,5)
2. Back up the WAIT_FOR_ENDBR bit across signal handling (PATCH 3,4)
3. Provide a way for usermode to enable it (PATCH 5)

This builds on top of Yu Cheng's work, with some adaptations:
- FRED support
- Implemented the existing prctl(PR_CFI_*) API
- Removed ELF parsing (can be added later)

Quoted 8 lines

Unresolved questions:
- Is there a cleaner way to do the WAIT_FOR_ENDBR XSAVE fallback?
- What to do about 'notrack jmp *rax'?
I leave CET_NO_TRACK_EN enabled, which weakens IBT, by enabling a jump prefix that skips the ENDBR check. GCC emits it for jump tables (-mcet-switch). We could introduce a PR_CFI_IBT_STRICT bit.
- There's some obvious overlap with arch_prctl(ARCH_SHSTK_*). Happy to use that API instead.

Right, I think the problem was mostly one of ABI. Various distributions shipped with 'early' patches using an ABI that hadn't seen upstream review much, and when it hit we went uhhh, no!

And then glibc and distros were like, but uhmm, it has to be.

So here we are :-(

Anyway, the most contentious part was the whole backwards compat bitmap crap. When the dynamic linker composes a process of parts that support IBT and parts that do not, you get to deal with fallout.

The IBT spec has this horrid bitmap thing to try and deal with this, and those early patches exposed that piece of shit to userspace. Then later patches (suggested by me) used the ARM64/BTI approach of using PROT_BTI. We'd use a (software) page-table bit, and upon #CP consult that to see if we should eat the trap or produce a warn/signal whatever.

I think we were near something workable there when Rick got pulled from this and put onto something more 'important' and things just haven't moved ever since.

Anyway, glad to see someone has time to poke at this.

#14 lore raw
Quoted 3 lines · Peter Zijlstra

Anyway, the most contentious part was the whole backwards compat bitmap crap. When the dynamic linker composes a process of parts that support IBT and parts that do not, you get to deal with fallout.

Is it acceptable to do all-or-nothing IBT first? And then do a second round of patches with legacy support?

Until then, ld.so could:
- start with IBT, disable it upon loading incompatible DSO
- allow users to manually lock IBT

I thought this weak form of IBT is better than nothing at all, if there's a risk that legacy support derails things.

Btw, apparently OpenBSD enforces kernel+user IBT. I think the end goal is a user_ibt=force command-line param that locks IBT for all processes on startup.

Quoted 5 lines

The IBT spec has this horrid bitmap thing to try and deal with this, and those early patches exposed that piece of shit to userspace. Then later patches (suggested by me) used the ARM64/BTI approach of using PROT_BTI. We'd use a (software) page-table bit, and upon #CP consult that to see if we should eat the trap or produce a warn/signal whatever.

Nice, I'm happy to revive/rebase/test any of this if there's interest.

Quoted 5 lines

I think we were near something workable there when Rick got pulled from this and put onto something more 'important' and things just haven't moved ever since.

Anyway, glad to see someone has time to poke at this.

Happy to spend whatever time is needed to land IBT. I'm very glad the first reaction wasn't "absolutely no way" :-)

Thank you,
-Richard

#15 lore raw

+ Florian

Quoted 8 lines · Richard Patel
Quoted 3 lines · Peter Zijlstra

Anyway, the most contentious part was the whole backwards compat bitmap crap. When the dynamic linker composes a process of parts that support IBT and parts that do not, you get to deal with fallout.

Is it acceptable to do all-or-nothing IBT first? And then do a second round of patches with legacy support?

Quoted 6 lines

Until then, ld.so could:
- start with IBT, disable it upon loading incompatible DSO
- allow users to manually lock IBT

I thought this weak form of IBT is better than nothing at all, if there's a risk that legacy support derails things.

That wholly depends on how much churn glibc people are willing to put up with. Added Florian to answer some of that.

Quoted 12 lines

Btw, apparently OpenBSD enforces kernel+user IBT. I think the end goal is a user_ibt=force command-line param that locks IBT for all processes on startup.

Quoted 5 lines

The IBT spec has this horrid bitmap thing to try and deal with this, and those early patches exposed that piece of shit to userspace. Then later patches (suggested by me) used the ARM64/BTI approach of using PROT_BTI. We'd use a (software) page-table bit, and upon #CP consult that to see if we should eat the trap or produce a warn/signal whatever.

Nice, I'm happy to revive/rebase/test any of this if there's interest.

Quoted 8 lines
Quoted 5 lines

I think we were near something workable there when Rick got pulled from this and put onto something more 'important' and things just haven't moved ever since.

Anyway, glad to see someone has time to poke at this.

Happy to spend whatever time is needed to land IBT. I'm very glad the first reaction wasn't "absolutely no way" :-)

Yeah, its just something that fell between the cracks :/

#16 lore raw
In reply to Richard Patel
Quoted 20 lines · On Sun, 17 May 2026 13:30:17 -0500 Richard Patel <[email protected]> wrote:

I was quite surprised that the Linux kernel still does not allow userspace to enable x86 IBT (indirect jmp/call integrity).

Compilers and linkers have been emitting 'endbr64' IBT markers and ELF support notes for a while now.

The hard work was done years ago by Intel:
https://lore.kernel.org/all/[email protected]/

In summary, usermode IBT requires 3 things:
1. Set the CET_ENDBR_EN bit in MSR_IA32_U_CET for each IBT-enabled thread (PATCH 2,5)
2. Back up the WAIT_FOR_ENDBR bit across signal handling (PATCH 3,4)
3. Provide a way for usermode to enable it (PATCH 5)

This builds on top of Yu Cheng's work, with some adaptations:
- FRED support
- Implemented the existing prctl(PR_CFI_*) API
- Removed ELF parsing (can be added later)

Quoted 6 lines

Unresolved questions:
- Is there a cleaner way to do the WAIT_FOR_ENDBR XSAVE fallback?
- What to do about 'notrack jmp *rax'?
I leave CET_NO_TRACK_EN enabled, which weakens IBT, by enabling a jump prefix that skips the ENDBR check. GCC emits it for jump tables (-mcet-switch). We could introduce a PR_CFI_IBT_STRICT bit.

Isn't using 'notrack jmp *reg' for jump tables actually more secure? If an attacker can write code it doesn't matter.
The jump table in is RO memory so can't be written.
But if there are ENDBR on all the jump table targets they become possibly useful code addresses to arrange to write into some RW function pointer table - which might be useful.

-- David

Quoted 2 lines

- There's some obvious overlap with arch_prctl(ARCH_SHSTK_*). Happy to use that API instead.

...

#17 lore raw
Quoted 30 lines · David Laight
Quoted 20 lines · On Sun, 17 May 2026 13:30:17 -0500 Richard Patel <[email protected]> wrote:

I was quite surprised that the Linux kernel still does not allow userspace to enable x86 IBT (indirect jmp/call integrity).

Compilers and linkers have been emitting 'endbr64' IBT markers and ELF support notes for a while now.

The hard work was done years ago by Intel:
https://lore.kernel.org/all/[email protected]/

In summary, usermode IBT requires 3 things:
1. Set the CET_ENDBR_EN bit in MSR_IA32_U_CET for each IBT-enabled thread (PATCH 2,5)
2. Back up the WAIT_FOR_ENDBR bit across signal handling (PATCH 3,4)
3. Provide a way for usermode to enable it (PATCH 5)

This builds on top of Yu Cheng's work, with some adaptations:
- FRED support
- Implemented the existing prctl(PR_CFI_*) API
- Removed ELF parsing (can be added later)

Quoted 6 lines

Unresolved questions:
- Is there a cleaner way to do the WAIT_FOR_ENDBR XSAVE fallback?
- What to do about 'notrack jmp *rax'?
I leave CET_NO_TRACK_EN enabled, which weakens IBT, by enabling a jump prefix that skips the ENDBR check. GCC emits it for jump tables (-mcet-switch). We could introduce a PR_CFI_IBT_STRICT bit.

Quoted 6 lines

Isn't using 'notrack jmp *reg' for jump tables actually more secure? If an attacker can write code it doesn't matter.
The jump table in is RO memory so can't be written.
But if there are ENDBR on all the jump table targets they become possibly useful code addresses to arrange to write into some RW function pointer table - which might be useful.

One of the many reasons the kernel is built without jump-tables.

#18 lore raw
In reply to David Laight
Quoted 6 lines · David Laight

Isn't using 'notrack jmp *reg' for jump tables actually more secure? If an attacker can write code it doesn't matter.
The jump table in is RO memory so can't be written.
But if there are ENDBR on all the jump table targets they become possibly useful code addresses to arrange to write into some RW function pointer table - which might be useful.

You're right. I was worried about an invalid jump table index at first. Clang 22 happily optimizes away jump table index bounds checks. GCC 16 seems to be more careful. We should probably patch LLVM to never optimize it away, e.g.:

	// funny.c
	// clang -c -fcf-protection=branch -O2 -o funny.o funny.c
	// objdump -d funny.o -M intel
	int t0(void), t1(void), t2(void), t3(void);
	int funny(unsigned long target) {
		__builtin_assume(target < 4);
		switch (target) {
		case 0: return t0();
		case 1: return t1();
		case 2: return t2();
		case 3: return t3();
		}
	}

	// Clang 22
	0000000000000000 <funny>:
	       0: f3 0f 1e fa                  endbr64
	       4: 55                           push rbp
	       5: 48 89 e5                     mov rbp, rsp
	       8: 3e ff 24 fd 00 00 00 00      notrack jmp qword ptr [rdi*8+0x0] // vulnerable
	      10: 5d                           pop rbp
	      11: e9 00 00 00 00               jmp 0x16 <funny+0x16>
	      16: 5d                           pop rbp
	      17: e9 00 00 00 00               jmp 0x1c <funny+0x1c>
	      1c: 5d                           pop rbp
	      1d: e9 00 00 00 00               jmp 0x22 <funny+0x22>
	      22: 5d                           pop rbp
	      23: e9 00 00 00 00               jmp 0x28 <funny+0x28>

-Richard

#19 lore raw
Quoted 13 lines · On Tue, 19 May 2026 13:14:33 +0000 Richard Patel <[email protected]> wrote:
Quoted 6 lines · David Laight

Isn't using 'notrack jmp *reg' for jump tables actually more secure? If an attacker can write code it doesn't matter.
The jump table in is RO memory so can't be written.
But if there are ENDBR on all the jump table targets they become possibly useful code addresses to arrange to write into some RW function pointer table - which might be useful.

You're right. I was worried about an invalid jump table index at first. Clang 22 happily optimizes away jump table index bounds checks. GCC 16 seems to be more careful. We should probably patch LLVM to never optimize it away, e.g.:

Quoted 6 lines
	// funny.c
	// clang -c -fcf-protection=branch -O2 -o funny.o funny.c
	// objdump -d funny.o -M intel
	int t0(void), t1(void), t2(void), t3(void);
	int funny(unsigned long target) {
		__builtin_assume(target < 4);

If you use __builtin_assume() you get to clear up the mess.

I don't know if userspace ever cares about speculative array access. If it does you need one of the mitigration - eg using cmp+cmov to generate a jump table index that references the 'default'.

-- David

Quoted 24 lines
		switch (target) {
		case 0: return t0();
		case 1: return t1();
		case 2: return t2();
		case 3: return t3();
		}
	}

	// Clang 22
	0000000000000000 <funny>:
	       0: f3 0f 1e fa                  endbr64
	       4: 55                           push rbp
	       5: 48 89 e5                     mov rbp, rsp
	       8: 3e ff 24 fd 00 00 00 00      notrack jmp qword ptr [rdi*8+0x0] // vulnerable
	      10: 5d                           pop rbp
	      11: e9 00 00 00 00               jmp 0x16 <funny+0x16>
	      16: 5d                           pop rbp
	      17: e9 00 00 00 00               jmp 0x1c <funny+0x1c>
	      1c: 5d                           pop rbp
	      1d: e9 00 00 00 00               jmp 0x22 <funny+0x22>
	      22: 5d                           pop rbp
	      23: e9 00 00 00 00               jmp 0x28 <funny+0x28>

-Richard

#20 lore raw
Quoted 16 lines · David Laight
Quoted 13 lines · On Tue, 19 May 2026 13:14:33 +0000 Richard Patel <[email protected]> wrote:
Quoted 6 lines · David Laight

Isn't using 'notrack jmp *reg' for jump tables actually more secure? If an attacker can write code it doesn't matter.
The jump table in is RO memory so can't be written.
But if there are ENDBR on all the jump table targets they become possibly useful code addresses to arrange to write into some RW function pointer table - which might be useful.

You're right. I was worried about an invalid jump table index at first. Clang 22 happily optimizes away jump table index bounds checks. GCC 16 seems to be more careful. We should probably patch LLVM to never optimize it away, e.g.:

Quoted 8 lines
Quoted 6 lines
	// funny.c
	// clang -c -fcf-protection=branch -O2 -o funny.o funny.c
	// objdump -d funny.o -M intel
	int t0(void), t1(void), t2(void), t3(void);
	int funny(unsigned long target) {
		__builtin_assume(target < 4);

If you use __builtin_assume() you get to clear up the mess.

I'm pretty sure you'd get the same result with cross-function optimization across a bunch of static functions or LTO. Compiler goes "oh, this internal function is only reachable from these 3 callers in the same unit, which all already bound their input params. Guess I will skip the bounds check".

It is a compiler bug that Clang is at all able to generate unbounded 'notrack jmp' with -fcf-protection=branch, it blows a gap in IBT.

Anyways, I don't think we need kernel support for banning notrack in userland? There is no ABI (GNU note) standard for 'notrack-free' binaries AFAIK, and as you point out notrack is a secure way to do jump tables (if done properly).

Quoted 3 lines

I don't know if userspace ever cares about speculative array access. If it does you need one of the mitigration - eg using cmp+cmov to generate a jump table index that references the 'default'.

Intel docs say that "CET-IBT limits speculative execution at indirect branch targets that do not start with ENDBRANCH", with heavy emphasis on "limits" not "prevents" ... Is it too unreliable in practice?

https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html#inpage-nav-4-3

-- Richard

#21 lore raw
Quoted 9 lines · Richard Patel
Quoted 3 lines

I don't know if userspace ever cares about speculative array access. If it does you need one of the mitigration - eg using cmp+cmov to generate a jump table index that references the 'default'.

Intel docs say that "CET-IBT limits speculative execution at indirect branch targets that do not start with ENDBRANCH", with heavy emphasis on "limits" not "prevents" ... Is it too unreliable in practice?

https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/branch-history-injection.html#inpage-nav-4-3

You might find the commit log of: d8122c428076 ("x86/ibt: Implement FineIBT-BHI mitigation") instructive.