Re: [PATCH Part1 v5 23/38] x86/head/64: set up a startup %gs for stack protector

From: Joerg Roedel
Date: Wed Aug 25 2021 - 11:07:45 EST


On Fri, Aug 20, 2021 at 10:19:18AM -0500, Brijesh Singh wrote:
> void __head startup_64_setup_env(unsigned long physbase)
> {
> + u64 gs_area = (u64)fixup_pointer(startup_gs_area, physbase);
> +

This breaks as soon as the compiler decides that startup_64_setup_env()
needs stack protection too.

And the startup_gs_area is also not needed, there is initial_gs for
that.

What you need is something along these lines (untested):

diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index d8b3ebd2bb85..3c7c59bc9903 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -65,6 +65,16 @@ SYM_CODE_START_NOALIGN(startup_64)
leaq (__end_init_task - FRAME_SIZE)(%rip), %rsp

leaq _text(%rip), %rdi
+
+ movl $MSR_GS_BASE, %ecx
+ movq initial_gs(%rip), %rax
+ movq $_text, %rdx
+ subq %rdx, %rax
+ addq %rdi, %rax
+ movq %rax, %rdx
+ shrq $32, %rdx
+ wrmsr
+
pushq %rsi
call startup_64_setup_env
popq %rsi


It loads the initial_gs pointer, applies the fixup on it and loads it
into MSR_GS_BASE.