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