stackprotector.h 982 B

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