stackprotector.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef _ASM_STACKPROTECTOR_H
  2. #define _ASM_STACKPROTECTOR_H 1
  3. #include <asm/tsc.h>
  4. /*
  5. * Initialize the stackprotector canary value.
  6. *
  7. * NOTE: this must only be called from functions that never return,
  8. * and it must always be inlined.
  9. */
  10. static __always_inline void boot_init_stack_canary(void)
  11. {
  12. u64 canary;
  13. u64 tsc;
  14. /*
  15. * If we're the non-boot CPU, nothing set the PDA stack
  16. * canary up for us - and if we are the boot CPU we have
  17. * a 0 stack canary. This is a good place for updating
  18. * it, as we wont ever return from this function (so the
  19. * invalid canaries already on the stack wont ever
  20. * trigger).
  21. *
  22. * We both use the random pool and the current TSC as a source
  23. * of randomness. The TSC only matters for very early init,
  24. * there it already has some randomness on most systems. Later
  25. * on during the bootup the random pool has true entropy too.
  26. */
  27. get_random_bytes(&canary, sizeof(canary));
  28. tsc = __native_read_tsc();
  29. canary += tsc + (tsc << 32UL);
  30. current->stack_canary = canary;
  31. write_pda(stack_canary, canary);
  32. }
  33. #endif