cfi.c 7.9 KB

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