Message ID | 20170120164126.27624-2-thgarnie@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Fri, Jan 20, 2017 at 8:41 AM, Thomas Garnier <thgarnie@google.com> wrote: > Each processor holds a GDT in its per-cpu structure. The sgdt > instruction gives the base address of the current GDT. This address can > be used to bypass KASLR memory randomization. With another bug, an > attacker could target other per-cpu structures or deduce the base of > the main memory section (PAGE_OFFSET). > > This patch relocates the GDT table for each processor inside the > Fixmap section. The space is reserved based on number of supported > cpus. > > For consistency, the remapping is done by default on 32 and 64 bit. > > Each processor switches to its remapped GDT at the end of > initialization. For hibernation, the main processor returns with the > original GDT and switches back to the remapping at completion. > > On 32 bit, the maximum number of processors is now 256. The Fixmap > section cannot handle the original 512. Additional asserts ensure that > the Fixmap section cannot grow beyond the space available. > > This patch was tested on both architectures. Hibernation and KVM were > both tested specially for their usage of the GDT. > > Signed-off-by: Thomas Garnier <thgarnie@google.com> > --- > Based on next-20170119 > --- > arch/x86/Kconfig | 1 + > arch/x86/include/asm/fixmap.h | 4 ++++ > arch/x86/include/asm/processor.h | 1 + > arch/x86/kernel/cpu/common.c | 18 +++++++++++++++++- > arch/x86/mm/init_32.c | 2 ++ > arch/x86/power/cpu.c | 3 +++ > 6 files changed, 28 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index f1d4e8f2131f..b4ed35db10a8 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -912,6 +912,7 @@ config MAXSMP > config NR_CPUS > int "Maximum number of CPUs" if SMP && !MAXSMP > range 2 8 if SMP && X86_32 && !X86_BIGSMP > + range 2 256 if SMP && X86_32 && X86_BIGSMP > range 2 512 if SMP && !MAXSMP && !CPUMASK_OFFSTACK > range 2 8192 if SMP && !MAXSMP && CPUMASK_OFFSTACK && X86_64 > default "1" if !SMP > diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h > index c46289799b02..8b913b5e9383 100644 > --- a/arch/x86/include/asm/fixmap.h > +++ b/arch/x86/include/asm/fixmap.h > @@ -100,6 +100,10 @@ enum fixed_addresses { > #ifdef CONFIG_X86_INTEL_MID > FIX_LNW_VRTC, > #endif > + /* Fixmap entries to remap the GDTs, one per processor. */ > + FIX_GDT_REMAP_BEGIN, > + FIX_GDT_REMAP_END = FIX_GDT_REMAP_BEGIN + NR_CPUS - 1, > + > __end_of_permanent_fixed_addresses, > > /* > diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h > index 1be64da0384e..280211ad8be9 100644 > --- a/arch/x86/include/asm/processor.h > +++ b/arch/x86/include/asm/processor.h > @@ -705,6 +705,7 @@ extern struct desc_ptr early_gdt_descr; > > extern void cpu_set_gdt(int); > extern void switch_to_new_gdt(int); > +extern void load_remapped_gdt(int); > extern void load_percpu_segment(int); > extern void cpu_init(void); > > diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c > index e97ffc8d29f4..7d940b0e805a 100644 > --- a/arch/x86/kernel/cpu/common.c > +++ b/arch/x86/kernel/cpu/common.c > @@ -443,6 +443,19 @@ void load_percpu_segment(int cpu) > load_stack_canary_segment(); > } > > +/* Load a fixmap remapping of the per-cpu GDT */ > +void load_remapped_gdt(int cpu) > +{ > + struct desc_ptr gdt_descr; > + unsigned long idx = FIX_GDT_REMAP_BEGIN + cpu; > + > + __set_fixmap(idx, __pa(get_cpu_gdt_table(cpu)), PAGE_KERNEL); > + > + gdt_descr.address = (long)__fix_to_virt(idx); > + gdt_descr.size = GDT_SIZE - 1; > + load_gdt(&gdt_descr); > +} IMO this should be split into two functions, one to set up the fixmap entry and one to load the GDT. Also, would it be possible to rename the various gdt helpers so that their functionality is more obvious? For example, get_cpu_gdt_table() could be get_cpu_direct_gdt_table() and the function to load the gdt could be load_fixmap_gdt(). --Andy
On Fri, Jan 20, 2017 at 4:57 PM, Andy Lutomirski <luto@amacapital.net> wrote: > On Fri, Jan 20, 2017 at 8:41 AM, Thomas Garnier <thgarnie@google.com> wrote: >> Each processor holds a GDT in its per-cpu structure. The sgdt >> instruction gives the base address of the current GDT. This address can >> be used to bypass KASLR memory randomization. With another bug, an >> attacker could target other per-cpu structures or deduce the base of >> the main memory section (PAGE_OFFSET). >> >> This patch relocates the GDT table for each processor inside the >> Fixmap section. The space is reserved based on number of supported >> cpus. >> >> For consistency, the remapping is done by default on 32 and 64 bit. >> >> Each processor switches to its remapped GDT at the end of >> initialization. For hibernation, the main processor returns with the >> original GDT and switches back to the remapping at completion. >> >> On 32 bit, the maximum number of processors is now 256. The Fixmap >> section cannot handle the original 512. Additional asserts ensure that >> the Fixmap section cannot grow beyond the space available. >> >> This patch was tested on both architectures. Hibernation and KVM were >> both tested specially for their usage of the GDT. >> >> Signed-off-by: Thomas Garnier <thgarnie@google.com> >> --- >> Based on next-20170119 >> --- >> arch/x86/Kconfig | 1 + >> arch/x86/include/asm/fixmap.h | 4 ++++ >> arch/x86/include/asm/processor.h | 1 + >> arch/x86/kernel/cpu/common.c | 18 +++++++++++++++++- >> arch/x86/mm/init_32.c | 2 ++ >> arch/x86/power/cpu.c | 3 +++ >> 6 files changed, 28 insertions(+), 1 deletion(-) >> >> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig >> index f1d4e8f2131f..b4ed35db10a8 100644 >> --- a/arch/x86/Kconfig >> +++ b/arch/x86/Kconfig >> @@ -912,6 +912,7 @@ config MAXSMP >> config NR_CPUS >> int "Maximum number of CPUs" if SMP && !MAXSMP >> range 2 8 if SMP && X86_32 && !X86_BIGSMP >> + range 2 256 if SMP && X86_32 && X86_BIGSMP >> range 2 512 if SMP && !MAXSMP && !CPUMASK_OFFSTACK >> range 2 8192 if SMP && !MAXSMP && CPUMASK_OFFSTACK && X86_64 >> default "1" if !SMP >> diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h >> index c46289799b02..8b913b5e9383 100644 >> --- a/arch/x86/include/asm/fixmap.h >> +++ b/arch/x86/include/asm/fixmap.h >> @@ -100,6 +100,10 @@ enum fixed_addresses { >> #ifdef CONFIG_X86_INTEL_MID >> FIX_LNW_VRTC, >> #endif >> + /* Fixmap entries to remap the GDTs, one per processor. */ >> + FIX_GDT_REMAP_BEGIN, >> + FIX_GDT_REMAP_END = FIX_GDT_REMAP_BEGIN + NR_CPUS - 1, >> + >> __end_of_permanent_fixed_addresses, >> >> /* >> diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h >> index 1be64da0384e..280211ad8be9 100644 >> --- a/arch/x86/include/asm/processor.h >> +++ b/arch/x86/include/asm/processor.h >> @@ -705,6 +705,7 @@ extern struct desc_ptr early_gdt_descr; >> >> extern void cpu_set_gdt(int); >> extern void switch_to_new_gdt(int); >> +extern void load_remapped_gdt(int); >> extern void load_percpu_segment(int); >> extern void cpu_init(void); >> >> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c >> index e97ffc8d29f4..7d940b0e805a 100644 >> --- a/arch/x86/kernel/cpu/common.c >> +++ b/arch/x86/kernel/cpu/common.c >> @@ -443,6 +443,19 @@ void load_percpu_segment(int cpu) >> load_stack_canary_segment(); >> } >> >> +/* Load a fixmap remapping of the per-cpu GDT */ >> +void load_remapped_gdt(int cpu) >> +{ >> + struct desc_ptr gdt_descr; >> + unsigned long idx = FIX_GDT_REMAP_BEGIN + cpu; >> + >> + __set_fixmap(idx, __pa(get_cpu_gdt_table(cpu)), PAGE_KERNEL); >> + >> + gdt_descr.address = (long)__fix_to_virt(idx); >> + gdt_descr.size = GDT_SIZE - 1; >> + load_gdt(&gdt_descr); >> +} > > IMO this should be split into two functions, one to set up the fixmap > entry and one to load the GDT. > That make sense. > Also, would it be possible to rename the various gdt helpers so that > their functionality is more obvious? For example, get_cpu_gdt_table() > could be get_cpu_direct_gdt_table() and the function to load the gdt > could be load_fixmap_gdt(). > Sure no problem. > --Andy
Hi Thomas,
[auto build test ERROR on tip/auto-latest]
[also build test ERROR on v4.10-rc4 next-20170120]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Thomas-Garnier/x86-mm-Adapt-MODULES_END-based-on-Fixmap-section-size/20170121-094756
config: i386-randconfig-x004-201703 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=i386
All errors (new ones prefixed by >>):
In file included from include/uapi/linux/stddef.h:1:0,
from include/linux/stddef.h:4,
from include/uapi/linux/posix_types.h:4,
from include/uapi/linux/types.h:13,
from include/linux/types.h:5,
from include/linux/list.h:4,
from include/linux/signal.h:4,
from arch/x86/mm/init_32.c:8:
arch/x86/mm/init_32.c: In function 'mem_init':
>> include/linux/compiler.h:518:38: error: call to '__compiletime_assert_801' declared with attribute error: BUILD_BUG_ON failed: __fix_to_virt(__end_of_fixed_addresses) <= PKMAP_BASE + LAST_PKMAP*PAGE_SIZE
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^
include/linux/compiler.h:501:4: note: in definition of macro '__compiletime_assert'
prefix ## suffix(); \
^~~~~~
include/linux/compiler.h:518:2: note: in expansion of macro '_compiletime_assert'
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^~~~~~~~~~~~~~~~~~~
include/linux/bug.h:54:37: note: in expansion of macro 'compiletime_assert'
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^~~~~~~~~~~~~~~~~~
include/linux/bug.h:78:2: note: in expansion of macro 'BUILD_BUG_ON_MSG'
BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
^~~~~~~~~~~~~~~~
arch/x86/mm/init_32.c:801:2: note: in expansion of macro 'BUILD_BUG_ON'
BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= PKMAP_BASE + LAST_PKMAP*PAGE_SIZE);
^~~~~~~~~~~~
include/linux/compiler.h:518:38: error: call to '__compiletime_assert_805' declared with attribute error: BUILD_BUG_ON failed: __fix_to_virt(__end_of_fixed_addresses) <= VMALLOC_END
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^
include/linux/compiler.h:501:4: note: in definition of macro '__compiletime_assert'
prefix ## suffix(); \
^~~~~~
include/linux/compiler.h:518:2: note: in expansion of macro '_compiletime_assert'
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
^~~~~~~~~~~~~~~~~~~
include/linux/bug.h:54:37: note: in expansion of macro 'compiletime_assert'
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^~~~~~~~~~~~~~~~~~
include/linux/bug.h:78:2: note: in expansion of macro 'BUILD_BUG_ON_MSG'
BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
^~~~~~~~~~~~~~~~
arch/x86/mm/init_32.c:805:2: note: in expansion of macro 'BUILD_BUG_ON'
BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= VMALLOC_END);
^~~~~~~~~~~~
vim +/__compiletime_assert_801 +518 include/linux/compiler.h
9a8ab1c3 Daniel Santos 2013-02-21 512 *
9a8ab1c3 Daniel Santos 2013-02-21 513 * In tradition of POSIX assert, this macro will break the build if the
9a8ab1c3 Daniel Santos 2013-02-21 514 * supplied condition is *false*, emitting the supplied error message if the
9a8ab1c3 Daniel Santos 2013-02-21 515 * compiler has support to do so.
9a8ab1c3 Daniel Santos 2013-02-21 516 */
9a8ab1c3 Daniel Santos 2013-02-21 517 #define compiletime_assert(condition, msg) \
9a8ab1c3 Daniel Santos 2013-02-21 @518 _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
9a8ab1c3 Daniel Santos 2013-02-21 519
47933ad4 Peter Zijlstra 2013-11-06 520 #define compiletime_assert_atomic_type(t) \
47933ad4 Peter Zijlstra 2013-11-06 521 compiletime_assert(__native_word(t), \
:::::: The code at line 518 was first introduced by commit
:::::: 9a8ab1c39970a4938a72d94e6fd13be88a797590 bug.h, compiler.h: introduce compiletime_assert & BUILD_BUG_ON_MSG
:::::: TO: Daniel Santos <daniel.santos@pobox.com>
:::::: CC: Linus Torvalds <torvalds@linux-foundation.org>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Hi Thomas, [auto build test ERROR on tip/auto-latest] [also build test ERROR on v4.10-rc4 next-20170120] [cannot apply to tip/x86/core] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system] url: https://github.com/0day-ci/linux/commits/Thomas-Garnier/x86-mm-Adapt-MODULES_END-based-on-Fixmap-section-size/20170121-094756 config: i386-randconfig-x003-201703 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce: # save the attached .config to linux build tree make ARCH=i386 All error/warnings (new ones prefixed by >>): In file included from include/uapi/linux/stddef.h:1:0, from include/linux/stddef.h:4, from include/uapi/linux/posix_types.h:4, from include/uapi/linux/types.h:13, from include/linux/types.h:5, from include/linux/list.h:4, from include/linux/signal.h:4, from arch/x86/mm/init_32.c:8: arch/x86/mm/init_32.c: In function 'mem_init': >> include/linux/compiler.h:518:38: error: call to '__compiletime_assert_805' declared with attribute error: BUILD_BUG_ON failed: __fix_to_virt(__end_of_fixed_addresses) <= VMALLOC_END _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) ^ include/linux/compiler.h:501:4: note: in definition of macro '__compiletime_assert' prefix ## suffix(); \ ^~~~~~ include/linux/compiler.h:518:2: note: in expansion of macro '_compiletime_assert' _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) ^~~~~~~~~~~~~~~~~~~ include/linux/bug.h:54:37: note: in expansion of macro 'compiletime_assert' #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) ^~~~~~~~~~~~~~~~~~ include/linux/bug.h:78:2: note: in expansion of macro 'BUILD_BUG_ON_MSG' BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) ^~~~~~~~~~~~~~~~ >> arch/x86/mm/init_32.c:805:2: note: in expansion of macro 'BUILD_BUG_ON' BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= VMALLOC_END); ^~~~~~~~~~~~ vim +/__compiletime_assert_805 +518 include/linux/compiler.h 9a8ab1c3 Daniel Santos 2013-02-21 512 * 9a8ab1c3 Daniel Santos 2013-02-21 513 * In tradition of POSIX assert, this macro will break the build if the 9a8ab1c3 Daniel Santos 2013-02-21 514 * supplied condition is *false*, emitting the supplied error message if the 9a8ab1c3 Daniel Santos 2013-02-21 515 * compiler has support to do so. 9a8ab1c3 Daniel Santos 2013-02-21 516 */ 9a8ab1c3 Daniel Santos 2013-02-21 517 #define compiletime_assert(condition, msg) \ 9a8ab1c3 Daniel Santos 2013-02-21 @518 _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) 9a8ab1c3 Daniel Santos 2013-02-21 519 47933ad4 Peter Zijlstra 2013-11-06 520 #define compiletime_assert_atomic_type(t) \ 47933ad4 Peter Zijlstra 2013-11-06 521 compiletime_assert(__native_word(t), \ :::::: The code at line 518 was first introduced by commit :::::: 9a8ab1c39970a4938a72d94e6fd13be88a797590 bug.h, compiler.h: introduce compiletime_assert & BUILD_BUG_ON_MSG :::::: TO: Daniel Santos <daniel.santos@pobox.com> :::::: CC: Linus Torvalds <torvalds@linux-foundation.org> --- 0-DAY kernel test infrastructure Open Source Technology Center https://lists.01.org/pipermail/kbuild-all Intel Corporation
The kbuild bot found interesting build errors with the new BUILD_BUG_ON on 32bit (64G mem support). I think I went a bit too far with them given the ioremap part is just temporary on early boot. I removed them and tested different configurations trying to use as much fixmap as possible (DEBUG_HIGHMEM, 64G/PAE support and more). Everything looks good and we can still support 512 processors. Next iteration, I will remove the BUILD_BUG_ON that I added and remove restriction to 256 back to 512. On top of all changes suggested by Andy on the patch set. On Fri, Jan 20, 2017 at 5:06 PM, Thomas Garnier <thgarnie@google.com> wrote: > On Fri, Jan 20, 2017 at 4:57 PM, Andy Lutomirski <luto@amacapital.net> wrote: >> On Fri, Jan 20, 2017 at 8:41 AM, Thomas Garnier <thgarnie@google.com> wrote: >>> Each processor holds a GDT in its per-cpu structure. The sgdt >>> instruction gives the base address of the current GDT. This address can >>> be used to bypass KASLR memory randomization. With another bug, an >>> attacker could target other per-cpu structures or deduce the base of >>> the main memory section (PAGE_OFFSET). >>> >>> This patch relocates the GDT table for each processor inside the >>> Fixmap section. The space is reserved based on number of supported >>> cpus. >>> >>> For consistency, the remapping is done by default on 32 and 64 bit. >>> >>> Each processor switches to its remapped GDT at the end of >>> initialization. For hibernation, the main processor returns with the >>> original GDT and switches back to the remapping at completion. >>> >>> On 32 bit, the maximum number of processors is now 256. The Fixmap >>> section cannot handle the original 512. Additional asserts ensure that >>> the Fixmap section cannot grow beyond the space available. >>> >>> This patch was tested on both architectures. Hibernation and KVM were >>> both tested specially for their usage of the GDT. >>> >>> Signed-off-by: Thomas Garnier <thgarnie@google.com> >>> --- >>> Based on next-20170119 >>> --- >>> arch/x86/Kconfig | 1 + >>> arch/x86/include/asm/fixmap.h | 4 ++++ >>> arch/x86/include/asm/processor.h | 1 + >>> arch/x86/kernel/cpu/common.c | 18 +++++++++++++++++- >>> arch/x86/mm/init_32.c | 2 ++ >>> arch/x86/power/cpu.c | 3 +++ >>> 6 files changed, 28 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig >>> index f1d4e8f2131f..b4ed35db10a8 100644 >>> --- a/arch/x86/Kconfig >>> +++ b/arch/x86/Kconfig >>> @@ -912,6 +912,7 @@ config MAXSMP >>> config NR_CPUS >>> int "Maximum number of CPUs" if SMP && !MAXSMP >>> range 2 8 if SMP && X86_32 && !X86_BIGSMP >>> + range 2 256 if SMP && X86_32 && X86_BIGSMP >>> range 2 512 if SMP && !MAXSMP && !CPUMASK_OFFSTACK >>> range 2 8192 if SMP && !MAXSMP && CPUMASK_OFFSTACK && X86_64 >>> default "1" if !SMP >>> diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h >>> index c46289799b02..8b913b5e9383 100644 >>> --- a/arch/x86/include/asm/fixmap.h >>> +++ b/arch/x86/include/asm/fixmap.h >>> @@ -100,6 +100,10 @@ enum fixed_addresses { >>> #ifdef CONFIG_X86_INTEL_MID >>> FIX_LNW_VRTC, >>> #endif >>> + /* Fixmap entries to remap the GDTs, one per processor. */ >>> + FIX_GDT_REMAP_BEGIN, >>> + FIX_GDT_REMAP_END = FIX_GDT_REMAP_BEGIN + NR_CPUS - 1, >>> + >>> __end_of_permanent_fixed_addresses, >>> >>> /* >>> diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h >>> index 1be64da0384e..280211ad8be9 100644 >>> --- a/arch/x86/include/asm/processor.h >>> +++ b/arch/x86/include/asm/processor.h >>> @@ -705,6 +705,7 @@ extern struct desc_ptr early_gdt_descr; >>> >>> extern void cpu_set_gdt(int); >>> extern void switch_to_new_gdt(int); >>> +extern void load_remapped_gdt(int); >>> extern void load_percpu_segment(int); >>> extern void cpu_init(void); >>> >>> diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c >>> index e97ffc8d29f4..7d940b0e805a 100644 >>> --- a/arch/x86/kernel/cpu/common.c >>> +++ b/arch/x86/kernel/cpu/common.c >>> @@ -443,6 +443,19 @@ void load_percpu_segment(int cpu) >>> load_stack_canary_segment(); >>> } >>> >>> +/* Load a fixmap remapping of the per-cpu GDT */ >>> +void load_remapped_gdt(int cpu) >>> +{ >>> + struct desc_ptr gdt_descr; >>> + unsigned long idx = FIX_GDT_REMAP_BEGIN + cpu; >>> + >>> + __set_fixmap(idx, __pa(get_cpu_gdt_table(cpu)), PAGE_KERNEL); >>> + >>> + gdt_descr.address = (long)__fix_to_virt(idx); >>> + gdt_descr.size = GDT_SIZE - 1; >>> + load_gdt(&gdt_descr); >>> +} >> >> IMO this should be split into two functions, one to set up the fixmap >> entry and one to load the GDT. >> > > That make sense. > >> Also, would it be possible to rename the various gdt helpers so that >> their functionality is more obvious? For example, get_cpu_gdt_table() >> could be get_cpu_direct_gdt_table() and the function to load the gdt >> could be load_fixmap_gdt(). >> > > Sure no problem. > >> --Andy > > > > -- > Thomas
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index f1d4e8f2131f..b4ed35db10a8 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -912,6 +912,7 @@ config MAXSMP config NR_CPUS int "Maximum number of CPUs" if SMP && !MAXSMP range 2 8 if SMP && X86_32 && !X86_BIGSMP + range 2 256 if SMP && X86_32 && X86_BIGSMP range 2 512 if SMP && !MAXSMP && !CPUMASK_OFFSTACK range 2 8192 if SMP && !MAXSMP && CPUMASK_OFFSTACK && X86_64 default "1" if !SMP diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h index c46289799b02..8b913b5e9383 100644 --- a/arch/x86/include/asm/fixmap.h +++ b/arch/x86/include/asm/fixmap.h @@ -100,6 +100,10 @@ enum fixed_addresses { #ifdef CONFIG_X86_INTEL_MID FIX_LNW_VRTC, #endif + /* Fixmap entries to remap the GDTs, one per processor. */ + FIX_GDT_REMAP_BEGIN, + FIX_GDT_REMAP_END = FIX_GDT_REMAP_BEGIN + NR_CPUS - 1, + __end_of_permanent_fixed_addresses, /* diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 1be64da0384e..280211ad8be9 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -705,6 +705,7 @@ extern struct desc_ptr early_gdt_descr; extern void cpu_set_gdt(int); extern void switch_to_new_gdt(int); +extern void load_remapped_gdt(int); extern void load_percpu_segment(int); extern void cpu_init(void); diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index e97ffc8d29f4..7d940b0e805a 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -443,6 +443,19 @@ void load_percpu_segment(int cpu) load_stack_canary_segment(); } +/* Load a fixmap remapping of the per-cpu GDT */ +void load_remapped_gdt(int cpu) +{ + struct desc_ptr gdt_descr; + unsigned long idx = FIX_GDT_REMAP_BEGIN + cpu; + + __set_fixmap(idx, __pa(get_cpu_gdt_table(cpu)), PAGE_KERNEL); + + gdt_descr.address = (long)__fix_to_virt(idx); + gdt_descr.size = GDT_SIZE - 1; + load_gdt(&gdt_descr); +} + /* * Current gdt points %fs at the "master" per-cpu area: after this, * it's on the real one. @@ -455,7 +468,6 @@ void switch_to_new_gdt(int cpu) gdt_descr.size = GDT_SIZE - 1; load_gdt(&gdt_descr); /* Reload the per-cpu base */ - load_percpu_segment(cpu); } @@ -1508,6 +1520,8 @@ void cpu_init(void) if (is_uv_system()) uv_cpu_init(); + + load_remapped_gdt(cpu); } #else @@ -1563,6 +1577,8 @@ void cpu_init(void) dbg_restore_debug_regs(); fpu__init_cpu(); + + load_remapped_gdt(cpu); } #endif diff --git a/arch/x86/mm/init_32.c b/arch/x86/mm/init_32.c index 928d657de829..cfbcf42099d0 100644 --- a/arch/x86/mm/init_32.c +++ b/arch/x86/mm/init_32.c @@ -798,9 +798,11 @@ void __init mem_init(void) #ifdef CONFIG_HIGHMEM BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP*PAGE_SIZE > FIXADDR_START); BUILD_BUG_ON(VMALLOC_END > PKMAP_BASE); + BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= PKMAP_BASE + LAST_PKMAP*PAGE_SIZE); #endif #define high_memory (-128UL << 20) BUILD_BUG_ON(VMALLOC_START >= VMALLOC_END); + BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= VMALLOC_END); #undef high_memory #undef __FIXADDR_TOP diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c index 66ade16c7693..7578de6db833 100644 --- a/arch/x86/power/cpu.c +++ b/arch/x86/power/cpu.c @@ -183,6 +183,9 @@ static void fix_processor_context(void) load_mm_ldt(current->active_mm); /* This does lldt */ fpu__resume_cpu(); + + /* Load remapped GDT */ + load_remapped_gdt(cpu); } /**
Each processor holds a GDT in its per-cpu structure. The sgdt instruction gives the base address of the current GDT. This address can be used to bypass KASLR memory randomization. With another bug, an attacker could target other per-cpu structures or deduce the base of the main memory section (PAGE_OFFSET). This patch relocates the GDT table for each processor inside the Fixmap section. The space is reserved based on number of supported cpus. For consistency, the remapping is done by default on 32 and 64 bit. Each processor switches to its remapped GDT at the end of initialization. For hibernation, the main processor returns with the original GDT and switches back to the remapping at completion. On 32 bit, the maximum number of processors is now 256. The Fixmap section cannot handle the original 512. Additional asserts ensure that the Fixmap section cannot grow beyond the space available. This patch was tested on both architectures. Hibernation and KVM were both tested specially for their usage of the GDT. Signed-off-by: Thomas Garnier <thgarnie@google.com> --- Based on next-20170119 --- arch/x86/Kconfig | 1 + arch/x86/include/asm/fixmap.h | 4 ++++ arch/x86/include/asm/processor.h | 1 + arch/x86/kernel/cpu/common.c | 18 +++++++++++++++++- arch/x86/mm/init_32.c | 2 ++ arch/x86/power/cpu.c | 3 +++ 6 files changed, 28 insertions(+), 1 deletion(-)