stackprotector.h 1.04 KB
Newer Older
Ingo Molnar's avatar
Ingo Molnar committed
1 2 3
#ifndef _ASM_STACKPROTECTOR_H
#define _ASM_STACKPROTECTOR_H 1

4 5
#include <asm/tsc.h>

6 7 8 9 10 11 12 13
/*
 * Initialize the stackprotector canary value.
 *
 * NOTE: this must only be called from functions that never return,
 * and it must always be inlined.
 */
static __always_inline void boot_init_stack_canary(void)
{
14 15 16
	u64 canary;
	u64 tsc;

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

	current->stack_canary = canary;
	write_pda(stack_canary, canary);
36 37
}

Ingo Molnar's avatar
Ingo Molnar committed
38
#endif