~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/cfi.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Clang Control Flow Integrity (CFI) error handling.
  4  *
  5  * Copyright (C) 2022 Google LLC
  6  */
  7 
  8 #include <linux/cfi.h>
  9 
 10 enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long addr,
 11                                       unsigned long *target, u32 type)
 12 {
 13         if (target)
 14                 pr_err("CFI failure at %pS (target: %pS; expected type: 0x%08x)\n",
 15                        (void *)addr, (void *)*target, type);
 16         else
 17                 pr_err("CFI failure at %pS (no target information)\n",
 18                        (void *)addr);
 19 
 20         if (IS_ENABLED(CONFIG_CFI_PERMISSIVE)) {
 21                 __warn(NULL, 0, (void *)addr, 0, regs, NULL);
 22                 return BUG_TRAP_TYPE_WARN;
 23         }
 24 
 25         return BUG_TRAP_TYPE_BUG;
 26 }
 27 
 28 #ifdef CONFIG_ARCH_USES_CFI_TRAPS
 29 static inline unsigned long trap_address(s32 *p)
 30 {
 31         return (unsigned long)((long)p + (long)*p);
 32 }
 33 
 34 static bool is_trap(unsigned long addr, s32 *start, s32 *end)
 35 {
 36         s32 *p;
 37 
 38         for (p = start; p < end; ++p) {
 39                 if (trap_address(p) == addr)
 40                         return true;
 41         }
 42 
 43         return false;
 44 }
 45 
 46 #ifdef CONFIG_MODULES
 47 /* Populates `kcfi_trap(_end)?` fields in `struct module`. */
 48 void module_cfi_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
 49                          struct module *mod)
 50 {
 51         char *secstrings;
 52         unsigned int i;
 53 
 54         mod->kcfi_traps = NULL;
 55         mod->kcfi_traps_end = NULL;
 56 
 57         secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
 58 
 59         for (i = 1; i < hdr->e_shnum; i++) {
 60                 if (strcmp(secstrings + sechdrs[i].sh_name, "__kcfi_traps"))
 61                         continue;
 62 
 63                 mod->kcfi_traps = (s32 *)sechdrs[i].sh_addr;
 64                 mod->kcfi_traps_end = (s32 *)(sechdrs[i].sh_addr + sechdrs[i].sh_size);
 65                 break;
 66         }
 67 }
 68 
 69 static bool is_module_cfi_trap(unsigned long addr)
 70 {
 71         struct module *mod;
 72         bool found = false;
 73 
 74         rcu_read_lock_sched_notrace();
 75 
 76         mod = __module_address(addr);
 77         if (mod)
 78                 found = is_trap(addr, mod->kcfi_traps, mod->kcfi_traps_end);
 79 
 80         rcu_read_unlock_sched_notrace();
 81 
 82         return found;
 83 }
 84 #else /* CONFIG_MODULES */
 85 static inline bool is_module_cfi_trap(unsigned long addr)
 86 {
 87         return false;
 88 }
 89 #endif /* CONFIG_MODULES */
 90 
 91 extern s32 __start___kcfi_traps[];
 92 extern s32 __stop___kcfi_traps[];
 93 
 94 bool is_cfi_trap(unsigned long addr)
 95 {
 96         if (is_trap(addr, __start___kcfi_traps, __stop___kcfi_traps))
 97                 return true;
 98 
 99         return is_module_cfi_trap(addr);
100 }
101 #endif /* CONFIG_ARCH_USES_CFI_TRAPS */
102 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php