linux-hardening.vger.kernel.org archive mirror
 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,
	llvm@lists.linux.dev, Sami Tolvanen <samitolvanen@google.com>
Subject: [PATCH v4 04/15] cfi: Add DEFINE_CFI_IMMEDIATE_RETURN_STUB
Date: Thu, 30 Sep 2021 11:05:20 -0700	[thread overview]
Message-ID: <20210930180531.1190642-5-samitolvanen@google.com> (raw)
In-Reply-To: <20210930180531.1190642-1-samitolvanen@google.com>

This change introduces the DEFINE_CFI_IMMEDIATE_RETURN_STUB macro,
which defines a stub function that immediately returns and when
defined in the core kernel, always passes indirect call checking
with CONFIG_CFI_CLANG. Note that this macro should only be used when
a stub cannot be called using the correct function type.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
 include/asm-generic/vmlinux.lds.h | 11 +++++++++++
 include/linux/cfi.h               | 13 +++++++++++++
 kernel/cfi.c                      | 24 +++++++++++++++++++++++-
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index f2984af2b85b..5b77284f7221 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -407,6 +407,16 @@
 	KEEP(*(.static_call_tramp_key))					\
 	__stop_static_call_tramp_key = .;
 
+#ifdef CONFIG_CFI_CLANG
+#define CFI_EXCLUDED_DATA						\
+	. = ALIGN(8);							\
+	__start_cfi_excluded = .;					\
+	KEEP(*(.cfi_excluded_stubs))					\
+	__stop_cfi_excluded = .;
+#else
+#define CFI_EXCLUDED_DATA
+#endif
+
 /*
  * Allow architectures to handle ro_after_init data on their
  * own by defining an empty RO_AFTER_INIT_DATA.
@@ -430,6 +440,7 @@
 		__start_rodata = .;					\
 		*(.rodata) *(.rodata.*)					\
 		SCHED_DATA						\
+		CFI_EXCLUDED_DATA					\
 		RO_AFTER_INIT_DATA	/* Read only after init */	\
 		. = ALIGN(8);						\
 		__start___tracepoints_ptrs = .;				\
diff --git a/include/linux/cfi.h b/include/linux/cfi.h
index 879744aaa6e0..19f74af8eac2 100644
--- a/include/linux/cfi.h
+++ b/include/linux/cfi.h
@@ -20,6 +20,17 @@ extern void __cfi_check(uint64_t id, void *ptr, void *diag);
 #define __CFI_ADDRESSABLE(fn, __attr) \
 	const void *__cfi_jt_ ## fn __visible __attr = (void *)&fn
 
+/*
+ * Defines a stub function that returns immediately, and when defined and
+ * referenced in the core kernel, always passes CFI checking. This should
+ * be used only for stubs that cannot be called using the correct function
+ * pointer type, which should be rare.
+ */
+#define DEFINE_CFI_IMMEDIATE_RETURN_STUB(fn) \
+	void fn(void) { return; } \
+	const void *__cfi_excl_ ## fn __visible \
+		__section(".cfi_excluded_stubs") = (void *)&fn
+
 #ifdef CONFIG_CFI_CLANG_SHADOW
 
 extern void cfi_module_add(struct module *mod, unsigned long base_addr);
@@ -35,6 +46,8 @@ static inline void cfi_module_remove(struct module *mod, unsigned long base_addr
 #else /* !CONFIG_CFI_CLANG */
 
 #define __CFI_ADDRESSABLE(fn, __attr)
+#define DEFINE_CFI_IMMEDIATE_RETURN_STUB(fn) \
+	void fn(void) { return; }
 
 #endif /* CONFIG_CFI_CLANG */
 
diff --git a/kernel/cfi.c b/kernel/cfi.c
index 9594cfd1cf2c..8d931089141b 100644
--- a/kernel/cfi.c
+++ b/kernel/cfi.c
@@ -278,12 +278,34 @@ static inline cfi_check_fn find_module_check_fn(unsigned long ptr)
 	return fn;
 }
 
+extern unsigned long __start_cfi_excluded[];
+extern unsigned long __stop_cfi_excluded[];
+
+static inline bool is_cfi_excluded(unsigned long ptr)
+{
+	unsigned long *p = __start_cfi_excluded;
+
+	for ( ; p < __stop_cfi_excluded; ++p)
+		if (*p == ptr)
+			return true;
+
+	return false;
+}
+
+static void __cfi_pass(uint64_t id, void *ptr, void *diag)
+{
+}
+
 static inline cfi_check_fn find_check_fn(unsigned long ptr)
 {
 	cfi_check_fn fn = NULL;
 
-	if (is_kernel_text(ptr))
+	if (is_kernel_text(ptr)) {
+		if (unlikely(is_cfi_excluded(ptr)))
+			return __cfi_pass;
+
 		return __cfi_check;
+	}
 
 	/*
 	 * Indirect call checks can happen when RCU is not watching. Both
-- 
2.33.0.800.g4c38ced690-goog


  parent reply	other threads:[~2021-09-30 18:05 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30 18:05 [PATCH v4 00/15] x86: Add support for Clang CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 01/15] objtool: Add CONFIG_CFI_CLANG support Sami Tolvanen
2021-09-30 18:40   ` Nick Desaulniers
2021-10-06  3:36   ` Josh Poimboeuf
2021-10-06 16:18     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 02/15] objtool: Add ASM_STACK_FRAME_NON_STANDARD Sami Tolvanen
2021-10-06  3:37   ` Josh Poimboeuf
2021-09-30 18:05 ` [PATCH v4 03/15] linkage: Add DECLARE_ASM_FUNC_SYMBOL Sami Tolvanen
2021-09-30 18:05 ` Sami Tolvanen [this message]
2021-09-30 18:50   ` [PATCH v4 04/15] cfi: Add DEFINE_CFI_IMMEDIATE_RETURN_STUB Nick Desaulniers
2021-10-01 20:07     ` Sami Tolvanen
2021-10-04 13:50   ` Peter Zijlstra
2021-10-04 19:10     ` Sami Tolvanen
2021-10-05  6:59       ` Peter Zijlstra
2021-10-05 20:29         ` Sami Tolvanen
2021-10-05 20:56           ` Peter Zijlstra
2021-10-05 21:53             ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 05/15] tracepoint: Exclude tp_stub_func from CFI checking Sami Tolvanen
2021-09-30 18:50   ` Nick Desaulniers
2021-10-01 20:08     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 06/15] ftrace: Use an opaque type for functions not callable from C Sami Tolvanen
2021-10-06  3:29   ` Josh Poimboeuf
2021-10-06 13:02     ` Steven Rostedt
2021-10-06 13:54       ` Josh Poimboeuf
2021-10-06 14:16         ` Steven Rostedt
2021-10-06 16:31       ` Sami Tolvanen
2021-10-06 16:58         ` Steven Rostedt
2021-10-06 17:45           ` Sami Tolvanen
2021-10-06 20:43             ` Josh Poimboeuf
2021-10-06 21:10               ` Steven Rostedt
2021-10-06 21:23                 ` Josh Poimboeuf
2021-10-06 23:14                   ` Sami Tolvanen
2021-10-07  0:56                     ` Steven Rostedt
2021-09-30 18:05 ` [PATCH v4 07/15] lkdtm: Disable UNSET_SMEP with CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 08/15] lkdtm: Use an opaque type for lkdtm_rodata_do_nothing Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 09/15] x86: Use an opaque type for functions not callable from C Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 10/15] x86/purgatory: Disable CFI Sami Tolvanen
2021-09-30 19:05   ` Nick Desaulniers
2021-09-30 18:05 ` [PATCH v4 11/15] x86, relocs: Ignore __typeid__ relocations Sami Tolvanen
2021-10-06  3:31   ` Josh Poimboeuf
2021-10-06 16:17     ` Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 12/15] x86, module: " Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 13/15] x86, cpu: Use LTO for cpu.c with CFI Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 14/15] x86, kprobes: Fix optprobe_template_func type mismatch Sami Tolvanen
2021-09-30 18:05 ` [PATCH v4 15/15] x86, build: Allow CONFIG_CFI_CLANG to be selected Sami Tolvanen
2021-09-30 18:38 ` [PATCH v4 00/15] x86: Add support for Clang CFI Nick Desaulniers
2021-10-05 20:36 ` Josh Poimboeuf
2021-10-05 21:52   ` Sami Tolvanen
2021-10-06  2:42     ` Josh Poimboeuf

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=20210930180531.1190642-5-samitolvanen@google.com \
    --to=samitolvanen@google.com \
    --cc=jpoimboe@redhat.com \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).