usercopy.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * This implements the various checks for CONFIG_HARDENED_USERCOPY*,
  3. * which are designed to protect kernel memory from needless exposure
  4. * and overwrite under many unintended conditions. This code is based
  5. * on PAX_USERCOPY, which is:
  6. *
  7. * Copyright (C) 2001-2016 PaX Team, Bradley Spengler, Open Source
  8. * Security Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/mm.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/thread_info.h>
  22. #include <linux/atomic.h>
  23. #include <linux/jump_label.h>
  24. #include <asm/sections.h>
  25. /*
  26. * Checks if a given pointer and length is contained by the current
  27. * stack frame (if possible).
  28. *
  29. * Returns:
  30. * NOT_STACK: not at all on the stack
  31. * GOOD_FRAME: fully within a valid stack frame
  32. * GOOD_STACK: fully on the stack (when can't do frame-checking)
  33. * BAD_STACK: error condition (invalid stack position or bad stack frame)
  34. */
  35. static noinline int check_stack_object(const void *obj, unsigned long len)
  36. {
  37. const void * const stack = task_stack_page(current);
  38. const void * const stackend = stack + THREAD_SIZE;
  39. int ret;
  40. /* Object is not on the stack at all. */
  41. if (obj + len <= stack || stackend <= obj)
  42. return NOT_STACK;
  43. /*
  44. * Reject: object partially overlaps the stack (passing the
  45. * the check above means at least one end is within the stack,
  46. * so if this check fails, the other end is outside the stack).
  47. */
  48. if (obj < stack || stackend < obj + len)
  49. return BAD_STACK;
  50. /* Check if object is safely within a valid frame. */
  51. ret = arch_within_stack_frames(stack, stackend, obj, len);
  52. if (ret)
  53. return ret;
  54. return GOOD_STACK;
  55. }
  56. /*
  57. * If these functions are reached, then CONFIG_HARDENED_USERCOPY has found
  58. * an unexpected state during a copy_from_user() or copy_to_user() call.
  59. * There are several checks being performed on the buffer by the
  60. * __check_object_size() function. Normal stack buffer usage should never
  61. * trip the checks, and kernel text addressing will always trip the check.
  62. * For cache objects, it is checking that only the whitelisted range of
  63. * bytes for a given cache is being accessed (via the cache's usersize and
  64. * useroffset fields). To adjust a cache whitelist, use the usercopy-aware
  65. * kmem_cache_create_usercopy() function to create the cache (and
  66. * carefully audit the whitelist range).
  67. */
  68. void usercopy_warn(const char *name, const char *detail, bool to_user,
  69. unsigned long offset, unsigned long len)
  70. {
  71. WARN_ONCE(1, "Bad or missing usercopy whitelist? Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n",
  72. to_user ? "exposure" : "overwrite",
  73. to_user ? "from" : "to",
  74. name ? : "unknown?!",
  75. detail ? " '" : "", detail ? : "", detail ? "'" : "",
  76. offset, len);
  77. }
  78. void __noreturn usercopy_abort(const char *name, const char *detail,
  79. bool to_user, unsigned long offset,
  80. unsigned long len)
  81. {
  82. pr_emerg("Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n",
  83. to_user ? "exposure" : "overwrite",
  84. to_user ? "from" : "to",
  85. name ? : "unknown?!",
  86. detail ? " '" : "", detail ? : "", detail ? "'" : "",
  87. offset, len);
  88. /*
  89. * For greater effect, it would be nice to do do_group_exit(),
  90. * but BUG() actually hooks all the lock-breaking and per-arch
  91. * Oops code, so that is used here instead.
  92. */
  93. BUG();
  94. }
  95. /* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
  96. static bool overlaps(const unsigned long ptr, unsigned long n,
  97. unsigned long low, unsigned long high)
  98. {
  99. const unsigned long check_low = ptr;
  100. unsigned long check_high = check_low + n;
  101. /* Does not overlap if entirely above or entirely below. */
  102. if (check_low >= high || check_high <= low)
  103. return false;
  104. return true;
  105. }
  106. /* Is this address range in the kernel text area? */
  107. static inline void check_kernel_text_object(const unsigned long ptr,
  108. unsigned long n, bool to_user)
  109. {
  110. unsigned long textlow = (unsigned long)_stext;
  111. unsigned long texthigh = (unsigned long)_etext;
  112. unsigned long textlow_linear, texthigh_linear;
  113. if (overlaps(ptr, n, textlow, texthigh))
  114. usercopy_abort("kernel text", NULL, to_user, ptr - textlow, n);
  115. /*
  116. * Some architectures have virtual memory mappings with a secondary
  117. * mapping of the kernel text, i.e. there is more than one virtual
  118. * kernel address that points to the kernel image. It is usually
  119. * when there is a separate linear physical memory mapping, in that
  120. * __pa() is not just the reverse of __va(). This can be detected
  121. * and checked:
  122. */
  123. textlow_linear = (unsigned long)lm_alias(textlow);
  124. /* No different mapping: we're done. */
  125. if (textlow_linear == textlow)
  126. return;
  127. /* Check the secondary mapping... */
  128. texthigh_linear = (unsigned long)lm_alias(texthigh);
  129. if (overlaps(ptr, n, textlow_linear, texthigh_linear))
  130. usercopy_abort("linear kernel text", NULL, to_user,
  131. ptr - textlow_linear, n);
  132. }
  133. static inline void check_bogus_address(const unsigned long ptr, unsigned long n,
  134. bool to_user)
  135. {
  136. /* Reject if object wraps past end of memory. */
  137. if (ptr + n < ptr)
  138. usercopy_abort("wrapped address", NULL, to_user, 0, ptr + n);
  139. /* Reject if NULL or ZERO-allocation. */
  140. if (ZERO_OR_NULL_PTR(ptr))
  141. usercopy_abort("null address", NULL, to_user, ptr, n);
  142. }
  143. /* Checks for allocs that are marked in some way as spanning multiple pages. */
  144. static inline void check_page_span(const void *ptr, unsigned long n,
  145. struct page *page, bool to_user)
  146. {
  147. #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
  148. const void *end = ptr + n - 1;
  149. struct page *endpage;
  150. bool is_reserved, is_cma;
  151. /*
  152. * Sometimes the kernel data regions are not marked Reserved (see
  153. * check below). And sometimes [_sdata,_edata) does not cover
  154. * rodata and/or bss, so check each range explicitly.
  155. */
  156. /* Allow reads of kernel rodata region (if not marked as Reserved). */
  157. if (ptr >= (const void *)__start_rodata &&
  158. end <= (const void *)__end_rodata) {
  159. if (!to_user)
  160. usercopy_abort("rodata", NULL, to_user, 0, n);
  161. return;
  162. }
  163. /* Allow kernel data region (if not marked as Reserved). */
  164. if (ptr >= (const void *)_sdata && end <= (const void *)_edata)
  165. return;
  166. /* Allow kernel bss region (if not marked as Reserved). */
  167. if (ptr >= (const void *)__bss_start &&
  168. end <= (const void *)__bss_stop)
  169. return;
  170. /* Is the object wholly within one base page? */
  171. if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) ==
  172. ((unsigned long)end & (unsigned long)PAGE_MASK)))
  173. return;
  174. /* Allow if fully inside the same compound (__GFP_COMP) page. */
  175. endpage = virt_to_head_page(end);
  176. if (likely(endpage == page))
  177. return;
  178. /*
  179. * Reject if range is entirely either Reserved (i.e. special or
  180. * device memory), or CMA. Otherwise, reject since the object spans
  181. * several independently allocated pages.
  182. */
  183. is_reserved = PageReserved(page);
  184. is_cma = is_migrate_cma_page(page);
  185. if (!is_reserved && !is_cma)
  186. usercopy_abort("spans multiple pages", NULL, to_user, 0, n);
  187. for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) {
  188. page = virt_to_head_page(ptr);
  189. if (is_reserved && !PageReserved(page))
  190. usercopy_abort("spans Reserved and non-Reserved pages",
  191. NULL, to_user, 0, n);
  192. if (is_cma && !is_migrate_cma_page(page))
  193. usercopy_abort("spans CMA and non-CMA pages", NULL,
  194. to_user, 0, n);
  195. }
  196. #endif
  197. }
  198. static inline void check_heap_object(const void *ptr, unsigned long n,
  199. bool to_user)
  200. {
  201. struct page *page;
  202. if (!virt_addr_valid(ptr))
  203. return;
  204. page = virt_to_head_page(ptr);
  205. if (PageSlab(page)) {
  206. /* Check slab allocator for flags and size. */
  207. __check_heap_object(ptr, n, page, to_user);
  208. } else {
  209. /* Verify object does not incorrectly span multiple pages. */
  210. check_page_span(ptr, n, page, to_user);
  211. }
  212. }
  213. static DEFINE_STATIC_KEY_FALSE_RO(bypass_usercopy_checks);
  214. /*
  215. * Validates that the given object is:
  216. * - not bogus address
  217. * - known-safe heap or stack object
  218. * - not in kernel text
  219. */
  220. void __check_object_size(const void *ptr, unsigned long n, bool to_user)
  221. {
  222. if (static_branch_unlikely(&bypass_usercopy_checks))
  223. return;
  224. /* Skip all tests if size is zero. */
  225. if (!n)
  226. return;
  227. /* Check for invalid addresses. */
  228. check_bogus_address((const unsigned long)ptr, n, to_user);
  229. /* Check for bad heap object. */
  230. check_heap_object(ptr, n, to_user);
  231. /* Check for bad stack object. */
  232. switch (check_stack_object(ptr, n)) {
  233. case NOT_STACK:
  234. /* Object is not touching the current process stack. */
  235. break;
  236. case GOOD_FRAME:
  237. case GOOD_STACK:
  238. /*
  239. * Object is either in the correct frame (when it
  240. * is possible to check) or just generally on the
  241. * process stack (when frame checking not available).
  242. */
  243. return;
  244. default:
  245. usercopy_abort("process stack", NULL, to_user, 0, n);
  246. }
  247. /* Check for object in kernel to avoid text exposure. */
  248. check_kernel_text_object((const unsigned long)ptr, n, to_user);
  249. }
  250. EXPORT_SYMBOL(__check_object_size);
  251. static bool enable_checks __initdata = true;
  252. static int __init parse_hardened_usercopy(char *str)
  253. {
  254. return strtobool(str, &enable_checks);
  255. }
  256. __setup("hardened_usercopy=", parse_hardened_usercopy);
  257. static int __init set_hardened_usercopy(void)
  258. {
  259. if (enable_checks == false)
  260. static_branch_enable(&bypass_usercopy_checks);
  261. return 1;
  262. }
  263. late_initcall(set_hardened_usercopy);