lkdtm_perms.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /* Whether or not to fill the target memory area with do_nothing(). */
  14. #define CODE_WRITE true
  15. #define CODE_AS_IS false
  16. /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
  17. #define EXEC_SIZE 64
  18. /* This is non-const, so it will end up in the .data section. */
  19. static u8 data_area[EXEC_SIZE];
  20. /* This is cost, so it will end up in the .rodata section. */
  21. static const unsigned long rodata = 0xAA55AA55;
  22. /* This is marked __ro_after_init, so it should ultimately be .rodata. */
  23. static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
  24. /*
  25. * This just returns to the caller. It is designed to be copied into
  26. * non-executable memory regions.
  27. */
  28. static void do_nothing(void)
  29. {
  30. return;
  31. }
  32. /* Must immediately follow do_nothing for size calculuations to work out. */
  33. static void do_overwritten(void)
  34. {
  35. pr_info("do_overwritten wasn't overwritten!\n");
  36. return;
  37. }
  38. static noinline void execute_location(void *dst, bool write)
  39. {
  40. void (*func)(void) = dst;
  41. pr_info("attempting ok execution at %px\n", do_nothing);
  42. do_nothing();
  43. if (write == CODE_WRITE) {
  44. memcpy(dst, do_nothing, EXEC_SIZE);
  45. flush_icache_range((unsigned long)dst,
  46. (unsigned long)dst + EXEC_SIZE);
  47. }
  48. pr_info("attempting bad execution at %px\n", func);
  49. func();
  50. }
  51. static void execute_user_location(void *dst)
  52. {
  53. int copied;
  54. /* Intentionally crossing kernel/user memory boundary. */
  55. void (*func)(void) = dst;
  56. pr_info("attempting ok execution at %px\n", do_nothing);
  57. do_nothing();
  58. copied = access_process_vm(current, (unsigned long)dst, do_nothing,
  59. EXEC_SIZE, FOLL_WRITE);
  60. if (copied < EXEC_SIZE)
  61. return;
  62. pr_info("attempting bad execution at %px\n", func);
  63. func();
  64. }
  65. void lkdtm_WRITE_RO(void)
  66. {
  67. /* Explicitly cast away "const" for the test. */
  68. unsigned long *ptr = (unsigned long *)&rodata;
  69. pr_info("attempting bad rodata write at %px\n", ptr);
  70. *ptr ^= 0xabcd1234;
  71. }
  72. void lkdtm_WRITE_RO_AFTER_INIT(void)
  73. {
  74. unsigned long *ptr = &ro_after_init;
  75. /*
  76. * Verify we were written to during init. Since an Oops
  77. * is considered a "success", a failure is to just skip the
  78. * real test.
  79. */
  80. if ((*ptr & 0xAA) != 0xAA) {
  81. pr_info("%p was NOT written during init!?\n", ptr);
  82. return;
  83. }
  84. pr_info("attempting bad ro_after_init write at %px\n", ptr);
  85. *ptr ^= 0xabcd1234;
  86. }
  87. void lkdtm_WRITE_KERN(void)
  88. {
  89. size_t size;
  90. unsigned char *ptr;
  91. size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
  92. ptr = (unsigned char *)do_overwritten;
  93. pr_info("attempting bad %zu byte write at %px\n", size, ptr);
  94. memcpy(ptr, (unsigned char *)do_nothing, size);
  95. flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
  96. do_overwritten();
  97. }
  98. void lkdtm_EXEC_DATA(void)
  99. {
  100. execute_location(data_area, CODE_WRITE);
  101. }
  102. void lkdtm_EXEC_STACK(void)
  103. {
  104. u8 stack_area[EXEC_SIZE];
  105. execute_location(stack_area, CODE_WRITE);
  106. }
  107. void lkdtm_EXEC_KMALLOC(void)
  108. {
  109. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  110. execute_location(kmalloc_area, CODE_WRITE);
  111. kfree(kmalloc_area);
  112. }
  113. void lkdtm_EXEC_VMALLOC(void)
  114. {
  115. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  116. execute_location(vmalloc_area, CODE_WRITE);
  117. vfree(vmalloc_area);
  118. }
  119. void lkdtm_EXEC_RODATA(void)
  120. {
  121. execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS);
  122. }
  123. void lkdtm_EXEC_USERSPACE(void)
  124. {
  125. unsigned long user_addr;
  126. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  127. PROT_READ | PROT_WRITE | PROT_EXEC,
  128. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  129. if (user_addr >= TASK_SIZE) {
  130. pr_warn("Failed to allocate user memory\n");
  131. return;
  132. }
  133. execute_user_location((void *)user_addr);
  134. vm_munmap(user_addr, PAGE_SIZE);
  135. }
  136. void lkdtm_EXEC_NULL(void)
  137. {
  138. execute_location(NULL, CODE_AS_IS);
  139. }
  140. void lkdtm_ACCESS_USERSPACE(void)
  141. {
  142. unsigned long user_addr, tmp = 0;
  143. unsigned long *ptr;
  144. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  145. PROT_READ | PROT_WRITE | PROT_EXEC,
  146. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  147. if (user_addr >= TASK_SIZE) {
  148. pr_warn("Failed to allocate user memory\n");
  149. return;
  150. }
  151. if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
  152. pr_warn("copy_to_user failed\n");
  153. vm_munmap(user_addr, PAGE_SIZE);
  154. return;
  155. }
  156. ptr = (unsigned long *)user_addr;
  157. pr_info("attempting bad read at %px\n", ptr);
  158. tmp = *ptr;
  159. tmp += 0xc0dec0de;
  160. pr_info("attempting bad write at %px\n", ptr);
  161. *ptr = tmp;
  162. vm_munmap(user_addr, PAGE_SIZE);
  163. }
  164. void lkdtm_ACCESS_NULL(void)
  165. {
  166. unsigned long tmp;
  167. unsigned long *ptr = (unsigned long *)NULL;
  168. pr_info("attempting bad read at %px\n", ptr);
  169. tmp = *ptr;
  170. tmp += 0xc0dec0de;
  171. pr_info("attempting bad write at %px\n", ptr);
  172. *ptr = tmp;
  173. }
  174. void __init lkdtm_perms_init(void)
  175. {
  176. /* Make sure we can write to __ro_after_init values during __init */
  177. ro_after_init |= 0xAA;
  178. }