stackprotector.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * GCC stack protector support.
  4. *
  5. * Stack protector works by putting predefined pattern at the start of
  6. * the stack frame and verifying that it hasn't been overwritten when
  7. * returning from the function. The pattern is called stack canary
  8. * and unfortunately gcc historically required it to be at a fixed offset
  9. * from the percpu segment base. On x86_64, the offset is 40 bytes.
  10. *
  11. * The same segment is shared by percpu area and stack canary. On
  12. * x86_64, percpu symbols are zero based and %gs (64-bit) points to the
  13. * base of percpu area. The first occupant of the percpu area is always
  14. * fixed_percpu_data which contains stack_canary at the appropriate
  15. * offset. On x86_32, the stack canary is just a regular percpu
  16. * variable.
  17. *
  18. * Putting percpu data in %fs on 32-bit is a minor optimization compared to
  19. * using %gs. Since 32-bit userspace normally has %fs == 0, we are likely
  20. * to load 0 into %fs on exit to usermode, whereas with percpu data in
  21. * %gs, we are likely to load a non-null %gs on return to user mode.
  22. *
  23. * Once we are willing to require GCC 8.1 or better for 64-bit stackprotector
  24. * support, we can remove some of this complexity.
  25. */
  26. #ifndef _ASM_STACKPROTECTOR_H
  27. #define _ASM_STACKPROTECTOR_H 1
  28. #ifdef CONFIG_STACKPROTECTOR
  29. #include <asm/tsc.h>
  30. #include <asm/processor.h>
  31. #include <asm/percpu.h>
  32. #include <asm/desc.h>
  33. #include <linux/random.h>
  34. #include <linux/sched.h>
  35. /*
  36. * Initialize the stackprotector canary value.
  37. *
  38. * NOTE: this must only be called from functions that never return
  39. * and it must always be inlined.
  40. *
  41. * In addition, it should be called from a compilation unit for which
  42. * stack protector is disabled. Alternatively, the caller should not end
  43. * with a function call which gets tail-call optimized as that would
  44. * lead to checking a modified canary value.
  45. */
  46. static __always_inline void boot_init_stack_canary(void)
  47. {
  48. u64 canary;
  49. u64 tsc;
  50. #ifdef CONFIG_X86_64
  51. BUILD_BUG_ON(offsetof(struct fixed_percpu_data, stack_canary) != 40);
  52. #endif
  53. /*
  54. * We both use the random pool and the current TSC as a source
  55. * of randomness. The TSC only matters for very early init,
  56. * there it already has some randomness on most systems. Later
  57. * on during the bootup the random pool has true entropy too.
  58. */
  59. get_random_bytes(&canary, sizeof(canary));
  60. tsc = rdtsc();
  61. canary += tsc + (tsc << 32UL);
  62. canary &= CANARY_MASK;
  63. current->stack_canary = canary;
  64. #ifdef CONFIG_X86_64
  65. this_cpu_write(fixed_percpu_data.stack_canary, canary);
  66. #else
  67. this_cpu_write(__stack_chk_guard, canary);
  68. #endif
  69. }
  70. static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
  71. {
  72. #ifdef CONFIG_X86_64
  73. per_cpu(fixed_percpu_data.stack_canary, cpu) = idle->stack_canary;
  74. #else
  75. per_cpu(__stack_chk_guard, cpu) = idle->stack_canary;
  76. #endif
  77. }
  78. #else /* STACKPROTECTOR */
  79. /* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
  80. static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
  81. { }
  82. #endif /* STACKPROTECTOR */
  83. #endif /* _ASM_STACKPROTECTOR_H */