usercopy.c 7.5 KB

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