x86/mm: Randomize per-cpu entry area
Peter Zijlstra
maintainerSeth found that the CPU-entry-area; the piece of per-cpu data that is mapped into the userspace page-tables for kPTI is not subject to any randomization -- irrespective of kASLR settings.
On x86_64 a whole P4D (512 GB) of virtual address space is reserved for this structure, which is plenty large enough to randomize things a little.
As such, use a straight forward randomization scheme that avoids duplicates to spread the existing CPUs over the available space.
--- arch/x86/include/asm/cpu_entry_area.h | 4 -- arch/x86/include/asm/pgtable_areas.h | 8 +++++ arch/x86/kernel/hw_breakpoint.c | 2 - arch/x86/mm/cpu_entry_area.c | 46 +++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 10 deletions(-)
arch/x86/include/asm/cpu_entry_area.h +0 -4
@@ -130,10 +130,6 @@ struct cpu_entry_area { };#define CPU_ENTRY_AREA_SIZE (sizeof(struct cpu_entry_area)) -#define CPU_ENTRY_AREA_ARRAY_SIZE (CPU_ENTRY_AREA_SIZE * NR_CPUS) - -/* Total size includes the readonly IDT mapping page as well: */ -#define CPU_ENTRY_AREA_TOTAL_SIZE (CPU_ENTRY_AREA_ARRAY_SIZE + PAGE_SIZE) DECLARE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); DECLARE_PER_CPU(struct cea_exception_stacks *, cea_exception_stacks);
arch/x86/include/asm/pgtable_areas.h +7 -1
@@ -11,6 +11,12 @@#define CPU_ENTRY_AREA_RO_IDT_VADDR ((void *)CPU_ENTRY_AREA_RO_IDT) -#define CPU_ENTRY_AREA_MAP_SIZE (CPU_ENTRY_AREA_PER_CPU + CPU_ENTRY_AREA_ARRAY_SIZE - CPU_ENTRY_AREA_BASE) +#ifdef CONFIG_X86_32 +#define CPU_ENTRY_AREA_MAP_SIZE (CPU_ENTRY_AREA_PER_CPU + \ + (CPU_ENTRY_AREA_SIZE * NR_CPUS) - \ + CPU_ENTRY_AREA_BASE +#else +#define CPU_ENTRY_AREA_MAP_SIZE P4D_SIZE +#endif #endif /* _ASM_X86_PGTABLE_AREAS_H */
arch/x86/kernel/hw_breakpoint.c +1 -1
@@ -266,7 +266,7 @@ static inline bool within_cpu_entry(unsi/* CPU entry erea is always used for CPU entry */ if (within_area (addr, end,CPU_ENTRY_AREA_BASE , -CPU_ENTRY_AREA_TOTAL_SIZE )) +CPU_ENTRY_AREA_MAP_SIZE ))return true;/*
arch/x86/mm/cpu_entry_area.c +42 -4
71 lines not on this page · raw message
arch/x86/mm/cpu_entry_area.c static DEFINE_PER_CPU_PAGE_ALIGNED(struc · line 38
Quoted 8 lines · Peter Zijlstra
arch/x86/mm/cpu_entry_area.c +8
-0+ max_cea = (CPU_ENTRY_AREA_MAP_SIZE -PAGE_SIZE ) /CPU_ENTRY_AREA_SIZE ; + +/* O(sodding terrible) */ +for_each_possible_cpu (i) { +unsigned int cea; + +again: + cea =prandom_u32_max (max_cea);
Just FYI, max_cea is 2274876 (at least in my test rig), which means the values returned from prandom_u32_max() won't be uniformly distributed. Right now the kernel doesn't have a function that does rejection sampling to get uniform distribution, but I could add one if you need. Alternatively, maybe you don't actually *need* this to be perfectly distributed -- which seems to be a common perspective -- and so this is good enough. Your call, but just wanted to make you aware.
Jason
Peter Zijlstra
maintainerQuoted 11 lines · Jason A. Donenfeld
arch/x86/mm/cpu_entry_area.c static DEFINE_PER_CPU_PAGE_ALIGNED(struc · line 38
Quoted 8 lines · Peter Zijlstra
arch/x86/mm/cpu_entry_area.c +8
-0+ max_cea = (CPU_ENTRY_AREA_MAP_SIZE -PAGE_SIZE ) /CPU_ENTRY_AREA_SIZE ; + +/* O(sodding terrible) */ +for_each_possible_cpu (i) { +unsigned int cea; + +again: + cea =prandom_u32_max (max_cea);Just FYI,
max_ceais 2274876 (at least in my test rig), which means the
Yeah, that sounds about right, just over 21 bits.
Quoted 3 lines
values returned from
prandom_u32_max()won't be uniformly distributed. Right now the kernel doesn't have a function that does rejection sampling to get uniform distribution, but I could add one if you need.
I figured it was better than '% max_cea'.
Quoted 3 lines
Alternatively, maybe you don't actually *need* this to be perfectly distributed -- which seems to be a common perspective -- and so this is good enough. Your call, but just wanted to make you aware.
First iteration had a LFSR to 'uniformly' and uniquely distribute the CEAs around, but someone took offence to that :-). Anyway, I think anything non-obvious is good enough in this case.