usercopy.c 7.7 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 <asm/sections.h>
  19. enum {
  20. BAD_STACK = -1,
  21. NOT_STACK = 0,
  22. GOOD_FRAME,
  23. GOOD_STACK,
  24. };
  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. static void report_usercopy(const void *ptr, unsigned long len,
  57. bool to_user, const char *type)
  58. {
  59. pr_emerg("kernel memory %s attempt detected %s %p (%s) (%lu bytes)\n",
  60. to_user ? "exposure" : "overwrite",
  61. to_user ? "from" : "to", ptr, type ? : "unknown", len);
  62. /*
  63. * For greater effect, it would be nice to do do_group_exit(),
  64. * but BUG() actually hooks all the lock-breaking and per-arch
  65. * Oops code, so that is used here instead.
  66. */
  67. BUG();
  68. }
  69. /* Returns true if any portion of [ptr,ptr+n) over laps with [low,high). */
  70. static bool overlaps(const void *ptr, unsigned long n, unsigned long low,
  71. unsigned long high)
  72. {
  73. unsigned long check_low = (uintptr_t)ptr;
  74. unsigned long check_high = check_low + n;
  75. /* Does not overlap if entirely above or entirely below. */
  76. if (check_low >= high || check_high <= low)
  77. return false;
  78. return true;
  79. }
  80. /* Is this address range in the kernel text area? */
  81. static inline const char *check_kernel_text_object(const void *ptr,
  82. unsigned long n)
  83. {
  84. unsigned long textlow = (unsigned long)_stext;
  85. unsigned long texthigh = (unsigned long)_etext;
  86. unsigned long textlow_linear, texthigh_linear;
  87. if (overlaps(ptr, n, textlow, texthigh))
  88. return "<kernel text>";
  89. /*
  90. * Some architectures have virtual memory mappings with a secondary
  91. * mapping of the kernel text, i.e. there is more than one virtual
  92. * kernel address that points to the kernel image. It is usually
  93. * when there is a separate linear physical memory mapping, in that
  94. * __pa() is not just the reverse of __va(). This can be detected
  95. * and checked:
  96. */
  97. textlow_linear = (unsigned long)__va(__pa(textlow));
  98. /* No different mapping: we're done. */
  99. if (textlow_linear == textlow)
  100. return NULL;
  101. /* Check the secondary mapping... */
  102. texthigh_linear = (unsigned long)__va(__pa(texthigh));
  103. if (overlaps(ptr, n, textlow_linear, texthigh_linear))
  104. return "<linear kernel text>";
  105. return NULL;
  106. }
  107. static inline const char *check_bogus_address(const void *ptr, unsigned long n)
  108. {
  109. /* Reject if object wraps past end of memory. */
  110. if ((unsigned long)ptr + n < (unsigned long)ptr)
  111. return "<wrapped address>";
  112. /* Reject if NULL or ZERO-allocation. */
  113. if (ZERO_OR_NULL_PTR(ptr))
  114. return "<null>";
  115. return NULL;
  116. }
  117. /* Checks for allocs that are marked in some way as spanning multiple pages. */
  118. static inline const char *check_page_span(const void *ptr, unsigned long n,
  119. struct page *page, bool to_user)
  120. {
  121. #ifdef CONFIG_HARDENED_USERCOPY_PAGESPAN
  122. const void *end = ptr + n - 1;
  123. struct page *endpage;
  124. bool is_reserved, is_cma;
  125. /*
  126. * Sometimes the kernel data regions are not marked Reserved (see
  127. * check below). And sometimes [_sdata,_edata) does not cover
  128. * rodata and/or bss, so check each range explicitly.
  129. */
  130. /* Allow reads of kernel rodata region (if not marked as Reserved). */
  131. if (ptr >= (const void *)__start_rodata &&
  132. end <= (const void *)__end_rodata) {
  133. if (!to_user)
  134. return "<rodata>";
  135. return NULL;
  136. }
  137. /* Allow kernel data region (if not marked as Reserved). */
  138. if (ptr >= (const void *)_sdata && end <= (const void *)_edata)
  139. return NULL;
  140. /* Allow kernel bss region (if not marked as Reserved). */
  141. if (ptr >= (const void *)__bss_start &&
  142. end <= (const void *)__bss_stop)
  143. return NULL;
  144. /* Is the object wholly within one base page? */
  145. if (likely(((unsigned long)ptr & (unsigned long)PAGE_MASK) ==
  146. ((unsigned long)end & (unsigned long)PAGE_MASK)))
  147. return NULL;
  148. /* Allow if fully inside the same compound (__GFP_COMP) page. */
  149. endpage = virt_to_head_page(end);
  150. if (likely(endpage == page))
  151. return NULL;
  152. /*
  153. * Reject if range is entirely either Reserved (i.e. special or
  154. * device memory), or CMA. Otherwise, reject since the object spans
  155. * several independently allocated pages.
  156. */
  157. is_reserved = PageReserved(page);
  158. is_cma = is_migrate_cma_page(page);
  159. if (!is_reserved && !is_cma)
  160. return "<spans multiple pages>";
  161. for (ptr += PAGE_SIZE; ptr <= end; ptr += PAGE_SIZE) {
  162. page = virt_to_head_page(ptr);
  163. if (is_reserved && !PageReserved(page))
  164. return "<spans Reserved and non-Reserved pages>";
  165. if (is_cma && !is_migrate_cma_page(page))
  166. return "<spans CMA and non-CMA pages>";
  167. }
  168. #endif
  169. return NULL;
  170. }
  171. static inline const char *check_heap_object(const void *ptr, unsigned long n,
  172. bool to_user)
  173. {
  174. struct page *page;
  175. /*
  176. * Some architectures (arm64) return true for virt_addr_valid() on
  177. * vmalloced addresses. Work around this by checking for vmalloc
  178. * first.
  179. *
  180. * We also need to check for module addresses explicitly since we
  181. * may copy static data from modules to userspace
  182. */
  183. if (is_vmalloc_or_module_addr(ptr))
  184. return NULL;
  185. if (!virt_addr_valid(ptr))
  186. return NULL;
  187. page = virt_to_head_page(ptr);
  188. /* Check slab allocator for flags and size. */
  189. if (PageSlab(page))
  190. return __check_heap_object(ptr, n, page);
  191. /* Verify object does not incorrectly span multiple pages. */
  192. return check_page_span(ptr, n, page, to_user);
  193. }
  194. /*
  195. * Validates that the given object is:
  196. * - not bogus address
  197. * - known-safe heap or stack object
  198. * - not in kernel text
  199. */
  200. void __check_object_size(const void *ptr, unsigned long n, bool to_user)
  201. {
  202. const char *err;
  203. /* Skip all tests if size is zero. */
  204. if (!n)
  205. return;
  206. /* Check for invalid addresses. */
  207. err = check_bogus_address(ptr, n);
  208. if (err)
  209. goto report;
  210. /* Check for bad heap object. */
  211. err = check_heap_object(ptr, n, to_user);
  212. if (err)
  213. goto report;
  214. /* Check for bad stack object. */
  215. switch (check_stack_object(ptr, n)) {
  216. case NOT_STACK:
  217. /* Object is not touching the current process stack. */
  218. break;
  219. case GOOD_FRAME:
  220. case GOOD_STACK:
  221. /*
  222. * Object is either in the correct frame (when it
  223. * is possible to check) or just generally on the
  224. * process stack (when frame checking not available).
  225. */
  226. return;
  227. default:
  228. err = "<process stack>";
  229. goto report;
  230. }
  231. /* Check for object in kernel to avoid text exposure. */
  232. err = check_kernel_text_object(ptr, n);
  233. if (!err)
  234. return;
  235. report:
  236. report_usercopy(ptr, n, to_user, err);
  237. }
  238. EXPORT_SYMBOL(__check_object_size);