stackleak.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifdef CONFIG_STACKLEAK_RUNTIME_DISABLE
  14. #include <linux/jump_label.h>
  15. #include <linux/sysctl.h>
  16. static DEFINE_STATIC_KEY_FALSE(stack_erasing_bypass);
  17. int stack_erasing_sysctl(struct ctl_table *table, int write,
  18. void __user *buffer, size_t *lenp, loff_t *ppos)
  19. {
  20. int ret = 0;
  21. int state = !static_branch_unlikely(&stack_erasing_bypass);
  22. int prev_state = state;
  23. table->data = &state;
  24. table->maxlen = sizeof(int);
  25. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  26. state = !!state;
  27. if (ret || !write || state == prev_state)
  28. return ret;
  29. if (state)
  30. static_branch_disable(&stack_erasing_bypass);
  31. else
  32. static_branch_enable(&stack_erasing_bypass);
  33. pr_warn("stackleak: kernel stack erasing is %s\n",
  34. state ? "enabled" : "disabled");
  35. return ret;
  36. }
  37. #define skip_erasing() static_branch_unlikely(&stack_erasing_bypass)
  38. #else
  39. #define skip_erasing() false
  40. #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */
  41. asmlinkage void stackleak_erase(void)
  42. {
  43. /* It would be nice not to have 'kstack_ptr' and 'boundary' on stack */
  44. unsigned long kstack_ptr = current->lowest_stack;
  45. unsigned long boundary = (unsigned long)end_of_stack(current);
  46. unsigned int poison_count = 0;
  47. const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
  48. if (skip_erasing())
  49. return;
  50. /* Check that 'lowest_stack' value is sane */
  51. if (unlikely(kstack_ptr - boundary >= THREAD_SIZE))
  52. kstack_ptr = boundary;
  53. /* Search for the poison value in the kernel stack */
  54. while (kstack_ptr > boundary && poison_count <= depth) {
  55. if (*(unsigned long *)kstack_ptr == STACKLEAK_POISON)
  56. poison_count++;
  57. else
  58. poison_count = 0;
  59. kstack_ptr -= sizeof(unsigned long);
  60. }
  61. /*
  62. * One 'long int' at the bottom of the thread stack is reserved and
  63. * should not be poisoned (see CONFIG_SCHED_STACK_END_CHECK=y).
  64. */
  65. if (kstack_ptr == boundary)
  66. kstack_ptr += sizeof(unsigned long);
  67. #ifdef CONFIG_STACKLEAK_METRICS
  68. current->prev_lowest_stack = kstack_ptr;
  69. #endif
  70. /*
  71. * Now write the poison value to the kernel stack. Start from
  72. * 'kstack_ptr' and move up till the new 'boundary'. We assume that
  73. * the stack pointer doesn't change when we write poison.
  74. */
  75. if (on_thread_stack())
  76. boundary = current_stack_pointer;
  77. else
  78. boundary = current_top_of_stack();
  79. while (kstack_ptr < boundary) {
  80. *(unsigned long *)kstack_ptr = STACKLEAK_POISON;
  81. kstack_ptr += sizeof(unsigned long);
  82. }
  83. /* Reset the 'lowest_stack' value for the next syscall */
  84. current->lowest_stack = current_top_of_stack() - THREAD_SIZE/64;
  85. }
  86. void __used stackleak_track_stack(void)
  87. {
  88. /*
  89. * N.B. stackleak_erase() fills the kernel stack with the poison value,
  90. * which has the register width. That code assumes that the value
  91. * of 'lowest_stack' is aligned on the register width boundary.
  92. *
  93. * That is true for x86 and x86_64 because of the kernel stack
  94. * alignment on these platforms (for details, see 'cc_stack_align' in
  95. * arch/x86/Makefile). Take care of that when you port STACKLEAK to
  96. * new platforms.
  97. */
  98. unsigned long sp = (unsigned long)&sp;
  99. /*
  100. * Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
  101. * STACKLEAK_SEARCH_DEPTH makes the poison search in
  102. * stackleak_erase() unreliable. Let's prevent that.
  103. */
  104. BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE > STACKLEAK_SEARCH_DEPTH);
  105. if (sp < current->lowest_stack &&
  106. sp >= (unsigned long)task_stack_page(current) +
  107. sizeof(unsigned long)) {
  108. current->lowest_stack = sp;
  109. }
  110. }
  111. EXPORT_SYMBOL(stackleak_track_stack);