perms.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 just returns to the caller. It is designed to be copied into
  27. * non-executable memory regions.
  28. */
  29. static noinline void do_nothing(void)
  30. {
  31. return;
  32. }
  33. /* Must immediately follow do_nothing for size calculuations to work out. */
  34. static noinline void do_overwritten(void)
  35. {
  36. pr_info("do_overwritten wasn't overwritten!\n");
  37. return;
  38. }
  39. static noinline void do_almost_nothing(void)
  40. {
  41. pr_info("do_nothing was hijacked!\n");
  42. }
  43. static void *setup_function_descriptor(func_desc_t *fdesc, void *dst)
  44. {
  45. if (!have_function_descriptors())
  46. return dst;
  47. memcpy(fdesc, do_nothing, sizeof(*fdesc));
  48. fdesc->addr = (unsigned long)dst;
  49. barrier();
  50. return fdesc;
  51. }
  52. static noinline void execute_location(void *dst, bool write)
  53. {
  54. void (*func)(void);
  55. func_desc_t fdesc;
  56. void *do_nothing_text = dereference_function_descriptor(do_nothing);
  57. pr_info("attempting ok execution at %px\n", do_nothing_text);
  58. do_nothing();
  59. if (write == CODE_WRITE) {
  60. memcpy(dst, do_nothing_text, EXEC_SIZE);
  61. flush_icache_range((unsigned long)dst,
  62. (unsigned long)dst + EXEC_SIZE);
  63. }
  64. pr_info("attempting bad execution at %px\n", dst);
  65. func = setup_function_descriptor(&fdesc, dst);
  66. func();
  67. pr_err("FAIL: func returned\n");
  68. }
  69. static void execute_user_location(void *dst)
  70. {
  71. int copied;
  72. /* Intentionally crossing kernel/user memory boundary. */
  73. void (*func)(void);
  74. func_desc_t fdesc;
  75. void *do_nothing_text = dereference_function_descriptor(do_nothing);
  76. pr_info("attempting ok execution at %px\n", do_nothing_text);
  77. do_nothing();
  78. copied = access_process_vm(current, (unsigned long)dst, do_nothing_text,
  79. EXEC_SIZE, FOLL_WRITE);
  80. if (copied < EXEC_SIZE)
  81. return;
  82. pr_info("attempting bad execution at %px\n", dst);
  83. func = setup_function_descriptor(&fdesc, dst);
  84. func();
  85. pr_err("FAIL: func returned\n");
  86. }
  87. static void lkdtm_WRITE_RO(void)
  88. {
  89. /* Explicitly cast away "const" for the test and make volatile. */
  90. volatile unsigned long *ptr = (unsigned long *)&rodata;
  91. pr_info("attempting bad rodata write at %px\n", ptr);
  92. *ptr ^= 0xabcd1234;
  93. pr_err("FAIL: survived bad write\n");
  94. }
  95. static void lkdtm_WRITE_RO_AFTER_INIT(void)
  96. {
  97. volatile unsigned long *ptr = &ro_after_init;
  98. /*
  99. * Verify we were written to during init. Since an Oops
  100. * is considered a "success", a failure is to just skip the
  101. * real test.
  102. */
  103. if ((*ptr & 0xAA) != 0xAA) {
  104. pr_info("%p was NOT written during init!?\n", ptr);
  105. return;
  106. }
  107. pr_info("attempting bad ro_after_init write at %px\n", ptr);
  108. *ptr ^= 0xabcd1234;
  109. pr_err("FAIL: survived bad write\n");
  110. }
  111. static void lkdtm_WRITE_KERN(void)
  112. {
  113. size_t size;
  114. volatile unsigned char *ptr;
  115. size = (unsigned long)dereference_function_descriptor(do_overwritten) -
  116. (unsigned long)dereference_function_descriptor(do_nothing);
  117. ptr = dereference_function_descriptor(do_overwritten);
  118. pr_info("attempting bad %zu byte write at %px\n", size, ptr);
  119. memcpy((void *)ptr, (unsigned char *)do_nothing, size);
  120. flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
  121. pr_err("FAIL: survived bad write\n");
  122. do_overwritten();
  123. }
  124. static void lkdtm_WRITE_OPD(void)
  125. {
  126. size_t size = sizeof(func_desc_t);
  127. void (*func)(void) = do_nothing;
  128. if (!have_function_descriptors()) {
  129. pr_info("XFAIL: Platform doesn't use function descriptors.\n");
  130. return;
  131. }
  132. pr_info("attempting bad %zu bytes write at %px\n", size, do_nothing);
  133. memcpy(do_nothing, do_almost_nothing, size);
  134. pr_err("FAIL: survived bad write\n");
  135. asm("" : "=m"(func));
  136. func();
  137. }
  138. static void lkdtm_EXEC_DATA(void)
  139. {
  140. execute_location(data_area, CODE_WRITE);
  141. }
  142. static void lkdtm_EXEC_STACK(void)
  143. {
  144. u8 stack_area[EXEC_SIZE];
  145. execute_location(stack_area, CODE_WRITE);
  146. }
  147. static void lkdtm_EXEC_KMALLOC(void)
  148. {
  149. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  150. execute_location(kmalloc_area, CODE_WRITE);
  151. kfree(kmalloc_area);
  152. }
  153. static void lkdtm_EXEC_VMALLOC(void)
  154. {
  155. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  156. execute_location(vmalloc_area, CODE_WRITE);
  157. vfree(vmalloc_area);
  158. }
  159. static void lkdtm_EXEC_RODATA(void)
  160. {
  161. execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing),
  162. CODE_AS_IS);
  163. }
  164. static void lkdtm_EXEC_USERSPACE(void)
  165. {
  166. unsigned long user_addr;
  167. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  168. PROT_READ | PROT_WRITE | PROT_EXEC,
  169. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  170. if (user_addr >= TASK_SIZE) {
  171. pr_warn("Failed to allocate user memory\n");
  172. return;
  173. }
  174. execute_user_location((void *)user_addr);
  175. vm_munmap(user_addr, PAGE_SIZE);
  176. }
  177. static void lkdtm_EXEC_NULL(void)
  178. {
  179. execute_location(NULL, CODE_AS_IS);
  180. }
  181. static void lkdtm_ACCESS_USERSPACE(void)
  182. {
  183. unsigned long user_addr, tmp = 0;
  184. unsigned long *ptr;
  185. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  186. PROT_READ | PROT_WRITE | PROT_EXEC,
  187. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  188. if (user_addr >= TASK_SIZE) {
  189. pr_warn("Failed to allocate user memory\n");
  190. return;
  191. }
  192. if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
  193. pr_warn("copy_to_user failed\n");
  194. vm_munmap(user_addr, PAGE_SIZE);
  195. return;
  196. }
  197. ptr = (unsigned long *)user_addr;
  198. pr_info("attempting bad read at %px\n", ptr);
  199. tmp = *ptr;
  200. tmp += 0xc0dec0de;
  201. pr_err("FAIL: survived bad read\n");
  202. pr_info("attempting bad write at %px\n", ptr);
  203. *ptr = tmp;
  204. pr_err("FAIL: survived bad write\n");
  205. vm_munmap(user_addr, PAGE_SIZE);
  206. }
  207. static void lkdtm_ACCESS_NULL(void)
  208. {
  209. unsigned long tmp;
  210. volatile unsigned long *ptr = (unsigned long *)NULL;
  211. pr_info("attempting bad read at %px\n", ptr);
  212. tmp = *ptr;
  213. tmp += 0xc0dec0de;
  214. pr_err("FAIL: survived bad read\n");
  215. pr_info("attempting bad write at %px\n", ptr);
  216. *ptr = tmp;
  217. pr_err("FAIL: survived bad write\n");
  218. }
  219. void __init lkdtm_perms_init(void)
  220. {
  221. /* Make sure we can write to __ro_after_init values during __init */
  222. ro_after_init |= 0xAA;
  223. }
  224. static struct crashtype crashtypes[] = {
  225. CRASHTYPE(WRITE_RO),
  226. CRASHTYPE(WRITE_RO_AFTER_INIT),
  227. CRASHTYPE(WRITE_KERN),
  228. CRASHTYPE(WRITE_OPD),
  229. CRASHTYPE(EXEC_DATA),
  230. CRASHTYPE(EXEC_STACK),
  231. CRASHTYPE(EXEC_KMALLOC),
  232. CRASHTYPE(EXEC_VMALLOC),
  233. CRASHTYPE(EXEC_RODATA),
  234. CRASHTYPE(EXEC_USERSPACE),
  235. CRASHTYPE(EXEC_NULL),
  236. CRASHTYPE(ACCESS_USERSPACE),
  237. CRASHTYPE(ACCESS_NULL),
  238. };
  239. struct crashtype_category perms_crashtypes = {
  240. .crashtypes = crashtypes,
  241. .len = ARRAY_SIZE(crashtypes),
  242. };