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