All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sami Tolvanen <samitolvanen@google.com>
To: x86@kernel.org
Cc: Kees Cook <keescook@chromium.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Sedat Dilek <sedat.dilek@gmail.com>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	clang-built-linux@googlegroups.com,
	Sami Tolvanen <samitolvanen@google.com>
Subject: [PATCH 06/15] x86: Avoid CFI jump tables in IDT and entry points
Date: Fri, 16 Apr 2021 13:38:35 -0700	[thread overview]
Message-ID: <20210416203844.3803177-7-samitolvanen@google.com> (raw)
In-Reply-To: <20210416203844.3803177-1-samitolvanen@google.com>

With CONFIG_CFI_CLANG, the compiler replaces function addresses in C
code with jump table addresses. To avoid referring to jump tables in
entry code with PTI, disable CFI for IDT and paravirt code, and use
function_nocfi() to prevent jump table addresses from being added to
the IDT or system call entry points.

Reported-by: Sedat Dilek <sedat.dilek@gmail.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
---
 arch/x86/include/asm/desc.h  | 8 +++++++-
 arch/x86/kernel/Makefile     | 3 +++
 arch/x86/kernel/cpu/common.c | 8 ++++----
 arch/x86/kernel/idt.c        | 2 +-
 arch/x86/kernel/traps.c      | 2 +-
 arch/x86/xen/Makefile        | 2 ++
 6 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/desc.h b/arch/x86/include/asm/desc.h
index 476082a83d1c..96666256eec2 100644
--- a/arch/x86/include/asm/desc.h
+++ b/arch/x86/include/asm/desc.h
@@ -381,7 +381,13 @@ static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)
 	desc->limit1 = (limit >> 16) & 0xf;
 }
 
-void alloc_intr_gate(unsigned int n, const void *addr);
+/*
+ * Use function_nocfi() to prevent the compiler from replacing function
+ * addresses with jump table addresses when CONFIG_CFI_CLANG is enabled.
+ */
+#define alloc_intr_gate(n, addr) __alloc_intr_gate(n, function_nocfi(addr))
+
+void __alloc_intr_gate(unsigned int n, const void *addr);
 
 static inline void init_idt_data(struct idt_data *data, unsigned int n,
 				 const void *addr)
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 0704c2a94272..12a739eb208e 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -46,6 +46,9 @@ endif
 # non-deterministic coverage.
 KCOV_INSTRUMENT		:= n
 
+CFLAGS_REMOVE_idt.o		:= $(CC_FLAGS_CFI)
+CFLAGS_REMOVE_paravirt.o	:= $(CC_FLAGS_CFI)
+
 CFLAGS_head$(BITS).o	+= -fno-stack-protector
 
 CFLAGS_irq.o := -I $(srctree)/$(src)/../include/asm/trace
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 6bdb69a9a7dc..e6cff98b182a 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -1752,10 +1752,10 @@ DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK;
 void syscall_init(void)
 {
 	wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS);
-	wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);
+	wrmsrl(MSR_LSTAR, (unsigned long)function_nocfi(entry_SYSCALL_64));
 
 #ifdef CONFIG_IA32_EMULATION
-	wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat);
+	wrmsrl(MSR_CSTAR, (unsigned long)function_nocfi(entry_SYSCALL_compat));
 	/*
 	 * This only works on Intel CPUs.
 	 * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP.
@@ -1765,9 +1765,9 @@ void syscall_init(void)
 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
 		    (unsigned long)(cpu_entry_stack(smp_processor_id()) + 1));
-	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
+	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)function_nocfi(entry_SYSENTER_compat));
 #else
-	wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
+	wrmsrl(MSR_CSTAR, (unsigned long)function_nocfi(ignore_sysret));
 	wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG);
 	wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
 	wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL);
diff --git a/arch/x86/kernel/idt.c b/arch/x86/kernel/idt.c
index d552f177eca0..6574a742e373 100644
--- a/arch/x86/kernel/idt.c
+++ b/arch/x86/kernel/idt.c
@@ -340,7 +340,7 @@ void idt_invalidate(void *addr)
 	load_idt(&idt);
 }
 
-void __init alloc_intr_gate(unsigned int n, const void *addr)
+void __init __alloc_intr_gate(unsigned int n, const void *addr)
 {
 	if (WARN_ON(n < FIRST_SYSTEM_VECTOR))
 		return;
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 853ea7a80806..54616dc49116 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -403,7 +403,7 @@ DEFINE_IDTENTRY_DF(exc_double_fault)
 		 * which is what the stub expects, given that the faulting
 		 * RIP will be the IRET instruction.
 		 */
-		regs->ip = (unsigned long)asm_exc_general_protection;
+		regs->ip = (unsigned long)function_nocfi(asm_exc_general_protection);
 		regs->sp = (unsigned long)&gpregs->orig_ax;
 
 		return;
diff --git a/arch/x86/xen/Makefile b/arch/x86/xen/Makefile
index 40b5779fce21..61e2d9b2fa43 100644
--- a/arch/x86/xen/Makefile
+++ b/arch/x86/xen/Makefile
@@ -11,6 +11,8 @@ endif
 CFLAGS_enlighten_pv.o		:= -fno-stack-protector
 CFLAGS_mmu_pv.o			:= -fno-stack-protector
 
+CFLAGS_REMOVE_enlighten_pv.o	:= $(CC_FLAGS_CFI)
+
 obj-y				+= enlighten.o
 obj-y				+= mmu.o
 obj-y				+= time.o
-- 
2.31.1.368.gbe11c130af-goog


  parent reply	other threads:[~2021-04-16 20:39 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-16 20:38 [PATCH 00/15] x86: Add support for Clang CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 01/15] objtool: Find a destination for jumps beyond the section end Sami Tolvanen
2021-04-20 18:14   ` Josh Poimboeuf
2021-04-20 20:25     ` Sami Tolvanen
2021-04-20 22:55       ` Josh Poimboeuf
2021-04-20 22:58         ` Nick Desaulniers
2021-04-16 20:38 ` [PATCH 02/15] objtool: Add CONFIG_CFI_CLANG support Sami Tolvanen
2021-04-20 19:47   ` Josh Poimboeuf
2021-04-20 20:45     ` Sami Tolvanen
2021-04-16 20:38 ` [PATCH 03/15] objtool: Add ASM_STACK_FRAME_NON_STANDARD Sami Tolvanen
2021-04-16 20:38 ` [PATCH 04/15] static_call: Use global functions for the self-test Sami Tolvanen
2021-04-16 21:37   ` Thomas Gleixner
2021-04-17  0:16     ` Thomas Gleixner
2021-04-16 20:38 ` [PATCH 05/15] x86: Implement function_nocfi Sami Tolvanen
2021-04-16 21:18   ` Borislav Petkov
2021-04-16 21:49     ` Sami Tolvanen
2021-04-16 22:02       ` Borislav Petkov
2021-04-16 22:06         ` Andy Lutomirski
2021-04-16 22:14           ` Borislav Petkov
2021-04-16 22:20             ` Andy Lutomirski
2021-04-16 22:37               ` Kees Cook
2021-04-16 23:02                 ` Thomas Gleixner
2021-04-17 10:16                   ` Thomas Gleixner
2021-04-19 15:13                     ` Sami Tolvanen
2021-04-16 22:28           ` Kees Cook
2021-04-16 22:52             ` Andy Lutomirski
2021-04-16 22:58               ` Kees Cook
2021-04-16 23:40               ` Kees Cook
2021-04-17 23:19                 ` Andy Lutomirski
2021-04-17 23:53                   ` Thomas Gleixner
2021-04-18  0:11                     ` Andy Lutomirski
2021-04-18 16:17                       ` Thomas Gleixner
2021-04-18 22:57                         ` Andy Lutomirski
2021-04-19 15:20                           ` Sami Tolvanen
2021-04-19 15:26                       ` David Laight
2021-04-19 17:46                         ` Andy Lutomirski
2021-04-17 14:20             ` David Laight
2021-04-17 15:48               ` Andy Lutomirski
2021-04-19  8:40             ` Rasmus Villemoes
2021-04-19 16:45               ` Joao Moreira
2021-04-19 21:52               ` David Laight
2021-04-16 22:16         ` Kees Cook
2021-04-16 22:13       ` Thomas Gleixner
2021-04-16 20:38 ` Sami Tolvanen [this message]
2021-04-16 22:26   ` [PATCH 06/15] x86: Avoid CFI jump tables in IDT and entry points Thomas Gleixner
2021-04-16 23:56     ` Kees Cook
2021-04-17  0:02       ` Thomas Gleixner
2021-04-16 20:38 ` [PATCH 07/15] x86/ftrace: Use function_nocfi in MCOUNT_ADDR Sami Tolvanen
2021-04-16 20:38 ` [PATCH 08/15] x86/extable: Do not mark exception callback as CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 09/15] x86/alternatives: Use C int3 selftest but disable KASAN Sami Tolvanen
2021-04-17 11:37   ` Peter Zijlstra
2021-04-19 15:26     ` Sami Tolvanen
2021-04-20  7:19       ` Peter Zijlstra
2021-04-16 20:38 ` [PATCH 10/15] x86/purgatory: Disable CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 11/15] x86, relocs: Ignore __typeid__ relocations Sami Tolvanen
2021-04-16 20:38 ` [PATCH 12/15] x86, module: " Sami Tolvanen
2021-04-16 20:38 ` [PATCH 13/15] x86, cpu: Use LTO for cpu.c with CFI Sami Tolvanen
2021-04-16 20:38 ` [PATCH 14/15] x86, kprobes: Fix optprobe_template_func type mismatch Sami Tolvanen
2021-04-16 20:38 ` [PATCH 15/15] x86, build: Allow CONFIG_CFI_CLANG to be selected Sami Tolvanen

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=20210416203844.3803177-7-samitolvanen@google.com \
    --to=samitolvanen@google.com \
    --cc=clang-built-linux@googlegroups.com \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=sedat.dilek@gmail.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.