Re: [PATCH] x86: Disable kernel stack offset randomization for !TSC

From: Maciej W. Rozycki
Date: Sun Jan 08 2023 - 15:04:23 EST


On Sun, 8 Jan 2023, Borislav Petkov wrote:

> > For x86 kernel stack offset randomization uses the RDTSC instruction,
> > which causes an invalid opcode exception with hardware that does not
> > implement this instruction:
>
> Out of pure curiosity, what hw is that?

processor : 0
vendor_id : GenuineIntel
cpu family : 4
model : 3
model name : 486 DX/2
stepping : 5
fdiv_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme cpuid
bugs : itlb_multihit
bogomips : 32.35
clflush size : 32
cache_alignment : 32
address sizes : 32 bits physical, 32 bits virtual
power management:

> Also, I guess the fix should be something like this instead:
>
> ---
> diff --git a/arch/x86/include/asm/entry-common.h b/arch/x86/include/asm/entry-common.h
> index 117903881fe4..57b372ca6ce7 100644
> --- a/arch/x86/include/asm/entry-common.h
> +++ b/arch/x86/include/asm/entry-common.h
> @@ -85,7 +85,8 @@ static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
> * Therefore, final stack offset entropy will be 5 (x86_64) or
> * 6 (ia32) bits.
> */
> - choose_random_kstack_offset(rdtsc() & 0xFF);
> + if (cpu_feature_enabled(X86_FEATURE_TSC))
> + choose_random_kstack_offset(rdtsc() & 0xFF);

I'll check if that works, thanks for the suggestion.

Maciej