Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * GCC stack protector support.
0004  *
0005  * Stack protector works by putting predefined pattern at the start of
0006  * the stack frame and verifying that it hasn't been overwritten when
0007  * returning from the function.  The pattern is called stack canary
0008  * and unfortunately gcc historically required it to be at a fixed offset
0009  * from the percpu segment base.  On x86_64, the offset is 40 bytes.
0010  *
0011  * The same segment is shared by percpu area and stack canary.  On
0012  * x86_64, percpu symbols are zero based and %gs (64-bit) points to the
0013  * base of percpu area.  The first occupant of the percpu area is always
0014  * fixed_percpu_data which contains stack_canary at the appropriate
0015  * offset.  On x86_32, the stack canary is just a regular percpu
0016  * variable.
0017  *
0018  * Putting percpu data in %fs on 32-bit is a minor optimization compared to
0019  * using %gs.  Since 32-bit userspace normally has %fs == 0, we are likely
0020  * to load 0 into %fs on exit to usermode, whereas with percpu data in
0021  * %gs, we are likely to load a non-null %gs on return to user mode.
0022  *
0023  * Once we are willing to require GCC 8.1 or better for 64-bit stackprotector
0024  * support, we can remove some of this complexity.
0025  */
0026 
0027 #ifndef _ASM_STACKPROTECTOR_H
0028 #define _ASM_STACKPROTECTOR_H 1
0029 
0030 #ifdef CONFIG_STACKPROTECTOR
0031 
0032 #include <asm/tsc.h>
0033 #include <asm/processor.h>
0034 #include <asm/percpu.h>
0035 #include <asm/desc.h>
0036 
0037 #include <linux/random.h>
0038 #include <linux/sched.h>
0039 
0040 /*
0041  * Initialize the stackprotector canary value.
0042  *
0043  * NOTE: this must only be called from functions that never return
0044  * and it must always be inlined.
0045  *
0046  * In addition, it should be called from a compilation unit for which
0047  * stack protector is disabled. Alternatively, the caller should not end
0048  * with a function call which gets tail-call optimized as that would
0049  * lead to checking a modified canary value.
0050  */
0051 static __always_inline void boot_init_stack_canary(void)
0052 {
0053     u64 canary;
0054     u64 tsc;
0055 
0056 #ifdef CONFIG_X86_64
0057     BUILD_BUG_ON(offsetof(struct fixed_percpu_data, stack_canary) != 40);
0058 #endif
0059     /*
0060      * We both use the random pool and the current TSC as a source
0061      * of randomness. The TSC only matters for very early init,
0062      * there it already has some randomness on most systems. Later
0063      * on during the bootup the random pool has true entropy too.
0064      */
0065     get_random_bytes(&canary, sizeof(canary));
0066     tsc = rdtsc();
0067     canary += tsc + (tsc << 32UL);
0068     canary &= CANARY_MASK;
0069 
0070     current->stack_canary = canary;
0071 #ifdef CONFIG_X86_64
0072     this_cpu_write(fixed_percpu_data.stack_canary, canary);
0073 #else
0074     this_cpu_write(__stack_chk_guard, canary);
0075 #endif
0076 }
0077 
0078 static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
0079 {
0080 #ifdef CONFIG_X86_64
0081     per_cpu(fixed_percpu_data.stack_canary, cpu) = idle->stack_canary;
0082 #else
0083     per_cpu(__stack_chk_guard, cpu) = idle->stack_canary;
0084 #endif
0085 }
0086 
0087 #else   /* STACKPROTECTOR */
0088 
0089 /* dummy boot_init_stack_canary() is defined in linux/stackprotector.h */
0090 
0091 static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
0092 { }
0093 
0094 #endif  /* STACKPROTECTOR */
0095 #endif  /* _ASM_STACKPROTECTOR_H */