~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/include/asm/stackprotector.h

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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 
 33 #ifndef _ASM_STACKPROTECTOR_H
 34 #define _ASM_STACKPROTECTOR_H 1
 35 
 36 #ifdef CONFIG_CC_STACKPROTECTOR
 37 
 38 #include <asm/tsc.h>
 39 #include <asm/processor.h>
 40 #include <asm/percpu.h>
 41 #include <asm/desc.h>
 42 #include <linux/random.h>
 43 
 44 /*
 45  * 24 byte read-only segment initializer for stack canary.  Linker
 46  * can't handle the address bit shifting.  Address will be set in
 47  * head_32 for boot CPU and setup_per_cpu_areas() for others.
 48  */
 49 #define GDT_STACK_CANARY_INIT                                           \
 50         [GDT_ENTRY_STACK_CANARY] = GDT_ENTRY_INIT(0x4090, 0, 0x18),
 51 
 52 /*
 53  * Initialize the stackprotector canary value.
 54  *
 55  * NOTE: this must only be called from functions that never return,
 56  * and it must always be inlined.
 57  */
 58 static __always_inline void boot_init_stack_canary(void)
 59 {
 60         u64 canary;
 61         u64 tsc;
 62 
 63 #ifdef CONFIG_X86_64
 64         BUILD_BUG_ON(offsetof(union irq_stack_union, stack_canary) != 40);
 65 #endif
 66         /*
 67          * We both use the random pool and the current TSC as a source
 68          * of randomness. The TSC only matters for very early init,
 69          * there it already has some randomness on most systems. Later
 70          * on during the bootup the random pool has true entropy too.
 71          */
 72         get_random_bytes(&canary, sizeof(canary));
 73         tsc = __native_read_tsc();
 74         canary += tsc + (tsc << 32UL);
 75 
 76         current->stack_canary = canary;
 77 #ifdef CONFIG_X86_64
 78         this_cpu_write(irq_stack_union.stack_canary, canary);
 79 #else
 80         this_cpu_write(stack_canary.canary, canary);
 81 #endif
 82 }
 83 
 84 static inline void setup_stack_canary_segment(int cpu)
 85 {
 86 #ifdef CONFIG_X86_32
 87         unsigned long canary = (unsigned long)&per_cpu(stack_canary, cpu);
 88         struct desc_struct *gdt_table = get_cpu_gdt_table(cpu);
 89         struct desc_struct desc;
 90 
 91         desc = gdt_table[GDT_ENTRY_STACK_CANARY];
 92         set_desc_base(&desc, canary);
 93         write_gdt_entry(gdt_table, GDT_ENTRY_STACK_CANARY, &desc, DESCTYPE_S);
 94 #endif
 95 }
 96 
 97 static inline void load_stack_canary_segment(void)
 98 {
 99 #ifdef CONFIG_X86_32
100         asm("mov %0, %%gs" : : "r" (__KERNEL_STACK_CANARY) : "memory");
101 #endif
102 }
103 
104 #else   /* CC_STACKPROTECTOR */
105 
106 #define GDT_STACK_CANARY_INIT
107 
108 /* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
109 
110 static inline void setup_stack_canary_segment(int cpu)
111 { }
112 
113 static inline void load_stack_canary_segment(void)
114 {
115 #ifdef CONFIG_X86_32
116         asm volatile ("mov %0, %%gs" : : "r" (0));
117 #endif
118 }
119 
120 #endif  /* CC_STACKPROTECTOR */
121 #endif  /* _ASM_STACKPROTECTOR_H */
122 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp