blob: 8abedf1d650e9648f2a9864adf9ee815b000c0af [file] [log] [blame]
Tejun Heo60a53172009-02-09 22:17:40 +09001/*
2 * GCC stack protector support.
3 *
4 * Stack protector works by putting predefined pattern at the start of
5 * the stack frame and verifying that it hasn't been overwritten when
6 * returning from the function. The pattern is called stack canary
7 * and unfortunately gcc requires it to be at a fixed offset from %gs.
8 * On x86_64, the offset is 40 bytes and on x86_32 20 bytes. x86_64
9 * and x86_32 use segment registers differently and thus handles this
10 * requirement differently.
11 *
12 * On x86_64, %gs is shared by percpu area and stack canary. All
13 * percpu symbols are zero based and %gs points to the base of percpu
14 * area. The first occupant of the percpu area is always
15 * irq_stack_union which contains stack_canary at offset 40. Userland
16 * %gs is always saved and restored on kernel entry and exit using
17 * swapgs, so stack protector doesn't add any complexity there.
18 *
19 * On x86_32, it's slightly more complicated. As in x86_64, %gs is
20 * used for userland TLS. Unfortunately, some processors are much
21 * slower at loading segment registers with different value when
22 * entering and leaving the kernel, so the kernel uses %fs for percpu
23 * area and manages %gs lazily so that %gs is switched only when
24 * necessary, usually during task switch.
25 *
26 * As gcc requires the stack canary at %gs:20, %gs can't be managed
27 * lazily if stack protector is enabled, so the kernel saves and
28 * restores userland %gs on kernel entry and exit. This behavior is
29 * controlled by CONFIG_X86_32_LAZY_GS and accessors are defined in
30 * system.h to hide the details.
31 */
32
Ingo Molnarb2b062b2009-01-18 18:37:14 +010033#ifndef _ASM_STACKPROTECTOR_H
34#define _ASM_STACKPROTECTOR_H 1
35
Tejun Heo76397f72009-02-09 22:17:39 +090036#ifdef CONFIG_CC_STACKPROTECTOR
37
Ingo Molnarb2b062b2009-01-18 18:37:14 +010038#include <asm/tsc.h>
Brian Gerst947e76c2009-01-19 12:21:28 +090039#include <asm/processor.h>
Tejun Heo76397f72009-02-09 22:17:39 +090040#include <asm/percpu.h>
Tejun Heo60a53172009-02-09 22:17:40 +090041#include <asm/desc.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020042
Tejun Heo76397f72009-02-09 22:17:39 +090043#include <linux/random.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020044#include <linux/sched.h>
Ingo Molnarb2b062b2009-01-18 18:37:14 +010045
46/*
Tejun Heo60a53172009-02-09 22:17:40 +090047 * 24 byte read-only segment initializer for stack canary. Linker
48 * can't handle the address bit shifting. Address will be set in
49 * head_32 for boot CPU and setup_per_cpu_areas() for others.
50 */
51#define GDT_STACK_CANARY_INIT \
Akinobu Mita1e5de182009-07-19 00:12:20 +090052 [GDT_ENTRY_STACK_CANARY] = GDT_ENTRY_INIT(0x4090, 0, 0x18),
Tejun Heo60a53172009-02-09 22:17:40 +090053
54/*
Ingo Molnarb2b062b2009-01-18 18:37:14 +010055 * Initialize the stackprotector canary value.
56 *
57 * NOTE: this must only be called from functions that never return,
58 * and it must always be inlined.
59 */
60static __always_inline void boot_init_stack_canary(void)
61{
62 u64 canary;
63 u64 tsc;
64
Tejun Heo60a53172009-02-09 22:17:40 +090065#ifdef CONFIG_X86_64
Brian Gerst947e76c2009-01-19 12:21:28 +090066 BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
Tejun Heo60a53172009-02-09 22:17:40 +090067#endif
Tejun Heoc6e50f92009-01-20 12:29:19 +090068 /*
Ingo Molnarb2b062b2009-01-18 18:37:14 +010069 * We both use the random pool and the current TSC as a source
70 * of randomness. The TSC only matters for very early init,
71 * there it already has some randomness on most systems. Later
72 * on during the bootup the random pool has true entropy too.
73 */
74 get_random_bytes(&canary, sizeof(canary));
Andy Lutomirski4ea16362015-06-25 18:44:07 +020075 tsc = rdtsc();
Ingo Molnarb2b062b2009-01-18 18:37:14 +010076 canary += tsc + (tsc << 32UL);
Rik van Rielbf9eb542017-07-12 14:36:23 -070077 canary &= CANARY_MASK;
Ingo Molnarb2b062b2009-01-18 18:37:14 +010078
79 current->stack_canary = canary;
Tejun Heo60a53172009-02-09 22:17:40 +090080#ifdef CONFIG_X86_64
Alex Shic6ae41e2012-05-11 15:35:27 +080081 this_cpu_write(irq_stack_union.stack_canary, canary);
Tejun Heo60a53172009-02-09 22:17:40 +090082#else
Alex Shic6ae41e2012-05-11 15:35:27 +080083 this_cpu_write(stack_canary.canary, canary);
Tejun Heo60a53172009-02-09 22:17:40 +090084#endif
85}
86
87static inline void setup_stack_canary_segment(int cpu)
88{
89#ifdef CONFIG_X86_32
Jeremy Fitzhardinge1ea0d142009-09-03 12:27:15 -070090 unsigned long canary = (unsigned long)&per_cpu(stack_canary, cpu);
Thomas Garnier69218e42017-03-14 10:05:07 -070091 struct desc_struct *gdt_table = get_cpu_gdt_rw(cpu);
Tejun Heo60a53172009-02-09 22:17:40 +090092 struct desc_struct desc;
93
94 desc = gdt_table[GDT_ENTRY_STACK_CANARY];
Akinobu Mita57594742009-07-19 00:11:06 +090095 set_desc_base(&desc, canary);
Tejun Heo60a53172009-02-09 22:17:40 +090096 write_gdt_entry(gdt_table, GDT_ENTRY_STACK_CANARY, &desc, DESCTYPE_S);
97#endif
98}
99
100static inline void load_stack_canary_segment(void)
101{
102#ifdef CONFIG_X86_32
103 asm("mov %0, %%gs" : : "r" (__KERNEL_STACK_CANARY) : "memory");
104#endif
105}
106
107#else /* CC_STACKPROTECTOR */
108
109#define GDT_STACK_CANARY_INIT
110
111/* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
112
113static inline void setup_stack_canary_segment(int cpu)
114{ }
115
116static inline void load_stack_canary_segment(void)
117{
118#ifdef CONFIG_X86_32
119 asm volatile ("mov %0, %%gs" : : "r" (0));
120#endif
Ingo Molnarb2b062b2009-01-18 18:37:14 +0100121}
122
Tejun Heo76397f72009-02-09 22:17:39 +0900123#endif /* CC_STACKPROTECTOR */
124#endif /* _ASM_STACKPROTECTOR_H */