Blame view
kernel/cfi.c
7.41 KB
81f7e3824 Initial Release, ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
/* * CFI (Control Flow Integrity) error and slowpath handling * * Copyright (C) 2017 Google, Inc. */ #include <linux/gfp.h> #include <linux/module.h> #include <linux/printk.h> #include <linux/ratelimit.h> #include <linux/rcupdate.h> #include <linux/spinlock.h> #include <asm/bug.h> #include <asm/cacheflush.h> #include <asm/memory.h> #include <asm/set_memory.h> /* Compiler-defined handler names */ #ifdef CONFIG_CFI_PERMISSIVE #define cfi_failure_handler __ubsan_handle_cfi_check_fail #define cfi_slowpath_handler __cfi_slowpath_diag #else /* enforcing */ #define cfi_failure_handler __ubsan_handle_cfi_check_fail_abort #define cfi_slowpath_handler __cfi_slowpath #endif /* CONFIG_CFI_PERMISSIVE */ static inline void handle_cfi_failure(void *ptr) { #ifdef CONFIG_CFI_PERMISSIVE WARN_RATELIMIT(1, "CFI failure (target: [<%px>] %pF): ", ptr, ptr); #else pr_err("CFI failure (target: [<%px>] %pF): ", ptr, ptr); BUG(); #endif } #ifdef CONFIG_MODULES #ifdef CONFIG_CFI_CLANG_SHADOW struct shadow_range { /* Module address range */ unsigned long mod_min_addr; unsigned long mod_max_addr; /* Module page range */ unsigned long min_page; unsigned long max_page; }; #define SHADOW_ORDER 1 #define SHADOW_PAGES (1 << SHADOW_ORDER) #define SHADOW_SIZE \ ((SHADOW_PAGES * PAGE_SIZE - sizeof(struct shadow_range)) / sizeof(u16)) #define SHADOW_INVALID 0xFFFF struct cfi_shadow { /* Page range covered by the shadow */ struct shadow_range r; /* Page offsets to __cfi_check functions in modules */ u16 shadow[SHADOW_SIZE]; }; static DEFINE_SPINLOCK(shadow_update_lock); static struct cfi_shadow __rcu *cfi_shadow __read_mostly = NULL; static inline int ptr_to_shadow(const struct cfi_shadow *s, unsigned long ptr) { unsigned long index; unsigned long page = ptr >> PAGE_SHIFT; if (unlikely(page < s->r.min_page)) return -1; /* Outside of module area */ index = page - s->r.min_page; if (index >= SHADOW_SIZE) return -1; /* Cannot be addressed with shadow */ return (int)index; } static inline unsigned long shadow_to_ptr(const struct cfi_shadow *s, int index) { BUG_ON(index < 0 || index >= SHADOW_SIZE); if (unlikely(s->shadow[index] == SHADOW_INVALID)) return 0; return (s->r.min_page + s->shadow[index]) << PAGE_SHIFT; } static void prepare_next_shadow(const struct cfi_shadow __rcu *prev, struct cfi_shadow *next) { int i, index, check; /* Mark everything invalid */ memset(next->shadow, 0xFF, sizeof(next->shadow)); if (!prev) return; /* No previous shadow */ /* If the base address didn't change, update is not needed */ if (prev->r.min_page == next->r.min_page) { memcpy(next->shadow, prev->shadow, sizeof(next->shadow)); return; } /* Convert the previous shadow to the new address range */ for (i = 0; i < SHADOW_SIZE; ++i) { if (prev->shadow[i] == SHADOW_INVALID) continue; index = ptr_to_shadow(next, shadow_to_ptr(prev, i)); if (index < 0) continue; check = ptr_to_shadow(next, shadow_to_ptr(prev, prev->shadow[i])); if (check < 0) continue; next->shadow[index] = (u16)check; } } static void add_module_to_shadow(struct cfi_shadow *s, struct module *mod) { unsigned long ptr; unsigned long min_page_addr; unsigned long max_page_addr; unsigned long check = (unsigned long)mod->cfi_check; int check_index = ptr_to_shadow(s, check); BUG_ON((check & PAGE_MASK) != check); /* Must be page aligned */ if (check_index < 0) return; /* Module not addressable with shadow */ min_page_addr = (unsigned long)mod->core_layout.base & PAGE_MASK; max_page_addr = (unsigned long)mod->core_layout.base + mod->core_layout.text_size; max_page_addr &= PAGE_MASK; /* For each page, store the check function index in the shadow */ for (ptr = min_page_addr; ptr <= max_page_addr; ptr += PAGE_SIZE) { int index = ptr_to_shadow(s, ptr); if (index >= 0) { /* Assume a page only contains code for one module */ BUG_ON(s->shadow[index] != SHADOW_INVALID); s->shadow[index] = (u16)check_index; } } } static void remove_module_from_shadow(struct cfi_shadow *s, struct module *mod) { unsigned long ptr; unsigned long min_page_addr; unsigned long max_page_addr; min_page_addr = (unsigned long)mod->core_layout.base & PAGE_MASK; max_page_addr = (unsigned long)mod->core_layout.base + mod->core_layout.text_size; max_page_addr &= PAGE_MASK; for (ptr = min_page_addr; ptr <= max_page_addr; ptr += PAGE_SIZE) { int index = ptr_to_shadow(s, ptr); if (index >= 0) s->shadow[index] = SHADOW_INVALID; } } typedef void (*update_shadow_fn)(struct cfi_shadow *, struct module *); static void update_shadow(struct module *mod, unsigned long min_addr, unsigned long max_addr, update_shadow_fn fn) { struct cfi_shadow *prev; struct cfi_shadow *next = (struct cfi_shadow *) __get_free_pages(GFP_KERNEL, SHADOW_ORDER); BUG_ON(!next); next->r.mod_min_addr = min_addr; next->r.mod_max_addr = max_addr; next->r.min_page = min_addr >> PAGE_SHIFT; next->r.max_page = max_addr >> PAGE_SHIFT; spin_lock(&shadow_update_lock); prev = rcu_dereference_protected(cfi_shadow, 1); prepare_next_shadow(prev, next); fn(next, mod); set_memory_ro((unsigned long)next, SHADOW_PAGES); rcu_assign_pointer(cfi_shadow, next); spin_unlock(&shadow_update_lock); synchronize_rcu(); if (prev) { set_memory_rw((unsigned long)prev, SHADOW_PAGES); free_pages((unsigned long)prev, SHADOW_ORDER); } } void cfi_module_add(struct module *mod, unsigned long min_addr, unsigned long max_addr) { update_shadow(mod, min_addr, max_addr, add_module_to_shadow); } EXPORT_SYMBOL(cfi_module_add); void cfi_module_remove(struct module *mod, unsigned long min_addr, unsigned long max_addr) { update_shadow(mod, min_addr, max_addr, remove_module_from_shadow); } EXPORT_SYMBOL(cfi_module_remove); static inline cfi_check_fn ptr_to_check_fn(const struct cfi_shadow __rcu *s, unsigned long ptr) { int index; unsigned long check; if (unlikely(!s)) return NULL; /* No shadow available */ if (ptr < s->r.mod_min_addr || ptr > s->r.mod_max_addr) return NULL; /* Not in a mapped module */ index = ptr_to_shadow(s, ptr); if (index < 0) return NULL; /* Cannot be addressed with shadow */ return (cfi_check_fn)shadow_to_ptr(s, index); } #endif /* CONFIG_CFI_CLANG_SHADOW */ static inline cfi_check_fn find_module_cfi_check(void *ptr) { struct module *mod; preempt_disable(); mod = __module_address((unsigned long)ptr); preempt_enable(); if (mod) return mod->cfi_check; return CFI_CHECK_FN; } static inline cfi_check_fn find_cfi_check(void *ptr) { #ifdef CONFIG_CFI_CLANG_SHADOW cfi_check_fn f; if (!rcu_access_pointer(cfi_shadow)) return CFI_CHECK_FN; /* No loaded modules */ /* Look up the __cfi_check function to use */ rcu_read_lock(); f = ptr_to_check_fn(rcu_dereference(cfi_shadow), (unsigned long)ptr); rcu_read_unlock(); if (f) return f; /* * Fall back to find_module_cfi_check, which works also for a larger * module address space, but is slower. */ #endif /* CONFIG_CFI_CLANG_SHADOW */ return find_module_cfi_check(ptr); } void cfi_slowpath_handler(uint64_t id, void *ptr, void *diag) { cfi_check_fn check = find_cfi_check(ptr); if (likely(check)) check(id, ptr, diag); else /* Don't allow unchecked modules */ handle_cfi_failure(ptr); } EXPORT_SYMBOL(cfi_slowpath_handler); #endif /* CONFIG_MODULES */ void cfi_failure_handler(void *data, void *ptr, void *vtable) { handle_cfi_failure(ptr); } EXPORT_SYMBOL(cfi_failure_handler); void __cfi_check_fail(void *data, void *ptr) { handle_cfi_failure(ptr); } |