1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright(c) 2017 Intel Corporation. All rights reserved. 4 * 5 * This code is based in part on work published here: 6 * 7 * https://github.com/IAIK/KAISER 8 * 9 * The original work was written by and and signed off by for the Linux 10 * kernel by: 11 * 12 * Signed-off-by: Richard Fellner <richard.fellner@student.tugraz.at> 13 * Signed-off-by: Moritz Lipp <moritz.lipp@iaik.tugraz.at> 14 * Signed-off-by: Daniel Gruss <daniel.gruss@iaik.tugraz.at> 15 * Signed-off-by: Michael Schwarz <michael.schwarz@iaik.tugraz.at> 16 * 17 * Major changes to the original code by: Dave Hansen <dave.hansen@intel.com> 18 * Mostly rewritten by Thomas Gleixner <tglx@linutronix.de> and 19 * Andy Lutomirsky <luto@amacapital.net> 20 */ 21 #include <linux/kernel.h> 22 #include <linux/errno.h> 23 #include <linux/string.h> 24 #include <linux/types.h> 25 #include <linux/bug.h> 26 #include <linux/init.h> 27 #include <linux/spinlock.h> 28 #include <linux/mm.h> 29 #include <linux/uaccess.h> 30 #include <linux/cpu.h> 31 32 #include <asm/cpufeature.h> 33 #include <asm/hypervisor.h> 34 #include <asm/vsyscall.h> 35 #include <asm/cmdline.h> 36 #include <asm/pti.h> 37 #include <asm/pgtable.h> 38 #include <asm/pgalloc.h> 39 #include <asm/tlbflush.h> 40 #include <asm/desc.h> 41 #include <asm/sections.h> 42 #include <asm/set_memory.h> 43 44 #undef pr_fmt 45 #define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt 46 47 /* Backporting helper */ 48 #ifndef __GFP_NOTRACK 49 #define __GFP_NOTRACK 0 50 #endif 51 52 /* 53 * Define the page-table levels we clone for user-space on 32 54 * and 64 bit. 55 */ 56 #ifdef CONFIG_X86_64 57 #define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PMD 58 #else 59 #define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PTE 60 #endif 61 62 static void __init pti_print_if_insecure(const char *reason) 63 { 64 if (boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) 65 pr_info("%s\n", reason); 66 } 67 68 static void __init pti_print_if_secure(const char *reason) 69 { 70 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) 71 pr_info("%s\n", reason); 72 } 73 74 static enum pti_mode { 75 PTI_AUTO = 0, 76 PTI_FORCE_OFF, 77 PTI_FORCE_ON 78 } pti_mode; 79 80 void __init pti_check_boottime_disable(void) 81 { 82 char arg[5]; 83 int ret; 84 85 /* Assume mode is auto unless overridden. */ 86 pti_mode = PTI_AUTO; 87 88 if (hypervisor_is_type(X86_HYPER_XEN_PV)) { 89 pti_mode = PTI_FORCE_OFF; 90 pti_print_if_insecure("disabled on XEN PV."); 91 return; 92 } 93 94 ret = cmdline_find_option(boot_command_line, "pti", arg, sizeof(arg)); 95 if (ret > 0) { 96 if (ret == 3 && !strncmp(arg, "off", 3)) { 97 pti_mode = PTI_FORCE_OFF; 98 pti_print_if_insecure("disabled on command line."); 99 return; 100 } 101 if (ret == 2 && !strncmp(arg, "on", 2)) { 102 pti_mode = PTI_FORCE_ON; 103 pti_print_if_secure("force enabled on command line."); 104 goto enable; 105 } 106 if (ret == 4 && !strncmp(arg, "auto", 4)) { 107 pti_mode = PTI_AUTO; 108 goto autosel; 109 } 110 } 111 112 if (cmdline_find_option_bool(boot_command_line, "nopti") || 113 cpu_mitigations_off()) { 114 pti_mode = PTI_FORCE_OFF; 115 pti_print_if_insecure("disabled on command line."); 116 return; 117 } 118 119 autosel: 120 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) 121 return; 122 enable: 123 setup_force_cpu_cap(X86_FEATURE_PTI); 124 } 125 126 pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) 127 { 128 /* 129 * Changes to the high (kernel) portion of the kernelmode page 130 * tables are not automatically propagated to the usermode tables. 131 * 132 * Users should keep in mind that, unlike the kernelmode tables, 133 * there is no vmalloc_fault equivalent for the usermode tables. 134 * Top-level entries added to init_mm's usermode pgd after boot 135 * will not be automatically propagated to other mms. 136 */ 137 if (!pgdp_maps_userspace(pgdp)) 138 return pgd; 139 140 /* 141 * The user page tables get the full PGD, accessible from 142 * userspace: 143 */ 144 kernel_to_user_pgdp(pgdp)->pgd = pgd.pgd; 145 146 /* 147 * If this is normal user memory, make it NX in the kernel 148 * pagetables so that, if we somehow screw up and return to 149 * usermode with the kernel CR3 loaded, we'll get a page fault 150 * instead of allowing user code to execute with the wrong CR3. 151 * 152 * As exceptions, we don't set NX if: 153 * - _PAGE_USER is not set. This could be an executable 154 * EFI runtime mapping or something similar, and the kernel 155 * may execute from it 156 * - we don't have NX support 157 * - we're clearing the PGD (i.e. the new pgd is not present). 158 */ 159 if ((pgd.pgd & (_PAGE_USER|_PAGE_PRESENT)) == (_PAGE_USER|_PAGE_PRESENT) && 160 (__supported_pte_mask & _PAGE_NX)) 161 pgd.pgd |= _PAGE_NX; 162 163 /* return the copy of the PGD we want the kernel to use: */ 164 return pgd; 165 } 166 167 /* 168 * Walk the user copy of the page tables (optionally) trying to allocate 169 * page table pages on the way down. 170 * 171 * Returns a pointer to a P4D on success, or NULL on failure. 172 */ 173 static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address) 174 { 175 pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address)); 176 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); 177 178 if (address < PAGE_OFFSET) { 179 WARN_ONCE(1, "attempt to walk user address\n"); 180 return NULL; 181 } 182 183 if (pgd_none(*pgd)) { 184 unsigned long new_p4d_page = __get_free_page(gfp); 185 if (WARN_ON_ONCE(!new_p4d_page)) 186 return NULL; 187 188 set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page))); 189 } 190 BUILD_BUG_ON(pgd_large(*pgd) != 0); 191 192 return p4d_offset(pgd, address); 193 } 194 195 /* 196 * Walk the user copy of the page tables (optionally) trying to allocate 197 * page table pages on the way down. 198 * 199 * Returns a pointer to a PMD on success, or NULL on failure. 200 */ 201 static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address) 202 { 203 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); 204 p4d_t *p4d; 205 pud_t *pud; 206 207 p4d = pti_user_pagetable_walk_p4d(address); 208 if (!p4d) 209 return NULL; 210 211 BUILD_BUG_ON(p4d_large(*p4d) != 0); 212 if (p4d_none(*p4d)) { 213 unsigned long new_pud_page = __get_free_page(gfp); 214 if (WARN_ON_ONCE(!new_pud_page)) 215 return NULL; 216 217 set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page))); 218 } 219 220 pud = pud_offset(p4d, address); 221 /* The user page tables do not use large mappings: */ 222 if (pud_large(*pud)) { 223 WARN_ON(1); 224 return NULL; 225 } 226 if (pud_none(*pud)) { 227 unsigned long new_pmd_page = __get_free_page(gfp); 228 if (WARN_ON_ONCE(!new_pmd_page)) 229 return NULL; 230 231 set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page))); 232 } 233 234 return pmd_offset(pud, address); 235 } 236 237 /* 238 * Walk the shadow copy of the page tables (optionally) trying to allocate 239 * page table pages on the way down. Does not support large pages. 240 * 241 * Note: this is only used when mapping *new* kernel data into the 242 * user/shadow page tables. It is never used for userspace data. 243 * 244 * Returns a pointer to a PTE on success, or NULL on failure. 245 */ 246 static pte_t *pti_user_pagetable_walk_pte(unsigned long address) 247 { 248 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); 249 pmd_t *pmd; 250 pte_t *pte; 251 252 pmd = pti_user_pagetable_walk_pmd(address); 253 if (!pmd) 254 return NULL; 255 256 /* We can't do anything sensible if we hit a large mapping. */ 257 if (pmd_large(*pmd)) { 258 WARN_ON(1); 259 return NULL; 260 } 261 262 if (pmd_none(*pmd)) { 263 unsigned long new_pte_page = __get_free_page(gfp); 264 if (!new_pte_page) 265 return NULL; 266 267 set_pmd(pmd, __pmd(_KERNPG_TABLE | __pa(new_pte_page))); 268 } 269 270 pte = pte_offset_kernel(pmd, address); 271 if (pte_flags(*pte) & _PAGE_USER) { 272 WARN_ONCE(1, "attempt to walk to user pte\n"); 273 return NULL; 274 } 275 return pte; 276 } 277 278 #ifdef CONFIG_X86_VSYSCALL_EMULATION 279 static void __init pti_setup_vsyscall(void) 280 { 281 pte_t *pte, *target_pte; 282 unsigned int level; 283 284 pte = lookup_address(VSYSCALL_ADDR, &level); 285 if (!pte || WARN_ON(level != PG_LEVEL_4K) || pte_none(*pte)) 286 return; 287 288 target_pte = pti_user_pagetable_walk_pte(VSYSCALL_ADDR); 289 if (WARN_ON(!target_pte)) 290 return; 291 292 *target_pte = *pte; 293 set_vsyscall_pgtable_user_bits(kernel_to_user_pgdp(swapper_pg_dir)); 294 } 295 #else 296 static void __init pti_setup_vsyscall(void) { } 297 #endif 298 299 enum pti_clone_level { 300 PTI_CLONE_PMD, 301 PTI_CLONE_PTE, 302 }; 303 304 static void 305 pti_clone_pgtable(unsigned long start, unsigned long end, 306 enum pti_clone_level level) 307 { 308 unsigned long addr; 309 310 /* 311 * Clone the populated PMDs which cover start to end. These PMD areas 312 * can have holes. 313 */ 314 for (addr = start; addr < end;) { 315 pte_t *pte, *target_pte; 316 pmd_t *pmd, *target_pmd; 317 pgd_t *pgd; 318 p4d_t *p4d; 319 pud_t *pud; 320 321 /* Overflow check */ 322 if (addr < start) 323 break; 324 325 pgd = pgd_offset_k(addr); 326 if (WARN_ON(pgd_none(*pgd))) 327 return; 328 p4d = p4d_offset(pgd, addr); 329 if (WARN_ON(p4d_none(*p4d))) 330 return; 331 332 pud = pud_offset(p4d, addr); 333 if (pud_none(*pud)) { 334 WARN_ON_ONCE(addr & ~PUD_MASK); 335 addr = round_up(addr + 1, PUD_SIZE); 336 continue; 337 } 338 339 pmd = pmd_offset(pud, addr); 340 if (pmd_none(*pmd)) { 341 WARN_ON_ONCE(addr & ~PMD_MASK); 342 addr = round_up(addr + 1, PMD_SIZE); 343 continue; 344 } 345 346 if (pmd_large(*pmd) || level == PTI_CLONE_PMD) { 347 target_pmd = pti_user_pagetable_walk_pmd(addr); 348 if (WARN_ON(!target_pmd)) 349 return; 350 351 /* 352 * Only clone present PMDs. This ensures only setting 353 * _PAGE_GLOBAL on present PMDs. This should only be 354 * called on well-known addresses anyway, so a non- 355 * present PMD would be a surprise. 356 */ 357 if (WARN_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT))) 358 return; 359 360 /* 361 * Setting 'target_pmd' below creates a mapping in both 362 * the user and kernel page tables. It is effectively 363 * global, so set it as global in both copies. Note: 364 * the X86_FEATURE_PGE check is not _required_ because 365 * the CPU ignores _PAGE_GLOBAL when PGE is not 366 * supported. The check keeps consistentency with 367 * code that only set this bit when supported. 368 */ 369 if (boot_cpu_has(X86_FEATURE_PGE)) 370 *pmd = pmd_set_flags(*pmd, _PAGE_GLOBAL); 371 372 /* 373 * Copy the PMD. That is, the kernelmode and usermode 374 * tables will share the last-level page tables of this 375 * address range 376 */ 377 *target_pmd = *pmd; 378 379 addr += PMD_SIZE; 380 381 } else if (level == PTI_CLONE_PTE) { 382 383 /* Walk the page-table down to the pte level */ 384 pte = pte_offset_kernel(pmd, addr); 385 if (pte_none(*pte)) { 386 addr += PAGE_SIZE; 387 continue; 388 } 389 390 /* Only clone present PTEs */ 391 if (WARN_ON(!(pte_flags(*pte) & _PAGE_PRESENT))) 392 return; 393 394 /* Allocate PTE in the user page-table */ 395 target_pte = pti_user_pagetable_walk_pte(addr); 396 if (WARN_ON(!target_pte)) 397 return; 398 399 /* Set GLOBAL bit in both PTEs */ 400 if (boot_cpu_has(X86_FEATURE_PGE)) 401 *pte = pte_set_flags(*pte, _PAGE_GLOBAL); 402 403 /* Clone the PTE */ 404 *target_pte = *pte; 405 406 addr += PAGE_SIZE; 407 408 } else { 409 BUG(); 410 } 411 } 412 } 413 414 #ifdef CONFIG_X86_64 415 /* 416 * Clone a single p4d (i.e. a top-level entry on 4-level systems and a 417 * next-level entry on 5-level systems. 418 */ 419 static void __init pti_clone_p4d(unsigned long addr) 420 { 421 p4d_t *kernel_p4d, *user_p4d; 422 pgd_t *kernel_pgd; 423 424 user_p4d = pti_user_pagetable_walk_p4d(addr); 425 if (!user_p4d) 426 return; 427 428 kernel_pgd = pgd_offset_k(addr); 429 kernel_p4d = p4d_offset(kernel_pgd, addr); 430 *user_p4d = *kernel_p4d; 431 } 432 433 /* 434 * Clone the CPU_ENTRY_AREA and associated data into the user space visible 435 * page table. 436 */ 437 static void __init pti_clone_user_shared(void) 438 { 439 unsigned int cpu; 440 441 pti_clone_p4d(CPU_ENTRY_AREA_BASE); 442 443 for_each_possible_cpu(cpu) { 444 /* 445 * The SYSCALL64 entry code needs to be able to find the 446 * thread stack and needs one word of scratch space in which 447 * to spill a register. All of this lives in the TSS, in 448 * the sp1 and sp2 slots. 449 * 450 * This is done for all possible CPUs during boot to ensure 451 * that it's propagated to all mms. If we were to add one of 452 * these mappings during CPU hotplug, we would need to take 453 * some measure to make sure that every mm that subsequently 454 * ran on that CPU would have the relevant PGD entry in its 455 * pagetables. The usual vmalloc_fault() mechanism would not 456 * work for page faults taken in entry_SYSCALL_64 before RSP 457 * is set up. 458 */ 459 460 unsigned long va = (unsigned long)&per_cpu(cpu_tss_rw, cpu); 461 phys_addr_t pa = per_cpu_ptr_to_phys((void *)va); 462 pte_t *target_pte; 463 464 target_pte = pti_user_pagetable_walk_pte(va); 465 if (WARN_ON(!target_pte)) 466 return; 467 468 *target_pte = pfn_pte(pa >> PAGE_SHIFT, PAGE_KERNEL); 469 } 470 } 471 472 #else /* CONFIG_X86_64 */ 473 474 /* 475 * On 32 bit PAE systems with 1GB of Kernel address space there is only 476 * one pgd/p4d for the whole kernel. Cloning that would map the whole 477 * address space into the user page-tables, making PTI useless. So clone 478 * the page-table on the PMD level to prevent that. 479 */ 480 static void __init pti_clone_user_shared(void) 481 { 482 unsigned long start, end; 483 484 start = CPU_ENTRY_AREA_BASE; 485 end = start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES); 486 487 pti_clone_pgtable(start, end, PTI_CLONE_PMD); 488 } 489 #endif /* CONFIG_X86_64 */ 490 491 /* 492 * Clone the ESPFIX P4D into the user space visible page table 493 */ 494 static void __init pti_setup_espfix64(void) 495 { 496 #ifdef CONFIG_X86_ESPFIX64 497 pti_clone_p4d(ESPFIX_BASE_ADDR); 498 #endif 499 } 500 501 /* 502 * Clone the populated PMDs of the entry and irqentry text and force it RO. 503 */ 504 static void pti_clone_entry_text(void) 505 { 506 pti_clone_pgtable((unsigned long) __entry_text_start, 507 (unsigned long) __irqentry_text_end, 508 PTI_CLONE_PMD); 509 } 510 511 /* 512 * Global pages and PCIDs are both ways to make kernel TLB entries 513 * live longer, reduce TLB misses and improve kernel performance. 514 * But, leaving all kernel text Global makes it potentially accessible 515 * to Meltdown-style attacks which make it trivial to find gadgets or 516 * defeat KASLR. 517 * 518 * Only use global pages when it is really worth it. 519 */ 520 static inline bool pti_kernel_image_global_ok(void) 521 { 522 /* 523 * Systems with PCIDs get litlle benefit from global 524 * kernel text and are not worth the downsides. 525 */ 526 if (cpu_feature_enabled(X86_FEATURE_PCID)) 527 return false; 528 529 /* 530 * Only do global kernel image for pti=auto. Do the most 531 * secure thing (not global) if pti=on specified. 532 */ 533 if (pti_mode != PTI_AUTO) 534 return false; 535 536 /* 537 * K8 may not tolerate the cleared _PAGE_RW on the userspace 538 * global kernel image pages. Do the safe thing (disable 539 * global kernel image). This is unlikely to ever be 540 * noticed because PTI is disabled by default on AMD CPUs. 541 */ 542 if (boot_cpu_has(X86_FEATURE_K8)) 543 return false; 544 545 /* 546 * RANDSTRUCT derives its hardening benefits from the 547 * attacker's lack of knowledge about the layout of kernel 548 * data structures. Keep the kernel image non-global in 549 * cases where RANDSTRUCT is in use to help keep the layout a 550 * secret. 551 */ 552 if (IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT)) 553 return false; 554 555 return true; 556 } 557 558 /* 559 * For some configurations, map all of kernel text into the user page 560 * tables. This reduces TLB misses, especially on non-PCID systems. 561 */ 562 static void pti_clone_kernel_text(void) 563 { 564 /* 565 * rodata is part of the kernel image and is normally 566 * readable on the filesystem or on the web. But, do not 567 * clone the areas past rodata, they might contain secrets. 568 */ 569 unsigned long start = PFN_ALIGN(_text); 570 unsigned long end_clone = (unsigned long)__end_rodata_aligned; 571 unsigned long end_global = PFN_ALIGN((unsigned long)_etext); 572 573 if (!pti_kernel_image_global_ok()) 574 return; 575 576 pr_debug("mapping partial kernel image into user address space\n"); 577 578 /* 579 * Note that this will undo _some_ of the work that 580 * pti_set_kernel_image_nonglobal() did to clear the 581 * global bit. 582 */ 583 pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE); 584 585 /* 586 * pti_clone_pgtable() will set the global bit in any PMDs 587 * that it clones, but we also need to get any PTEs in 588 * the last level for areas that are not huge-page-aligned. 589 */ 590 591 /* Set the global bit for normal non-__init kernel text: */ 592 set_memory_global(start, (end_global - start) >> PAGE_SHIFT); 593 } 594 595 static void pti_set_kernel_image_nonglobal(void) 596 { 597 /* 598 * The identity map is created with PMDs, regardless of the 599 * actual length of the kernel. We need to clear 600 * _PAGE_GLOBAL up to a PMD boundary, not just to the end 601 * of the image. 602 */ 603 unsigned long start = PFN_ALIGN(_text); 604 unsigned long end = ALIGN((unsigned long)_end, PMD_PAGE_SIZE); 605 606 /* 607 * This clears _PAGE_GLOBAL from the entire kernel image. 608 * pti_clone_kernel_text() map put _PAGE_GLOBAL back for 609 * areas that are mapped to userspace. 610 */ 611 set_memory_nonglobal(start, (end - start) >> PAGE_SHIFT); 612 } 613 614 /* 615 * Initialize kernel page table isolation 616 */ 617 void __init pti_init(void) 618 { 619 if (!boot_cpu_has(X86_FEATURE_PTI)) 620 return; 621 622 pr_info("enabled\n"); 623 624 #ifdef CONFIG_X86_32 625 /* 626 * We check for X86_FEATURE_PCID here. But the init-code will 627 * clear the feature flag on 32 bit because the feature is not 628 * supported on 32 bit anyway. To print the warning we need to 629 * check with cpuid directly again. 630 */ 631 if (cpuid_ecx(0x1) & BIT(17)) { 632 /* Use printk to work around pr_fmt() */ 633 printk(KERN_WARNING "\n"); 634 printk(KERN_WARNING "************************************************************\n"); 635 printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n"); 636 printk(KERN_WARNING "** **\n"); 637 printk(KERN_WARNING "** You are using 32-bit PTI on a 64-bit PCID-capable CPU. **\n"); 638 printk(KERN_WARNING "** Your performance will increase dramatically if you **\n"); 639 printk(KERN_WARNING "** switch to a 64-bit kernel! **\n"); 640 printk(KERN_WARNING "** **\n"); 641 printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n"); 642 printk(KERN_WARNING "************************************************************\n"); 643 } 644 #endif 645 646 pti_clone_user_shared(); 647 648 /* Undo all global bits from the init pagetables in head_64.S: */ 649 pti_set_kernel_image_nonglobal(); 650 /* Replace some of the global bits just for shared entry text: */ 651 pti_clone_entry_text(); 652 pti_setup_espfix64(); 653 pti_setup_vsyscall(); 654 } 655 656 /* 657 * Finalize the kernel mappings in the userspace page-table. Some of the 658 * mappings for the kernel image might have changed since pti_init() 659 * cloned them. This is because parts of the kernel image have been 660 * mapped RO and/or NX. These changes need to be cloned again to the 661 * userspace page-table. 662 */ 663 void pti_finalize(void) 664 { 665 if (!boot_cpu_has(X86_FEATURE_PTI)) 666 return; 667 /* 668 * We need to clone everything (again) that maps parts of the 669 * kernel image. 670 */ 671 pti_clone_entry_text(); 672 pti_clone_kernel_text(); 673 674 debug_checkwx_user(); 675 } 676