stackleak.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STACKLEAK_H
  3. #define _LINUX_STACKLEAK_H
  4. #include <linux/sched.h>
  5. #include <linux/sched/task_stack.h>
  6. /*
  7. * Check that the poison value points to the unused hole in the
  8. * virtual memory map for your platform.
  9. */
  10. #define STACKLEAK_POISON -0xBEEF
  11. #define STACKLEAK_SEARCH_DEPTH 128
  12. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  13. #include <asm/stacktrace.h>
  14. /*
  15. * The lowest address on tsk's stack which we can plausibly erase.
  16. */
  17. static __always_inline unsigned long
  18. stackleak_task_low_bound(const struct task_struct *tsk)
  19. {
  20. /*
  21. * The lowest unsigned long on the task stack contains STACK_END_MAGIC,
  22. * which we must not corrupt.
  23. */
  24. return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
  25. }
  26. /*
  27. * The address immediately after the highest address on tsk's stack which we
  28. * can plausibly erase.
  29. */
  30. static __always_inline unsigned long
  31. stackleak_task_high_bound(const struct task_struct *tsk)
  32. {
  33. /*
  34. * The task's pt_regs lives at the top of the task stack and will be
  35. * overwritten by exception entry, so there's no need to erase them.
  36. */
  37. return (unsigned long)task_pt_regs(tsk);
  38. }
  39. /*
  40. * Find the address immediately above the poisoned region of the stack, where
  41. * that region falls between 'low' (inclusive) and 'high' (exclusive).
  42. */
  43. static __always_inline unsigned long
  44. stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
  45. {
  46. const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
  47. unsigned int poison_count = 0;
  48. unsigned long poison_high = high;
  49. unsigned long sp = high;
  50. while (sp > low && poison_count < depth) {
  51. sp -= sizeof(unsigned long);
  52. if (*(unsigned long *)sp == STACKLEAK_POISON) {
  53. poison_count++;
  54. } else {
  55. poison_count = 0;
  56. poison_high = sp;
  57. }
  58. }
  59. return poison_high;
  60. }
  61. static inline void stackleak_task_init(struct task_struct *t)
  62. {
  63. t->lowest_stack = stackleak_task_low_bound(t);
  64. # ifdef CONFIG_STACKLEAK_METRICS
  65. t->prev_lowest_stack = t->lowest_stack;
  66. # endif
  67. }
  68. #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
  69. static inline void stackleak_task_init(struct task_struct *t) { }
  70. #endif
  71. #endif