stackprotector.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef _ASM_STACKPROTECTOR_H
  2. #define _ASM_STACKPROTECTOR_H 1
  3. #ifdef CONFIG_CC_STACKPROTECTOR
  4. #include <asm/tsc.h>
  5. #include <asm/processor.h>
  6. #include <asm/percpu.h>
  7. #include <linux/random.h>
  8. /*
  9. * Initialize the stackprotector canary value.
  10. *
  11. * NOTE: this must only be called from functions that never return,
  12. * and it must always be inlined.
  13. */
  14. static __always_inline void boot_init_stack_canary(void)
  15. {
  16. u64 canary;
  17. u64 tsc;
  18. /*
  19. * Build time only check to make sure the stack_canary is at
  20. * offset 40 in the pda; this is a gcc ABI requirement
  21. */
  22. BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
  23. /*
  24. * We both use the random pool and the current TSC as a source
  25. * of randomness. The TSC only matters for very early init,
  26. * there it already has some randomness on most systems. Later
  27. * on during the bootup the random pool has true entropy too.
  28. */
  29. get_random_bytes(&canary, sizeof(canary));
  30. tsc = __native_read_tsc();
  31. canary += tsc + (tsc << 32UL);
  32. current->stack_canary = canary;
  33. percpu_write(irq_stack_union.stack_canary, canary);
  34. }
  35. #endif /* CC_STACKPROTECTOR */
  36. #endif /* _ASM_STACKPROTECTOR_H */