1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42#undef pr_fmt
43#define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt
44
45
46#ifndef __GFP_NOTRACK
47#define __GFP_NOTRACK 0
48#endif
49
50
51
52
53
54#ifdef CONFIG_X86_64
55#define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PMD
56#else
57#define PTI_LEVEL_KERNEL_IMAGE PTI_CLONE_PTE
58#endif
59
60static void __init pti_print_if_insecure(const char *reason)
61{
62 if (boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
63 pr_info("%s\n", reason);
64}
65
66static void __init pti_print_if_secure(const char *reason)
67{
68 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
69 pr_info("%s\n", reason);
70}
71
72static enum pti_mode {
73 PTI_AUTO = 0,
74 PTI_FORCE_OFF,
75 PTI_FORCE_ON
76} pti_mode;
77
78void __init pti_check_boottime_disable(void)
79{
80 char arg[5];
81 int ret;
82
83
84 pti_mode = PTI_AUTO;
85
86 if (hypervisor_is_type(X86_HYPER_XEN_PV)) {
87 pti_mode = PTI_FORCE_OFF;
88 pti_print_if_insecure("disabled on XEN PV.");
89 return;
90 }
91
92 ret = cmdline_find_option(boot_command_line, "pti", arg, sizeof(arg));
93 if (ret > 0) {
94 if (ret == 3 && !strncmp(arg, "off", 3)) {
95 pti_mode = PTI_FORCE_OFF;
96 pti_print_if_insecure("disabled on command line.");
97 return;
98 }
99 if (ret == 2 && !strncmp(arg, "on", 2)) {
100 pti_mode = PTI_FORCE_ON;
101 pti_print_if_secure("force enabled on command line.");
102 goto enable;
103 }
104 if (ret == 4 && !strncmp(arg, "auto", 4)) {
105 pti_mode = PTI_AUTO;
106 goto autosel;
107 }
108 }
109
110 if (cmdline_find_option_bool(boot_command_line, "nopti") ||
111 cpu_mitigations_off()) {
112 pti_mode = PTI_FORCE_OFF;
113 pti_print_if_insecure("disabled on command line.");
114 return;
115 }
116
117autosel:
118 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
119 return;
120enable:
121 setup_force_cpu_cap(X86_FEATURE_PTI);
122}
123
124pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
125{
126
127
128
129
130
131
132
133
134
135 if (!pgdp_maps_userspace(pgdp))
136 return pgd;
137
138
139
140
141
142 kernel_to_user_pgdp(pgdp)->pgd = pgd.pgd;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 if ((pgd.pgd & (_PAGE_USER|_PAGE_PRESENT)) == (_PAGE_USER|_PAGE_PRESENT) &&
158 (__supported_pte_mask & _PAGE_NX))
159 pgd.pgd |= _PAGE_NX;
160
161
162 return pgd;
163}
164
165
166
167
168
169
170
171static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
172{
173 pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address));
174 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
175
176 if (address < PAGE_OFFSET) {
177 WARN_ONCE(1, "attempt to walk user address\n");
178 return NULL;
179 }
180
181 if (pgd_none(*pgd)) {
182 unsigned long new_p4d_page = __get_free_page(gfp);
183 if (WARN_ON_ONCE(!new_p4d_page))
184 return NULL;
185
186 set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
187 }
188 BUILD_BUG_ON(pgd_large(*pgd) != 0);
189
190 return p4d_offset(pgd, address);
191}
192
193
194
195
196
197
198
199static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
200{
201 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
202 p4d_t *p4d;
203 pud_t *pud;
204
205 p4d = pti_user_pagetable_walk_p4d(address);
206 if (!p4d)
207 return NULL;
208
209 BUILD_BUG_ON(p4d_large(*p4d) != 0);
210 if (p4d_none(*p4d)) {
211 unsigned long new_pud_page = __get_free_page(gfp);
212 if (WARN_ON_ONCE(!new_pud_page))
213 return NULL;
214
215 set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
216 }
217
218 pud = pud_offset(p4d, address);
219
220 if (pud_large(*pud)) {
221 WARN_ON(1);
222 return NULL;
223 }
224 if (pud_none(*pud)) {
225 unsigned long new_pmd_page = __get_free_page(gfp);
226 if (WARN_ON_ONCE(!new_pmd_page))
227 return NULL;
228
229 set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
230 }
231
232 return pmd_offset(pud, address);
233}
234
235
236
237
238
239
240
241
242
243
244static pte_t *pti_user_pagetable_walk_pte(unsigned long address)
245{
246 gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
247 pmd_t *pmd;
248 pte_t *pte;
249
250 pmd = pti_user_pagetable_walk_pmd(address);
251 if (!pmd)
252 return NULL;
253
254
255 if (pmd_large(*pmd)) {
256 WARN_ON(1);
257 return NULL;
258 }
259
260 if (pmd_none(*pmd)) {
261 unsigned long new_pte_page = __get_free_page(gfp);
262 if (!new_pte_page)
263 return NULL;
264
265 set_pmd(pmd, __pmd(_KERNPG_TABLE | __pa(new_pte_page)));
266 }
267
268 pte = pte_offset_kernel(pmd, address);
269 if (pte_flags(*pte) & _PAGE_USER) {
270 WARN_ONCE(1, "attempt to walk to user pte\n");
271 return NULL;
272 }
273 return pte;
274}
275
276#ifdef CONFIG_X86_VSYSCALL_EMULATION
277static void __init pti_setup_vsyscall(void)
278{
279 pte_t *pte, *target_pte;
280 unsigned int level;
281
282 pte = lookup_address(VSYSCALL_ADDR, &level);
283 if (!pte || WARN_ON(level != PG_LEVEL_4K) || pte_none(*pte))
284 return;
285
286 target_pte = pti_user_pagetable_walk_pte(VSYSCALL_ADDR);
287 if (WARN_ON(!target_pte))
288 return;
289
290 *target_pte = *pte;
291 set_vsyscall_pgtable_user_bits(kernel_to_user_pgdp(swapper_pg_dir));
292}
293#else
294static void __init pti_setup_vsyscall(void) { }
295#endif
296
297enum pti_clone_level {
298 PTI_CLONE_PMD,
299 PTI_CLONE_PTE,
300};
301
302static void
303pti_clone_pgtable(unsigned long start, unsigned long end,
304 enum pti_clone_level level)
305{
306 unsigned long addr;
307
308
309
310
311
312 for (addr = start; addr < end;) {
313 pte_t *pte, *target_pte;
314 pmd_t *pmd, *target_pmd;
315 pgd_t *pgd;
316 p4d_t *p4d;
317 pud_t *pud;
318
319
320 if (addr < start)
321 break;
322
323 pgd = pgd_offset_k(addr);
324 if (WARN_ON(pgd_none(*pgd)))
325 return;
326 p4d = p4d_offset(pgd, addr);
327 if (WARN_ON(p4d_none(*p4d)))
328 return;
329
330 pud = pud_offset(p4d, addr);
331 if (pud_none(*pud)) {
332 WARN_ON_ONCE(addr & ~PUD_MASK);
333 addr = round_up(addr + 1, PUD_SIZE);
334 continue;
335 }
336
337 pmd = pmd_offset(pud, addr);
338 if (pmd_none(*pmd)) {
339 WARN_ON_ONCE(addr & ~PMD_MASK);
340 addr = round_up(addr + 1, PMD_SIZE);
341 continue;
342 }
343
344 if (pmd_large(*pmd) || level == PTI_CLONE_PMD) {
345 target_pmd = pti_user_pagetable_walk_pmd(addr);
346 if (WARN_ON(!target_pmd))
347 return;
348
349
350
351
352
353
354
355 if (WARN_ON(!(pmd_flags(*pmd) & _PAGE_PRESENT)))
356 return;
357
358
359
360
361
362
363
364
365
366
367 if (boot_cpu_has(X86_FEATURE_PGE))
368 *pmd = pmd_set_flags(*pmd, _PAGE_GLOBAL);
369
370
371
372
373
374
375 *target_pmd = *pmd;
376
377 addr += PMD_SIZE;
378
379 } else if (level == PTI_CLONE_PTE) {
380
381
382 pte = pte_offset_kernel(pmd, addr);
383 if (pte_none(*pte)) {
384 addr += PAGE_SIZE;
385 continue;
386 }
387
388
389 if (WARN_ON(!(pte_flags(*pte) & _PAGE_PRESENT)))
390 return;
391
392
393 target_pte = pti_user_pagetable_walk_pte(addr);
394 if (WARN_ON(!target_pte))
395 return;
396
397
398 if (boot_cpu_has(X86_FEATURE_PGE))
399 *pte = pte_set_flags(*pte, _PAGE_GLOBAL);
400
401
402 *target_pte = *pte;
403
404 addr += PAGE_SIZE;
405
406 } else {
407 BUG();
408 }
409 }
410}
411
412#ifdef CONFIG_X86_64
413
414
415
416
417static void __init pti_clone_p4d(unsigned long addr)
418{
419 p4d_t *kernel_p4d, *user_p4d;
420 pgd_t *kernel_pgd;
421
422 user_p4d = pti_user_pagetable_walk_p4d(addr);
423 if (!user_p4d)
424 return;
425
426 kernel_pgd = pgd_offset_k(addr);
427 kernel_p4d = p4d_offset(kernel_pgd, addr);
428 *user_p4d = *kernel_p4d;
429}
430
431
432
433
434
435static void __init pti_clone_user_shared(void)
436{
437 unsigned int cpu;
438
439 pti_clone_p4d(CPU_ENTRY_AREA_BASE);
440
441 for_each_possible_cpu(cpu) {
442
443
444
445
446
447
448
449
450
451
452 unsigned long va = (unsigned long)&per_cpu(cpu_tss_rw, cpu);
453 phys_addr_t pa = per_cpu_ptr_to_phys((void *)va);
454 pte_t *target_pte;
455
456 target_pte = pti_user_pagetable_walk_pte(va);
457 if (WARN_ON(!target_pte))
458 return;
459
460 *target_pte = pfn_pte(pa >> PAGE_SHIFT, PAGE_KERNEL);
461 }
462}
463
464#else
465
466
467
468
469
470
471
472static void __init pti_clone_user_shared(void)
473{
474 unsigned long start, end;
475
476 start = CPU_ENTRY_AREA_BASE;
477 end = start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES);
478
479 pti_clone_pgtable(start, end, PTI_CLONE_PMD);
480}
481#endif
482
483
484
485
486static void __init pti_setup_espfix64(void)
487{
488#ifdef CONFIG_X86_ESPFIX64
489 pti_clone_p4d(ESPFIX_BASE_ADDR);
490#endif
491}
492
493
494
495
496static void pti_clone_entry_text(void)
497{
498 pti_clone_pgtable((unsigned long) __entry_text_start,
499 (unsigned long) __entry_text_end,
500 PTI_CLONE_PMD);
501}
502
503
504
505
506
507
508
509
510
511
512static inline bool pti_kernel_image_global_ok(void)
513{
514
515
516
517
518 if (cpu_feature_enabled(X86_FEATURE_PCID))
519 return false;
520
521
522
523
524
525 if (pti_mode != PTI_AUTO)
526 return false;
527
528
529
530
531
532
533
534 if (boot_cpu_has(X86_FEATURE_K8))
535 return false;
536
537
538
539
540
541
542
543
544 if (IS_ENABLED(CONFIG_GCC_PLUGIN_RANDSTRUCT))
545 return false;
546
547 return true;
548}
549
550
551
552
553
554static void pti_clone_kernel_text(void)
555{
556
557
558
559
560
561 unsigned long start = PFN_ALIGN(_text);
562 unsigned long end_clone = (unsigned long)__end_rodata_aligned;
563 unsigned long end_global = PFN_ALIGN((unsigned long)_etext);
564
565 if (!pti_kernel_image_global_ok())
566 return;
567
568 pr_debug("mapping partial kernel image into user address space\n");
569
570
571
572
573
574
575 pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE);
576
577
578
579
580
581
582
583
584 set_memory_global(start, (end_global - start) >> PAGE_SHIFT);
585}
586
587static void pti_set_kernel_image_nonglobal(void)
588{
589
590
591
592
593
594
595 unsigned long start = PFN_ALIGN(_text);
596 unsigned long end = ALIGN((unsigned long)_end, PMD_PAGE_SIZE);
597
598
599
600
601
602
603 set_memory_nonglobal(start, (end - start) >> PAGE_SHIFT);
604}
605
606
607
608
609void __init pti_init(void)
610{
611 if (!boot_cpu_has(X86_FEATURE_PTI))
612 return;
613
614 pr_info("enabled\n");
615
616#ifdef CONFIG_X86_32
617
618
619
620
621
622
623 if (cpuid_ecx(0x1) & BIT(17)) {
624
625 printk(KERN_WARNING "\n");
626 printk(KERN_WARNING "************************************************************\n");
627 printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n");
628 printk(KERN_WARNING "** **\n");
629 printk(KERN_WARNING "** You are using 32-bit PTI on a 64-bit PCID-capable CPU. **\n");
630 printk(KERN_WARNING "** Your performance will increase dramatically if you **\n");
631 printk(KERN_WARNING "** switch to a 64-bit kernel! **\n");
632 printk(KERN_WARNING "** **\n");
633 printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! **\n");
634 printk(KERN_WARNING "************************************************************\n");
635 }
636#endif
637
638 pti_clone_user_shared();
639
640
641 pti_set_kernel_image_nonglobal();
642
643 pti_clone_entry_text();
644 pti_setup_espfix64();
645 pti_setup_vsyscall();
646}
647
648
649
650
651
652
653
654
655void pti_finalize(void)
656{
657 if (!boot_cpu_has(X86_FEATURE_PTI))
658 return;
659
660
661
662
663 pti_clone_entry_text();
664 pti_clone_kernel_text();
665
666 debug_checkwx_user();
667}
668