scs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shadow Call Stack support.
  4. *
  5. * Copyright (C) 2019 Google LLC
  6. */
  7. #include <linux/cpuhotplug.h>
  8. #include <linux/kasan.h>
  9. #include <linux/mm.h>
  10. #include <linux/mmzone.h>
  11. #include <linux/scs.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/vmstat.h>
  15. #include <asm/scs.h>
  16. static inline void *__scs_base(struct task_struct *tsk)
  17. {
  18. /*
  19. * To minimize risk the of exposure, architectures may clear a
  20. * task's thread_info::shadow_call_stack while that task is
  21. * running, and only save/restore the active shadow call stack
  22. * pointer when the usual register may be clobbered (e.g. across
  23. * context switches).
  24. *
  25. * The shadow call stack is aligned to SCS_SIZE, and grows
  26. * upwards, so we can mask out the low bits to extract the base
  27. * when the task is not running.
  28. */
  29. return (void *)((unsigned long)task_scs(tsk) & ~(SCS_SIZE - 1));
  30. }
  31. static inline unsigned long *scs_magic(void *s)
  32. {
  33. return (unsigned long *)(s + SCS_SIZE) - 1;
  34. }
  35. static inline void scs_set_magic(void *s)
  36. {
  37. *scs_magic(s) = SCS_END_MAGIC;
  38. }
  39. #ifdef CONFIG_SHADOW_CALL_STACK_VMAP
  40. /* Matches NR_CACHED_STACKS for VMAP_STACK */
  41. #define NR_CACHED_SCS 2
  42. static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]);
  43. static void *scs_alloc(int node)
  44. {
  45. int i;
  46. void *s;
  47. for (i = 0; i < NR_CACHED_SCS; i++) {
  48. s = this_cpu_xchg(scs_cache[i], NULL);
  49. if (s) {
  50. memset(s, 0, SCS_SIZE);
  51. goto out;
  52. }
  53. }
  54. /*
  55. * We allocate a full page for the shadow stack, which should be
  56. * more than we need. Check the assumption nevertheless.
  57. */
  58. BUILD_BUG_ON(SCS_SIZE > PAGE_SIZE);
  59. s = __vmalloc_node_range(PAGE_SIZE, SCS_SIZE,
  60. VMALLOC_START, VMALLOC_END,
  61. GFP_SCS, PAGE_KERNEL, 0,
  62. node, __builtin_return_address(0));
  63. out:
  64. if (s)
  65. scs_set_magic(s);
  66. /* TODO: poison for KASAN, unpoison in scs_free */
  67. return s;
  68. }
  69. static void scs_free(void *s)
  70. {
  71. int i;
  72. for (i = 0; i < NR_CACHED_SCS; i++)
  73. if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL)
  74. return;
  75. vfree_atomic(s);
  76. }
  77. static struct page *__scs_page(struct task_struct *tsk)
  78. {
  79. return vmalloc_to_page(__scs_base(tsk));
  80. }
  81. static int scs_cleanup(unsigned int cpu)
  82. {
  83. int i;
  84. void **cache = per_cpu_ptr(scs_cache, cpu);
  85. for (i = 0; i < NR_CACHED_SCS; i++) {
  86. vfree(cache[i]);
  87. cache[i] = NULL;
  88. }
  89. return 0;
  90. }
  91. void __init scs_init(void)
  92. {
  93. WARN_ON(cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "scs:scs_cache", NULL,
  94. scs_cleanup) < 0);
  95. }
  96. #else /* !CONFIG_SHADOW_CALL_STACK_VMAP */
  97. static struct kmem_cache *scs_cache;
  98. static inline void *scs_alloc(int node)
  99. {
  100. void *s;
  101. s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
  102. if (s) {
  103. scs_set_magic(s);
  104. /*
  105. * Poison the allocation to catch unintentional accesses to
  106. * the shadow stack when KASAN is enabled.
  107. */
  108. kasan_poison_object_data(scs_cache, s);
  109. }
  110. return s;
  111. }
  112. static inline void scs_free(void *s)
  113. {
  114. kasan_unpoison_object_data(scs_cache, s);
  115. kmem_cache_free(scs_cache, s);
  116. }
  117. static struct page *__scs_page(struct task_struct *tsk)
  118. {
  119. return virt_to_page(__scs_base(tsk));
  120. }
  121. void __init scs_init(void)
  122. {
  123. scs_cache = kmem_cache_create("scs_cache", SCS_SIZE, SCS_SIZE,
  124. 0, NULL);
  125. WARN_ON(!scs_cache);
  126. }
  127. #endif /* CONFIG_SHADOW_CALL_STACK_VMAP */
  128. void scs_task_reset(struct task_struct *tsk)
  129. {
  130. /*
  131. * Reset the shadow stack to the base address in case the task
  132. * is reused.
  133. */
  134. task_set_scs(tsk, __scs_base(tsk));
  135. }
  136. static void scs_account(struct task_struct *tsk, int account)
  137. {
  138. mod_zone_page_state(page_zone(__scs_page(tsk)), NR_KERNEL_SCS_BYTES,
  139. account * SCS_SIZE);
  140. }
  141. int scs_prepare(struct task_struct *tsk, int node)
  142. {
  143. void *s;
  144. s = scs_alloc(node);
  145. if (!s)
  146. return -ENOMEM;
  147. task_set_scs(tsk, s);
  148. scs_account(tsk, 1);
  149. return 0;
  150. }
  151. #ifdef CONFIG_DEBUG_STACK_USAGE
  152. static void scs_check_usage(struct task_struct *tsk)
  153. {
  154. static unsigned long highest;
  155. unsigned long *p = __scs_base(tsk);
  156. unsigned long *end = scs_magic(p);
  157. unsigned long prev, curr = highest, used = 0;
  158. for (; p < end; ++p) {
  159. if (!READ_ONCE_NOCHECK(*p))
  160. break;
  161. used += sizeof(*p);
  162. }
  163. while (used > curr) {
  164. prev = cmpxchg_relaxed(&highest, curr, used);
  165. if (prev == curr) {
  166. pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
  167. tsk->comm, task_pid_nr(tsk), used);
  168. break;
  169. }
  170. curr = prev;
  171. }
  172. }
  173. #else
  174. static inline void scs_check_usage(struct task_struct *tsk)
  175. {
  176. }
  177. #endif
  178. bool scs_corrupted(struct task_struct *tsk)
  179. {
  180. unsigned long *magic = scs_magic(__scs_base(tsk));
  181. return READ_ONCE_NOCHECK(*magic) != SCS_END_MAGIC;
  182. }
  183. void scs_release(struct task_struct *tsk)
  184. {
  185. void *s;
  186. s = __scs_base(tsk);
  187. if (!s)
  188. return;
  189. WARN_ON(scs_corrupted(tsk));
  190. scs_check_usage(tsk);
  191. scs_account(tsk, -1);
  192. task_set_scs(tsk, NULL);
  193. scs_free(s);
  194. }