lkdtm_perms.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /* Intentionally crossing kernel/user memory boundary. */
  53. void (*func)(void) = dst;
  54. pr_info("attempting ok execution at %p\n", do_nothing);
  55. do_nothing();
  56. if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
  57. return;
  58. flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
  59. pr_info("attempting bad execution at %p\n", func);
  60. func();
  61. }
  62. void lkdtm_WRITE_RO(void)
  63. {
  64. /* Explicitly cast away "const" for the test. */
  65. unsigned long *ptr = (unsigned long *)&rodata;
  66. pr_info("attempting bad rodata write at %p\n", ptr);
  67. *ptr ^= 0xabcd1234;
  68. }
  69. void lkdtm_WRITE_RO_AFTER_INIT(void)
  70. {
  71. unsigned long *ptr = &ro_after_init;
  72. /*
  73. * Verify we were written to during init. Since an Oops
  74. * is considered a "success", a failure is to just skip the
  75. * real test.
  76. */
  77. if ((*ptr & 0xAA) != 0xAA) {
  78. pr_info("%p was NOT written during init!?\n", ptr);
  79. return;
  80. }
  81. pr_info("attempting bad ro_after_init write at %p\n", ptr);
  82. *ptr ^= 0xabcd1234;
  83. }
  84. void lkdtm_WRITE_KERN(void)
  85. {
  86. size_t size;
  87. unsigned char *ptr;
  88. size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
  89. ptr = (unsigned char *)do_overwritten;
  90. pr_info("attempting bad %zu byte write at %p\n", size, ptr);
  91. memcpy(ptr, (unsigned char *)do_nothing, size);
  92. flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
  93. do_overwritten();
  94. }
  95. void lkdtm_EXEC_DATA(void)
  96. {
  97. execute_location(data_area, CODE_WRITE);
  98. }
  99. void lkdtm_EXEC_STACK(void)
  100. {
  101. u8 stack_area[EXEC_SIZE];
  102. execute_location(stack_area, CODE_WRITE);
  103. }
  104. void lkdtm_EXEC_KMALLOC(void)
  105. {
  106. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  107. execute_location(kmalloc_area, CODE_WRITE);
  108. kfree(kmalloc_area);
  109. }
  110. void lkdtm_EXEC_VMALLOC(void)
  111. {
  112. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  113. execute_location(vmalloc_area, CODE_WRITE);
  114. vfree(vmalloc_area);
  115. }
  116. void lkdtm_EXEC_RODATA(void)
  117. {
  118. execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS);
  119. }
  120. void lkdtm_EXEC_USERSPACE(void)
  121. {
  122. unsigned long user_addr;
  123. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  124. PROT_READ | PROT_WRITE | PROT_EXEC,
  125. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  126. if (user_addr >= TASK_SIZE) {
  127. pr_warn("Failed to allocate user memory\n");
  128. return;
  129. }
  130. execute_user_location((void *)user_addr);
  131. vm_munmap(user_addr, PAGE_SIZE);
  132. }
  133. void lkdtm_ACCESS_USERSPACE(void)
  134. {
  135. unsigned long user_addr, tmp = 0;
  136. unsigned long *ptr;
  137. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  138. PROT_READ | PROT_WRITE | PROT_EXEC,
  139. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  140. if (user_addr >= TASK_SIZE) {
  141. pr_warn("Failed to allocate user memory\n");
  142. return;
  143. }
  144. if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
  145. pr_warn("copy_to_user failed\n");
  146. vm_munmap(user_addr, PAGE_SIZE);
  147. return;
  148. }
  149. ptr = (unsigned long *)user_addr;
  150. pr_info("attempting bad read at %p\n", ptr);
  151. tmp = *ptr;
  152. tmp += 0xc0dec0de;
  153. pr_info("attempting bad write at %p\n", ptr);
  154. *ptr = tmp;
  155. vm_munmap(user_addr, PAGE_SIZE);
  156. }
  157. void __init lkdtm_perms_init(void)
  158. {
  159. /* Make sure we can write to __ro_after_init values during __init */
  160. ro_after_init |= 0xAA;
  161. }