Skip to content

Commit 16a3fe6

Browse files
joergroedelKAGA-KOKO
authored andcommitted
x86/mm/pti: Clone kernel-image on PTE level for 32 bit
On 32 bit the kernel sections are not huge-page aligned. When we clone them on PMD-level we unevitably map some areas that are normal kernel memory and may contain secrets to user-space. To prevent that we need to clone the kernel-image on PTE-level for 32 bit. Also make the page-table cloning code more general so that it can handle PMD and PTE level cloning. This can be generalized further in the future to also handle clones on the P4D-level. Signed-off-by: Joerg Roedel <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: "H . Peter Anvin" <[email protected]> Cc: [email protected] Cc: Linus Torvalds <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Juergen Gross <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Boris Ostrovsky <[email protected]> Cc: Brian Gerst <[email protected]> Cc: David Laight <[email protected]> Cc: Denys Vlasenko <[email protected]> Cc: Eduardo Valentin <[email protected]> Cc: Greg KH <[email protected]> Cc: Will Deacon <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: Andrea Arcangeli <[email protected]> Cc: Waiman Long <[email protected]> Cc: Pavel Machek <[email protected]> Cc: "David H . Gutteridge" <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 30514ef commit 16a3fe6

File tree

1 file changed

+99
-41
lines changed

1 file changed

+99
-41
lines changed

arch/x86/mm/pti.c

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@
5454
#define __GFP_NOTRACK 0
5555
#endif
5656

57+
/*
58+
* Define the page-table levels we clone for user-space on 32
59+
* and 64 bit.
60+
*/
61+
#ifdef CONFIG_X86_64
62+
#define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PMD
63+
#else
64+
#define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PTE
65+
#endif
66+
5767
static void __init pti_print_if_insecure(const char *reason)
5868
{
5969
if (boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
@@ -228,7 +238,6 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
228238
return pmd_offset(pud, address);
229239
}
230240

231-
#ifdef CONFIG_X86_VSYSCALL_EMULATION
232241
/*
233242
* Walk the shadow copy of the page tables (optionally) trying to allocate
234243
* page table pages on the way down. Does not support large pages.
@@ -270,6 +279,7 @@ static __init pte_t *pti_user_pagetable_walk_pte(unsigned long address)
270279
return pte;
271280
}
272281

282+
#ifdef CONFIG_X86_VSYSCALL_EMULATION
273283
static void __init pti_setup_vsyscall(void)
274284
{
275285
pte_t *pte, *target_pte;
@@ -290,16 +300,23 @@ static void __init pti_setup_vsyscall(void)
290300
static void __init pti_setup_vsyscall(void) { }
291301
#endif
292302

303+
enum pti_clone_level {
304+
PTI_CLONE_PMD,
305+
PTI_CLONE_PTE,
306+
};
307+
293308
static void
294-
pti_clone_pmds(unsigned long start, unsigned long end)
309+
pti_clone_pgtable(unsigned long start, unsigned long end,
310+
enum pti_clone_level level)
295311
{
296312
unsigned long addr;
297313

298314
/*
299315
* Clone the populated PMDs which cover start to end. These PMD areas
300316
* can have holes.
301317
*/
302-
for (addr = start; addr < end; addr += PMD_SIZE) {
318+
for (addr = start; addr < end;) {
319+
pte_t *pte, *target_pte;
303320
pmd_t *pmd, *target_pmd;
304321
pgd_t *pgd;
305322
p4d_t *p4d;
@@ -315,44 +332,84 @@ pti_clone_pmds(unsigned long start, unsigned long end)
315332
p4d = p4d_offset(pgd, addr);
316333
if (WARN_ON(p4d_none(*p4d)))
317334
return;
335+
318336
pud = pud_offset(p4d, addr);
319-
if (pud_none(*pud))
337+
if (pud_none(*pud)) {
338+
addr += PUD_SIZE;
320339
continue;
340+
}
341+
321342
pmd = pmd_offset(pud, addr);
322-
if (pmd_none(*pmd))
343+
if (pmd_none(*pmd)) {
344+
addr += PMD_SIZE;
323345
continue;
346+
}
324347

325-
target_pmd = pti_user_pagetable_walk_pmd(addr);
326-
if (WARN_ON(!target_pmd))
327-
return;
328-
329-
/*
330-
* Only clone present PMDs. This ensures only setting
331-
* _PAGE_GLOBAL on present PMDs. This should only be
332-
* called on well-known addresses anyway, so a non-
333-
* present PMD would be a surprise.
334-
*/
335-
if (WARN_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT)))
336-
return;
337-
338-
/*
339-
* Setting 'target_pmd' below creates a mapping in both
340-
* the user and kernel page tables. It is effectively
341-
* global, so set it as global in both copies. Note:
342-
* the X86_FEATURE_PGE check is not _required_ because
343-
* the CPU ignores _PAGE_GLOBAL when PGE is not
344-
* supported. The check keeps consistentency with
345-
* code that only set this bit when supported.
346-
*/
347-
if (boot_cpu_has(X86_FEATURE_PGE))
348-
*pmd = pmd_set_flags(*pmd, _PAGE_GLOBAL);
349-
350-
/*
351-
* Copy the PMD. That is, the kernelmode and usermode
352-
* tables will share the last-level page tables of this
353-
* address range
354-
*/
355-
*target_pmd = *pmd;
348+
if (pmd_large(*pmd) || level == PTI_CLONE_PMD) {
349+
target_pmd = pti_user_pagetable_walk_pmd(addr);
350+
if (WARN_ON(!target_pmd))
351+
return;
352+
353+
/*
354+
* Only clone present PMDs. This ensures only setting
355+
* _PAGE_GLOBAL on present PMDs. This should only be
356+
* called on well-known addresses anyway, so a non-
357+
* present PMD would be a surprise.
358+
*/
359+
if (WARN_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT)))
360+
return;
361+
362+
/*
363+
* Setting 'target_pmd' below creates a mapping in both
364+
* the user and kernel page tables. It is effectively
365+
* global, so set it as global in both copies. Note:
366+
* the X86_FEATURE_PGE check is not _required_ because
367+
* the CPU ignores _PAGE_GLOBAL when PGE is not
368+
* supported. The check keeps consistentency with
369+
* code that only set this bit when supported.
370+
*/
371+
if (boot_cpu_has(X86_FEATURE_PGE))
372+
*pmd = pmd_set_flags(*pmd, _PAGE_GLOBAL);
373+
374+
/*
375+
* Copy the PMD. That is, the kernelmode and usermode
376+
* tables will share the last-level page tables of this
377+
* address range
378+
*/
379+
*target_pmd = *pmd;
380+
381+
addr += PMD_SIZE;
382+
383+
} else if (level == PTI_CLONE_PTE) {
384+
385+
/* Walk the page-table down to the pte level */
386+
pte = pte_offset_kernel(pmd, addr);
387+
if (pte_none(*pte)) {
388+
addr += PAGE_SIZE;
389+
continue;
390+
}
391+
392+
/* Only clone present PTEs */
393+
if (WARN_ON(!(pte_flags(*pte) & _PAGE_PRESENT)))
394+
return;
395+
396+
/* Allocate PTE in the user page-table */
397+
target_pte = pti_user_pagetable_walk_pte(addr);
398+
if (WARN_ON(!target_pte))
399+
return;
400+
401+
/* Set GLOBAL bit in both PTEs */
402+
if (boot_cpu_has(X86_FEATURE_PGE))
403+
*pte = pte_set_flags(*pte, _PAGE_GLOBAL);
404+
405+
/* Clone the PTE */
406+
*target_pte = *pte;
407+
408+
addr += PAGE_SIZE;
409+
410+
} else {
411+
BUG();
412+
}
356413
}
357414
}
358415

@@ -398,7 +455,7 @@ static void __init pti_clone_user_shared(void)
398455
start = CPU_ENTRY_AREA_BASE;
399456
end = start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES);
400457

401-
pti_clone_pmds(start, end);
458+
pti_clone_pgtable(start, end, PTI_CLONE_PMD);
402459
}
403460
#endif /* CONFIG_X86_64 */
404461

@@ -417,8 +474,9 @@ static void __init pti_setup_espfix64(void)
417474
*/
418475
static void pti_clone_entry_text(void)
419476
{
420-
pti_clone_pmds((unsigned long) __entry_text_start,
421-
(unsigned long) __irqentry_text_end);
477+
pti_clone_pgtable((unsigned long) __entry_text_start,
478+
(unsigned long) __irqentry_text_end,
479+
PTI_CLONE_PMD);
422480
}
423481

424482
/*
@@ -500,10 +558,10 @@ static void pti_clone_kernel_text(void)
500558
* pti_set_kernel_image_nonglobal() did to clear the
501559
* global bit.
502560
*/
503-
pti_clone_pmds(start, end_clone);
561+
pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE);
504562

505563
/*
506-
* pti_clone_pmds() will set the global bit in any PMDs
564+
* pti_clone_pgtable() will set the global bit in any PMDs
507565
* that it clones, but we also need to get any PTEs in
508566
* the last level for areas that are not huge-page-aligned.
509567
*/

0 commit comments

Comments
 (0)