b4-sent.feeds.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
To: mricon@kernel.org
Subject: [PATCH v5 06/21] cfi: Add type helper macros
Date: Thu, 01 Sep 2022 15:28:51 -0400	[thread overview]
Message-ID: <20220901-kcfi_support-v5-6-be2007d8da63@linuxfoundation.org> (raw)
In-Reply-To: <20220901-kcfi_support-v5-0-be2007d8da63@linuxfoundation.org>

From: Sami Tolvanen <samitolvanen@google.com>

With CONFIG_CFI_CLANG, assembly functions called indirectly
from C code must be annotated with type identifiers to pass CFI
checking.  In order to make this easier, the compiler emits a
__kcfi_typeid_<function> symbol for each address-taken function
declaration in C, which contains the expected type identifier that
we can refer to in assembly code.

Add typed versions of SYM_FUNC_START and SYM_FUNC_START_ALIAS, which
emit the type identifier before the function. Architectures that
support KCFI can define their own __CFI_TYPE macro to override the
default preamble format.

As an example, for the x86_64 blowfish_dec_blk function, the
compiler emits the following type symbol:

$ readelf -sW vmlinux | grep __kcfi_typeid_blowfish_dec_blk
121794: ffffffffef478db5     0 NOTYPE  WEAK   DEFAULT   ABS
	__kcfi_typeid_blowfish_dec_blk

And SYM_FUNC_START will generate the following preamble based on
the __CFI_TYPE definition for the architecture:

$ objdump -dr arch/x86/crypto/blowfish-x86_64-asm_64.o
     ...
00000000000003f7 <__cfi_blowfish_dec_blk>:
     3f7:       cc                      int3
     3f8:       cc                      int3
     3f9:       8b 04 25 00 00 00 00    mov    0x0,%eax
                        3fc: R_X86_64_32S __kcfi_typeid_blowfish_dec_blk
     400:       cc                      int3
     401:       cc                      int3

0000000000000402 <blowfish_dec_blk>:
     ...

Note that the address of all assembly functions annotated with
SYM_FUNC_START* must be taken in C code that's linked into the
binary or the missing __kcfi_typeid_ symbol will result in a linker
error with CONFIG_CFI_CLANG. If the code that contains the indirect
call is not always compiled in, __ADDRESSABLE(functionname) can be
used to ensure that the __kcfi_typeid_ symbol is emitted.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Tested-by: Kees Cook <keescook@chromium.org>

diff --git a/include/linux/cfi_types.h b/include/linux/cfi_types.h
new file mode 100644
index 000000000000..dd16e755a197
--- /dev/null
+++ b/include/linux/cfi_types.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Clang Control Flow Integrity (CFI) type definitions.
+ */
+#ifndef _LINUX_CFI_TYPES_H
+#define _LINUX_CFI_TYPES_H
+
+#ifdef CONFIG_CFI_CLANG
+#include <linux/linkage.h>
+
+#ifdef __ASSEMBLY__
+/*
+ * Use the __kcfi_typeid_<function> type identifier symbol to
+ * annotate indirectly called assembly functions. The compiler emits
+ * these symbols for all address-taken function declarations in C
+ * code.
+ */
+#ifndef __CFI_TYPE
+#define __CFI_TYPE(name)				\
+	.4byte __kcfi_typeid_##name
+#endif
+
+#define SYM_TYPED_ENTRY(name, fname, linkage, align...)	\
+	linkage(name) ASM_NL				\
+	align ASM_NL					\
+	__CFI_TYPE(fname) ASM_NL			\
+	name:
+
+#define __SYM_TYPED_FUNC_START_ALIAS(name, fname) \
+	SYM_TYPED_ENTRY(name, fname, SYM_L_GLOBAL, SYM_A_ALIGN)
+
+#define __SYM_TYPED_FUNC_START(name, fname) \
+	SYM_TYPED_ENTRY(name, fname, SYM_L_GLOBAL, SYM_A_ALIGN)
+
+#endif /* __ASSEMBLY__ */
+
+#else /* CONFIG_CFI_CLANG */
+
+#ifdef __ASSEMBLY__
+#define __SYM_TYPED_FUNC_START_ALIAS(name, fname) \
+	SYM_FUNC_START_ALIAS(name)
+
+#define __SYM_TYPED_FUNC_START(name, fname) \
+	SYM_FUNC_START(name)
+#endif /* __ASSEMBLY__ */
+
+#endif /* CONFIG_CFI_CLANG */
+
+#ifdef __ASSEMBLY__
+#define SYM_TYPED_FUNC_START_ALIAS(name) \
+	__SYM_TYPED_FUNC_START_ALIAS(name, name)
+
+#define SYM_TYPED_FUNC_START(name) \
+	__SYM_TYPED_FUNC_START(name, name)
+#endif /* __ASSEMBLY__ */
+
+#endif /* _LINUX_CFI_TYPES_H */

-- 
b4 0.10.0-dev-03aea

  parent reply	other threads:[~2022-09-01 19:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 19:28 [PATCH v5 00/21] KCFI support Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 01/21] treewide: Filter out CC_FLAGS_CFI Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 02/21] scripts/kallsyms: Ignore __kcfi_typeid_ Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 03/21] cfi: Remove CONFIG_CFI_CLANG_SHADOW Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 04/21] cfi: Drop __CFI_ADDRESSABLE Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 05/21] cfi: Switch to -fsanitize=kcfi Konstantin Ryabitsev
2022-09-01 19:28 ` Konstantin Ryabitsev [this message]
2022-09-01 19:28 ` [PATCH v5 07/21] lkdtm: Emit an indirect call for CFI tests Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 08/21] psci: Fix the function type for psci_initcall_t Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 09/21] arm64: Add types to indirect called assembly functions Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 10/21] arm64: Add CFI error handling Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 11/21] arm64: Drop unneeded __nocfi attributes Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 12/21] init: Drop __nocfi from __init Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 13/21] treewide: Drop function_nocfi Konstantin Ryabitsev
2022-09-01 19:28 ` [PATCH v5 14/21] treewide: Drop WARN_ON_FUNCTION_MISMATCH Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 15/21] treewide: Drop __cficanonical Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 16/21] objtool: Disable CFI warnings Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 17/21] kallsyms: Drop CONFIG_CFI_CLANG workarounds Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 18/21] x86/tools/relocs: Ignore __kcfi_typeid_ relocations Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 19/21] x86: Add types to indirectly called assembly functions Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 20/21] x86/purgatory: Disable CFI Konstantin Ryabitsev
2022-09-01 19:29 ` [PATCH v5 21/21] x86: Add support for CONFIG_CFI_CLANG Konstantin Ryabitsev

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=20220901-kcfi_support-v5-6-be2007d8da63@linuxfoundation.org \
    --to=konstantin@linuxfoundation.org \
    --cc=mricon@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 read-only IMAP folder(s) and NNTP newsgroup(s).