usercopy.c 9.6 KB

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