lkdtm_usercopy.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 *bad_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. static void do_usercopy_heap_size(bool to_user)
  103. {
  104. unsigned long user_addr;
  105. unsigned char *one, *two;
  106. size_t size = unconst + 1024;
  107. one = kmalloc(size, GFP_KERNEL);
  108. two = kmalloc(size, GFP_KERNEL);
  109. if (!one || !two) {
  110. pr_warn("Failed to allocate kernel memory\n");
  111. goto free_kernel;
  112. }
  113. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  114. PROT_READ | PROT_WRITE | PROT_EXEC,
  115. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  116. if (user_addr >= TASK_SIZE) {
  117. pr_warn("Failed to allocate user memory\n");
  118. goto free_kernel;
  119. }
  120. memset(one, 'A', size);
  121. memset(two, 'B', size);
  122. if (to_user) {
  123. pr_info("attempting good copy_to_user of correct size\n");
  124. if (copy_to_user((void __user *)user_addr, one, size)) {
  125. pr_warn("copy_to_user failed unexpectedly?!\n");
  126. goto free_user;
  127. }
  128. pr_info("attempting bad copy_to_user of too large size\n");
  129. if (copy_to_user((void __user *)user_addr, one, 2 * size)) {
  130. pr_warn("copy_to_user failed, but lacked Oops\n");
  131. goto free_user;
  132. }
  133. } else {
  134. pr_info("attempting good copy_from_user of correct size\n");
  135. if (copy_from_user(one, (void __user *)user_addr, size)) {
  136. pr_warn("copy_from_user failed unexpectedly?!\n");
  137. goto free_user;
  138. }
  139. pr_info("attempting bad copy_from_user of too large size\n");
  140. if (copy_from_user(one, (void __user *)user_addr, 2 * size)) {
  141. pr_warn("copy_from_user failed, but lacked Oops\n");
  142. goto free_user;
  143. }
  144. }
  145. free_user:
  146. vm_munmap(user_addr, PAGE_SIZE);
  147. free_kernel:
  148. kfree(one);
  149. kfree(two);
  150. }
  151. static void do_usercopy_heap_flag(bool to_user)
  152. {
  153. unsigned long user_addr;
  154. unsigned char *good_buf = NULL;
  155. unsigned char *bad_buf = NULL;
  156. /* Make sure cache was prepared. */
  157. if (!bad_cache) {
  158. pr_warn("Failed to allocate kernel cache\n");
  159. return;
  160. }
  161. /*
  162. * Allocate one buffer from each cache (kmalloc will have the
  163. * SLAB_USERCOPY flag already, but "bad_cache" won't).
  164. */
  165. good_buf = kmalloc(cache_size, GFP_KERNEL);
  166. bad_buf = kmem_cache_alloc(bad_cache, GFP_KERNEL);
  167. if (!good_buf || !bad_buf) {
  168. pr_warn("Failed to allocate buffers from caches\n");
  169. goto free_alloc;
  170. }
  171. /* Allocate user memory we'll poke at. */
  172. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  173. PROT_READ | PROT_WRITE | PROT_EXEC,
  174. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  175. if (user_addr >= TASK_SIZE) {
  176. pr_warn("Failed to allocate user memory\n");
  177. goto free_alloc;
  178. }
  179. memset(good_buf, 'A', cache_size);
  180. memset(bad_buf, 'B', cache_size);
  181. if (to_user) {
  182. pr_info("attempting good copy_to_user with SLAB_USERCOPY\n");
  183. if (copy_to_user((void __user *)user_addr, good_buf,
  184. cache_size)) {
  185. pr_warn("copy_to_user failed unexpectedly?!\n");
  186. goto free_user;
  187. }
  188. pr_info("attempting bad copy_to_user w/o SLAB_USERCOPY\n");
  189. if (copy_to_user((void __user *)user_addr, bad_buf,
  190. cache_size)) {
  191. pr_warn("copy_to_user failed, but lacked Oops\n");
  192. goto free_user;
  193. }
  194. } else {
  195. pr_info("attempting good copy_from_user with SLAB_USERCOPY\n");
  196. if (copy_from_user(good_buf, (void __user *)user_addr,
  197. cache_size)) {
  198. pr_warn("copy_from_user failed unexpectedly?!\n");
  199. goto free_user;
  200. }
  201. pr_info("attempting bad copy_from_user w/o SLAB_USERCOPY\n");
  202. if (copy_from_user(bad_buf, (void __user *)user_addr,
  203. cache_size)) {
  204. pr_warn("copy_from_user failed, but lacked Oops\n");
  205. goto free_user;
  206. }
  207. }
  208. free_user:
  209. vm_munmap(user_addr, PAGE_SIZE);
  210. free_alloc:
  211. if (bad_buf)
  212. kmem_cache_free(bad_cache, bad_buf);
  213. kfree(good_buf);
  214. }
  215. /* Callable tests. */
  216. void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
  217. {
  218. do_usercopy_heap_size(true);
  219. }
  220. void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
  221. {
  222. do_usercopy_heap_size(false);
  223. }
  224. void lkdtm_USERCOPY_HEAP_FLAG_TO(void)
  225. {
  226. do_usercopy_heap_flag(true);
  227. }
  228. void lkdtm_USERCOPY_HEAP_FLAG_FROM(void)
  229. {
  230. do_usercopy_heap_flag(false);
  231. }
  232. void lkdtm_USERCOPY_STACK_FRAME_TO(void)
  233. {
  234. do_usercopy_stack(true, true);
  235. }
  236. void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
  237. {
  238. do_usercopy_stack(false, true);
  239. }
  240. void lkdtm_USERCOPY_STACK_BEYOND(void)
  241. {
  242. do_usercopy_stack(true, false);
  243. }
  244. void lkdtm_USERCOPY_KERNEL(void)
  245. {
  246. unsigned long user_addr;
  247. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  248. PROT_READ | PROT_WRITE | PROT_EXEC,
  249. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  250. if (user_addr >= TASK_SIZE) {
  251. pr_warn("Failed to allocate user memory\n");
  252. return;
  253. }
  254. pr_info("attempting good copy_to_user from kernel rodata\n");
  255. if (copy_to_user((void __user *)user_addr, test_text,
  256. unconst + sizeof(test_text))) {
  257. pr_warn("copy_to_user failed unexpectedly?!\n");
  258. goto free_user;
  259. }
  260. pr_info("attempting bad copy_to_user from kernel text\n");
  261. if (copy_to_user((void __user *)user_addr, vm_mmap,
  262. unconst + PAGE_SIZE)) {
  263. pr_warn("copy_to_user failed, but lacked Oops\n");
  264. goto free_user;
  265. }
  266. free_user:
  267. vm_munmap(user_addr, PAGE_SIZE);
  268. }
  269. void __init lkdtm_usercopy_init(void)
  270. {
  271. /* Prepare cache that lacks SLAB_USERCOPY flag. */
  272. bad_cache = kmem_cache_create("lkdtm-no-usercopy", cache_size, 0,
  273. 0, NULL);
  274. }
  275. void __exit lkdtm_usercopy_exit(void)
  276. {
  277. kmem_cache_destroy(bad_cache);
  278. }