usercopy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/highmem.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/mman.h>
  12. #include <linux/uaccess.h>
  13. #include <asm/cacheflush.h>
  14. /*
  15. * Many of the tests here end up using const sizes, but those would
  16. * normally be ignored by hardened usercopy, so force the compiler
  17. * into choosing the non-const path to make sure we trigger the
  18. * hardened usercopy checks by added "unconst" to all the const copies,
  19. * and making sure "cache_size" isn't optimized into a const.
  20. */
  21. static volatile size_t unconst;
  22. static volatile size_t cache_size = 1024;
  23. static struct kmem_cache *whitelist_cache;
  24. static const unsigned char test_text[] = "This is a test.\n";
  25. /*
  26. * Instead of adding -Wno-return-local-addr, just pass the stack address
  27. * through a function to obfuscate it from the compiler.
  28. */
  29. static noinline unsigned char *trick_compiler(unsigned char *stack)
  30. {
  31. return stack + unconst;
  32. }
  33. static noinline unsigned char *do_usercopy_stack_callee(int value)
  34. {
  35. unsigned char buf[128];
  36. int i;
  37. /* Exercise stack to avoid everything living in registers. */
  38. for (i = 0; i < sizeof(buf); i++) {
  39. buf[i] = value & 0xff;
  40. }
  41. /*
  42. * Put the target buffer in the middle of stack allocation
  43. * so that we don't step on future stack users regardless
  44. * of stack growth direction.
  45. */
  46. return trick_compiler(&buf[(128/2)-32]);
  47. }
  48. static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
  49. {
  50. unsigned long user_addr;
  51. unsigned char good_stack[32];
  52. unsigned char *bad_stack;
  53. int i;
  54. /* Exercise stack to avoid everything living in registers. */
  55. for (i = 0; i < sizeof(good_stack); i++)
  56. good_stack[i] = test_text[i % sizeof(test_text)];
  57. /* This is a pointer to outside our current stack frame. */
  58. if (bad_frame) {
  59. bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
  60. } else {
  61. /* Put start address just inside stack. */
  62. bad_stack = task_stack_page(current) + THREAD_SIZE;
  63. bad_stack -= sizeof(unsigned long);
  64. }
  65. #ifdef ARCH_HAS_CURRENT_STACK_POINTER
  66. pr_info("stack : %px\n", (void *)current_stack_pointer);
  67. #endif
  68. pr_info("good_stack: %px-%px\n", good_stack, good_stack + sizeof(good_stack));
  69. pr_info("bad_stack : %px-%px\n", bad_stack, bad_stack + sizeof(good_stack));
  70. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  71. PROT_READ | PROT_WRITE | PROT_EXEC,
  72. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  73. if (user_addr >= TASK_SIZE) {
  74. pr_warn("Failed to allocate user memory\n");
  75. return;
  76. }
  77. if (to_user) {
  78. pr_info("attempting good copy_to_user of local stack\n");
  79. if (copy_to_user((void __user *)user_addr, good_stack,
  80. unconst + sizeof(good_stack))) {
  81. pr_warn("copy_to_user failed unexpectedly?!\n");
  82. goto free_user;
  83. }
  84. pr_info("attempting bad copy_to_user of distant stack\n");
  85. if (copy_to_user((void __user *)user_addr, bad_stack,
  86. unconst + sizeof(good_stack))) {
  87. pr_warn("copy_to_user failed, but lacked Oops\n");
  88. goto free_user;
  89. }
  90. } else {
  91. /*
  92. * There isn't a safe way to not be protected by usercopy
  93. * if we're going to write to another thread's stack.
  94. */
  95. if (!bad_frame)
  96. goto free_user;
  97. pr_info("attempting good copy_from_user of local stack\n");
  98. if (copy_from_user(good_stack, (void __user *)user_addr,
  99. unconst + sizeof(good_stack))) {
  100. pr_warn("copy_from_user failed unexpectedly?!\n");
  101. goto free_user;
  102. }
  103. pr_info("attempting bad copy_from_user of distant stack\n");
  104. if (copy_from_user(bad_stack, (void __user *)user_addr,
  105. unconst + sizeof(good_stack))) {
  106. pr_warn("copy_from_user failed, but lacked Oops\n");
  107. goto free_user;
  108. }
  109. }
  110. free_user:
  111. vm_munmap(user_addr, PAGE_SIZE);
  112. }
  113. /*
  114. * This checks for whole-object size validation with hardened usercopy,
  115. * with or without usercopy whitelisting.
  116. */
  117. static void do_usercopy_slab_size(bool to_user)
  118. {
  119. unsigned long user_addr;
  120. unsigned char *one, *two;
  121. void __user *test_user_addr;
  122. void *test_kern_addr;
  123. size_t size = unconst + 1024;
  124. one = kmalloc(size, GFP_KERNEL);
  125. two = kmalloc(size, GFP_KERNEL);
  126. if (!one || !two) {
  127. pr_warn("Failed to allocate kernel memory\n");
  128. goto free_kernel;
  129. }
  130. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  131. PROT_READ | PROT_WRITE | PROT_EXEC,
  132. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  133. if (user_addr >= TASK_SIZE) {
  134. pr_warn("Failed to allocate user memory\n");
  135. goto free_kernel;
  136. }
  137. memset(one, 'A', size);
  138. memset(two, 'B', size);
  139. test_user_addr = (void __user *)(user_addr + 16);
  140. test_kern_addr = one + 16;
  141. if (to_user) {
  142. pr_info("attempting good copy_to_user of correct size\n");
  143. if (copy_to_user(test_user_addr, test_kern_addr, size / 2)) {
  144. pr_warn("copy_to_user failed unexpectedly?!\n");
  145. goto free_user;
  146. }
  147. pr_info("attempting bad copy_to_user of too large size\n");
  148. if (copy_to_user(test_user_addr, test_kern_addr, size)) {
  149. pr_warn("copy_to_user failed, but lacked Oops\n");
  150. goto free_user;
  151. }
  152. } else {
  153. pr_info("attempting good copy_from_user of correct size\n");
  154. if (copy_from_user(test_kern_addr, test_user_addr, size / 2)) {
  155. pr_warn("copy_from_user failed unexpectedly?!\n");
  156. goto free_user;
  157. }
  158. pr_info("attempting bad copy_from_user of too large size\n");
  159. if (copy_from_user(test_kern_addr, test_user_addr, size)) {
  160. pr_warn("copy_from_user failed, but lacked Oops\n");
  161. goto free_user;
  162. }
  163. }
  164. pr_err("FAIL: bad usercopy not detected!\n");
  165. pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
  166. free_user:
  167. vm_munmap(user_addr, PAGE_SIZE);
  168. free_kernel:
  169. kfree(one);
  170. kfree(two);
  171. }
  172. /*
  173. * This checks for the specific whitelist window within an object. If this
  174. * test passes, then do_usercopy_slab_size() tests will pass too.
  175. */
  176. static void do_usercopy_slab_whitelist(bool to_user)
  177. {
  178. unsigned long user_alloc;
  179. unsigned char *buf = NULL;
  180. unsigned char __user *user_addr;
  181. size_t offset, size;
  182. /* Make sure cache was prepared. */
  183. if (!whitelist_cache) {
  184. pr_warn("Failed to allocate kernel cache\n");
  185. return;
  186. }
  187. /*
  188. * Allocate a buffer with a whitelisted window in the buffer.
  189. */
  190. buf = kmem_cache_alloc(whitelist_cache, GFP_KERNEL);
  191. if (!buf) {
  192. pr_warn("Failed to allocate buffer from whitelist cache\n");
  193. goto free_alloc;
  194. }
  195. /* Allocate user memory we'll poke at. */
  196. user_alloc = vm_mmap(NULL, 0, PAGE_SIZE,
  197. PROT_READ | PROT_WRITE | PROT_EXEC,
  198. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  199. if (user_alloc >= TASK_SIZE) {
  200. pr_warn("Failed to allocate user memory\n");
  201. goto free_alloc;
  202. }
  203. user_addr = (void __user *)user_alloc;
  204. memset(buf, 'B', cache_size);
  205. /* Whitelisted window in buffer, from kmem_cache_create_usercopy. */
  206. offset = (cache_size / 4) + unconst;
  207. size = (cache_size / 16) + unconst;
  208. if (to_user) {
  209. pr_info("attempting good copy_to_user inside whitelist\n");
  210. if (copy_to_user(user_addr, buf + offset, size)) {
  211. pr_warn("copy_to_user failed unexpectedly?!\n");
  212. goto free_user;
  213. }
  214. pr_info("attempting bad copy_to_user outside whitelist\n");
  215. if (copy_to_user(user_addr, buf + offset - 1, size)) {
  216. pr_warn("copy_to_user failed, but lacked Oops\n");
  217. goto free_user;
  218. }
  219. } else {
  220. pr_info("attempting good copy_from_user inside whitelist\n");
  221. if (copy_from_user(buf + offset, user_addr, size)) {
  222. pr_warn("copy_from_user failed unexpectedly?!\n");
  223. goto free_user;
  224. }
  225. pr_info("attempting bad copy_from_user outside whitelist\n");
  226. if (copy_from_user(buf + offset - 1, user_addr, size)) {
  227. pr_warn("copy_from_user failed, but lacked Oops\n");
  228. goto free_user;
  229. }
  230. }
  231. pr_err("FAIL: bad usercopy not detected!\n");
  232. pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
  233. free_user:
  234. vm_munmap(user_alloc, PAGE_SIZE);
  235. free_alloc:
  236. if (buf)
  237. kmem_cache_free(whitelist_cache, buf);
  238. }
  239. /* Callable tests. */
  240. static void lkdtm_USERCOPY_SLAB_SIZE_TO(void)
  241. {
  242. do_usercopy_slab_size(true);
  243. }
  244. static void lkdtm_USERCOPY_SLAB_SIZE_FROM(void)
  245. {
  246. do_usercopy_slab_size(false);
  247. }
  248. static void lkdtm_USERCOPY_SLAB_WHITELIST_TO(void)
  249. {
  250. do_usercopy_slab_whitelist(true);
  251. }
  252. static void lkdtm_USERCOPY_SLAB_WHITELIST_FROM(void)
  253. {
  254. do_usercopy_slab_whitelist(false);
  255. }
  256. static void lkdtm_USERCOPY_STACK_FRAME_TO(void)
  257. {
  258. do_usercopy_stack(true, true);
  259. }
  260. static void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
  261. {
  262. do_usercopy_stack(false, true);
  263. }
  264. static void lkdtm_USERCOPY_STACK_BEYOND(void)
  265. {
  266. do_usercopy_stack(true, false);
  267. }
  268. static void lkdtm_USERCOPY_KERNEL(void)
  269. {
  270. unsigned long user_addr;
  271. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  272. PROT_READ | PROT_WRITE | PROT_EXEC,
  273. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  274. if (user_addr >= TASK_SIZE) {
  275. pr_warn("Failed to allocate user memory\n");
  276. return;
  277. }
  278. pr_info("attempting good copy_to_user from kernel rodata: %px\n",
  279. test_text);
  280. if (copy_to_user((void __user *)user_addr, test_text,
  281. unconst + sizeof(test_text))) {
  282. pr_warn("copy_to_user failed unexpectedly?!\n");
  283. goto free_user;
  284. }
  285. pr_info("attempting bad copy_to_user from kernel text: %px\n",
  286. vm_mmap);
  287. if (copy_to_user((void __user *)user_addr, vm_mmap,
  288. unconst + PAGE_SIZE)) {
  289. pr_warn("copy_to_user failed, but lacked Oops\n");
  290. goto free_user;
  291. }
  292. pr_err("FAIL: bad copy_to_user() not detected!\n");
  293. pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
  294. free_user:
  295. vm_munmap(user_addr, PAGE_SIZE);
  296. }
  297. /*
  298. * This expects "kaddr" to point to a PAGE_SIZE allocation, which means
  299. * a more complete test that would include copy_from_user() would risk
  300. * memory corruption. Just test copy_to_user() here, as that exercises
  301. * almost exactly the same code paths.
  302. */
  303. static void do_usercopy_page_span(const char *name, void *kaddr)
  304. {
  305. unsigned long uaddr;
  306. uaddr = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
  307. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  308. if (uaddr >= TASK_SIZE) {
  309. pr_warn("Failed to allocate user memory\n");
  310. return;
  311. }
  312. /* Initialize contents. */
  313. memset(kaddr, 0xAA, PAGE_SIZE);
  314. /* Bump the kaddr forward to detect a page-spanning overflow. */
  315. kaddr += PAGE_SIZE / 2;
  316. pr_info("attempting good copy_to_user() from kernel %s: %px\n",
  317. name, kaddr);
  318. if (copy_to_user((void __user *)uaddr, kaddr,
  319. unconst + (PAGE_SIZE / 2))) {
  320. pr_err("copy_to_user() failed unexpectedly?!\n");
  321. goto free_user;
  322. }
  323. pr_info("attempting bad copy_to_user() from kernel %s: %px\n",
  324. name, kaddr);
  325. if (copy_to_user((void __user *)uaddr, kaddr, unconst + PAGE_SIZE)) {
  326. pr_warn("Good, copy_to_user() failed, but lacked Oops(?!)\n");
  327. goto free_user;
  328. }
  329. pr_err("FAIL: bad copy_to_user() not detected!\n");
  330. pr_expected_config_param(CONFIG_HARDENED_USERCOPY, "hardened_usercopy");
  331. free_user:
  332. vm_munmap(uaddr, PAGE_SIZE);
  333. }
  334. static void lkdtm_USERCOPY_VMALLOC(void)
  335. {
  336. void *addr;
  337. addr = vmalloc(PAGE_SIZE);
  338. if (!addr) {
  339. pr_err("vmalloc() failed!?\n");
  340. return;
  341. }
  342. do_usercopy_page_span("vmalloc", addr);
  343. vfree(addr);
  344. }
  345. static void lkdtm_USERCOPY_FOLIO(void)
  346. {
  347. struct folio *folio;
  348. void *addr;
  349. /*
  350. * FIXME: Folio checking currently misses 0-order allocations, so
  351. * allocate and bump forward to the last page.
  352. */
  353. folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, 1);
  354. if (!folio) {
  355. pr_err("folio_alloc() failed!?\n");
  356. return;
  357. }
  358. addr = folio_address(folio);
  359. if (addr)
  360. do_usercopy_page_span("folio", addr + PAGE_SIZE);
  361. else
  362. pr_err("folio_address() failed?!\n");
  363. folio_put(folio);
  364. }
  365. void __init lkdtm_usercopy_init(void)
  366. {
  367. /* Prepare cache that lacks SLAB_USERCOPY flag. */
  368. whitelist_cache =
  369. kmem_cache_create_usercopy("lkdtm-usercopy", cache_size,
  370. 0, 0,
  371. cache_size / 4,
  372. cache_size / 16,
  373. NULL);
  374. }
  375. void __exit lkdtm_usercopy_exit(void)
  376. {
  377. kmem_cache_destroy(whitelist_cache);
  378. }
  379. static struct crashtype crashtypes[] = {
  380. CRASHTYPE(USERCOPY_SLAB_SIZE_TO),
  381. CRASHTYPE(USERCOPY_SLAB_SIZE_FROM),
  382. CRASHTYPE(USERCOPY_SLAB_WHITELIST_TO),
  383. CRASHTYPE(USERCOPY_SLAB_WHITELIST_FROM),
  384. CRASHTYPE(USERCOPY_STACK_FRAME_TO),
  385. CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
  386. CRASHTYPE(USERCOPY_STACK_BEYOND),
  387. CRASHTYPE(USERCOPY_VMALLOC),
  388. CRASHTYPE(USERCOPY_FOLIO),
  389. CRASHTYPE(USERCOPY_KERNEL),
  390. };
  391. struct crashtype_category usercopy_crashtypes = {
  392. .crashtypes = crashtypes,
  393. .len = ARRAY_SIZE(crashtypes),
  394. };