2020-06-04 22:22:08

by Brian Gerst

[permalink] [raw]
Subject: [PATCH] x86/stackprotector: Pre-initialize canary for secondary CPUs

The idle tasks created for each secondary CPU already have a random stack
canary generated by fork(). Copy the canary to the percpu variable before
starting the secondary CPU which removes the need to call
boot_init_stack_canary().

Signed-off-by: Brian Gerst <[email protected]>
---
arch/x86/include/asm/stackprotector.h | 12 ++++++++++++
arch/x86/kernel/smpboot.c | 12 +-----------
arch/x86/xen/smp_pv.c | 2 --
3 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
index 9804a7957f4e..7fb482f0f25b 100644
--- a/arch/x86/include/asm/stackprotector.h
+++ b/arch/x86/include/asm/stackprotector.h
@@ -90,6 +90,15 @@ static __always_inline void boot_init_stack_canary(void)
#endif
}

+static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
+{
+#ifdef CONFIG_X86_64
+ per_cpu(fixed_percpu_data.stack_canary, cpu) = idle->stack_canary;
+#else
+ per_cpu(stack_canary.canary, cpu) = idle->stack_canary;
+#endif
+}
+
static inline void setup_stack_canary_segment(int cpu)
{
#ifdef CONFIG_X86_32
@@ -119,6 +128,9 @@ static inline void load_stack_canary_segment(void)
static inline void setup_stack_canary_segment(int cpu)
{ }

+static inline void cpu_init_stack_canary(int cpu, struct task_struct *idle)
+{ }
+
static inline void load_stack_canary_segment(void)
{
#ifdef CONFIG_X86_32
diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 2467f3dd35d3..dad7f9ca6478 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -259,21 +259,10 @@ static void notrace start_secondary(void *unused)
/* enable local interrupts */
local_irq_enable();

- /* to prevent fake stack check failure in clock setup */
- boot_init_stack_canary();
-
x86_cpuinit.setup_percpu_clockev();

wmb();
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
-
- /*
- * Prevent tail call to cpu_startup_entry() because the stack protector
- * guard has been changed a couple of function calls up, in
- * boot_init_stack_canary() and must not be checked before tail calling
- * another function.
- */
- prevent_tail_call_optimization();
}

/**
@@ -1011,6 +1000,7 @@ int common_cpu_up(unsigned int cpu, struct task_struct *idle)
alternatives_enable_smp();

per_cpu(current_task, cpu) = idle;
+ cpu_init_stack_canary(cpu, idle);

/* Initialize the interrupt stack(s) */
ret = irq_init_percpu_irqstack(cpu);
diff --git a/arch/x86/xen/smp_pv.c b/arch/x86/xen/smp_pv.c
index ae4d0f283df3..e9f5d6ec30a6 100644
--- a/arch/x86/xen/smp_pv.c
+++ b/arch/x86/xen/smp_pv.c
@@ -92,9 +92,7 @@ static void cpu_bringup(void)
asmlinkage __visible void cpu_bringup_and_idle(void)
{
cpu_bringup();
- boot_init_stack_canary();
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
- prevent_tail_call_optimization();
}

void xen_smp_intr_free_pv(unsigned int cpu)

base-commit: cc7a4a02564c6cc8dc981fb0a37313830ee8c2d4
--
2.25.4


2020-06-17 11:33:00

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH] x86/stackprotector: Pre-initialize canary for secondary CPUs

On Thu, Jun 04, 2020 at 10:03:59AM -0400, Brian Gerst wrote:
> The idle tasks created for each secondary CPU already have a random stack
> canary generated by fork(). Copy the canary to the percpu variable before
> starting the secondary CPU which removes the need to call
> boot_init_stack_canary().
>
> Signed-off-by: Brian Gerst <[email protected]>
> ---
> arch/x86/include/asm/stackprotector.h | 12 ++++++++++++
> arch/x86/kernel/smpboot.c | 12 +-----------
> arch/x86/xen/smp_pv.c | 2 --
> 3 files changed, 13 insertions(+), 13 deletions(-)

arch/x86/kernel/smpboot.c: In function ‘common_cpu_up’:
arch/x86/kernel/smpboot.c:1003:2: error: implicit declaration of function ‘cpu_init_stack_canary’; did you mean ‘boot_init_stack_canary’? [-Werror=implicit-function-declaration]
cpu_init_stack_canary(cpu, idle);
make[2]: *** [scripts/Makefile.build:281: arch/x86/kernel/smpboot.o] Error 1
make[1]: *** [scripts/Makefile.build:497: arch/x86/kernel] Error 2
make: *** [Makefile:1764: arch/x86] Error 2

.config attached.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette


Attachments:
(No filename) (1.16 kB)
12-05-52-randconfig-x86_64-8723.cfg (92.52 kB)
Download all attachments