stackprotector.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. * If we're the non-boot CPU, nothing set the PDA stack
  17. * canary up for us - and if we are the boot CPU we have
  18. * a 0 stack canary. This is a good place for updating
  19. * it, as we wont ever return from this function (so the
  20. * invalid canaries already on the stack wont ever
  21. * trigger).
  22. *
  23. * We both use the random pool and the current TSC as a source
  24. * of randomness. The TSC only matters for very early init,
  25. * there it already has some randomness on most systems. Later
  26. * on during the bootup the random pool has true entropy too.
  27. */
  28. get_random_bytes(&canary, sizeof(canary));
  29. tsc = __native_read_tsc();
  30. canary += tsc + (tsc << 32UL);
  31. current->stack_canary = canary;
  32. write_pda(stack_canary, canary);
  33. }
  34. #endif