1 // SPDX-License-Identifier: GPL-2.0
3 * Clang Control Flow Integrity (CFI) error handling.
5 * Copyright (C) 2022 Google LLC
10 bool cfi_warn __ro_after_init = IS_ENABLED(CONFIG_CFI_PERMISSIVE);
12 enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,
13 unsigned long *target, u32 type)
16 pr_err("CFI failure at %pS (target: %pS; expected type: 0x%08x)\n",
17 (void *)addr, (void *)*target, type);
19 pr_err("CFI failure at %pS (no target information)\n",
23 __warn(NULL, 0, (void *)addr, 0, regs, NULL);
24 return BUG_TRAP_TYPE_WARN;
27 return BUG_TRAP_TYPE_BUG;
30 #ifdef CONFIG_ARCH_USES_CFI_TRAPS
31 static inline unsigned long trap_address(s32 *p)
33 return (unsigned long)((long)p + (long)*p);
36 static bool is_trap(unsigned long addr, s32 *start, s32 *end)
40 for (p = start; p < end; ++p) {
41 if (trap_address(p) == addr)
49 /* Populates `kcfi_trap(_end)?` fields in `struct module`. */
50 void module_cfi_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
56 mod->kcfi_traps = NULL;
57 mod->kcfi_traps_end = NULL;
59 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
61 for (i = 1; i < hdr->e_shnum; i++) {
62 if (strcmp(secstrings + sechdrs[i].sh_name, "__kcfi_traps"))
65 mod->kcfi_traps = (s32 *)sechdrs[i].sh_addr;
66 mod->kcfi_traps_end = (s32 *)(sechdrs[i].sh_addr + sechdrs[i].sh_size);
71 static bool is_module_cfi_trap(unsigned long addr)
77 mod = __module_address(addr);
79 found = is_trap(addr, mod->kcfi_traps, mod->kcfi_traps_end);
83 #else /* CONFIG_MODULES */
84 static inline bool is_module_cfi_trap(unsigned long addr)
88 #endif /* CONFIG_MODULES */
90 extern s32 __start___kcfi_traps[];
91 extern s32 __stop___kcfi_traps[];
93 bool is_cfi_trap(unsigned long addr)
95 if (is_trap(addr, __start___kcfi_traps, __stop___kcfi_traps))
98 return is_module_cfi_trap(addr);
100 #endif /* CONFIG_ARCH_USES_CFI_TRAPS */