]> git.proxmox.com Git - pve-kernel.git/blob - patches/kernel/0196-x86-mm-pti-Add-functions-to-clone-kernel-PMDs.patch
KPTI: add follow-up fixes
[pve-kernel.git] / patches / kernel / 0196-x86-mm-pti-Add-functions-to-clone-kernel-PMDs.patch
1 From bcb5ffbfba8c6c557ad536eb9084040b8e52923e Mon Sep 17 00:00:00 2001
2 From: Andy Lutomirski <luto@kernel.org>
3 Date: Mon, 4 Dec 2017 15:07:42 +0100
4 Subject: [PATCH 196/241] x86/mm/pti: Add functions to clone kernel PMDs
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 CVE-2017-5754
10
11 Provide infrastructure to:
12
13 - find a kernel PMD for a mapping which must be visible to user space for
14 the entry/exit code to work.
15
16 - walk an address range and share the kernel PMD with it.
17
18 This reuses a small part of the original KAISER patches to populate the
19 user space page table.
20
21 [ tglx: Made it universally usable so it can be used for any kind of shared
22 mapping. Add a mechanism to clear specific bits in the user space
23 visible PMD entry. Folded Andys simplifactions ]
24
25 Originally-by: Dave Hansen <dave.hansen@linux.intel.com>
26 Signed-off-by: Andy Lutomirski <luto@kernel.org>
27 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
28 Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
29 Cc: Borislav Petkov <bp@alien8.de>
30 Cc: Borislav Petkov <bpetkov@suse.de>
31 Cc: Brian Gerst <brgerst@gmail.com>
32 Cc: Dave Hansen <dave.hansen@linux.intel.com>
33 Cc: David Laight <David.Laight@aculab.com>
34 Cc: Denys Vlasenko <dvlasenk@redhat.com>
35 Cc: Eduardo Valentin <eduval@amazon.com>
36 Cc: Greg KH <gregkh@linuxfoundation.org>
37 Cc: H. Peter Anvin <hpa@zytor.com>
38 Cc: Josh Poimboeuf <jpoimboe@redhat.com>
39 Cc: Juergen Gross <jgross@suse.com>
40 Cc: Linus Torvalds <torvalds@linux-foundation.org>
41 Cc: Peter Zijlstra <peterz@infradead.org>
42 Cc: Will Deacon <will.deacon@arm.com>
43 Cc: aliguori@amazon.com
44 Cc: daniel.gruss@iaik.tugraz.at
45 Cc: hughd@google.com
46 Cc: keescook@google.com
47 Signed-off-by: Ingo Molnar <mingo@kernel.org>
48 (cherry picked from commit 03f4424f348e8be95eb1bbeba09461cd7b867828)
49 Signed-off-by: Andy Whitcroft <apw@canonical.com>
50 Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
51 (cherry picked from commit 262ab7e8665e88581d20ccaefa107340457224bb)
52 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
53 ---
54 arch/x86/mm/pti.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
55 1 file changed, 127 insertions(+)
56
57 diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
58 index 69a983365392..d58bcee470fc 100644
59 --- a/arch/x86/mm/pti.c
60 +++ b/arch/x86/mm/pti.c
61 @@ -48,6 +48,11 @@
62 #undef pr_fmt
63 #define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt
64
65 +/* Backporting helper */
66 +#ifndef __GFP_NOTRACK
67 +#define __GFP_NOTRACK 0
68 +#endif
69 +
70 static void __init pti_print_if_insecure(const char *reason)
71 {
72 if (boot_cpu_has_bug(X86_BUG_CPU_INSECURE))
73 @@ -137,6 +142,128 @@ pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
74 return pgd;
75 }
76
77 +/*
78 + * Walk the user copy of the page tables (optionally) trying to allocate
79 + * page table pages on the way down.
80 + *
81 + * Returns a pointer to a P4D on success, or NULL on failure.
82 + */
83 +static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
84 +{
85 + pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address));
86 + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
87 +
88 + if (address < PAGE_OFFSET) {
89 + WARN_ONCE(1, "attempt to walk user address\n");
90 + return NULL;
91 + }
92 +
93 + if (pgd_none(*pgd)) {
94 + unsigned long new_p4d_page = __get_free_page(gfp);
95 + if (!new_p4d_page)
96 + return NULL;
97 +
98 + if (pgd_none(*pgd)) {
99 + set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
100 + new_p4d_page = 0;
101 + }
102 + if (new_p4d_page)
103 + free_page(new_p4d_page);
104 + }
105 + BUILD_BUG_ON(pgd_large(*pgd) != 0);
106 +
107 + return p4d_offset(pgd, address);
108 +}
109 +
110 +/*
111 + * Walk the user copy of the page tables (optionally) trying to allocate
112 + * page table pages on the way down.
113 + *
114 + * Returns a pointer to a PMD on success, or NULL on failure.
115 + */
116 +static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
117 +{
118 + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
119 + p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
120 + pud_t *pud;
121 +
122 + BUILD_BUG_ON(p4d_large(*p4d) != 0);
123 + if (p4d_none(*p4d)) {
124 + unsigned long new_pud_page = __get_free_page(gfp);
125 + if (!new_pud_page)
126 + return NULL;
127 +
128 + if (p4d_none(*p4d)) {
129 + set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
130 + new_pud_page = 0;
131 + }
132 + if (new_pud_page)
133 + free_page(new_pud_page);
134 + }
135 +
136 + pud = pud_offset(p4d, address);
137 + /* The user page tables do not use large mappings: */
138 + if (pud_large(*pud)) {
139 + WARN_ON(1);
140 + return NULL;
141 + }
142 + if (pud_none(*pud)) {
143 + unsigned long new_pmd_page = __get_free_page(gfp);
144 + if (!new_pmd_page)
145 + return NULL;
146 +
147 + if (pud_none(*pud)) {
148 + set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
149 + new_pmd_page = 0;
150 + }
151 + if (new_pmd_page)
152 + free_page(new_pmd_page);
153 + }
154 +
155 + return pmd_offset(pud, address);
156 +}
157 +
158 +static void __init
159 +pti_clone_pmds(unsigned long start, unsigned long end, pmdval_t clear)
160 +{
161 + unsigned long addr;
162 +
163 + /*
164 + * Clone the populated PMDs which cover start to end. These PMD areas
165 + * can have holes.
166 + */
167 + for (addr = start; addr < end; addr += PMD_SIZE) {
168 + pmd_t *pmd, *target_pmd;
169 + pgd_t *pgd;
170 + p4d_t *p4d;
171 + pud_t *pud;
172 +
173 + pgd = pgd_offset_k(addr);
174 + if (WARN_ON(pgd_none(*pgd)))
175 + return;
176 + p4d = p4d_offset(pgd, addr);
177 + if (WARN_ON(p4d_none(*p4d)))
178 + return;
179 + pud = pud_offset(p4d, addr);
180 + if (pud_none(*pud))
181 + continue;
182 + pmd = pmd_offset(pud, addr);
183 + if (pmd_none(*pmd))
184 + continue;
185 +
186 + target_pmd = pti_user_pagetable_walk_pmd(addr);
187 + if (WARN_ON(!target_pmd))
188 + return;
189 +
190 + /*
191 + * Copy the PMD. That is, the kernelmode and usermode
192 + * tables will share the last-level page tables of this
193 + * address range
194 + */
195 + *target_pmd = pmd_clear_flags(*pmd, clear);
196 + }
197 +}
198 +
199 /*
200 * Initialize kernel page table isolation
201 */
202 --
203 2.14.2
204