gcs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/mm.h>
  3. #include <linux/mman.h>
  4. #include <linux/syscalls.h>
  5. #include <linux/types.h>
  6. #include <asm/cmpxchg.h>
  7. #include <asm/cpufeature.h>
  8. #include <asm/gcs.h>
  9. #include <asm/page.h>
  10. static unsigned long alloc_gcs(unsigned long addr, unsigned long size)
  11. {
  12. int flags = MAP_ANONYMOUS | MAP_PRIVATE;
  13. struct mm_struct *mm = current->mm;
  14. unsigned long mapped_addr, unused;
  15. if (addr)
  16. flags |= MAP_FIXED_NOREPLACE;
  17. mmap_write_lock(mm);
  18. mapped_addr = do_mmap(NULL, addr, size, PROT_READ, flags,
  19. VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL);
  20. mmap_write_unlock(mm);
  21. return mapped_addr;
  22. }
  23. static unsigned long gcs_size(unsigned long size)
  24. {
  25. if (size)
  26. return PAGE_ALIGN(size);
  27. /* Allocate RLIMIT_STACK/2 with limits of PAGE_SIZE..2G */
  28. size = PAGE_ALIGN(min_t(unsigned long long,
  29. rlimit(RLIMIT_STACK) / 2, SZ_2G));
  30. return max(PAGE_SIZE, size);
  31. }
  32. unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
  33. const struct kernel_clone_args *args)
  34. {
  35. unsigned long addr, size;
  36. if (!system_supports_gcs())
  37. return 0;
  38. if (!task_gcs_el0_enabled(tsk))
  39. return 0;
  40. if ((args->flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) {
  41. tsk->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0);
  42. return 0;
  43. }
  44. size = args->stack_size / 2;
  45. size = gcs_size(size);
  46. addr = alloc_gcs(0, size);
  47. if (IS_ERR_VALUE(addr))
  48. return addr;
  49. tsk->thread.gcs_base = addr;
  50. tsk->thread.gcs_size = size;
  51. tsk->thread.gcspr_el0 = addr + size - sizeof(u64);
  52. return addr;
  53. }
  54. SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags)
  55. {
  56. unsigned long alloc_size;
  57. unsigned long __user *cap_ptr;
  58. unsigned long cap_val;
  59. int ret = 0;
  60. int cap_offset;
  61. if (!system_supports_gcs())
  62. return -EOPNOTSUPP;
  63. if (flags & ~(SHADOW_STACK_SET_TOKEN | SHADOW_STACK_SET_MARKER))
  64. return -EINVAL;
  65. if (!PAGE_ALIGNED(addr))
  66. return -EINVAL;
  67. if (size == 8 || !IS_ALIGNED(size, 8))
  68. return -EINVAL;
  69. /*
  70. * An overflow would result in attempting to write the restore token
  71. * to the wrong location. Not catastrophic, but just return the right
  72. * error code and block it.
  73. */
  74. alloc_size = PAGE_ALIGN(size);
  75. if (alloc_size < size)
  76. return -EOVERFLOW;
  77. addr = alloc_gcs(addr, alloc_size);
  78. if (IS_ERR_VALUE(addr))
  79. return addr;
  80. /*
  81. * Put a cap token at the end of the allocated region so it
  82. * can be switched to.
  83. */
  84. if (flags & SHADOW_STACK_SET_TOKEN) {
  85. /* Leave an extra empty frame as a top of stack marker? */
  86. if (flags & SHADOW_STACK_SET_MARKER)
  87. cap_offset = 2;
  88. else
  89. cap_offset = 1;
  90. cap_ptr = (unsigned long __user *)(addr + size -
  91. (cap_offset * sizeof(unsigned long)));
  92. cap_val = GCS_CAP(cap_ptr);
  93. put_user_gcs(cap_val, cap_ptr, &ret);
  94. if (ret != 0) {
  95. vm_munmap(addr, size);
  96. return -EFAULT;
  97. }
  98. /*
  99. * Ensure the new cap is ordered before standard
  100. * memory accesses to the same location.
  101. */
  102. gcsb_dsync();
  103. }
  104. return addr;
  105. }
  106. /*
  107. * Apply the GCS mode configured for the specified task to the
  108. * hardware.
  109. */
  110. void gcs_set_el0_mode(struct task_struct *task)
  111. {
  112. u64 gcscre0_el1 = GCSCRE0_EL1_nTR;
  113. if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE)
  114. gcscre0_el1 |= GCSCRE0_EL1_RVCHKEN | GCSCRE0_EL1_PCRSEL;
  115. if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_WRITE)
  116. gcscre0_el1 |= GCSCRE0_EL1_STREn;
  117. if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_PUSH)
  118. gcscre0_el1 |= GCSCRE0_EL1_PUSHMEn;
  119. write_sysreg_s(gcscre0_el1, SYS_GCSCRE0_EL1);
  120. }
  121. void gcs_free(struct task_struct *task)
  122. {
  123. if (!system_supports_gcs())
  124. return;
  125. if (!task->mm || task->mm != current->mm)
  126. return;
  127. if (task->thread.gcs_base)
  128. vm_munmap(task->thread.gcs_base, task->thread.gcs_size);
  129. task->thread.gcspr_el0 = 0;
  130. task->thread.gcs_base = 0;
  131. task->thread.gcs_size = 0;
  132. }
  133. int arch_set_shadow_stack_status(struct task_struct *task, unsigned long arg)
  134. {
  135. unsigned long gcs, size;
  136. int ret;
  137. if (!system_supports_gcs())
  138. return -EINVAL;
  139. if (is_compat_thread(task_thread_info(task)))
  140. return -EINVAL;
  141. /* Reject unknown flags */
  142. if (arg & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK)
  143. return -EINVAL;
  144. ret = gcs_check_locked(task, arg);
  145. if (ret != 0)
  146. return ret;
  147. /* If we are enabling GCS then make sure we have a stack */
  148. if (arg & PR_SHADOW_STACK_ENABLE &&
  149. !task_gcs_el0_enabled(task)) {
  150. /* Do not allow GCS to be reenabled */
  151. if (task->thread.gcs_base || task->thread.gcspr_el0)
  152. return -EINVAL;
  153. if (task != current)
  154. return -EBUSY;
  155. size = gcs_size(0);
  156. gcs = alloc_gcs(0, size);
  157. if (IS_ERR_VALUE(gcs))
  158. return gcs;
  159. task->thread.gcspr_el0 = gcs + size - sizeof(u64);
  160. task->thread.gcs_base = gcs;
  161. task->thread.gcs_size = size;
  162. if (task == current)
  163. write_sysreg_s(task->thread.gcspr_el0,
  164. SYS_GCSPR_EL0);
  165. }
  166. task->thread.gcs_el0_mode = arg;
  167. if (task == current)
  168. gcs_set_el0_mode(task);
  169. return 0;
  170. }
  171. int arch_get_shadow_stack_status(struct task_struct *task,
  172. unsigned long __user *arg)
  173. {
  174. if (!system_supports_gcs())
  175. return -EINVAL;
  176. if (is_compat_thread(task_thread_info(task)))
  177. return -EINVAL;
  178. return put_user(task->thread.gcs_el0_mode, arg);
  179. }
  180. int arch_lock_shadow_stack_status(struct task_struct *task,
  181. unsigned long arg)
  182. {
  183. if (!system_supports_gcs())
  184. return -EINVAL;
  185. if (is_compat_thread(task_thread_info(task)))
  186. return -EINVAL;
  187. /*
  188. * We support locking unknown bits so applications can prevent
  189. * any changes in a future proof manner.
  190. */
  191. task->thread.gcs_el0_locked |= arg;
  192. return 0;
  193. }