0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/hardirq.h>
0009 #include <linux/kallsyms.h>
0010 #include <linux/module.h>
0011 #include <linux/mutex.h>
0012 #include <linux/printk.h>
0013 #include <linux/ratelimit.h>
0014 #include <linux/rcupdate.h>
0015 #include <linux/vmalloc.h>
0016 #include <asm/cacheflush.h>
0017 #include <asm/set_memory.h>
0018
0019
0020 #ifdef CONFIG_CFI_PERMISSIVE
0021 #define cfi_failure_handler __ubsan_handle_cfi_check_fail
0022 #else
0023 #define cfi_failure_handler __ubsan_handle_cfi_check_fail_abort
0024 #endif
0025
0026 static inline void handle_cfi_failure(void *ptr)
0027 {
0028 if (IS_ENABLED(CONFIG_CFI_PERMISSIVE))
0029 WARN_RATELIMIT(1, "CFI failure (target: %pS):\n", ptr);
0030 else
0031 panic("CFI failure (target: %pS)\n", ptr);
0032 }
0033
0034 #ifdef CONFIG_MODULES
0035 #ifdef CONFIG_CFI_CLANG_SHADOW
0036
0037
0038
0039
0040 typedef u16 shadow_t;
0041 #define SHADOW_INVALID ((shadow_t)~0UL)
0042
0043 struct cfi_shadow {
0044
0045 unsigned long base;
0046
0047 shadow_t shadow[1];
0048 } __packed;
0049
0050
0051
0052
0053
0054 #define __SHADOW_RANGE (_UL(SZ_128M) >> PAGE_SHIFT)
0055
0056
0057 #define __SHADOW_PAGES ((__SHADOW_RANGE * sizeof(shadow_t)) >> PAGE_SHIFT)
0058 #define SHADOW_PAGES max(1UL, __SHADOW_PAGES)
0059 #define SHADOW_SIZE (SHADOW_PAGES << PAGE_SHIFT)
0060
0061
0062 #define SHADOW_ARR_SIZE (SHADOW_SIZE - offsetof(struct cfi_shadow, shadow))
0063 #define SHADOW_ARR_SLOTS (SHADOW_ARR_SIZE / sizeof(shadow_t))
0064
0065 static DEFINE_MUTEX(shadow_update_lock);
0066 static struct cfi_shadow __rcu *cfi_shadow __read_mostly;
0067
0068
0069 static inline int ptr_to_shadow(const struct cfi_shadow *s, unsigned long ptr)
0070 {
0071 unsigned long index;
0072 unsigned long page = ptr >> PAGE_SHIFT;
0073
0074 if (unlikely(page < s->base))
0075 return -1;
0076
0077 index = page - s->base;
0078
0079 if (index >= SHADOW_ARR_SLOTS)
0080 return -1;
0081
0082 return (int)index;
0083 }
0084
0085
0086 static inline unsigned long shadow_to_ptr(const struct cfi_shadow *s,
0087 int index)
0088 {
0089 if (unlikely(index < 0 || index >= SHADOW_ARR_SLOTS))
0090 return 0;
0091
0092 return (s->base + index) << PAGE_SHIFT;
0093 }
0094
0095
0096 static inline unsigned long shadow_to_check_fn(const struct cfi_shadow *s,
0097 int index)
0098 {
0099 if (unlikely(index < 0 || index >= SHADOW_ARR_SLOTS))
0100 return 0;
0101
0102 if (unlikely(s->shadow[index] == SHADOW_INVALID))
0103 return 0;
0104
0105
0106 return (s->base + s->shadow[index]) << PAGE_SHIFT;
0107 }
0108
0109 static void prepare_next_shadow(const struct cfi_shadow __rcu *prev,
0110 struct cfi_shadow *next)
0111 {
0112 int i, index, check;
0113
0114
0115 memset(next->shadow, 0xFF, SHADOW_ARR_SIZE);
0116
0117 if (!prev)
0118 return;
0119
0120
0121 if (prev->base == next->base) {
0122 memcpy(next->shadow, prev->shadow, SHADOW_ARR_SIZE);
0123 return;
0124 }
0125
0126
0127 for (i = 0; i < SHADOW_ARR_SLOTS; ++i) {
0128 if (prev->shadow[i] == SHADOW_INVALID)
0129 continue;
0130
0131 index = ptr_to_shadow(next, shadow_to_ptr(prev, i));
0132 if (index < 0)
0133 continue;
0134
0135 check = ptr_to_shadow(next,
0136 shadow_to_check_fn(prev, prev->shadow[i]));
0137 if (check < 0)
0138 continue;
0139
0140 next->shadow[index] = (shadow_t)check;
0141 }
0142 }
0143
0144 static void add_module_to_shadow(struct cfi_shadow *s, struct module *mod,
0145 unsigned long min_addr, unsigned long max_addr)
0146 {
0147 int check_index;
0148 unsigned long check = (unsigned long)mod->cfi_check;
0149 unsigned long ptr;
0150
0151 if (unlikely(!PAGE_ALIGNED(check))) {
0152 pr_warn("cfi: not using shadow for module %s\n", mod->name);
0153 return;
0154 }
0155
0156 check_index = ptr_to_shadow(s, check);
0157 if (check_index < 0)
0158 return;
0159
0160
0161 for (ptr = min_addr; ptr <= max_addr; ptr += PAGE_SIZE) {
0162 int index = ptr_to_shadow(s, ptr);
0163
0164 if (index >= 0) {
0165
0166 WARN_ON_ONCE(s->shadow[index] != SHADOW_INVALID);
0167 s->shadow[index] = (shadow_t)check_index;
0168 }
0169 }
0170 }
0171
0172 static void remove_module_from_shadow(struct cfi_shadow *s, struct module *mod,
0173 unsigned long min_addr, unsigned long max_addr)
0174 {
0175 unsigned long ptr;
0176
0177 for (ptr = min_addr; ptr <= max_addr; ptr += PAGE_SIZE) {
0178 int index = ptr_to_shadow(s, ptr);
0179
0180 if (index >= 0)
0181 s->shadow[index] = SHADOW_INVALID;
0182 }
0183 }
0184
0185 typedef void (*update_shadow_fn)(struct cfi_shadow *, struct module *,
0186 unsigned long min_addr, unsigned long max_addr);
0187
0188 static void update_shadow(struct module *mod, unsigned long base_addr,
0189 update_shadow_fn fn)
0190 {
0191 struct cfi_shadow *prev;
0192 struct cfi_shadow *next;
0193 unsigned long min_addr, max_addr;
0194
0195 next = vmalloc(SHADOW_SIZE);
0196
0197 mutex_lock(&shadow_update_lock);
0198 prev = rcu_dereference_protected(cfi_shadow,
0199 mutex_is_locked(&shadow_update_lock));
0200
0201 if (next) {
0202 next->base = base_addr >> PAGE_SHIFT;
0203 prepare_next_shadow(prev, next);
0204
0205 min_addr = (unsigned long)mod->core_layout.base;
0206 max_addr = min_addr + mod->core_layout.text_size;
0207 fn(next, mod, min_addr & PAGE_MASK, max_addr & PAGE_MASK);
0208
0209 set_memory_ro((unsigned long)next, SHADOW_PAGES);
0210 }
0211
0212 rcu_assign_pointer(cfi_shadow, next);
0213 mutex_unlock(&shadow_update_lock);
0214 synchronize_rcu();
0215
0216 if (prev) {
0217 set_memory_rw((unsigned long)prev, SHADOW_PAGES);
0218 vfree(prev);
0219 }
0220 }
0221
0222 void cfi_module_add(struct module *mod, unsigned long base_addr)
0223 {
0224 update_shadow(mod, base_addr, add_module_to_shadow);
0225 }
0226
0227 void cfi_module_remove(struct module *mod, unsigned long base_addr)
0228 {
0229 update_shadow(mod, base_addr, remove_module_from_shadow);
0230 }
0231
0232 static inline cfi_check_fn ptr_to_check_fn(const struct cfi_shadow __rcu *s,
0233 unsigned long ptr)
0234 {
0235 int index;
0236
0237 if (unlikely(!s))
0238 return NULL;
0239
0240 index = ptr_to_shadow(s, ptr);
0241 if (index < 0)
0242 return NULL;
0243
0244 return (cfi_check_fn)shadow_to_check_fn(s, index);
0245 }
0246
0247 static inline cfi_check_fn find_shadow_check_fn(unsigned long ptr)
0248 {
0249 cfi_check_fn fn;
0250
0251 rcu_read_lock_sched_notrace();
0252 fn = ptr_to_check_fn(rcu_dereference_sched(cfi_shadow), ptr);
0253 rcu_read_unlock_sched_notrace();
0254
0255 return fn;
0256 }
0257
0258 #else
0259
0260 static inline cfi_check_fn find_shadow_check_fn(unsigned long ptr)
0261 {
0262 return NULL;
0263 }
0264
0265 #endif
0266
0267 static inline cfi_check_fn find_module_check_fn(unsigned long ptr)
0268 {
0269 cfi_check_fn fn = NULL;
0270 struct module *mod;
0271
0272 rcu_read_lock_sched_notrace();
0273 mod = __module_address(ptr);
0274 if (mod)
0275 fn = mod->cfi_check;
0276 rcu_read_unlock_sched_notrace();
0277
0278 return fn;
0279 }
0280
0281 static inline cfi_check_fn find_check_fn(unsigned long ptr)
0282 {
0283 cfi_check_fn fn = NULL;
0284 unsigned long flags;
0285 bool rcu_idle;
0286
0287 if (is_kernel_text(ptr))
0288 return __cfi_check;
0289
0290
0291
0292
0293
0294
0295 rcu_idle = !rcu_is_watching();
0296 if (rcu_idle) {
0297 local_irq_save(flags);
0298 ct_irq_enter();
0299 }
0300
0301 if (IS_ENABLED(CONFIG_CFI_CLANG_SHADOW))
0302 fn = find_shadow_check_fn(ptr);
0303 if (!fn)
0304 fn = find_module_check_fn(ptr);
0305
0306 if (rcu_idle) {
0307 ct_irq_exit();
0308 local_irq_restore(flags);
0309 }
0310
0311 return fn;
0312 }
0313
0314 void __cfi_slowpath_diag(uint64_t id, void *ptr, void *diag)
0315 {
0316 cfi_check_fn fn = find_check_fn((unsigned long)ptr);
0317
0318 if (likely(fn))
0319 fn(id, ptr, diag);
0320 else
0321 handle_cfi_failure(ptr);
0322 }
0323 EXPORT_SYMBOL(__cfi_slowpath_diag);
0324
0325 #else
0326
0327 void __cfi_slowpath_diag(uint64_t id, void *ptr, void *diag)
0328 {
0329 handle_cfi_failure(ptr);
0330 }
0331 EXPORT_SYMBOL(__cfi_slowpath_diag);
0332
0333 #endif
0334
0335 void cfi_failure_handler(void *data, void *ptr, void *vtable)
0336 {
0337 handle_cfi_failure(ptr);
0338 }
0339 EXPORT_SYMBOL(cfi_failure_handler);