perms.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is for all the tests related to validating kernel memory
  4. * permissions: non-executable regions, non-writable regions, and
  5. * even non-readable regions.
  6. */
  7. #include "lkdtm.h"
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/mman.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/sections.h>
  14. /* Whether or not to fill the target memory area with do_nothing(). */
  15. #define CODE_WRITE true
  16. #define CODE_AS_IS false
  17. /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
  18. #define EXEC_SIZE 64
  19. /* This is non-const, so it will end up in the .data section. */
  20. static u8 data_area[EXEC_SIZE];
  21. /* This is const, so it will end up in the .rodata section. */
  22. static const unsigned long rodata = 0xAA55AA55;
  23. /* This is marked __ro_after_init, so it should ultimately be .rodata. */
  24. static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
  25. /*
  26. * This is a pointer to do_nothing() which is initialized at runtime rather
  27. * than build time to avoid objtool IBT validation warnings caused by an
  28. * inlined unrolled memcpy() in execute_location().
  29. */
  30. static void __ro_after_init *do_nothing_ptr;
  31. /*
  32. * This just returns to the caller. It is designed to be copied into
  33. * non-executable memory regions.
  34. */
  35. static noinline void do_nothing(void)
  36. {
  37. return;
  38. }
  39. /* Must immediately follow do_nothing for size calculuations to work out. */
  40. static noinline void do_overwritten(void)
  41. {
  42. pr_info("do_overwritten wasn't overwritten!\n");
  43. return;
  44. }
  45. static noinline void do_almost_nothing(void)
  46. {
  47. pr_info("do_nothing was hijacked!\n");
  48. }
  49. static void *setup_function_descriptor(func_desc_t *fdesc, void *dst)
  50. {
  51. if (!have_function_descriptors())
  52. return dst;
  53. memcpy(fdesc, do_nothing, sizeof(*fdesc));
  54. fdesc->addr = (unsigned long)dst;
  55. barrier();
  56. return fdesc;
  57. }
  58. static noinline __nocfi void execute_location(void *dst, bool write)
  59. {
  60. void (*func)(void);
  61. func_desc_t fdesc;
  62. pr_info("attempting ok execution at %px\n", do_nothing_ptr);
  63. do_nothing();
  64. if (write == CODE_WRITE) {
  65. memcpy(dst, do_nothing_ptr, EXEC_SIZE);
  66. flush_icache_range((unsigned long)dst,
  67. (unsigned long)dst + EXEC_SIZE);
  68. }
  69. pr_info("attempting bad execution at %px\n", dst);
  70. func = setup_function_descriptor(&fdesc, dst);
  71. func();
  72. pr_err("FAIL: func returned\n");
  73. }
  74. static void execute_user_location(void *dst)
  75. {
  76. int copied;
  77. /* Intentionally crossing kernel/user memory boundary. */
  78. void (*func)(void);
  79. func_desc_t fdesc;
  80. void *do_nothing_text = dereference_function_descriptor(do_nothing);
  81. pr_info("attempting ok execution at %px\n", do_nothing_text);
  82. do_nothing();
  83. copied = access_process_vm(current, (unsigned long)dst, do_nothing_text,
  84. EXEC_SIZE, FOLL_WRITE);
  85. if (copied < EXEC_SIZE)
  86. return;
  87. pr_info("attempting bad execution at %px\n", dst);
  88. func = setup_function_descriptor(&fdesc, dst);
  89. func();
  90. pr_err("FAIL: func returned\n");
  91. }
  92. static void lkdtm_WRITE_RO(void)
  93. {
  94. /* Explicitly cast away "const" for the test and make volatile. */
  95. volatile unsigned long *ptr = (unsigned long *)&rodata;
  96. pr_info("attempting bad rodata write at %px\n", ptr);
  97. *ptr ^= 0xabcd1234;
  98. pr_err("FAIL: survived bad write\n");
  99. }
  100. static void lkdtm_WRITE_RO_AFTER_INIT(void)
  101. {
  102. volatile unsigned long *ptr = &ro_after_init;
  103. /*
  104. * Verify we were written to during init. Since an Oops
  105. * is considered a "success", a failure is to just skip the
  106. * real test.
  107. */
  108. if ((*ptr & 0xAA) != 0xAA) {
  109. pr_info("%p was NOT written during init!?\n", ptr);
  110. return;
  111. }
  112. pr_info("attempting bad ro_after_init write at %px\n", ptr);
  113. *ptr ^= 0xabcd1234;
  114. pr_err("FAIL: survived bad write\n");
  115. }
  116. static void lkdtm_WRITE_KERN(void)
  117. {
  118. size_t size;
  119. volatile unsigned char *ptr;
  120. size = (unsigned long)dereference_function_descriptor(do_overwritten) -
  121. (unsigned long)dereference_function_descriptor(do_nothing);
  122. ptr = dereference_function_descriptor(do_overwritten);
  123. pr_info("attempting bad %zu byte write at %px\n", size, ptr);
  124. memcpy((void *)ptr, (unsigned char *)do_nothing, size);
  125. flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
  126. pr_err("FAIL: survived bad write\n");
  127. do_overwritten();
  128. }
  129. static void lkdtm_WRITE_OPD(void)
  130. {
  131. size_t size = sizeof(func_desc_t);
  132. void (*func)(void) = do_nothing;
  133. if (!have_function_descriptors()) {
  134. pr_info("XFAIL: Platform doesn't use function descriptors.\n");
  135. return;
  136. }
  137. pr_info("attempting bad %zu bytes write at %px\n", size, do_nothing);
  138. memcpy(do_nothing, do_almost_nothing, size);
  139. pr_err("FAIL: survived bad write\n");
  140. asm("" : "=m"(func));
  141. func();
  142. }
  143. static void lkdtm_EXEC_DATA(void)
  144. {
  145. execute_location(data_area, CODE_WRITE);
  146. }
  147. static void lkdtm_EXEC_STACK(void)
  148. {
  149. u8 stack_area[EXEC_SIZE];
  150. execute_location(stack_area, CODE_WRITE);
  151. }
  152. static void lkdtm_EXEC_KMALLOC(void)
  153. {
  154. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  155. execute_location(kmalloc_area, CODE_WRITE);
  156. kfree(kmalloc_area);
  157. }
  158. static void lkdtm_EXEC_VMALLOC(void)
  159. {
  160. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  161. execute_location(vmalloc_area, CODE_WRITE);
  162. vfree(vmalloc_area);
  163. }
  164. static void lkdtm_EXEC_RODATA(void)
  165. {
  166. execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing),
  167. CODE_AS_IS);
  168. }
  169. static void lkdtm_EXEC_USERSPACE(void)
  170. {
  171. unsigned long user_addr;
  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. return;
  178. }
  179. execute_user_location((void *)user_addr);
  180. vm_munmap(user_addr, PAGE_SIZE);
  181. }
  182. static void lkdtm_EXEC_NULL(void)
  183. {
  184. execute_location(NULL, CODE_AS_IS);
  185. }
  186. static void lkdtm_ACCESS_USERSPACE(void)
  187. {
  188. unsigned long user_addr, tmp = 0;
  189. unsigned long *ptr;
  190. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  191. PROT_READ | PROT_WRITE | PROT_EXEC,
  192. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  193. if (user_addr >= TASK_SIZE) {
  194. pr_warn("Failed to allocate user memory\n");
  195. return;
  196. }
  197. if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
  198. pr_warn("copy_to_user failed\n");
  199. vm_munmap(user_addr, PAGE_SIZE);
  200. return;
  201. }
  202. ptr = (unsigned long *)user_addr;
  203. pr_info("attempting bad read at %px\n", ptr);
  204. tmp = *ptr;
  205. tmp += 0xc0dec0de;
  206. pr_err("FAIL: survived bad read\n");
  207. pr_info("attempting bad write at %px\n", ptr);
  208. *ptr = tmp;
  209. pr_err("FAIL: survived bad write\n");
  210. vm_munmap(user_addr, PAGE_SIZE);
  211. }
  212. static void lkdtm_ACCESS_NULL(void)
  213. {
  214. unsigned long tmp;
  215. volatile unsigned long *ptr = (unsigned long *)NULL;
  216. pr_info("attempting bad read at %px\n", ptr);
  217. tmp = *ptr;
  218. tmp += 0xc0dec0de;
  219. pr_err("FAIL: survived bad read\n");
  220. pr_info("attempting bad write at %px\n", ptr);
  221. *ptr = tmp;
  222. pr_err("FAIL: survived bad write\n");
  223. }
  224. void __init lkdtm_perms_init(void)
  225. {
  226. do_nothing_ptr = dereference_function_descriptor(do_nothing);
  227. /* Make sure we can write to __ro_after_init values during __init */
  228. ro_after_init |= 0xAA;
  229. }
  230. static struct crashtype crashtypes[] = {
  231. CRASHTYPE(WRITE_RO),
  232. CRASHTYPE(WRITE_RO_AFTER_INIT),
  233. CRASHTYPE(WRITE_KERN),
  234. CRASHTYPE(WRITE_OPD),
  235. CRASHTYPE(EXEC_DATA),
  236. CRASHTYPE(EXEC_STACK),
  237. CRASHTYPE(EXEC_KMALLOC),
  238. CRASHTYPE(EXEC_VMALLOC),
  239. CRASHTYPE(EXEC_RODATA),
  240. CRASHTYPE(EXEC_USERSPACE),
  241. CRASHTYPE(EXEC_NULL),
  242. CRASHTYPE(ACCESS_USERSPACE),
  243. CRASHTYPE(ACCESS_NULL),
  244. };
  245. struct crashtype_category perms_crashtypes = {
  246. .crashtypes = crashtypes,
  247. .len = ARRAY_SIZE(crashtypes),
  248. };