stackprotector.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 requires it to be at a fixed offset from %gs.
  9. * On x86_64, the offset is 40 bytes and on x86_32 20 bytes. x86_64
  10. * and x86_32 use segment registers differently and thus handles this
  11. * requirement differently.
  12. *
  13. * On x86_64, %gs is shared by percpu area and stack canary. All
  14. * percpu symbols are zero based and %gs points to the base of percpu
  15. * area. The first occupant of the percpu area is always
  16. * irq_stack_union which contains stack_canary at offset 40. Userland
  17. * %gs is always saved and restored on kernel entry and exit using
  18. * swapgs, so stack protector doesn't add any complexity there.
  19. *
  20. * On x86_32, it's slightly more complicated. As in x86_64, %gs is
  21. * used for userland TLS. Unfortunately, some processors are much
  22. * slower at loading segment registers with different value when
  23. * entering and leaving the kernel, so the kernel uses %fs for percpu
  24. * area and manages %gs lazily so that %gs is switched only when
  25. * necessary, usually during task switch.
  26. *
  27. * As gcc requires the stack canary at %gs:20, %gs can't be managed
  28. * lazily if stack protector is enabled, so the kernel saves and
  29. * restores userland %gs on kernel entry and exit. This behavior is
  30. * controlled by CONFIG_X86_32_LAZY_GS and accessors are defined in
  31. * system.h to hide the details.
  32. */
  33. #ifndef _ASM_STACKPROTECTOR_H
  34. #define _ASM_STACKPROTECTOR_H 1
  35. #ifdef CONFIG_CC_STACKPROTECTOR
  36. #include <asm/tsc.h>
  37. #include <asm/processor.h>
  38. #include <asm/percpu.h>
  39. #include <asm/desc.h>
  40. #include <linux/random.h>
  41. #include <linux/sched.h>
  42. /*
  43. * 24 byte read-only segment initializer for stack canary. Linker
  44. * can't handle the address bit shifting. Address will be set in
  45. * head_32 for boot CPU and setup_per_cpu_areas() for others.
  46. */
  47. #define GDT_STACK_CANARY_INIT \
  48. [GDT_ENTRY_STACK_CANARY] = GDT_ENTRY_INIT(0x4090, 0, 0x18),
  49. /*
  50. * Initialize the stackprotector canary value.
  51. *
  52. * NOTE: this must only be called from functions that never return,
  53. * and it must always be inlined.
  54. */
  55. static __always_inline void boot_init_stack_canary(void)
  56. {
  57. u64 canary;
  58. u64 tsc;
  59. #ifdef CONFIG_X86_64
  60. BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
  61. #endif
  62. /*
  63. * We both use the random pool and the current TSC as a source
  64. * of randomness. The TSC only matters for very early init,
  65. * there it already has some randomness on most systems. Later
  66. * on during the bootup the random pool has true entropy too.
  67. */
  68. get_random_bytes(&canary, sizeof(canary));
  69. tsc = rdtsc();
  70. canary += tsc + (tsc << 32UL);
  71. canary &= CANARY_MASK;
  72. current->stack_canary = canary;
  73. #ifdef CONFIG_X86_64
  74. this_cpu_write(irq_stack_union.stack_canary, canary);
  75. #else
  76. this_cpu_write(stack_canary.canary, canary);
  77. #endif
  78. }
  79. static inline void setup_stack_canary_segment(int cpu)
  80. {
  81. #ifdef CONFIG_X86_32
  82. unsigned long canary = (unsigned long)&per_cpu(stack_canary, cpu);
  83. struct desc_struct *gdt_table = get_cpu_gdt_rw(cpu);
  84. struct desc_struct desc;
  85. desc = gdt_table[GDT_ENTRY_STACK_CANARY];
  86. set_desc_base(&desc, canary);
  87. write_gdt_entry(gdt_table, GDT_ENTRY_STACK_CANARY, &desc, DESCTYPE_S);
  88. #endif
  89. }
  90. static inline void load_stack_canary_segment(void)
  91. {
  92. #ifdef CONFIG_X86_32
  93. asm("mov %0, %%gs" : : "r" (__KERNEL_STACK_CANARY) : "memory");
  94. #endif
  95. }
  96. #else /* CC_STACKPROTECTOR */
  97. #define GDT_STACK_CANARY_INIT
  98. /* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
  99. static inline void setup_stack_canary_segment(int cpu)
  100. { }
  101. static inline void load_stack_canary_segment(void)
  102. {
  103. #ifdef CONFIG_X86_32
  104. asm volatile ("mov %0, %%gs" : : "r" (0));
  105. #endif
  106. }
  107. #endif /* CC_STACKPROTECTOR */
  108. #endif /* _ASM_STACKPROTECTOR_H */