usercopy.c 8.8 KB

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