usercopy.c 9.7 KB

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