Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add support for Clang CFI

This change adds support for Clang’s forward-edge Control Flow
Integrity (CFI) checking. With CONFIG_CFI_CLANG, the compiler
injects a runtime check before each indirect function call to ensure
the target is a valid function with the correct static type. This
restricts possible call targets and makes it more difficult for
an attacker to exploit bugs that allow the modification of stored
function pointers. For more details, see:

https://clang.llvm.org/docs/ControlFlowIntegrity.html

Clang requires CONFIG_LTO_CLANG to be enabled with CFI to gain
visibility to possible call targets. Kernel modules are supported
with Clang’s cross-DSO CFI mode, which allows checking between
independently compiled components.

With CFI enabled, the compiler injects a __cfi_check() function into
the kernel and each module for validating local call targets. For
cross-module calls that cannot be validated locally, the compiler
calls the global __cfi_slowpath_diag() function, which determines
the target module and calls the correct __cfi_check() function. This
patch includes a slowpath implementation that uses __module_address()
to resolve call targets, and with CONFIG_CFI_CLANG_SHADOW enabled, a
shadow map that speeds up module look-ups by ~3x.

Clang implements indirect call checking using jump tables and
offers two methods of generating them. With canonical jump tables,
the compiler renames each address-taken function to <function>.cfi
and points the original symbol to a jump table entry, which passes
__cfi_check() validation. This isn’t compatible with stand-alone
assembly code, which the compiler doesn’t instrument, and would
result in indirect calls to assembly code to fail. Therefore, we
default to using non-canonical jump tables instead, where the compiler
generates a local jump table entry <function>.cfi_jt for each
address-taken function, and replaces all references to the function
with the address of the jump table entry.

Note that because non-canonical jump table addresses are local
to each component, they break cross-module function address
equality. Specifically, the address of a global function will be
different in each module, as it's replaced with the address of a local
jump table entry. If this address is passed to a different module,
it won’t match the address of the same function taken there. This
may break code that relies on comparing addresses passed from other
components.

CFI checking can be disabled in a function with the __nocfi attribute.
Additionally, CFI can be disabled for an entire compilation unit by
filtering out CC_FLAGS_CFI.

By default, CFI failures result in a kernel panic to stop a potential
exploit. CONFIG_CFI_PERMISSIVE enables a permissive mode, where the
kernel prints out a rate-limited warning instead, and allows execution
to continue. This option is helpful for locating type mismatches, but
should only be enabled during development.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20210408182843.1754385-2-samitolvanen@google.com

authored by

Sami Tolvanen and committed by
Kees Cook
cf68fffb e49d033b

+534 -6
+17
Makefile
··· 920 920 export CC_FLAGS_LTO 921 921 endif 922 922 923 + ifdef CONFIG_CFI_CLANG 924 + CC_FLAGS_CFI := -fsanitize=cfi \ 925 + -fsanitize-cfi-cross-dso \ 926 + -fno-sanitize-cfi-canonical-jump-tables \ 927 + -fno-sanitize-trap=cfi \ 928 + -fno-sanitize-blacklist 929 + 930 + ifdef CONFIG_CFI_PERMISSIVE 931 + CC_FLAGS_CFI += -fsanitize-recover=cfi 932 + endif 933 + 934 + # If LTO flags are filtered out, we must also filter out CFI. 935 + CC_FLAGS_LTO += $(CC_FLAGS_CFI) 936 + KBUILD_CFLAGS += $(CC_FLAGS_CFI) 937 + export CC_FLAGS_CFI 938 + endif 939 + 923 940 ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B 924 941 KBUILD_CFLAGS += -falign-functions=32 925 942 endif
+45
arch/Kconfig
··· 692 692 If unsure, say Y. 693 693 endchoice 694 694 695 + config ARCH_SUPPORTS_CFI_CLANG 696 + bool 697 + help 698 + An architecture should select this option if it can support Clang's 699 + Control-Flow Integrity (CFI) checking. 700 + 701 + config CFI_CLANG 702 + bool "Use Clang's Control Flow Integrity (CFI)" 703 + depends on LTO_CLANG && ARCH_SUPPORTS_CFI_CLANG 704 + # Clang >= 12: 705 + # - https://bugs.llvm.org/show_bug.cgi?id=46258 706 + # - https://bugs.llvm.org/show_bug.cgi?id=47479 707 + depends on CLANG_VERSION >= 120000 708 + select KALLSYMS 709 + help 710 + This option enables Clang’s forward-edge Control Flow Integrity 711 + (CFI) checking, where the compiler injects a runtime check to each 712 + indirect function call to ensure the target is a valid function with 713 + the correct static type. This restricts possible call targets and 714 + makes it more difficult for an attacker to exploit bugs that allow 715 + the modification of stored function pointers. More information can be 716 + found from Clang's documentation: 717 + 718 + https://clang.llvm.org/docs/ControlFlowIntegrity.html 719 + 720 + config CFI_CLANG_SHADOW 721 + bool "Use CFI shadow to speed up cross-module checks" 722 + default y 723 + depends on CFI_CLANG && MODULES 724 + help 725 + If you select this option, the kernel builds a fast look-up table of 726 + CFI check functions in loaded modules to reduce performance overhead. 727 + 728 + If unsure, say Y. 729 + 730 + config CFI_PERMISSIVE 731 + bool "Use CFI in permissive mode" 732 + depends on CFI_CLANG 733 + help 734 + When selected, Control Flow Integrity (CFI) violations result in a 735 + warning instead of a kernel panic. This option should only be used 736 + for finding indirect call type mismatches during development. 737 + 738 + If unsure, say N. 739 + 695 740 config HAVE_ARCH_WITHIN_STACK_FRAMES 696 741 bool 697 742 help
+16
include/asm-generic/bug.h
··· 241 241 # define WARN_ON_SMP(x) ({0;}) 242 242 #endif 243 243 244 + /* 245 + * WARN_ON_FUNCTION_MISMATCH() warns if a value doesn't match a 246 + * function address, and can be useful for catching issues with 247 + * callback functions, for example. 248 + * 249 + * With CONFIG_CFI_CLANG, the warning is disabled because the 250 + * compiler replaces function addresses taken in C code with 251 + * local jump table addresses, which breaks cross-module function 252 + * address equality. 253 + */ 254 + #if defined(CONFIG_CFI_CLANG) && defined(CONFIG_MODULES) 255 + # define WARN_ON_FUNCTION_MISMATCH(x, fn) ({ 0; }) 256 + #else 257 + # define WARN_ON_FUNCTION_MISMATCH(x, fn) WARN_ON_ONCE((x) != (fn)) 258 + #endif 259 + 244 260 #endif /* __ASSEMBLY__ */ 245 261 246 262 #endif
+19 -1
include/asm-generic/vmlinux.lds.h
··· 544 544 . = ALIGN((align)); \ 545 545 __end_rodata = .; 546 546 547 + 548 + /* 549 + * .text..L.cfi.jumptable.* contain Control-Flow Integrity (CFI) 550 + * jump table entries. 551 + */ 552 + #ifdef CONFIG_CFI_CLANG 553 + #define TEXT_CFI_JT \ 554 + . = ALIGN(PMD_SIZE); \ 555 + __cfi_jt_start = .; \ 556 + *(.text..L.cfi.jumptable .text..L.cfi.jumptable.*) \ 557 + . = ALIGN(PMD_SIZE); \ 558 + __cfi_jt_end = .; 559 + #else 560 + #define TEXT_CFI_JT 561 + #endif 562 + 547 563 /* 548 564 * Non-instrumentable text section 549 565 */ ··· 586 570 NOINSTR_TEXT \ 587 571 *(.text..refcount) \ 588 572 *(.ref.text) \ 573 + TEXT_CFI_JT \ 589 574 MEM_KEEP(init.text*) \ 590 575 MEM_KEEP(exit.text*) \ 591 576 ··· 991 974 * keep any .init_array.* sections. 992 975 * https://bugs.llvm.org/show_bug.cgi?id=46478 993 976 */ 994 - #if defined(CONFIG_GCOV_KERNEL) || defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KCSAN) 977 + #if defined(CONFIG_GCOV_KERNEL) || defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KCSAN) || \ 978 + defined(CONFIG_CFI_CLANG) 995 979 # ifdef CONFIG_CONSTRUCTORS 996 980 # define SANITIZER_DISCARDS \ 997 981 *(.eh_frame)
+41
include/linux/cfi.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Clang Control Flow Integrity (CFI) support. 4 + * 5 + * Copyright (C) 2021 Google LLC 6 + */ 7 + #ifndef _LINUX_CFI_H 8 + #define _LINUX_CFI_H 9 + 10 + #ifdef CONFIG_CFI_CLANG 11 + typedef void (*cfi_check_fn)(uint64_t id, void *ptr, void *diag); 12 + 13 + /* Compiler-generated function in each module, and the kernel */ 14 + extern void __cfi_check(uint64_t id, void *ptr, void *diag); 15 + 16 + /* 17 + * Force the compiler to generate a CFI jump table entry for a function 18 + * and store the jump table address to __cfi_jt_<function>. 19 + */ 20 + #define __CFI_ADDRESSABLE(fn, __attr) \ 21 + const void *__cfi_jt_ ## fn __visible __attr = (void *)&fn 22 + 23 + #ifdef CONFIG_CFI_CLANG_SHADOW 24 + 25 + extern void cfi_module_add(struct module *mod, unsigned long base_addr); 26 + extern void cfi_module_remove(struct module *mod, unsigned long base_addr); 27 + 28 + #else 29 + 30 + static inline void cfi_module_add(struct module *mod, unsigned long base_addr) {} 31 + static inline void cfi_module_remove(struct module *mod, unsigned long base_addr) {} 32 + 33 + #endif /* CONFIG_CFI_CLANG_SHADOW */ 34 + 35 + #else /* !CONFIG_CFI_CLANG */ 36 + 37 + #define __CFI_ADDRESSABLE(fn, __attr) 38 + 39 + #endif /* CONFIG_CFI_CLANG */ 40 + 41 + #endif /* _LINUX_CFI_H */
+2
include/linux/compiler-clang.h
··· 61 61 #if __has_feature(shadow_call_stack) 62 62 # define __noscs __attribute__((__no_sanitize__("shadow-call-stack"))) 63 63 #endif 64 + 65 + #define __nocfi __attribute__((__no_sanitize__("cfi")))
+4
include/linux/compiler_types.h
··· 242 242 # define __noscs 243 243 #endif 244 244 245 + #ifndef __nocfi 246 + # define __nocfi 247 + #endif 248 + 245 249 #ifndef asm_volatile_goto 246 250 #define asm_volatile_goto(x...) asm goto(x) 247 251 #endif
+1 -1
include/linux/init.h
··· 47 47 48 48 /* These are for everybody (although not all archs will actually 49 49 discard it in modules) */ 50 - #define __init __section(".init.text") __cold __latent_entropy __noinitretpoline 50 + #define __init __section(".init.text") __cold __latent_entropy __noinitretpoline __nocfi 51 51 #define __initdata __section(".init.data") 52 52 #define __initconst __section(".init.rodata") 53 53 #define __exitdata __section(".exit.data")
+11 -2
include/linux/module.h
··· 26 26 #include <linux/tracepoint-defs.h> 27 27 #include <linux/srcu.h> 28 28 #include <linux/static_call_types.h> 29 + #include <linux/cfi.h> 29 30 30 31 #include <linux/percpu.h> 31 32 #include <asm/module.h> ··· 129 128 #define module_init(initfn) \ 130 129 static inline initcall_t __maybe_unused __inittest(void) \ 131 130 { return initfn; } \ 132 - int init_module(void) __copy(initfn) __attribute__((alias(#initfn))); 131 + int init_module(void) __copy(initfn) \ 132 + __attribute__((alias(#initfn))); \ 133 + __CFI_ADDRESSABLE(init_module, __initdata); 133 134 134 135 /* This is only required if you want to be unloadable. */ 135 136 #define module_exit(exitfn) \ 136 137 static inline exitcall_t __maybe_unused __exittest(void) \ 137 138 { return exitfn; } \ 138 - void cleanup_module(void) __copy(exitfn) __attribute__((alias(#exitfn))); 139 + void cleanup_module(void) __copy(exitfn) \ 140 + __attribute__((alias(#exitfn))); \ 141 + __CFI_ADDRESSABLE(cleanup_module, __exitdata); 139 142 140 143 #endif 141 144 ··· 380 375 const struct kernel_symbol *syms; 381 376 const s32 *crcs; 382 377 unsigned int num_syms; 378 + 379 + #ifdef CONFIG_CFI_CLANG 380 + cfi_check_fn cfi_check; 381 + #endif 383 382 384 383 /* Kernel parameters. */ 385 384 #ifdef CONFIG_SYSFS
+1 -1
init/Kconfig
··· 2296 2296 2297 2297 config MODULES_TREE_LOOKUP 2298 2298 def_bool y 2299 - depends on PERF_EVENTS || TRACING 2299 + depends on PERF_EVENTS || TRACING || CFI_CLANG 2300 2300 2301 2301 config INIT_ALL_POSSIBLE 2302 2302 bool
+4
kernel/Makefile
··· 41 41 UBSAN_SANITIZE_kcov.o := n 42 42 CFLAGS_kcov.o := $(call cc-option, -fno-conserve-stack) -fno-stack-protector 43 43 44 + # Don't instrument error handlers 45 + CFLAGS_REMOVE_cfi.o := $(CC_FLAGS_CFI) 46 + 44 47 obj-y += sched/ 45 48 obj-y += locking/ 46 49 obj-y += power/ ··· 114 111 obj-$(CONFIG_KCSAN) += kcsan/ 115 112 obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o 116 113 obj-$(CONFIG_HAVE_STATIC_CALL_INLINE) += static_call.o 114 + obj-$(CONFIG_CFI_CLANG) += cfi.o 117 115 118 116 obj-$(CONFIG_PERF_EVENTS) += events/ 119 117
+329
kernel/cfi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Clang Control Flow Integrity (CFI) error and slowpath handling. 4 + * 5 + * Copyright (C) 2021 Google LLC 6 + */ 7 + 8 + #include <linux/hardirq.h> 9 + #include <linux/kallsyms.h> 10 + #include <linux/module.h> 11 + #include <linux/mutex.h> 12 + #include <linux/printk.h> 13 + #include <linux/ratelimit.h> 14 + #include <linux/rcupdate.h> 15 + #include <linux/vmalloc.h> 16 + #include <asm/cacheflush.h> 17 + #include <asm/set_memory.h> 18 + 19 + /* Compiler-defined handler names */ 20 + #ifdef CONFIG_CFI_PERMISSIVE 21 + #define cfi_failure_handler __ubsan_handle_cfi_check_fail 22 + #else 23 + #define cfi_failure_handler __ubsan_handle_cfi_check_fail_abort 24 + #endif 25 + 26 + static inline void handle_cfi_failure(void *ptr) 27 + { 28 + if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) 29 + WARN_RATELIMIT(1, "CFI failure (target: %pS):\n", ptr); 30 + else 31 + panic("CFI failure (target: %pS)\n", ptr); 32 + } 33 + 34 + #ifdef CONFIG_MODULES 35 + #ifdef CONFIG_CFI_CLANG_SHADOW 36 + /* 37 + * Index type. A 16-bit index can address at most (2^16)-2 pages (taking 38 + * into account SHADOW_INVALID), i.e. ~256M with 4k pages. 39 + */ 40 + typedef u16 shadow_t; 41 + #define SHADOW_INVALID ((shadow_t)~0UL) 42 + 43 + struct cfi_shadow { 44 + /* Page index for the beginning of the shadow */ 45 + unsigned long base; 46 + /* An array of __cfi_check locations (as indices to the shadow) */ 47 + shadow_t shadow[1]; 48 + } __packed; 49 + 50 + /* 51 + * The shadow covers ~128M from the beginning of the module region. If 52 + * the region is larger, we fall back to __module_address for the rest. 53 + */ 54 + #define __SHADOW_RANGE (_UL(SZ_128M) >> PAGE_SHIFT) 55 + 56 + /* The in-memory size of struct cfi_shadow, always at least one page */ 57 + #define __SHADOW_PAGES ((__SHADOW_RANGE * sizeof(shadow_t)) >> PAGE_SHIFT) 58 + #define SHADOW_PAGES max(1UL, __SHADOW_PAGES) 59 + #define SHADOW_SIZE (SHADOW_PAGES << PAGE_SHIFT) 60 + 61 + /* The actual size of the shadow array, minus metadata */ 62 + #define SHADOW_ARR_SIZE (SHADOW_SIZE - offsetof(struct cfi_shadow, shadow)) 63 + #define SHADOW_ARR_SLOTS (SHADOW_ARR_SIZE / sizeof(shadow_t)) 64 + 65 + static DEFINE_MUTEX(shadow_update_lock); 66 + static struct cfi_shadow __rcu *cfi_shadow __read_mostly; 67 + 68 + /* Returns the index in the shadow for the given address */ 69 + static inline int ptr_to_shadow(const struct cfi_shadow *s, unsigned long ptr) 70 + { 71 + unsigned long index; 72 + unsigned long page = ptr >> PAGE_SHIFT; 73 + 74 + if (unlikely(page < s->base)) 75 + return -1; /* Outside of module area */ 76 + 77 + index = page - s->base; 78 + 79 + if (index >= SHADOW_ARR_SLOTS) 80 + return -1; /* Cannot be addressed with shadow */ 81 + 82 + return (int)index; 83 + } 84 + 85 + /* Returns the page address for an index in the shadow */ 86 + static inline unsigned long shadow_to_ptr(const struct cfi_shadow *s, 87 + int index) 88 + { 89 + if (unlikely(index < 0 || index >= SHADOW_ARR_SLOTS)) 90 + return 0; 91 + 92 + return (s->base + index) << PAGE_SHIFT; 93 + } 94 + 95 + /* Returns the __cfi_check function address for the given shadow location */ 96 + static inline unsigned long shadow_to_check_fn(const struct cfi_shadow *s, 97 + int index) 98 + { 99 + if (unlikely(index < 0 || index >= SHADOW_ARR_SLOTS)) 100 + return 0; 101 + 102 + if (unlikely(s->shadow[index] == SHADOW_INVALID)) 103 + return 0; 104 + 105 + /* __cfi_check is always page aligned */ 106 + return (s->base + s->shadow[index]) << PAGE_SHIFT; 107 + } 108 + 109 + static void prepare_next_shadow(const struct cfi_shadow __rcu *prev, 110 + struct cfi_shadow *next) 111 + { 112 + int i, index, check; 113 + 114 + /* Mark everything invalid */ 115 + memset(next->shadow, 0xFF, SHADOW_ARR_SIZE); 116 + 117 + if (!prev) 118 + return; /* No previous shadow */ 119 + 120 + /* If the base address didn't change, an update is not needed */ 121 + if (prev->base == next->base) { 122 + memcpy(next->shadow, prev->shadow, SHADOW_ARR_SIZE); 123 + return; 124 + } 125 + 126 + /* Convert the previous shadow to the new address range */ 127 + for (i = 0; i < SHADOW_ARR_SLOTS; ++i) { 128 + if (prev->shadow[i] == SHADOW_INVALID) 129 + continue; 130 + 131 + index = ptr_to_shadow(next, shadow_to_ptr(prev, i)); 132 + if (index < 0) 133 + continue; 134 + 135 + check = ptr_to_shadow(next, 136 + shadow_to_check_fn(prev, prev->shadow[i])); 137 + if (check < 0) 138 + continue; 139 + 140 + next->shadow[index] = (shadow_t)check; 141 + } 142 + } 143 + 144 + static void add_module_to_shadow(struct cfi_shadow *s, struct module *mod, 145 + unsigned long min_addr, unsigned long max_addr) 146 + { 147 + int check_index; 148 + unsigned long check = (unsigned long)mod->cfi_check; 149 + unsigned long ptr; 150 + 151 + if (unlikely(!PAGE_ALIGNED(check))) { 152 + pr_warn("cfi: not using shadow for module %s\n", mod->name); 153 + return; 154 + } 155 + 156 + check_index = ptr_to_shadow(s, check); 157 + if (check_index < 0) 158 + return; /* Module not addressable with shadow */ 159 + 160 + /* For each page, store the check function index in the shadow */ 161 + for (ptr = min_addr; ptr <= max_addr; ptr += PAGE_SIZE) { 162 + int index = ptr_to_shadow(s, ptr); 163 + 164 + if (index >= 0) { 165 + /* Each page must only contain one module */ 166 + WARN_ON_ONCE(s->shadow[index] != SHADOW_INVALID); 167 + s->shadow[index] = (shadow_t)check_index; 168 + } 169 + } 170 + } 171 + 172 + static void remove_module_from_shadow(struct cfi_shadow *s, struct module *mod, 173 + unsigned long min_addr, unsigned long max_addr) 174 + { 175 + unsigned long ptr; 176 + 177 + for (ptr = min_addr; ptr <= max_addr; ptr += PAGE_SIZE) { 178 + int index = ptr_to_shadow(s, ptr); 179 + 180 + if (index >= 0) 181 + s->shadow[index] = SHADOW_INVALID; 182 + } 183 + } 184 + 185 + typedef void (*update_shadow_fn)(struct cfi_shadow *, struct module *, 186 + unsigned long min_addr, unsigned long max_addr); 187 + 188 + static void update_shadow(struct module *mod, unsigned long base_addr, 189 + update_shadow_fn fn) 190 + { 191 + struct cfi_shadow *prev; 192 + struct cfi_shadow *next; 193 + unsigned long min_addr, max_addr; 194 + 195 + next = vmalloc(SHADOW_SIZE); 196 + 197 + mutex_lock(&shadow_update_lock); 198 + prev = rcu_dereference_protected(cfi_shadow, 199 + mutex_is_locked(&shadow_update_lock)); 200 + 201 + if (next) { 202 + next->base = base_addr >> PAGE_SHIFT; 203 + prepare_next_shadow(prev, next); 204 + 205 + min_addr = (unsigned long)mod->core_layout.base; 206 + max_addr = min_addr + mod->core_layout.text_size; 207 + fn(next, mod, min_addr & PAGE_MASK, max_addr & PAGE_MASK); 208 + 209 + set_memory_ro((unsigned long)next, SHADOW_PAGES); 210 + } 211 + 212 + rcu_assign_pointer(cfi_shadow, next); 213 + mutex_unlock(&shadow_update_lock); 214 + synchronize_rcu(); 215 + 216 + if (prev) { 217 + set_memory_rw((unsigned long)prev, SHADOW_PAGES); 218 + vfree(prev); 219 + } 220 + } 221 + 222 + void cfi_module_add(struct module *mod, unsigned long base_addr) 223 + { 224 + update_shadow(mod, base_addr, add_module_to_shadow); 225 + } 226 + 227 + void cfi_module_remove(struct module *mod, unsigned long base_addr) 228 + { 229 + update_shadow(mod, base_addr, remove_module_from_shadow); 230 + } 231 + 232 + static inline cfi_check_fn ptr_to_check_fn(const struct cfi_shadow __rcu *s, 233 + unsigned long ptr) 234 + { 235 + int index; 236 + 237 + if (unlikely(!s)) 238 + return NULL; /* No shadow available */ 239 + 240 + index = ptr_to_shadow(s, ptr); 241 + if (index < 0) 242 + return NULL; /* Cannot be addressed with shadow */ 243 + 244 + return (cfi_check_fn)shadow_to_check_fn(s, index); 245 + } 246 + 247 + static inline cfi_check_fn find_shadow_check_fn(unsigned long ptr) 248 + { 249 + cfi_check_fn fn; 250 + 251 + rcu_read_lock_sched(); 252 + fn = ptr_to_check_fn(rcu_dereference_sched(cfi_shadow), ptr); 253 + rcu_read_unlock_sched(); 254 + 255 + return fn; 256 + } 257 + 258 + #else /* !CONFIG_CFI_CLANG_SHADOW */ 259 + 260 + static inline cfi_check_fn find_shadow_check_fn(unsigned long ptr) 261 + { 262 + return NULL; 263 + } 264 + 265 + #endif /* CONFIG_CFI_CLANG_SHADOW */ 266 + 267 + static inline cfi_check_fn find_module_check_fn(unsigned long ptr) 268 + { 269 + cfi_check_fn fn = NULL; 270 + struct module *mod; 271 + 272 + rcu_read_lock_sched(); 273 + mod = __module_address(ptr); 274 + if (mod) 275 + fn = mod->cfi_check; 276 + rcu_read_unlock_sched(); 277 + 278 + return fn; 279 + } 280 + 281 + static inline cfi_check_fn find_check_fn(unsigned long ptr) 282 + { 283 + cfi_check_fn fn = NULL; 284 + 285 + if (is_kernel_text(ptr)) 286 + return __cfi_check; 287 + 288 + /* 289 + * Indirect call checks can happen when RCU is not watching. Both 290 + * the shadow and __module_address use RCU, so we need to wake it 291 + * up if necessary. 292 + */ 293 + RCU_NONIDLE({ 294 + if (IS_ENABLED(CONFIG_CFI_CLANG_SHADOW)) 295 + fn = find_shadow_check_fn(ptr); 296 + 297 + if (!fn) 298 + fn = find_module_check_fn(ptr); 299 + }); 300 + 301 + return fn; 302 + } 303 + 304 + void __cfi_slowpath_diag(uint64_t id, void *ptr, void *diag) 305 + { 306 + cfi_check_fn fn = find_check_fn((unsigned long)ptr); 307 + 308 + if (likely(fn)) 309 + fn(id, ptr, diag); 310 + else /* Don't allow unchecked modules */ 311 + handle_cfi_failure(ptr); 312 + } 313 + EXPORT_SYMBOL(__cfi_slowpath_diag); 314 + 315 + #else /* !CONFIG_MODULES */ 316 + 317 + void __cfi_slowpath_diag(uint64_t id, void *ptr, void *diag) 318 + { 319 + handle_cfi_failure(ptr); /* No modules */ 320 + } 321 + EXPORT_SYMBOL(__cfi_slowpath_diag); 322 + 323 + #endif /* CONFIG_MODULES */ 324 + 325 + void cfi_failure_handler(void *data, void *ptr, void *vtable) 326 + { 327 + handle_cfi_failure(ptr); 328 + } 329 + EXPORT_SYMBOL(cfi_failure_handler);
+43
kernel/module.c
··· 2146 2146 { 2147 2147 } 2148 2148 2149 + static void cfi_cleanup(struct module *mod); 2150 + 2149 2151 /* Free a module, remove from lists, etc. */ 2150 2152 static void free_module(struct module *mod) 2151 2153 { ··· 2188 2186 /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */ 2189 2187 synchronize_rcu(); 2190 2188 mutex_unlock(&module_mutex); 2189 + 2190 + /* Clean up CFI for the module. */ 2191 + cfi_cleanup(mod); 2191 2192 2192 2193 /* This may be empty, but that's OK */ 2193 2194 module_arch_freeing_init(mod); ··· 3871 3866 return 0; 3872 3867 } 3873 3868 3869 + static void cfi_init(struct module *mod); 3870 + 3874 3871 /* 3875 3872 * Allocate and load the module: note that size of section 0 is always 3876 3873 * zero, and we rely on this for optional sections. ··· 4004 3997 4005 3998 flush_module_icache(mod); 4006 3999 4000 + /* Setup CFI for the module. */ 4001 + cfi_init(mod); 4002 + 4007 4003 /* Now copy in args */ 4008 4004 mod->args = strndup_user(uargs, ~0UL >> 1); 4009 4005 if (IS_ERR(mod->args)) { ··· 4080 4070 synchronize_rcu(); 4081 4071 kfree(mod->args); 4082 4072 free_arch_cleanup: 4073 + cfi_cleanup(mod); 4083 4074 module_arch_cleanup(mod); 4084 4075 free_modinfo: 4085 4076 free_modinfo(mod); ··· 4425 4414 } 4426 4415 #endif /* CONFIG_LIVEPATCH */ 4427 4416 #endif /* CONFIG_KALLSYMS */ 4417 + 4418 + static void cfi_init(struct module *mod) 4419 + { 4420 + #ifdef CONFIG_CFI_CLANG 4421 + initcall_t *init; 4422 + exitcall_t *exit; 4423 + 4424 + rcu_read_lock_sched(); 4425 + mod->cfi_check = (cfi_check_fn) 4426 + find_kallsyms_symbol_value(mod, "__cfi_check"); 4427 + init = (initcall_t *) 4428 + find_kallsyms_symbol_value(mod, "__cfi_jt_init_module"); 4429 + exit = (exitcall_t *) 4430 + find_kallsyms_symbol_value(mod, "__cfi_jt_cleanup_module"); 4431 + rcu_read_unlock_sched(); 4432 + 4433 + /* Fix init/exit functions to point to the CFI jump table */ 4434 + if (init) 4435 + mod->init = *init; 4436 + if (exit) 4437 + mod->exit = *exit; 4438 + 4439 + cfi_module_add(mod, module_addr_min); 4440 + #endif 4441 + } 4442 + 4443 + static void cfi_cleanup(struct module *mod) 4444 + { 4445 + #ifdef CONFIG_CFI_CLANG 4446 + cfi_module_remove(mod, module_addr_min); 4447 + #endif 4448 + } 4428 4449 4429 4450 /* Maximum number of characters written by module_flags() */ 4430 4451 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
+1 -1
scripts/Makefile.modfinal
··· 23 23 part-of-module = y 24 24 25 25 quiet_cmd_cc_o_c = CC [M] $@ 26 - cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< 26 + cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI), $(c_flags)) -c -o $@ $< 27 27 28 28 %.mod.o: %.mod.c FORCE 29 29 $(call if_changed_dep,cc_o_c)