stackleak.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code tests that the current task stack is properly erased (filled
  4. * with STACKLEAK_POISON).
  5. *
  6. * Authors:
  7. * Alexander Popov <[email protected]>
  8. * Tycho Andersen <[email protected]>
  9. */
  10. #include "lkdtm.h"
  11. #include <linux/stackleak.h>
  12. #if defined(CONFIG_GCC_PLUGIN_STACKLEAK)
  13. /*
  14. * Check that stackleak tracks the lowest stack pointer and erases the stack
  15. * below this as expected.
  16. *
  17. * To prevent the lowest stack pointer changing during the test, IRQs are
  18. * masked and instrumentation of this function is disabled. We assume that the
  19. * compiler will create a fixed-size stack frame for this function.
  20. *
  21. * Any non-inlined function may make further use of the stack, altering the
  22. * lowest stack pointer and/or clobbering poison values. To avoid spurious
  23. * failures we must avoid printing until the end of the test or have already
  24. * encountered a failure condition.
  25. */
  26. static void noinstr check_stackleak_irqoff(void)
  27. {
  28. const unsigned long task_stack_base = (unsigned long)task_stack_page(current);
  29. const unsigned long task_stack_low = stackleak_task_low_bound(current);
  30. const unsigned long task_stack_high = stackleak_task_high_bound(current);
  31. const unsigned long current_sp = current_stack_pointer;
  32. const unsigned long lowest_sp = current->lowest_stack;
  33. unsigned long untracked_high;
  34. unsigned long poison_high, poison_low;
  35. bool test_failed = false;
  36. /*
  37. * Check that the current and lowest recorded stack pointer values fall
  38. * within the expected task stack boundaries. These tests should never
  39. * fail unless the boundaries are incorrect or we're clobbering the
  40. * STACK_END_MAGIC, and in either casee something is seriously wrong.
  41. */
  42. if (current_sp < task_stack_low || current_sp >= task_stack_high) {
  43. instrumentation_begin();
  44. pr_err("FAIL: current_stack_pointer (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
  45. current_sp, task_stack_low, task_stack_high - 1);
  46. test_failed = true;
  47. goto out;
  48. }
  49. if (lowest_sp < task_stack_low || lowest_sp >= task_stack_high) {
  50. instrumentation_begin();
  51. pr_err("FAIL: current->lowest_stack (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
  52. lowest_sp, task_stack_low, task_stack_high - 1);
  53. test_failed = true;
  54. goto out;
  55. }
  56. /*
  57. * Depending on what has run prior to this test, the lowest recorded
  58. * stack pointer could be above or below the current stack pointer.
  59. * Start from the lowest of the two.
  60. *
  61. * Poison values are naturally-aligned unsigned longs. As the current
  62. * stack pointer might not be sufficiently aligned, we must align
  63. * downwards to find the lowest known stack pointer value. This is the
  64. * high boundary for a portion of the stack which may have been used
  65. * without being tracked, and has to be scanned for poison.
  66. */
  67. untracked_high = min(current_sp, lowest_sp);
  68. untracked_high = ALIGN_DOWN(untracked_high, sizeof(unsigned long));
  69. /*
  70. * Find the top of the poison in the same way as the erasing code.
  71. */
  72. poison_high = stackleak_find_top_of_poison(task_stack_low, untracked_high);
  73. /*
  74. * Check whether the poisoned portion of the stack (if any) consists
  75. * entirely of poison. This verifies the entries that
  76. * stackleak_find_top_of_poison() should have checked.
  77. */
  78. poison_low = poison_high;
  79. while (poison_low > task_stack_low) {
  80. poison_low -= sizeof(unsigned long);
  81. if (*(unsigned long *)poison_low == STACKLEAK_POISON)
  82. continue;
  83. instrumentation_begin();
  84. pr_err("FAIL: non-poison value %lu bytes below poison boundary: 0x%lx\n",
  85. poison_high - poison_low, *(unsigned long *)poison_low);
  86. test_failed = true;
  87. goto out;
  88. }
  89. instrumentation_begin();
  90. pr_info("stackleak stack usage:\n"
  91. " high offset: %lu bytes\n"
  92. " current: %lu bytes\n"
  93. " lowest: %lu bytes\n"
  94. " tracked: %lu bytes\n"
  95. " untracked: %lu bytes\n"
  96. " poisoned: %lu bytes\n"
  97. " low offset: %lu bytes\n",
  98. task_stack_base + THREAD_SIZE - task_stack_high,
  99. task_stack_high - current_sp,
  100. task_stack_high - lowest_sp,
  101. task_stack_high - untracked_high,
  102. untracked_high - poison_high,
  103. poison_high - task_stack_low,
  104. task_stack_low - task_stack_base);
  105. out:
  106. if (test_failed) {
  107. pr_err("FAIL: the thread stack is NOT properly erased!\n");
  108. } else {
  109. pr_info("OK: the rest of the thread stack is properly erased\n");
  110. }
  111. instrumentation_end();
  112. }
  113. static void lkdtm_STACKLEAK_ERASING(void)
  114. {
  115. unsigned long flags;
  116. local_irq_save(flags);
  117. check_stackleak_irqoff();
  118. local_irq_restore(flags);
  119. }
  120. #else /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
  121. static void lkdtm_STACKLEAK_ERASING(void)
  122. {
  123. if (IS_ENABLED(CONFIG_HAVE_ARCH_STACKLEAK)) {
  124. pr_err("XFAIL: stackleak is not enabled (CONFIG_GCC_PLUGIN_STACKLEAK=n)\n");
  125. } else {
  126. pr_err("XFAIL: stackleak is not supported on this arch (HAVE_ARCH_STACKLEAK=n)\n");
  127. }
  128. }
  129. #endif /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
  130. static struct crashtype crashtypes[] = {
  131. CRASHTYPE(STACKLEAK_ERASING),
  132. };
  133. struct crashtype_category stackleak_crashtypes = {
  134. .crashtypes = crashtypes,
  135. .len = ARRAY_SIZE(crashtypes),
  136. };