lkdtm_usercopy.c 8.2 KB

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