]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/ldt.c
x86/pti: Put the LDT in its own PGD if PTI is on
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / ldt.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 * Copyright (C) 2002 Andi Kleen
78aa1f66 5 *
1da177e4 6 * This handles calls from both 32bit and 64bit mode.
bf7ee649
PZ
7 *
8 * Lock order:
9 * contex.ldt_usr_sem
10 * mmap_sem
11 * context.lock
1da177e4
LT
12 */
13
14#include <linux/errno.h>
5a0e3ad6 15#include <linux/gfp.h>
1da177e4
LT
16#include <linux/sched.h>
17#include <linux/string.h>
18#include <linux/mm.h>
19#include <linux/smp.h>
d865f635 20#include <linux/syscalls.h>
37868fe1 21#include <linux/slab.h>
1da177e4 22#include <linux/vmalloc.h>
423a5405 23#include <linux/uaccess.h>
1da177e4 24
1da177e4 25#include <asm/ldt.h>
c2506438 26#include <asm/tlb.h>
1da177e4 27#include <asm/desc.h>
70f5088d 28#include <asm/mmu_context.h>
bbc1f698 29#include <asm/syscalls.h>
1da177e4 30
295cb0b0
AL
31static void refresh_ldt_segments(void)
32{
33#ifdef CONFIG_X86_64
34 unsigned short sel;
35
36 /*
37 * Make sure that the cached DS and ES descriptors match the updated
38 * LDT.
39 */
40 savesegment(ds, sel);
41 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
42 loadsegment(ds, sel);
43
44 savesegment(es, sel);
45 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
46 loadsegment(es, sel);
47#endif
48}
49
bf7ee649 50/* context.lock is held by the task which issued the smp function call */
3d28ebce 51static void flush_ldt(void *__mm)
1da177e4 52{
3d28ebce 53 struct mm_struct *mm = __mm;
37868fe1 54
3d28ebce 55 if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
37868fe1
AL
56 return;
57
c2506438 58 load_mm_ldt(mm);
295cb0b0
AL
59
60 refresh_ldt_segments();
1da177e4 61}
1da177e4 62
37868fe1 63/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
bbf79d21 64static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
1da177e4 65{
37868fe1 66 struct ldt_struct *new_ldt;
990e9dc3 67 unsigned int alloc_size;
37868fe1 68
bbf79d21 69 if (num_entries > LDT_ENTRIES)
37868fe1
AL
70 return NULL;
71
72 new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
73 if (!new_ldt)
74 return NULL;
75
76 BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
bbf79d21 77 alloc_size = num_entries * LDT_ENTRY_SIZE;
37868fe1
AL
78
79 /*
80 * Xen is very picky: it requires a page-aligned LDT that has no
81 * trailing nonzero bytes in any page that contains LDT descriptors.
82 * Keep it simple: zero the whole allocation and never allocate less
83 * than PAGE_SIZE.
84 */
85 if (alloc_size > PAGE_SIZE)
86 new_ldt->entries = vzalloc(alloc_size);
1da177e4 87 else
f454b478 88 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
1da177e4 89
37868fe1
AL
90 if (!new_ldt->entries) {
91 kfree(new_ldt);
92 return NULL;
93 }
77e463d1 94
c2506438
AL
95 /* The new LDT isn't aliased for PTI yet. */
96 new_ldt->slot = -1;
97
bbf79d21 98 new_ldt->nr_entries = num_entries;
37868fe1
AL
99 return new_ldt;
100}
38ffbe66 101
c2506438
AL
102/*
103 * If PTI is enabled, this maps the LDT into the kernelmode and
104 * usermode tables for the given mm.
105 *
106 * There is no corresponding unmap function. Even if the LDT is freed, we
107 * leave the PTEs around until the slot is reused or the mm is destroyed.
108 * This is harmless: the LDT is always in ordinary memory, and no one will
109 * access the freed slot.
110 *
111 * If we wanted to unmap freed LDTs, we'd also need to do a flush to make
112 * it useful, and the flush would slow down modify_ldt().
113 */
114static int
115map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
116{
117#ifdef CONFIG_PAGE_TABLE_ISOLATION
118 bool is_vmalloc, had_top_level_entry;
119 unsigned long va;
120 spinlock_t *ptl;
121 pgd_t *pgd;
122 int i;
123
124 if (!static_cpu_has(X86_FEATURE_PTI))
125 return 0;
126
127 /*
128 * Any given ldt_struct should have map_ldt_struct() called at most
129 * once.
130 */
131 WARN_ON(ldt->slot != -1);
132
133 /*
134 * Did we already have the top level entry allocated? We can't
135 * use pgd_none() for this because it doens't do anything on
136 * 4-level page table kernels.
137 */
138 pgd = pgd_offset(mm, LDT_BASE_ADDR);
139 had_top_level_entry = (pgd->pgd != 0);
140
141 is_vmalloc = is_vmalloc_addr(ldt->entries);
142
143 for (i = 0; i * PAGE_SIZE < ldt->nr_entries * LDT_ENTRY_SIZE; i++) {
144 unsigned long offset = i << PAGE_SHIFT;
145 const void *src = (char *)ldt->entries + offset;
146 unsigned long pfn;
147 pte_t pte, *ptep;
148
149 va = (unsigned long)ldt_slot_va(slot) + offset;
150 pfn = is_vmalloc ? vmalloc_to_pfn(src) :
151 page_to_pfn(virt_to_page(src));
152 /*
153 * Treat the PTI LDT range as a *userspace* range.
154 * get_locked_pte() will allocate all needed pagetables
155 * and account for them in this mm.
156 */
157 ptep = get_locked_pte(mm, va, &ptl);
158 if (!ptep)
159 return -ENOMEM;
160 pte = pfn_pte(pfn, __pgprot(__PAGE_KERNEL & ~_PAGE_GLOBAL));
161 set_pte_at(mm, va, ptep, pte);
162 pte_unmap_unlock(ptep, ptl);
163 }
164
165 if (mm->context.ldt) {
166 /*
167 * We already had an LDT. The top-level entry should already
168 * have been allocated and synchronized with the usermode
169 * tables.
170 */
171 WARN_ON(!had_top_level_entry);
172 if (static_cpu_has(X86_FEATURE_PTI))
173 WARN_ON(!kernel_to_user_pgdp(pgd)->pgd);
174 } else {
175 /*
176 * This is the first time we're mapping an LDT for this process.
177 * Sync the pgd to the usermode tables.
178 */
179 WARN_ON(had_top_level_entry);
180 if (static_cpu_has(X86_FEATURE_PTI)) {
181 WARN_ON(kernel_to_user_pgdp(pgd)->pgd);
182 set_pgd(kernel_to_user_pgdp(pgd), *pgd);
183 }
184 }
185
186 va = (unsigned long)ldt_slot_va(slot);
187 flush_tlb_mm_range(mm, va, va + LDT_SLOT_STRIDE, 0);
188
189 ldt->slot = slot;
190#endif
191 return 0;
192}
193
194static void free_ldt_pgtables(struct mm_struct *mm)
195{
196#ifdef CONFIG_PAGE_TABLE_ISOLATION
197 struct mmu_gather tlb;
198 unsigned long start = LDT_BASE_ADDR;
199 unsigned long end = start + (1UL << PGDIR_SHIFT);
200
201 if (!static_cpu_has(X86_FEATURE_PTI))
202 return;
203
204 tlb_gather_mmu(&tlb, mm, start, end);
205 free_pgd_range(&tlb, start, end, start, end);
206 tlb_finish_mmu(&tlb, start, end);
207#endif
208}
209
37868fe1
AL
210/* After calling this, the LDT is immutable. */
211static void finalize_ldt_struct(struct ldt_struct *ldt)
212{
bbf79d21 213 paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
1da177e4
LT
214}
215
bf7ee649 216static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
1da177e4 217{
bf7ee649
PZ
218 mutex_lock(&mm->context.lock);
219
7252704b 220 /* Synchronizes with READ_ONCE in load_mm_ldt. */
bf7ee649 221 smp_store_release(&mm->context.ldt, ldt);
37868fe1 222
bf7ee649
PZ
223 /* Activate the LDT for all CPUs using currents mm. */
224 on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
225
226 mutex_unlock(&mm->context.lock);
37868fe1 227}
78aa1f66 228
37868fe1
AL
229static void free_ldt_struct(struct ldt_struct *ldt)
230{
231 if (likely(!ldt))
232 return;
38ffbe66 233
bbf79d21
BP
234 paravirt_free_ldt(ldt->entries, ldt->nr_entries);
235 if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
8d5341a6 236 vfree_atomic(ldt->entries);
37868fe1 237 else
f454b478 238 free_page((unsigned long)ldt->entries);
37868fe1 239 kfree(ldt);
1da177e4
LT
240}
241
242/*
f90d2542
TG
243 * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
244 * the new task is not running, so nothing can be installed.
1da177e4 245 */
f90d2542 246int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
1da177e4 247{
37868fe1 248 struct ldt_struct *new_ldt;
1da177e4
LT
249 int retval = 0;
250
f90d2542 251 if (!old_mm)
37868fe1 252 return 0;
37868fe1
AL
253
254 mutex_lock(&old_mm->context.lock);
f90d2542 255 if (!old_mm->context.ldt)
37868fe1 256 goto out_unlock;
37868fe1 257
bbf79d21 258 new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
37868fe1
AL
259 if (!new_ldt) {
260 retval = -ENOMEM;
261 goto out_unlock;
262 }
263
264 memcpy(new_ldt->entries, old_mm->context.ldt->entries,
bbf79d21 265 new_ldt->nr_entries * LDT_ENTRY_SIZE);
37868fe1
AL
266 finalize_ldt_struct(new_ldt);
267
c2506438
AL
268 retval = map_ldt_struct(mm, new_ldt, 0);
269 if (retval) {
270 free_ldt_pgtables(mm);
271 free_ldt_struct(new_ldt);
272 goto out_unlock;
273 }
37868fe1
AL
274 mm->context.ldt = new_ldt;
275
276out_unlock:
277 mutex_unlock(&old_mm->context.lock);
1da177e4
LT
278 return retval;
279}
280
281/*
77e463d1
TG
282 * No need to lock the MM as we are the last user
283 *
284 * 64bit: Don't touch the LDT register - we're already in the next thread.
1da177e4 285 */
39a0526f 286void destroy_context_ldt(struct mm_struct *mm)
1da177e4 287{
37868fe1
AL
288 free_ldt_struct(mm->context.ldt);
289 mm->context.ldt = NULL;
1da177e4
LT
290}
291
c2506438
AL
292void ldt_arch_exit_mmap(struct mm_struct *mm)
293{
294 free_ldt_pgtables(mm);
295}
296
78aa1f66 297static int read_ldt(void __user *ptr, unsigned long bytecount)
1da177e4 298{
78aa1f66 299 struct mm_struct *mm = current->mm;
bbf79d21
BP
300 unsigned long entries_size;
301 int retval;
1da177e4 302
bf7ee649 303 down_read(&mm->context.ldt_usr_sem);
37868fe1
AL
304
305 if (!mm->context.ldt) {
306 retval = 0;
307 goto out_unlock;
308 }
309
78aa1f66
TG
310 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
311 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
1da177e4 312
bbf79d21
BP
313 entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
314 if (entries_size > bytecount)
315 entries_size = bytecount;
1da177e4 316
bbf79d21 317 if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
37868fe1
AL
318 retval = -EFAULT;
319 goto out_unlock;
320 }
321
bbf79d21 322 if (entries_size != bytecount) {
37868fe1 323 /* Zero-fill the rest and pretend we read bytecount bytes. */
bbf79d21 324 if (clear_user(ptr + entries_size, bytecount - entries_size)) {
37868fe1
AL
325 retval = -EFAULT;
326 goto out_unlock;
1da177e4
LT
327 }
328 }
37868fe1
AL
329 retval = bytecount;
330
331out_unlock:
bf7ee649 332 up_read(&mm->context.ldt_usr_sem);
37868fe1 333 return retval;
1da177e4
LT
334}
335
78aa1f66 336static int read_default_ldt(void __user *ptr, unsigned long bytecount)
1da177e4 337{
77e463d1
TG
338 /* CHECKME: Can we use _one_ random number ? */
339#ifdef CONFIG_X86_32
340 unsigned long size = 5 * sizeof(struct desc_struct);
341#else
342 unsigned long size = 128;
343#endif
344 if (bytecount > size)
345 bytecount = size;
1da177e4
LT
346 if (clear_user(ptr, bytecount))
347 return -EFAULT;
78aa1f66 348 return bytecount;
1da177e4
LT
349}
350
78aa1f66 351static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
1da177e4 352{
70f5088d 353 struct mm_struct *mm = current->mm;
990e9dc3 354 struct ldt_struct *new_ldt, *old_ldt;
bbf79d21 355 unsigned int old_nr_entries, new_nr_entries;
990e9dc3 356 struct user_desc ldt_info;
5af72502 357 struct desc_struct ldt;
1da177e4 358 int error;
1da177e4
LT
359
360 error = -EINVAL;
1da177e4
LT
361 if (bytecount != sizeof(ldt_info))
362 goto out;
78aa1f66 363 error = -EFAULT;
70f5088d 364 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
1da177e4
LT
365 goto out;
366
367 error = -EINVAL;
368 if (ldt_info.entry_number >= LDT_ENTRIES)
369 goto out;
370 if (ldt_info.contents == 3) {
371 if (oldmode)
372 goto out;
373 if (ldt_info.seg_not_present == 0)
374 goto out;
375 }
376
37868fe1
AL
377 if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
378 LDT_empty(&ldt_info)) {
379 /* The user wants to clear the entry. */
380 memset(&ldt, 0, sizeof(ldt));
381 } else {
382 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
383 error = -EINVAL;
384 goto out;
1da177e4 385 }
37868fe1
AL
386
387 fill_ldt(&ldt, &ldt_info);
388 if (oldmode)
389 ldt.avl = 0;
1da177e4
LT
390 }
391
bf7ee649
PZ
392 if (down_write_killable(&mm->context.ldt_usr_sem))
393 return -EINTR;
37868fe1 394
bbf79d21
BP
395 old_ldt = mm->context.ldt;
396 old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
397 new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
37868fe1
AL
398
399 error = -ENOMEM;
bbf79d21 400 new_ldt = alloc_ldt_struct(new_nr_entries);
37868fe1 401 if (!new_ldt)
34273f41 402 goto out_unlock;
34273f41 403
37868fe1 404 if (old_ldt)
bbf79d21
BP
405 memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
406
37868fe1
AL
407 new_ldt->entries[ldt_info.entry_number] = ldt;
408 finalize_ldt_struct(new_ldt);
1da177e4 409
c2506438
AL
410 /*
411 * If we are using PTI, map the new LDT into the userspace pagetables.
412 * If there is already an LDT, use the other slot so that other CPUs
413 * will continue to use the old LDT until install_ldt() switches
414 * them over to the new LDT.
415 */
416 error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
417 if (error) {
418 free_ldt_struct(old_ldt);
419 goto out_unlock;
420 }
421
37868fe1
AL
422 install_ldt(mm, new_ldt);
423 free_ldt_struct(old_ldt);
1da177e4
LT
424 error = 0;
425
426out_unlock:
bf7ee649 427 up_write(&mm->context.ldt_usr_sem);
1da177e4
LT
428out:
429 return error;
430}
431
d865f635
DH
432SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
433 unsigned long , bytecount)
1da177e4
LT
434{
435 int ret = -ENOSYS;
436
437 switch (func) {
438 case 0:
439 ret = read_ldt(ptr, bytecount);
440 break;
441 case 1:
442 ret = write_ldt(ptr, bytecount, 1);
443 break;
444 case 2:
445 ret = read_default_ldt(ptr, bytecount);
446 break;
447 case 0x11:
448 ret = write_ldt(ptr, bytecount, 0);
449 break;
450 }
d865f635
DH
451 /*
452 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
453 * return type, but tht ABI for sys_modify_ldt() expects
454 * 'int'. This cast gives us an int-sized value in %rax
455 * for the return code. The 'unsigned' is necessary so
456 * the compiler does not try to sign-extend the negative
457 * return codes into the high half of the register when
458 * taking the value from int->long.
459 */
460 return (unsigned int)ret;
1da177e4 461}