usercopy.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is for all the tests related to copy_to_user() and copy_from_user()
  4. * hardening.
  5. */
  6. #include "lkdtm.h"
  7. #include <linux/slab.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/mman.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/cacheflush.h>
  13. /*
  14. * Many of the tests here end up using const sizes, but those would
  15. * normally be ignored by hardened usercopy, so force the compiler
  16. * into choosing the non-const path to make sure we trigger the
  17. * hardened usercopy checks by added "unconst" to all the const copies,
  18. * and making sure "cache_size" isn't optimized into a const.
  19. */
  20. static volatile size_t unconst = 0;
  21. static volatile size_t cache_size = 1024;
  22. static struct kmem_cache *whitelist_cache;
  23. static const unsigned char test_text[] = "This is a test.\n";
  24. /*
  25. * Instead of adding -Wno-return-local-addr, just pass the stack address
  26. * through a function to obfuscate it from the compiler.
  27. */
  28. static noinline unsigned char *trick_compiler(unsigned char *stack)
  29. {
  30. return stack + 0;
  31. }
  32. static noinline unsigned char *do_usercopy_stack_callee(int value)
  33. {
  34. unsigned char buf[32];
  35. int i;
  36. /* Exercise stack to avoid everything living in registers. */
  37. for (i = 0; i < sizeof(buf); i++) {
  38. buf[i] = value & 0xff;
  39. }
  40. return trick_compiler(buf);
  41. }
  42. static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
  43. {
  44. unsigned long user_addr;
  45. unsigned char good_stack[32];
  46. unsigned char *bad_stack;
  47. int i;
  48. /* Exercise stack to avoid everything living in registers. */
  49. for (i = 0; i < sizeof(good_stack); i++)
  50. good_stack[i] = test_text[i % sizeof(test_text)];
  51. /* This is a pointer to outside our current stack frame. */
  52. if (bad_frame) {
  53. bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
  54. } else {
  55. /* Put start address just inside stack. */
  56. bad_stack = task_stack_page(current) + THREAD_SIZE;
  57. bad_stack -= sizeof(unsigned long);
  58. }
  59. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  60. PROT_READ | PROT_WRITE | PROT_EXEC,
  61. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  62. if (user_addr >= TASK_SIZE) {
  63. pr_warn("Failed to allocate user memory\n");
  64. return;
  65. }
  66. if (to_user) {
  67. pr_info("attempting good copy_to_user of local stack\n");
  68. if (copy_to_user((void __user *)user_addr, good_stack,
  69. unconst + sizeof(good_stack))) {
  70. pr_warn("copy_to_user failed unexpectedly?!\n");
  71. goto free_user;
  72. }
  73. pr_info("attempting bad copy_to_user of distant stack\n");
  74. if (copy_to_user((void __user *)user_addr, bad_stack,
  75. unconst + sizeof(good_stack))) {
  76. pr_warn("copy_to_user failed, but lacked Oops\n");
  77. goto free_user;
  78. }
  79. } else {
  80. /*
  81. * There isn't a safe way to not be protected by usercopy
  82. * if we're going to write to another thread's stack.
  83. */
  84. if (!bad_frame)
  85. goto free_user;
  86. pr_info("attempting good copy_from_user of local stack\n");
  87. if (copy_from_user(good_stack, (void __user *)user_addr,
  88. unconst + sizeof(good_stack))) {
  89. pr_warn("copy_from_user failed unexpectedly?!\n");
  90. goto free_user;
  91. }
  92. pr_info("attempting bad copy_from_user of distant stack\n");
  93. if (copy_from_user(bad_stack, (void __user *)user_addr,
  94. unconst + sizeof(good_stack))) {
  95. pr_warn("copy_from_user failed, but lacked Oops\n");
  96. goto free_user;
  97. }
  98. }
  99. free_user:
  100. vm_munmap(user_addr, PAGE_SIZE);
  101. }
  102. /*
  103. * This checks for whole-object size validation with hardened usercopy,
  104. * with or without usercopy whitelisting.
  105. */
  106. static void do_usercopy_heap_size(bool to_user)
  107. {
  108. unsigned long user_addr;
  109. unsigned char *one, *two;
  110. void __user *test_user_addr;
  111. void *test_kern_addr;
  112. size_t size = unconst + 1024;
  113. one = kmalloc(size, GFP_KERNEL);
  114. two = kmalloc(size, GFP_KERNEL);
  115. if (!one || !two) {
  116. pr_warn("Failed to allocate kernel memory\n");
  117. goto free_kernel;
  118. }
  119. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  120. PROT_READ | PROT_WRITE | PROT_EXEC,
  121. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  122. if (user_addr >= TASK_SIZE) {
  123. pr_warn("Failed to allocate user memory\n");
  124. goto free_kernel;
  125. }
  126. memset(one, 'A', size);
  127. memset(two, 'B', size);
  128. test_user_addr = (void __user *)(user_addr + 16);
  129. test_kern_addr = one + 16;
  130. if (to_user) {
  131. pr_info("attempting good copy_to_user of correct size\n");
  132. if (copy_to_user(test_user_addr, test_kern_addr, size / 2)) {
  133. pr_warn("copy_to_user failed unexpectedly?!\n");
  134. goto free_user;
  135. }
  136. pr_info("attempting bad copy_to_user of too large size\n");
  137. if (copy_to_user(test_user_addr, test_kern_addr, size)) {
  138. pr_warn("copy_to_user failed, but lacked Oops\n");
  139. goto free_user;
  140. }
  141. } else {
  142. pr_info("attempting good copy_from_user of correct size\n");
  143. if (copy_from_user(test_kern_addr, test_user_addr, size / 2)) {
  144. pr_warn("copy_from_user failed unexpectedly?!\n");
  145. goto free_user;
  146. }
  147. pr_info("attempting bad copy_from_user of too large size\n");
  148. if (copy_from_user(test_kern_addr, test_user_addr, size)) {
  149. pr_warn("copy_from_user failed, but lacked Oops\n");
  150. goto free_user;
  151. }
  152. }
  153. free_user:
  154. vm_munmap(user_addr, PAGE_SIZE);
  155. free_kernel:
  156. kfree(one);
  157. kfree(two);
  158. }
  159. /*
  160. * This checks for the specific whitelist window within an object. If this
  161. * test passes, then do_usercopy_heap_size() tests will pass too.
  162. */
  163. static void do_usercopy_heap_whitelist(bool to_user)
  164. {
  165. unsigned long user_alloc;
  166. unsigned char *buf = NULL;
  167. unsigned char __user *user_addr;
  168. size_t offset, size;
  169. /* Make sure cache was prepared. */
  170. if (!whitelist_cache) {
  171. pr_warn("Failed to allocate kernel cache\n");
  172. return;
  173. }
  174. /*
  175. * Allocate a buffer with a whitelisted window in the buffer.
  176. */
  177. buf = kmem_cache_alloc(whitelist_cache, GFP_KERNEL);
  178. if (!buf) {
  179. pr_warn("Failed to allocate buffer from whitelist cache\n");
  180. goto free_alloc;
  181. }
  182. /* Allocate user memory we'll poke at. */
  183. user_alloc = vm_mmap(NULL, 0, PAGE_SIZE,
  184. PROT_READ | PROT_WRITE | PROT_EXEC,
  185. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  186. if (user_alloc >= TASK_SIZE) {
  187. pr_warn("Failed to allocate user memory\n");
  188. goto free_alloc;
  189. }
  190. user_addr = (void __user *)user_alloc;
  191. memset(buf, 'B', cache_size);
  192. /* Whitelisted window in buffer, from kmem_cache_create_usercopy. */
  193. offset = (cache_size / 4) + unconst;
  194. size = (cache_size / 16) + unconst;
  195. if (to_user) {
  196. pr_info("attempting good copy_to_user inside whitelist\n");
  197. if (copy_to_user(user_addr, buf + offset, size)) {
  198. pr_warn("copy_to_user failed unexpectedly?!\n");
  199. goto free_user;
  200. }
  201. pr_info("attempting bad copy_to_user outside whitelist\n");
  202. if (copy_to_user(user_addr, buf + offset - 1, size)) {
  203. pr_warn("copy_to_user failed, but lacked Oops\n");
  204. goto free_user;
  205. }
  206. } else {
  207. pr_info("attempting good copy_from_user inside whitelist\n");
  208. if (copy_from_user(buf + offset, user_addr, size)) {
  209. pr_warn("copy_from_user failed unexpectedly?!\n");
  210. goto free_user;
  211. }
  212. pr_info("attempting bad copy_from_user outside whitelist\n");
  213. if (copy_from_user(buf + offset - 1, user_addr, size)) {
  214. pr_warn("copy_from_user failed, but lacked Oops\n");
  215. goto free_user;
  216. }
  217. }
  218. free_user:
  219. vm_munmap(user_alloc, PAGE_SIZE);
  220. free_alloc:
  221. if (buf)
  222. kmem_cache_free(whitelist_cache, buf);
  223. }
  224. /* Callable tests. */
  225. void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
  226. {
  227. do_usercopy_heap_size(true);
  228. }
  229. void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
  230. {
  231. do_usercopy_heap_size(false);
  232. }
  233. void lkdtm_USERCOPY_HEAP_WHITELIST_TO(void)
  234. {
  235. do_usercopy_heap_whitelist(true);
  236. }
  237. void lkdtm_USERCOPY_HEAP_WHITELIST_FROM(void)
  238. {
  239. do_usercopy_heap_whitelist(false);
  240. }
  241. void lkdtm_USERCOPY_STACK_FRAME_TO(void)
  242. {
  243. do_usercopy_stack(true, true);
  244. }
  245. void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
  246. {
  247. do_usercopy_stack(false, true);
  248. }
  249. void lkdtm_USERCOPY_STACK_BEYOND(void)
  250. {
  251. do_usercopy_stack(true, false);
  252. }
  253. void lkdtm_USERCOPY_KERNEL(void)
  254. {
  255. unsigned long user_addr;
  256. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  257. PROT_READ | PROT_WRITE | PROT_EXEC,
  258. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  259. if (user_addr >= TASK_SIZE) {
  260. pr_warn("Failed to allocate user memory\n");
  261. return;
  262. }
  263. pr_info("attempting good copy_to_user from kernel rodata\n");
  264. if (copy_to_user((void __user *)user_addr, test_text,
  265. unconst + sizeof(test_text))) {
  266. pr_warn("copy_to_user failed unexpectedly?!\n");
  267. goto free_user;
  268. }
  269. pr_info("attempting bad copy_to_user from kernel text\n");
  270. if (copy_to_user((void __user *)user_addr, vm_mmap,
  271. unconst + PAGE_SIZE)) {
  272. pr_warn("copy_to_user failed, but lacked Oops\n");
  273. goto free_user;
  274. }
  275. free_user:
  276. vm_munmap(user_addr, PAGE_SIZE);
  277. }
  278. void __init lkdtm_usercopy_init(void)
  279. {
  280. /* Prepare cache that lacks SLAB_USERCOPY flag. */
  281. whitelist_cache =
  282. kmem_cache_create_usercopy("lkdtm-usercopy", cache_size,
  283. 0, 0,
  284. cache_size / 4,
  285. cache_size / 16,
  286. NULL);
  287. }
  288. void __exit lkdtm_usercopy_exit(void)
  289. {
  290. kmem_cache_destroy(whitelist_cache);
  291. }