scs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/scs.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/vmstat.h>
  13. #ifdef CONFIG_DYNAMIC_SCS
  14. DEFINE_STATIC_KEY_FALSE(dynamic_scs_enabled);
  15. #endif
  16. static void __scs_account(void *s, int account)
  17. {
  18. struct page *scs_page = vmalloc_to_page(s);
  19. mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB,
  20. account * (SCS_SIZE / SZ_1K));
  21. }
  22. /* Matches NR_CACHED_STACKS for VMAP_STACK */
  23. #define NR_CACHED_SCS 2
  24. static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]);
  25. static void *__scs_alloc(int node)
  26. {
  27. int i;
  28. void *s;
  29. for (i = 0; i < NR_CACHED_SCS; i++) {
  30. s = this_cpu_xchg(scs_cache[i], NULL);
  31. if (s) {
  32. s = kasan_unpoison_vmalloc(s, SCS_SIZE,
  33. KASAN_VMALLOC_PROT_NORMAL);
  34. memset(s, 0, SCS_SIZE);
  35. goto out;
  36. }
  37. }
  38. s = __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END,
  39. GFP_SCS, PAGE_KERNEL, 0, node,
  40. __builtin_return_address(0));
  41. out:
  42. return kasan_reset_tag(s);
  43. }
  44. void *scs_alloc(int node)
  45. {
  46. void *s;
  47. s = __scs_alloc(node);
  48. if (!s)
  49. return NULL;
  50. *__scs_magic(s) = SCS_END_MAGIC;
  51. /*
  52. * Poison the allocation to catch unintentional accesses to
  53. * the shadow stack when KASAN is enabled.
  54. */
  55. kasan_poison_vmalloc(s, SCS_SIZE);
  56. __scs_account(s, 1);
  57. return s;
  58. }
  59. void scs_free(void *s)
  60. {
  61. int i;
  62. __scs_account(s, -1);
  63. /*
  64. * We cannot sleep as this can be called in interrupt context,
  65. * so use this_cpu_cmpxchg to update the cache, and vfree_atomic
  66. * to free the stack.
  67. */
  68. for (i = 0; i < NR_CACHED_SCS; i++)
  69. if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL)
  70. return;
  71. kasan_unpoison_vmalloc(s, SCS_SIZE, KASAN_VMALLOC_PROT_NORMAL);
  72. vfree_atomic(s);
  73. }
  74. static int scs_cleanup(unsigned int cpu)
  75. {
  76. int i;
  77. void **cache = per_cpu_ptr(scs_cache, cpu);
  78. for (i = 0; i < NR_CACHED_SCS; i++) {
  79. vfree(cache[i]);
  80. cache[i] = NULL;
  81. }
  82. return 0;
  83. }
  84. void __init scs_init(void)
  85. {
  86. if (!scs_is_enabled())
  87. return;
  88. cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "scs:scs_cache", NULL,
  89. scs_cleanup);
  90. }
  91. int scs_prepare(struct task_struct *tsk, int node)
  92. {
  93. void *s;
  94. if (!scs_is_enabled())
  95. return 0;
  96. s = scs_alloc(node);
  97. if (!s)
  98. return -ENOMEM;
  99. task_scs(tsk) = task_scs_sp(tsk) = s;
  100. return 0;
  101. }
  102. static void scs_check_usage(struct task_struct *tsk)
  103. {
  104. static unsigned long highest;
  105. unsigned long *p, prev, curr = highest, used = 0;
  106. if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
  107. return;
  108. for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
  109. if (!READ_ONCE_NOCHECK(*p))
  110. break;
  111. used += sizeof(*p);
  112. }
  113. while (used > curr) {
  114. prev = cmpxchg_relaxed(&highest, curr, used);
  115. if (prev == curr) {
  116. pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
  117. tsk->comm, task_pid_nr(tsk), used);
  118. break;
  119. }
  120. curr = prev;
  121. }
  122. }
  123. void scs_release(struct task_struct *tsk)
  124. {
  125. void *s = task_scs(tsk);
  126. if (!scs_is_enabled() || !s)
  127. return;
  128. WARN(task_scs_end_corrupted(tsk),
  129. "corrupted shadow stack detected when freeing task\n");
  130. scs_check_usage(tsk);
  131. scs_free(s);
  132. }