All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: x86@kernel.org, joao@overdrivepizza.com, hjl.tools@gmail.com,
	jpoimboe@redhat.com, andrew.cooper3@citrix.com
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	ndesaulniers@google.com, keescook@chromium.org,
	samitolvanen@google.com, mark.rutland@arm.com,
	alyssa.milburn@intel.com
Subject: [PATCH 29/29] x86/alternative: Use .ibt_endbr_sites to seal indirect calls
Date: Fri, 18 Feb 2022 17:49:31 +0100	[thread overview]
Message-ID: <20220218171410.298372848@infradead.org> (raw)
In-Reply-To: 20220218164902.008644515@infradead.org

Objtool's --ibt-seal option generates .ibt_endbr_sites which lists
superfluous ENDBR instructions. That is those instructions for which
the function is never indirectly called.

Additionally, objtool's --ibt-fix-direct ensures direct calls never
target an ENDBR instruction.

Combined this yields that these instructions should never be executed.

Poison them using a 4 byte UD1 instruction; for IBT hardware this will
raise an #CP exception due to WAIT-FOR-ENDBR not getting what it
wants. For !IBT hardware it'll trigger #UD.

In either case, it will be 'impossible' to indirectly call these
functions thereafter.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 Makefile                           |    5 ++++
 arch/um/kernel/um_arch.c           |    4 +++
 arch/x86/Kconfig                   |   12 +++++++++
 arch/x86/include/asm/alternative.h |    1 
 arch/x86/include/asm/ibt.h         |    4 ++-
 arch/x86/kernel/alternative.c      |   46 +++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/module.c           |   10 ++++++--
 arch/x86/kernel/traps.c            |   35 ++++++++++++++++++++++++++--
 scripts/Makefile.build             |    3 +-
 scripts/link-vmlinux.sh            |   10 ++++++--
 10 files changed, 122 insertions(+), 8 deletions(-)

--- a/Makefile
+++ b/Makefile
@@ -911,6 +911,11 @@ BUILD_LTO	:= y
 export BUILD_LTO
 endif
 
+ifdef CONFIG_X86_IBT_SEAL
+BUILD_LTO	:= y
+export BUILD_LTO
+endif
+
 ifdef CONFIG_CFI_CLANG
 CC_FLAGS_CFI	:= -fsanitize=cfi \
 		   -fsanitize-cfi-cross-dso \
--- a/arch/um/kernel/um_arch.c
+++ b/arch/um/kernel/um_arch.c
@@ -424,6 +424,10 @@ void __init check_bugs(void)
 	os_check_bugs();
 }
 
+void apply_ibt_endbr(s32 *start, s32 *end)
+{
+}
+
 void apply_retpolines(s32 *start, s32 *end)
 {
 }
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1876,6 +1876,18 @@ config X86_IBT
 	  an ENDBR instruction, as such, the compiler will litter the
 	  code with them to make this happen.
 
+config X86_IBT_SEAL
+	prompt "Seal functions"
+	def_bool y
+	depends on X86_IBT && STACK_VALIDATION
+	help
+	  In addition to building the kernel with IBT, seal all functions that
+	  are not indirect call targets, avoiding them ever becomming one.
+
+	  This requires LTO like objtool runs and will slow down the build. It
+	  does significantly reduce the number of ENDBR instructions in the
+	  kernel image as well as provide some validation for !IBT hardware.
+
 config X86_INTEL_MEMORY_PROTECTION_KEYS
 	prompt "Memory Protection Keys"
 	def_bool y
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -76,6 +76,7 @@ extern int alternatives_patched;
 extern void alternative_instructions(void);
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
 extern void apply_retpolines(s32 *start, s32 *end);
+extern void apply_ibt_endbr(s32 *start, s32 *end);
 
 struct module;
 
--- a/arch/x86/include/asm/ibt.h
+++ b/arch/x86/include/asm/ibt.h
@@ -23,8 +23,10 @@
 static inline bool is_endbr(const void *addr)
 {
 	unsigned int val = ~*(unsigned int *)addr;
+	if (val == ~0x0040b90f) /* ud1_endbr */
+		return true;
 	val |= 0x01000000U;
-	return val == ~0xfa1e0ff3;
+	return val == ~0xfa1e0ff3; /* endbr */
 }
 
 extern u64 ibt_save(void);
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -115,6 +115,7 @@ static void __init_or_module add_nops(vo
 }
 
 extern s32 __retpoline_sites[], __retpoline_sites_end[];
+extern s32 __ibt_endbr_sites[], __ibt_endbr_sites_end[];
 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
 extern s32 __smp_locks[], __smp_locks_end[];
 void text_poke_early(void *addr, const void *opcode, size_t len);
@@ -512,6 +513,49 @@ void __init_or_module noinline apply_ret
 
 #endif /* CONFIG_RETPOLINE && CONFIG_STACK_VALIDATION */
 
+#ifdef CONFIG_X86_IBT_SEAL
+
+/*
+ * ud1    0x0(%rax),%eax -- a 4 byte #UD instruction for when we don't have
+ *                          IBT and still want to trigger fail.
+ */
+static const u8 ud1_endbr[4] = { 0x0f, 0xb9, 0x40, 0x00 };
+
+/*
+ * Generated by: objtool --ibt-seal
+ */
+void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end)
+{
+	s32 *s;
+
+	for (s = start; s < end; s++) {
+		void *addr = (void *)s + *s;
+		u32 endbr;
+
+		if (WARN_ON_ONCE(get_kernel_nofault(endbr, addr)))
+			continue;
+
+		if (WARN_ON_ONCE(!is_endbr(&endbr)))
+			continue;
+
+		DPRINTK("ENDBR at: %pS (%px)", addr, addr);
+
+		/*
+		 * When we have IBT, the lack of ENDBR will trigger #CP
+		 * When we don't have IBT, explicitly trigger #UD
+		 */
+		DUMP_BYTES(((u8*)addr), 4, "%px: orig: ", addr);
+		DUMP_BYTES(((u8*)ud1_endbr), 4, "%px: repl: ", addr);
+		text_poke_early(addr, ud1_endbr, 4);
+	}
+}
+
+#else
+
+void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end) { }
+
+#endif /* CONFIG_X86_IBT_SEAL */
+
 #ifdef CONFIG_SMP
 static void alternatives_smp_lock(const s32 *start, const s32 *end,
 				  u8 *text, u8 *text_end)
@@ -832,6 +876,8 @@ void __init alternative_instructions(voi
 	 */
 	apply_alternatives(__alt_instructions, __alt_instructions_end);
 
+	apply_ibt_endbr(__ibt_endbr_sites, __ibt_endbr_sites_end);
+
 #ifdef CONFIG_SMP
 	/* Patch to UP if other cpus not imminent. */
 	if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -132,7 +132,7 @@ int apply_relocate(Elf32_Shdr *sechdrs,
 
 static inline void ibt_fix_direct(void *loc, u64 *val)
 {
-#ifdef CONFIG_X86_IBT
+#ifdef CONFIG_X86_IBT_SEAL
 	const void *addr = (void *)(4 + *val);
 	union text_poke_insn text;
 	u32 insn;
@@ -287,7 +287,7 @@ int module_finalize(const Elf_Ehdr *hdr,
 {
 	const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
 		*para = NULL, *orc = NULL, *orc_ip = NULL,
-		*retpolines = NULL;
+		*retpolines = NULL, *ibt_endbr = NULL;
 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
 
 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
@@ -305,6 +305,8 @@ int module_finalize(const Elf_Ehdr *hdr,
 			orc_ip = s;
 		if (!strcmp(".retpoline_sites", secstrings + s->sh_name))
 			retpolines = s;
+		if (!strcmp(".ibt_endbr_sites", secstrings + s->sh_name))
+			ibt_endbr = s;
 	}
 
 	if (para) {
@@ -320,6 +322,10 @@ int module_finalize(const Elf_Ehdr *hdr,
 		void *aseg = (void *)alt->sh_addr;
 		apply_alternatives(aseg, aseg + alt->sh_size);
 	}
+	if (ibt_endbr) {
+		void *iseg = (void *)ibt_endbr->sh_addr;
+		apply_ibt_endbr(iseg, iseg + ibt_endbr->sh_size);
+	}
 	if (locks && text) {
 		void *lseg = (void *)locks->sh_addr;
 		void *tseg = (void *)text->sh_addr;
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -214,6 +214,12 @@ DEFINE_IDTENTRY(exc_overflow)
 
 static bool ibt_fatal = true;
 
+static void handle_endbr(struct pt_regs *regs)
+{
+	pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
+	BUG_ON(ibt_fatal);
+}
+
 extern unsigned long ibt_selftest_ip; /* defined in asm beow */
 static volatile bool ibt_selftest_ok = false;
 
@@ -232,8 +238,7 @@ DEFINE_IDTENTRY_ERRORCODE(exc_control_pr
 		return;
 	}
 
-	pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
-	BUG_ON(ibt_fatal);
+	handle_endbr(regs);
 }
 
 bool ibt_selftest(void)
@@ -277,6 +282,29 @@ static int __init ibt_setup(char *str)
 
 __setup("ibt=", ibt_setup);
 
+static bool handle_ud1_endbr(struct pt_regs *regs)
+{
+	u32 ud1;
+
+	if (get_kernel_nofault(ud1, (u32 *)regs->ip))
+		return false;
+
+	if (ud1 == 0x0040b90f) {
+		handle_endbr(regs);
+		regs->ip += 4;
+		return true;
+	}
+
+	return false;
+}
+
+#else /* CONFIG_X86_IBT */
+
+static bool handle_ud1_endbr(struct pt_regs *regs)
+{
+	return false;
+}
+
 #endif /* CONFIG_X86_IBT */
 
 #ifdef CONFIG_X86_F00F_BUG
@@ -285,6 +313,9 @@ void handle_invalid_op(struct pt_regs *r
 static inline void handle_invalid_op(struct pt_regs *regs)
 #endif
 {
+	if (!user_mode(regs) && handle_ud1_endbr(regs))
+		return;
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -231,6 +231,7 @@ objtool_args =								\
 	$(if $(CONFIG_UNWINDER_ORC),orc generate,check)			\
 	$(if $(part-of-module), --module)				\
 	$(if $(BUILD_LTO), --lto)					\
+	$(if $(CONFIG_X86_IBT_SEAL), --ibt --ibt-fix-direct --ibt-seal)	\
 	$(if $(CONFIG_FRAME_POINTER),, --no-fp)				\
 	$(if $(CONFIG_GCOV_KERNEL)$(CONFIG_LTO_CLANG), --no-unreachable)\
 	$(if $(CONFIG_RETPOLINE), --retpoline)				\
@@ -305,7 +306,7 @@ quiet_cmd_cc_lto_link_modules = LTO [M]
 		--whole-archive $(filter-out FORCE,$^)			\
 		$(cmd_objtool)
 else
-quiet_cmd_cc_lto_link_modules = LD [M] $@
+quiet_cmd_cc_lto_link_modules = LD [M]  $@
       cmd_cc_lto_link_modules =						\
 	$(LD) $(ld_flags) -r -o $@					\
 		$(filter-out FORCE,$^)					\
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -108,7 +108,9 @@ objtool_link()
 	local objtoolcmd;
 	local objtoolopt;
 
-	if is_enabled CONFIG_LTO_CLANG && is_enabled CONFIG_STACK_VALIDATION; then
+	if is_enabled CONFIG_STACK_VALIDATION && \
+	   ( is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_IBT_SEAL ); then
+
 		# Don't perform vmlinux validation unless explicitly requested,
 		# but run objtool on vmlinux.o now that we have an object file.
 		if is_enabled CONFIG_UNWINDER_ORC; then
@@ -117,6 +119,10 @@ objtool_link()
 
 		objtoolopt="${objtoolopt} --lto"
 
+		if is_enabled CONFIG_X86_IBT_SEAL; then
+			objtoolopt="${objtoolopt} --ibt --ibt-fix-direct --ibt-seal"
+		fi
+
 		if is_enabled CONFIG_FTRACE_MCOUNT_USE_OBJTOOL; then
 			objtoolopt="${objtoolopt} --mcount"
 		fi
@@ -168,7 +174,7 @@ vmlinux_link()
 	# skip output file argument
 	shift
 
-	if is_enabled CONFIG_LTO_CLANG; then
+	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_IBT_SEAL; then
 		# Use vmlinux.o instead of performing the slow LTO link again.
 		objs=vmlinux.o
 		libs=



  parent reply	other threads:[~2022-02-18 17:15 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-18 16:49 [PATCH 00/29] x86: Kernel IBT Peter Zijlstra
2022-02-18 16:49 ` [PATCH 01/29] static_call: Avoid building empty .static_call_sites Peter Zijlstra
2022-02-18 16:49 ` [PATCH 02/29] x86/module: Fix the paravirt vs alternative order Peter Zijlstra
2022-02-18 20:28   ` Josh Poimboeuf
2022-02-18 21:22     ` Peter Zijlstra
2022-02-18 23:28       ` Josh Poimboeuf
2022-02-18 16:49 ` [PATCH 03/29] objtool: Add --dry-run Peter Zijlstra
2022-02-18 16:49 ` [PATCH 04/29] x86/livepatch: Validate __fentry__ location Peter Zijlstra
2022-02-18 21:08   ` Josh Poimboeuf
2022-02-23 10:09     ` Peter Zijlstra
2022-02-23 10:21       ` Miroslav Benes
2022-02-23 10:57       ` Peter Zijlstra
2022-02-23 12:41         ` Steven Rostedt
2022-02-23 14:05           ` Peter Zijlstra
2022-02-23 14:16             ` Steven Rostedt
2022-02-23 14:23           ` Steven Rostedt
2022-02-23 14:33             ` Steven Rostedt
2022-02-23 14:49             ` Peter Zijlstra
2022-02-23 15:54               ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 05/29] x86: Base IBT bits Peter Zijlstra
2022-02-18 20:49   ` Andrew Cooper
2022-02-18 21:11     ` David Laight
2022-02-18 21:24       ` Andrew Cooper
2022-02-18 22:37         ` David Laight
2022-02-18 21:26     ` Peter Zijlstra
2022-02-18 21:14   ` Josh Poimboeuf
2022-02-18 21:21     ` Peter Zijlstra
2022-02-18 22:12   ` Joao Moreira
2022-02-19  1:07   ` Edgecombe, Rick P
2022-02-18 16:49 ` [PATCH 06/29] x86/ibt: Add ANNOTATE_NOENDBR Peter Zijlstra
2022-02-18 16:49 ` [PATCH 07/29] x86/entry: Sprinkle ENDBR dust Peter Zijlstra
2022-02-19  0:23   ` Josh Poimboeuf
2022-02-19 23:08     ` Peter Zijlstra
2022-02-19  0:36   ` Josh Poimboeuf
2022-02-18 16:49 ` [PATCH 08/29] x86/linkage: Add ENDBR to SYM_FUNC_START*() Peter Zijlstra
2022-02-18 16:49 ` [PATCH 09/29] x86/ibt,paravirt: Sprinkle ENDBR Peter Zijlstra
2022-02-18 16:49 ` [PATCH 10/29] x86/bpf: Add ENDBR instructions to prologue Peter Zijlstra
2022-02-18 16:49 ` [PATCH 11/29] x86/ibt,crypto: Add ENDBR for the jump-table entries Peter Zijlstra
2022-02-18 16:49 ` [PATCH 12/29] x86/ibt,kvm: Add ENDBR to fastops Peter Zijlstra
2022-02-18 16:49 ` [PATCH 13/29] x86/ibt,ftrace: Add ENDBR to samples/ftrace Peter Zijlstra
2022-02-18 16:49 ` [PATCH 14/29] x86/ibt: Add IBT feature, MSR and #CP handling Peter Zijlstra
2022-02-18 19:31   ` Andrew Cooper
2022-02-18 21:15     ` Peter Zijlstra
2022-02-19  1:20   ` Edgecombe, Rick P
2022-02-19  1:21   ` Josh Poimboeuf
2022-02-19  9:24     ` Peter Zijlstra
2022-02-21  8:24   ` Kees Cook
2022-02-22  4:38   ` Edgecombe, Rick P
2022-02-22  9:32     ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 15/29] x86: Disable IBT around firmware Peter Zijlstra
2022-02-21  8:27   ` Kees Cook
2022-02-21 10:06     ` Peter Zijlstra
2022-02-21 13:22       ` Peter Zijlstra
2022-02-21 15:54       ` Kees Cook
2022-02-21 16:10         ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 16/29] x86/bugs: Disable Retpoline when IBT Peter Zijlstra
2022-02-19  2:15   ` Josh Poimboeuf
2022-02-22 15:00     ` Peter Zijlstra
2022-02-25  0:19       ` Josh Poimboeuf
2022-02-18 16:49 ` [PATCH 17/29] x86/ibt: Annotate text references Peter Zijlstra
2022-02-19  5:22   ` Josh Poimboeuf
2022-02-19  9:39     ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 18/29] x86/ibt,ftrace: Annotate ftrace code patching Peter Zijlstra
2022-02-18 16:49 ` [PATCH 19/29] x86/ibt,xen: Annotate away warnings Peter Zijlstra
2022-02-18 20:24   ` Andrew Cooper
2022-02-18 21:05     ` Peter Zijlstra
2022-02-18 23:07       ` Andrew Cooper
2022-02-21 14:20         ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 20/29] x86/ibt,sev: Annotations Peter Zijlstra
2022-02-18 16:49 ` [PATCH 21/29] objtool: Rename --duplicate to --lto Peter Zijlstra
2022-02-26 19:42   ` Josh Poimboeuf
2022-02-26 21:48     ` Josh Poimboeuf
2022-02-28 11:05       ` Peter Zijlstra
2022-02-28 18:32         ` Josh Poimboeuf
2022-02-28 20:09           ` Peter Zijlstra
2022-02-28 20:18             ` Josh Poimboeuf
2022-03-01 14:19               ` Miroslav Benes
2022-02-18 16:49 ` [PATCH 22/29] Kbuild: Prepare !CLANG whole module objtool Peter Zijlstra
2022-02-18 16:49 ` [PATCH 23/29] objtool: Read the NOENDBR annotation Peter Zijlstra
2022-02-18 16:49 ` [PATCH 24/29] x86/text-patching: Make text_gen_insn() IBT aware Peter Zijlstra
2022-02-24  1:18   ` Joao Moreira
2022-02-24  9:10     ` Peter Zijlstra
2022-02-18 16:49 ` [PATCH 25/29] x86/ibt: Dont generate ENDBR in .discard.text Peter Zijlstra
2022-02-18 16:49 ` [PATCH 26/29] objtool: Add IBT validation / fixups Peter Zijlstra
2022-02-18 16:49 ` [PATCH 27/29] x86/ibt: Finish --ibt-fix-direct on module loading Peter Zijlstra
2022-02-18 16:49 ` [PATCH 28/29] x86/ibt: Ensure module init/exit points have references Peter Zijlstra
2022-02-18 16:49 ` Peter Zijlstra [this message]
2022-02-19  1:29 ` [PATCH 00/29] x86: Kernel IBT Edgecombe, Rick P
2022-02-19  9:58   ` Peter Zijlstra
2022-02-19 16:00     ` Andrew Cooper
2022-02-21  8:42     ` Kees Cook
2022-02-21  9:24       ` Peter Zijlstra
2022-02-23  7:26   ` Kees Cook
2022-02-24 16:47     ` Mike Rapoport

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220218171410.298372848@infradead.org \
    --to=peterz@infradead.org \
    --cc=alyssa.milburn@intel.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=hjl.tools@gmail.com \
    --cc=joao@overdrivepizza.com \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=ndesaulniers@google.com \
    --cc=samitolvanen@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.