Re: [PATCH -tip] x86/stackprotector: Move stack canary to struct pcpu_hot

From: Brian Gerst
Date: Fri Feb 21 2025 - 07:54:38 EST


On Thu, Feb 20, 2025 at 3:04 PM Uros Bizjak <ubizjak@xxxxxxxxx> wrote:
>
> Move stack canary from __stack_chk_guard to struct pcpu_hot and
> alias __stack_chk_guard to point to the new location in the
> linker script.
>
> __stack_chk_guard is one of the hottest data structures on x86, so
> moving it there makes sense even if its benefit cannot be measured
> explicitly.
>
> Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Borislav Petkov <bp@xxxxxxxxx>
> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
> Cc: Brian Gerst <brgerst@xxxxxxxxx>
> Cc: Ard Biesheuvel <ardb@xxxxxxxxxx>
> ---
> arch/x86/include/asm/current.h | 13 +++++++++++++
> arch/x86/kernel/cpu/common.c | 1 -
> arch/x86/kernel/vmlinux.lds.S | 2 ++
> 3 files changed, 15 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/include/asm/current.h b/arch/x86/include/asm/current.h
> index bf5953883ec3..e4ff1d15b465 100644
> --- a/arch/x86/include/asm/current.h
> +++ b/arch/x86/include/asm/current.h
> @@ -15,6 +15,9 @@ struct task_struct;
> struct pcpu_hot {
> union {
> struct {
> +#ifdef CONFIG_STACKPROTECTOR
> + unsigned long stack_canary;
> +#endif
> struct task_struct *current_task;
> int preempt_count;
> int cpu_number;
> @@ -35,6 +38,16 @@ struct pcpu_hot {
> };
> static_assert(sizeof(struct pcpu_hot) == 64);
>
> +/*
> + * stack_canary should be at the beginning of struct pcpu_hot to avoid:
> + *
> + * Invalid absolute R_X86_64_32S relocation: __stack_chk_guard

This should be R_X86_64_PC32 relocations.

> + *
> + * error when aliasing __stack_chk_guard to struct pcpu_hot
> + * - see arch/x86/kernel/vmlinux.lds.S.
> + */
> +static_assert(offsetof(struct pcpu_hot, stack_canary) == 0);

The simple solution to this is to add the symbol to the whitelist in
tools/relocs.c:
/*
* These symbols are known to be relative, even if the linker marks them
* as absolute (typically defined outside any section in the linker script.)
*/

I just got rid of hardcoding fixed_percpu_data from the start of
percpu memory. I'd rather not add something similar back in.


Brian Gerst