lkdtm_perms.c 4.6 KB

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