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