stackprotector.h 1.1 KB
Newer Older
I
Ingo Molnar 已提交
1 2 3
#ifndef _ASM_STACKPROTECTOR_H
#define _ASM_STACKPROTECTOR_H 1

T
Tejun Heo 已提交
4 5
#ifdef CONFIG_CC_STACKPROTECTOR

6
#include <asm/tsc.h>
7
#include <asm/processor.h>
T
Tejun Heo 已提交
8 9
#include <asm/percpu.h>
#include <linux/random.h>
10

11 12 13 14 15 16 17 18
/*
 * 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)
{
19 20 21
	u64 canary;
	u64 tsc;

22
	/*
T
Tejun Heo 已提交
23 24 25
	 * Build time only check to make sure the stack_canary is at
	 * offset 40 in the pda; this is a gcc ABI requirement
	 */
26
	BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
T
Tejun Heo 已提交
27 28

	/*
29 30 31 32
	 * 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.
33
	 */
34 35 36 37 38
	get_random_bytes(&canary, sizeof(canary));
	tsc = __native_read_tsc();
	canary += tsc + (tsc << 32UL);

	current->stack_canary = canary;
39
	percpu_write(irq_stack_union.stack_canary, canary);
40 41
}

T
Tejun Heo 已提交
42 43
#endif	/* CC_STACKPROTECTOR */
#endif	/* _ASM_STACKPROTECTOR_H */