aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKirill A. Shutemov <kirill.shutemov@linux.intel.com>2017-06-28 08:17:30 -0400
committerIngo Molnar <mingo@kernel.org>2017-06-30 02:56:53 -0400
commita24261d70e00e4ce03cf45bbf18398f52a7b9229 (patch)
tree952085e6380dfcb43b3b1987909c84d0154d8cd7
parentbb43dbc5e09d52c6085dfee65f4f923b3fbcd1d4 (diff)
x86/KASLR: Fix detection 32/64 bit bootloaders for 5-level paging
KASLR uses hack to detect whether we booted via startup_32() or startup_64(): it checks what is loaded into cr3 and compares it to _pgtables. _pgtables is the array of page tables where early code allocates page table from. KASLR expects cr3 to point to _pgtables if we booted via startup_32(), but that's not true if we booted with 5-level paging enabled. In this case top level page table is allocated separately and only the first p4d page table is allocated from the array. Let's modify the check to cover both 4- and 5-level paging cases. The patch also renames 'level4p' to 'top_level_pgt' as it now can hold page table for 4th or 5th level, depending on configuration. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Acked-by: Kees Cook <keescook@chromium.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-arch@vger.kernel.org Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20170628121730.43079-1-kirill.shutemov@linux.intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--arch/x86/boot/compressed/pagetable.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/arch/x86/boot/compressed/pagetable.c b/arch/x86/boot/compressed/pagetable.c
index 8e69df96492e..28029be47fbb 100644
--- a/arch/x86/boot/compressed/pagetable.c
+++ b/arch/x86/boot/compressed/pagetable.c
@@ -63,7 +63,7 @@ static void *alloc_pgt_page(void *context)
63static struct alloc_pgt_data pgt_data; 63static struct alloc_pgt_data pgt_data;
64 64
65/* The top level page table entry pointer. */ 65/* The top level page table entry pointer. */
66static unsigned long level4p; 66static unsigned long top_level_pgt;
67 67
68/* 68/*
69 * Mapping information structure passed to kernel_ident_mapping_init(). 69 * Mapping information structure passed to kernel_ident_mapping_init().
@@ -91,9 +91,15 @@ void initialize_identity_maps(void)
91 * If we came here via startup_32(), cr3 will be _pgtable already 91 * If we came here via startup_32(), cr3 will be _pgtable already
92 * and we must append to the existing area instead of entirely 92 * and we must append to the existing area instead of entirely
93 * overwriting it. 93 * overwriting it.
94 *
95 * With 5-level paging, we use '_pgtable' to allocate the p4d page table,
96 * the top-level page table is allocated separately.
97 *
98 * p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
99 * cases. On 4-level paging it's equal to 'top_level_pgt'.
94 */ 100 */
95 level4p = read_cr3_pa(); 101 top_level_pgt = read_cr3_pa();
96 if (level4p == (unsigned long)_pgtable) { 102 if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
97 debug_putstr("booted via startup_32()\n"); 103 debug_putstr("booted via startup_32()\n");
98 pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE; 104 pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
99 pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE; 105 pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
@@ -103,7 +109,7 @@ void initialize_identity_maps(void)
103 pgt_data.pgt_buf = _pgtable; 109 pgt_data.pgt_buf = _pgtable;
104 pgt_data.pgt_buf_size = BOOT_PGT_SIZE; 110 pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
105 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size); 111 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
106 level4p = (unsigned long)alloc_pgt_page(&pgt_data); 112 top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
107 } 113 }
108} 114}
109 115
@@ -123,7 +129,7 @@ void add_identity_map(unsigned long start, unsigned long size)
123 return; 129 return;
124 130
125 /* Build the mapping. */ 131 /* Build the mapping. */
126 kernel_ident_mapping_init(&mapping_info, (pgd_t *)level4p, 132 kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt,
127 start, end); 133 start, end);
128} 134}
129 135
@@ -134,5 +140,5 @@ void add_identity_map(unsigned long start, unsigned long size)
134 */ 140 */
135void finalize_identity_maps(void) 141void finalize_identity_maps(void)
136{ 142{
137 write_cr3(level4p); 143 write_cr3(top_level_pgt);
138} 144}