stackleak.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code fills the used part of the kernel stack with a poison value
  4. * before returning to userspace. It's part of the STACKLEAK feature
  5. * ported from grsecurity/PaX.
  6. *
  7. * Author: Alexander Popov <alex.popov@linux.com>
  8. *
  9. * STACKLEAK reduces the information which kernel stack leak bugs can
  10. * reveal and blocks some uninitialized stack variable attacks.
  11. */
  12. #include <linux/stackleak.h>
  13. #include <linux/kprobes.h>
  14. #ifdef CONFIG_STACKLEAK_RUNTIME_DISABLE
  15. #include <linux/jump_label.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/init.h>
  18. static DEFINE_STATIC_KEY_FALSE(stack_erasing_bypass);
  19. #ifdef CONFIG_SYSCTL
  20. static int stack_erasing_sysctl(const struct ctl_table *table, int write,
  21. void __user *buffer, size_t *lenp, loff_t *ppos)
  22. {
  23. int ret = 0;
  24. int state = !static_branch_unlikely(&stack_erasing_bypass);
  25. int prev_state = state;
  26. struct ctl_table table_copy = *table;
  27. table_copy.data = &state;
  28. ret = proc_dointvec_minmax(&table_copy, write, buffer, lenp, ppos);
  29. state = !!state;
  30. if (ret || !write || state == prev_state)
  31. return ret;
  32. if (state)
  33. static_branch_disable(&stack_erasing_bypass);
  34. else
  35. static_branch_enable(&stack_erasing_bypass);
  36. pr_warn("stackleak: kernel stack erasing is %s\n",
  37. state ? "enabled" : "disabled");
  38. return ret;
  39. }
  40. static struct ctl_table stackleak_sysctls[] = {
  41. {
  42. .procname = "stack_erasing",
  43. .data = NULL,
  44. .maxlen = sizeof(int),
  45. .mode = 0600,
  46. .proc_handler = stack_erasing_sysctl,
  47. .extra1 = SYSCTL_ZERO,
  48. .extra2 = SYSCTL_ONE,
  49. },
  50. };
  51. static int __init stackleak_sysctls_init(void)
  52. {
  53. register_sysctl_init("kernel", stackleak_sysctls);
  54. return 0;
  55. }
  56. late_initcall(stackleak_sysctls_init);
  57. #endif /* CONFIG_SYSCTL */
  58. #define skip_erasing() static_branch_unlikely(&stack_erasing_bypass)
  59. #else
  60. #define skip_erasing() false
  61. #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */
  62. #ifndef __stackleak_poison
  63. static __always_inline void __stackleak_poison(unsigned long erase_low,
  64. unsigned long erase_high,
  65. unsigned long poison)
  66. {
  67. while (erase_low < erase_high) {
  68. *(unsigned long *)erase_low = poison;
  69. erase_low += sizeof(unsigned long);
  70. }
  71. }
  72. #endif
  73. static __always_inline void __stackleak_erase(bool on_task_stack)
  74. {
  75. const unsigned long task_stack_low = stackleak_task_low_bound(current);
  76. const unsigned long task_stack_high = stackleak_task_high_bound(current);
  77. unsigned long erase_low, erase_high;
  78. erase_low = stackleak_find_top_of_poison(task_stack_low,
  79. current->lowest_stack);
  80. #ifdef CONFIG_STACKLEAK_METRICS
  81. current->prev_lowest_stack = erase_low;
  82. #endif
  83. /*
  84. * Write poison to the task's stack between 'erase_low' and
  85. * 'erase_high'.
  86. *
  87. * If we're running on a different stack (e.g. an entry trampoline
  88. * stack) we can erase everything below the pt_regs at the top of the
  89. * task stack.
  90. *
  91. * If we're running on the task stack itself, we must not clobber any
  92. * stack used by this function and its caller. We assume that this
  93. * function has a fixed-size stack frame, and the current stack pointer
  94. * doesn't change while we write poison.
  95. */
  96. if (on_task_stack)
  97. erase_high = current_stack_pointer;
  98. else
  99. erase_high = task_stack_high;
  100. __stackleak_poison(erase_low, erase_high, STACKLEAK_POISON);
  101. /* Reset the 'lowest_stack' value for the next syscall */
  102. current->lowest_stack = task_stack_high;
  103. }
  104. /*
  105. * Erase and poison the portion of the task stack used since the last erase.
  106. * Can be called from the task stack or an entry stack when the task stack is
  107. * no longer in use.
  108. */
  109. asmlinkage void noinstr stackleak_erase(void)
  110. {
  111. if (skip_erasing())
  112. return;
  113. __stackleak_erase(on_thread_stack());
  114. }
  115. /*
  116. * Erase and poison the portion of the task stack used since the last erase.
  117. * Can only be called from the task stack.
  118. */
  119. asmlinkage void noinstr stackleak_erase_on_task_stack(void)
  120. {
  121. if (skip_erasing())
  122. return;
  123. __stackleak_erase(true);
  124. }
  125. /*
  126. * Erase and poison the portion of the task stack used since the last erase.
  127. * Can only be called from a stack other than the task stack.
  128. */
  129. asmlinkage void noinstr stackleak_erase_off_task_stack(void)
  130. {
  131. if (skip_erasing())
  132. return;
  133. __stackleak_erase(false);
  134. }
  135. void __used __no_caller_saved_registers noinstr stackleak_track_stack(void)
  136. {
  137. unsigned long sp = current_stack_pointer;
  138. /*
  139. * Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
  140. * STACKLEAK_SEARCH_DEPTH makes the poison search in
  141. * stackleak_erase() unreliable. Let's prevent that.
  142. */
  143. BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE > STACKLEAK_SEARCH_DEPTH);
  144. /* 'lowest_stack' should be aligned on the register width boundary */
  145. sp = ALIGN(sp, sizeof(unsigned long));
  146. if (sp < current->lowest_stack &&
  147. sp >= stackleak_task_low_bound(current)) {
  148. current->lowest_stack = sp;
  149. }
  150. }
  151. EXPORT_SYMBOL(stackleak_track_stack);