x86: cleanup stack protector
PATCH 1/2 x86: cleanup stack protector
Impact: cleanup
Make the following cleanups.
* remove duplicate comment from boot_init_stack_canary() which fits better in the other place - cpu_idle().
* move stack_canary offset check from __switch_to() to boot_init_stack_canary().
Signed-off-by Tejun Heo <[email protected]>
---
This was part of core/percpu branch before rebasing but really belongs to stackprotector branch.
arch/x86/include/asm/pda.h | 2 -- arch/x86/include/asm/stackprotector.h | 13 ++++++------- arch/x86/kernel/process_64.c | 7 ------- 3 files changed, 6 insertions(+), 16 deletions(-)
arch/x86/include/asm/pda.h +0 -2
@@ -40,6 +40,4 @@ extern void pda_init(int);#endif -#define refresh_stack_canary () write_pda(stack_canary, current->stack_canary) -#endif /* _ASM_X86_PDA_H */
arch/x86/include/asm/stackprotector.h +6 -7
@@ -16,13 +16,12 @@ static __always_inline void boot_init_stack_canary(void)u64 tsc;/* -* If we're the non-boot CPU, nothing set the PDA stack -* canary up for us - and if we are the boot CPU we have -* a 0 stack canary. This is a good place for updating -* it, as we wont ever return from this function (so the -* invalid canaries already on the stack wont ever -* trigger). -* +* Build time only check to make sure the stack_canary is at +* offset 40 in the pda; this is a gcc ABI requirement +*/ +BUILD_BUG_ON (offsetof(struct x8664_pda , stack_canary) !=40 ); + +/* * 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
arch/x86/kernel/process_64.c +0 -7
@@ -638,13 +638,6 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)percpu_write (kernel_stack, (unsigned long )task_stack_page (next_p) +THREAD_SIZE -KERNEL_STACK_OFFSET ); -#ifdef CONFIG_CC_STACKPROTECTOR -/* -* Build time only check to make sure the stack_canary is at -* offset 40 in the pda; this is a gcc ABI requirement -*/ -BUILD_BUG_ON (offsetof(struct x8664_pda , stack_canary) !=40 ); -#endif /* * Now maybe reload the debug registers and handle I/O bitmaps
Signature · 1 line
1.6.0.2
PATCH 2/2 x86: conditionalize stack canary handling in hot path
Impact: no unnecessary stack canary swapping during context switch
There's no point in moving stack_canary around during context switch if it's not enabled. Conditionalize it.
Signed-off-by Tejun Heo <[email protected]>
---
Ditto as the previous one.
arch/x86/include/asm/system.h | 22 ++++++++++++++++------ 1 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/include/asm/system.h +16 -6
@@ -86,17 +86,28 @@ do { \ , "rcx", "rbx", "rdx", "r8", "r9", "r10", "r11", \ "r12", "r13", "r14", "r15" +#ifdef CONFIG_CC_STACKPROTECTOR +#define __switch_canary \ + "movq %P[task_canary](%%rsi),%%r8\n\t" \ + "movq %%r8,%%gs:%P[pda_canary]\n\t" +#define __switch_canary_param \ + , [task_canary] "i" (offsetof(struct task_struct, stack_canary)) \ + , [pda_canary] "i" (offsetof(struct x8664_pda, stack_canary)) +#else /* CC_STACKPROTECTOR */ +#define __switch_canary +#define __switch_canary_param +#endif /* CC_STACKPROTECTOR */ +/* Save restore flags to clear handle leaking NT */ #define switch_to (prev, next, last) \ - asm volatile(SAVE_CONTEXT \ + asm volatile(SAVE_CONTEXT \ "movq %%rsp,%P[threadrsp](%[prev])\n\t"/* save RSP */ \"movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \"call __switch_to\n\t" \".globl thread_return\n" \"thread_return:\n\t" \ "movq "__percpu_arg([current_task])",%%rsi\n\t" \ -"movq %P[task_canary](%%rsi),%%r8\n\t" \ -"movq %%r8,%%gs:%P[pda_canary]\n\t" \ + __switch_canary \"movq %P[thread_info](%%rsi),%%r8\n\t" \LOCK_PREFIX "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \"movq %%rax,%%rdi\n\t" \
@@ -108,9 +119,8 @@ do { \ [ti_flags] "i" (offsetof(struct thread_info, flags)), \[tif_fork] " i" (TIF_FORK), \ [thread_info] " i" (offsetof(struct task_struct, stack)), \ -[task_canary] " i" (offsetof(struct task_struct, stack_canary)),\ -[current_task] " m" (per_cpu_var(current_task)), \ -[pda_canary] " i" (offsetof(struct x8664_pda, stack_canary))\ +[current_task] " m" (per_cpu_var(current_task)) \ +__switch_canary_param \ : "memory", "cc" __EXTRA_CLOBBER) #endif
Signature · 1 line
1.6.0.2