blob: b2463fcb20a8116921203fb246d3b7ffa0ef1e88 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
4 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
5 * Copyright (C) 2002 Andi Kleen
Thomas Gleixner78aa1f62008-01-30 13:30:13 +01006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This handles calls from both 32bit and 64bit mode.
Peter Zijlstrac2b34962017-12-14 12:27:30 +01008 *
9 * Lock order:
10 * contex.ldt_usr_sem
11 * mmap_sem
12 * context.lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sched.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/smp.h>
Dave Hansenda20ab32017-10-18 10:21:07 -070021#include <linux/syscalls.h>
Andy Lutomirski37868fe2015-07-30 14:31:32 -070022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/vmalloc.h>
Jaswinder Singh Rajput423a5402008-12-31 16:42:20 +053024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/ldt.h>
Andy Lutomirskif55f0502017-12-12 07:56:45 -080027#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/desc.h>
Thomas Gleixner70f50882008-01-30 13:30:13 +010029#include <asm/mmu_context.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053030#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Andy Lutomirskia6323752017-07-26 07:16:30 -070032static void refresh_ldt_segments(void)
33{
34#ifdef CONFIG_X86_64
35 unsigned short sel;
36
37 /*
38 * Make sure that the cached DS and ES descriptors match the updated
39 * LDT.
40 */
41 savesegment(ds, sel);
42 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
43 loadsegment(ds, sel);
44
45 savesegment(es, sel);
46 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
47 loadsegment(es, sel);
48#endif
49}
50
Peter Zijlstrac2b34962017-12-14 12:27:30 +010051/* context.lock is held by the task which issued the smp function call */
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070052static void flush_ldt(void *__mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070054 struct mm_struct *mm = __mm;
Andy Lutomirski37868fe2015-07-30 14:31:32 -070055
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070056 if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
Andy Lutomirski37868fe2015-07-30 14:31:32 -070057 return;
58
Andy Lutomirskif55f0502017-12-12 07:56:45 -080059 load_mm_ldt(mm);
Andy Lutomirskia6323752017-07-26 07:16:30 -070060
61 refresh_ldt_segments();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Andy Lutomirski37868fe2015-07-30 14:31:32 -070064/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
Borislav Petkovbbf79d22017-06-06 19:31:16 +020065static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Andy Lutomirski37868fe2015-07-30 14:31:32 -070067 struct ldt_struct *new_ldt;
Thomas Gleixner990e9dc2016-12-10 00:13:51 +010068 unsigned int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Borislav Petkovbbf79d22017-06-06 19:31:16 +020070 if (num_entries > LDT_ENTRIES)
Andy Lutomirski37868fe2015-07-30 14:31:32 -070071 return NULL;
72
73 new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
74 if (!new_ldt)
75 return NULL;
76
77 BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
Borislav Petkovbbf79d22017-06-06 19:31:16 +020078 alloc_size = num_entries * LDT_ENTRY_SIZE;
Andy Lutomirski37868fe2015-07-30 14:31:32 -070079
80 /*
81 * Xen is very picky: it requires a page-aligned LDT that has no
82 * trailing nonzero bytes in any page that contains LDT descriptors.
83 * Keep it simple: zero the whole allocation and never allocate less
84 * than PAGE_SIZE.
85 */
86 if (alloc_size > PAGE_SIZE)
87 new_ldt->entries = vzalloc(alloc_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 else
Jan Beulichf454b472015-09-02 09:45:58 -060089 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Andy Lutomirski37868fe2015-07-30 14:31:32 -070091 if (!new_ldt->entries) {
92 kfree(new_ldt);
93 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Andy Lutomirski37868fe2015-07-30 14:31:32 -070095
Andy Lutomirskif55f0502017-12-12 07:56:45 -080096 /* The new LDT isn't aliased for PTI yet. */
97 new_ldt->slot = -1;
98
Borislav Petkovbbf79d22017-06-06 19:31:16 +020099 new_ldt->nr_entries = num_entries;
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700100 return new_ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Joerg Roedel9bae3192018-07-18 11:41:12 +0200103#ifdef CONFIG_PAGE_TABLE_ISOLATION
104
105static void do_sanity_check(struct mm_struct *mm,
106 bool had_kernel_mapping,
107 bool had_user_mapping)
108{
109 if (mm->context.ldt) {
110 /*
111 * We already had an LDT. The top-level entry should already
112 * have been allocated and synchronized with the usermode
113 * tables.
114 */
115 WARN_ON(!had_kernel_mapping);
Borislav Petkov67e87d42019-03-29 19:52:59 +0100116 if (boot_cpu_has(X86_FEATURE_PTI))
Joerg Roedel9bae3192018-07-18 11:41:12 +0200117 WARN_ON(!had_user_mapping);
118 } else {
119 /*
120 * This is the first time we're mapping an LDT for this process.
121 * Sync the pgd to the usermode tables.
122 */
123 WARN_ON(had_kernel_mapping);
Borislav Petkov67e87d42019-03-29 19:52:59 +0100124 if (boot_cpu_has(X86_FEATURE_PTI))
Joerg Roedel9bae3192018-07-18 11:41:12 +0200125 WARN_ON(had_user_mapping);
126 }
127}
128
Joerg Roedel6df934b2018-07-18 11:41:13 +0200129#ifdef CONFIG_X86_PAE
130
131static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
132{
133 p4d_t *p4d;
134 pud_t *pud;
135
136 if (pgd->pgd == 0)
137 return NULL;
138
139 p4d = p4d_offset(pgd, va);
140 if (p4d_none(*p4d))
141 return NULL;
142
143 pud = pud_offset(p4d, va);
144 if (pud_none(*pud))
145 return NULL;
146
147 return pmd_offset(pud, va);
148}
149
150static void map_ldt_struct_to_user(struct mm_struct *mm)
151{
152 pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
153 pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
154 pmd_t *k_pmd, *u_pmd;
155
156 k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
157 u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
158
Borislav Petkov67e87d42019-03-29 19:52:59 +0100159 if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
Joerg Roedel6df934b2018-07-18 11:41:13 +0200160 set_pmd(u_pmd, *k_pmd);
161}
162
163static void sanity_check_ldt_mapping(struct mm_struct *mm)
164{
165 pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
166 pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
167 bool had_kernel, had_user;
168 pmd_t *k_pmd, *u_pmd;
169
170 k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
171 u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
172 had_kernel = (k_pmd->pmd != 0);
173 had_user = (u_pmd->pmd != 0);
174
175 do_sanity_check(mm, had_kernel, had_user);
176}
177
178#else /* !CONFIG_X86_PAE */
179
Joerg Roedel9bae3192018-07-18 11:41:12 +0200180static void map_ldt_struct_to_user(struct mm_struct *mm)
181{
182 pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
183
Borislav Petkov67e87d42019-03-29 19:52:59 +0100184 if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
Joerg Roedel9bae3192018-07-18 11:41:12 +0200185 set_pgd(kernel_to_user_pgdp(pgd), *pgd);
186}
187
188static void sanity_check_ldt_mapping(struct mm_struct *mm)
189{
190 pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
191 bool had_kernel = (pgd->pgd != 0);
192 bool had_user = (kernel_to_user_pgdp(pgd)->pgd != 0);
193
194 do_sanity_check(mm, had_kernel, had_user);
195}
196
Joerg Roedel6df934b2018-07-18 11:41:13 +0200197#endif /* CONFIG_X86_PAE */
198
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800199/*
200 * If PTI is enabled, this maps the LDT into the kernelmode and
201 * usermode tables for the given mm.
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800202 */
203static int
204map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
205{
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800206 unsigned long va;
Joerg Roedel9bae3192018-07-18 11:41:12 +0200207 bool is_vmalloc;
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800208 spinlock_t *ptl;
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300209 int i, nr_pages;
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800210
Borislav Petkov67e87d42019-03-29 19:52:59 +0100211 if (!boot_cpu_has(X86_FEATURE_PTI))
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800212 return 0;
213
214 /*
215 * Any given ldt_struct should have map_ldt_struct() called at most
216 * once.
217 */
218 WARN_ON(ldt->slot != -1);
219
Joerg Roedel9bae3192018-07-18 11:41:12 +0200220 /* Check if the current mappings are sane */
221 sanity_check_ldt_mapping(mm);
222
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800223 is_vmalloc = is_vmalloc_addr(ldt->entries);
224
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300225 nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
226
227 for (i = 0; i < nr_pages; i++) {
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800228 unsigned long offset = i << PAGE_SHIFT;
229 const void *src = (char *)ldt->entries + offset;
230 unsigned long pfn;
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700231 pgprot_t pte_prot;
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800232 pte_t pte, *ptep;
233
234 va = (unsigned long)ldt_slot_va(slot) + offset;
235 pfn = is_vmalloc ? vmalloc_to_pfn(src) :
236 page_to_pfn(virt_to_page(src));
237 /*
238 * Treat the PTI LDT range as a *userspace* range.
239 * get_locked_pte() will allocate all needed pagetables
240 * and account for them in this mm.
241 */
242 ptep = get_locked_pte(mm, va, &ptl);
243 if (!ptep)
244 return -ENOMEM;
Thomas Gleixner9f5cb6b2017-12-15 20:35:11 +0100245 /*
246 * Map it RO so the easy to find address is not a primary
247 * target via some kernel interface which misses a
248 * permission check.
249 */
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700250 pte_prot = __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL);
251 /* Filter out unsuppored __PAGE_KERNEL* bits: */
Joerg Roedele6f39e82018-04-16 11:43:57 +0200252 pgprot_val(pte_prot) &= __supported_pte_mask;
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700253 pte = pfn_pte(pfn, pte_prot);
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800254 set_pte_at(mm, va, ptep, pte);
255 pte_unmap_unlock(ptep, ptl);
256 }
257
Joerg Roedel9bae3192018-07-18 11:41:12 +0200258 /* Propagate LDT mapping to the user page-table */
259 map_ldt_struct_to_user(mm);
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800260
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800261 ldt->slot = slot;
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800262 return 0;
263}
264
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300265static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
266{
267 unsigned long va;
268 int i, nr_pages;
269
270 if (!ldt)
271 return;
272
273 /* LDT map/unmap is only required for PTI */
Borislav Petkov67e87d42019-03-29 19:52:59 +0100274 if (!boot_cpu_has(X86_FEATURE_PTI))
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300275 return;
276
277 nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
278
279 for (i = 0; i < nr_pages; i++) {
280 unsigned long offset = i << PAGE_SHIFT;
281 spinlock_t *ptl;
282 pte_t *ptep;
283
284 va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
285 ptep = get_locked_pte(mm, va, &ptl);
286 pte_clear(mm, va, ptep);
287 pte_unmap_unlock(ptep, ptl);
288 }
289
290 va = (unsigned long)ldt_slot_va(ldt->slot);
291 flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, PAGE_SHIFT, false);
292}
293
Joerg Roedel9bae3192018-07-18 11:41:12 +0200294#else /* !CONFIG_PAGE_TABLE_ISOLATION */
295
296static int
297map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
298{
299 return 0;
300}
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300301
302static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
303{
304}
Joerg Roedel9bae3192018-07-18 11:41:12 +0200305#endif /* CONFIG_PAGE_TABLE_ISOLATION */
306
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800307static void free_ldt_pgtables(struct mm_struct *mm)
308{
309#ifdef CONFIG_PAGE_TABLE_ISOLATION
310 struct mmu_gather tlb;
311 unsigned long start = LDT_BASE_ADDR;
Joerg Roedel8195d862018-07-18 11:41:11 +0200312 unsigned long end = LDT_END_ADDR;
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800313
Borislav Petkov67e87d42019-03-29 19:52:59 +0100314 if (!boot_cpu_has(X86_FEATURE_PTI))
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800315 return;
316
317 tlb_gather_mmu(&tlb, mm, start, end);
318 free_pgd_range(&tlb, start, end, start, end);
319 tlb_finish_mmu(&tlb, start, end);
320#endif
321}
322
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700323/* After calling this, the LDT is immutable. */
324static void finalize_ldt_struct(struct ldt_struct *ldt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200326 paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700327}
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100328
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100329static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700330{
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100331 mutex_lock(&mm->context.lock);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700332
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100333 /* Synchronizes with READ_ONCE in load_mm_ldt. */
334 smp_store_release(&mm->context.ldt, ldt);
335
336 /* Activate the LDT for all CPUs using currents mm. */
337 on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
338
339 mutex_unlock(&mm->context.lock);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700340}
341
342static void free_ldt_struct(struct ldt_struct *ldt)
343{
344 if (likely(!ldt))
345 return;
346
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200347 paravirt_free_ldt(ldt->entries, ldt->nr_entries);
348 if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
Andrey Ryabinin8d5341a2016-12-12 16:44:17 -0800349 vfree_atomic(ldt->entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700350 else
Jan Beulichf454b472015-09-02 09:45:58 -0600351 free_page((unsigned long)ldt->entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700352 kfree(ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
Thomas Gleixnera4828f82017-12-14 12:27:31 +0100356 * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
357 * the new task is not running, so nothing can be installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Thomas Gleixnera4828f82017-12-14 12:27:31 +0100359int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700361 struct ldt_struct *new_ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int retval = 0;
363
Thomas Gleixnera4828f82017-12-14 12:27:31 +0100364 if (!old_mm)
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700365 return 0;
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700366
367 mutex_lock(&old_mm->context.lock);
Thomas Gleixnera4828f82017-12-14 12:27:31 +0100368 if (!old_mm->context.ldt)
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700369 goto out_unlock;
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700370
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200371 new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700372 if (!new_ldt) {
373 retval = -ENOMEM;
374 goto out_unlock;
375 }
376
377 memcpy(new_ldt->entries, old_mm->context.ldt->entries,
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200378 new_ldt->nr_entries * LDT_ENTRY_SIZE);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700379 finalize_ldt_struct(new_ldt);
380
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800381 retval = map_ldt_struct(mm, new_ldt, 0);
382 if (retval) {
383 free_ldt_pgtables(mm);
384 free_ldt_struct(new_ldt);
385 goto out_unlock;
386 }
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700387 mm->context.ldt = new_ldt;
388
389out_unlock:
390 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return retval;
392}
393
394/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100395 * No need to lock the MM as we are the last user
396 *
397 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Dave Hansen39a05262016-02-12 13:02:34 -0800399void destroy_context_ldt(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700401 free_ldt_struct(mm->context.ldt);
402 mm->context.ldt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800405void ldt_arch_exit_mmap(struct mm_struct *mm)
406{
407 free_ldt_pgtables(mm);
408}
409
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100410static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100412 struct mm_struct *mm = current->mm;
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200413 unsigned long entries_size;
414 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100416 down_read(&mm->context.ldt_usr_sem);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700417
418 if (!mm->context.ldt) {
419 retval = 0;
420 goto out_unlock;
421 }
422
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100423 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
424 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200426 entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
427 if (entries_size > bytecount)
428 entries_size = bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200430 if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700431 retval = -EFAULT;
432 goto out_unlock;
433 }
434
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200435 if (entries_size != bytecount) {
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700436 /* Zero-fill the rest and pretend we read bytecount bytes. */
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200437 if (clear_user(ptr + entries_size, bytecount - entries_size)) {
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700438 retval = -EFAULT;
439 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 }
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700442 retval = bytecount;
443
444out_unlock:
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100445 up_read(&mm->context.ldt_usr_sem);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700446 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100449static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100451 /* CHECKME: Can we use _one_ random number ? */
452#ifdef CONFIG_X86_32
453 unsigned long size = 5 * sizeof(struct desc_struct);
454#else
455 unsigned long size = 128;
456#endif
457 if (bytecount > size)
458 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (clear_user(ptr, bytecount))
460 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100461 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100464static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100466 struct mm_struct *mm = current->mm;
Thomas Gleixner990e9dc2016-12-10 00:13:51 +0100467 struct ldt_struct *new_ldt, *old_ldt;
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200468 unsigned int old_nr_entries, new_nr_entries;
Thomas Gleixner990e9dc2016-12-10 00:13:51 +0100469 struct user_desc ldt_info;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100470 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (bytecount != sizeof(ldt_info))
475 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100476 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100477 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out;
479
480 error = -EINVAL;
481 if (ldt_info.entry_number >= LDT_ENTRIES)
482 goto out;
483 if (ldt_info.contents == 3) {
484 if (oldmode)
485 goto out;
486 if (ldt_info.seg_not_present == 0)
487 goto out;
488 }
489
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700490 if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
491 LDT_empty(&ldt_info)) {
492 /* The user wants to clear the entry. */
493 memset(&ldt, 0, sizeof(ldt));
494 } else {
495 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
496 error = -EINVAL;
497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700499
500 fill_ldt(&ldt, &ldt_info);
501 if (oldmode)
502 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100505 if (down_write_killable(&mm->context.ldt_usr_sem))
506 return -EINTR;
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700507
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200508 old_ldt = mm->context.ldt;
509 old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
510 new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700511
512 error = -ENOMEM;
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200513 new_ldt = alloc_ldt_struct(new_nr_entries);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700514 if (!new_ldt)
H. Peter Anvin34273f42014-05-04 10:36:22 -0700515 goto out_unlock;
H. Peter Anvin34273f42014-05-04 10:36:22 -0700516
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700517 if (old_ldt)
Borislav Petkovbbf79d22017-06-06 19:31:16 +0200518 memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
519
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700520 new_ldt->entries[ldt_info.entry_number] = ldt;
521 finalize_ldt_struct(new_ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800523 /*
524 * If we are using PTI, map the new LDT into the userspace pagetables.
525 * If there is already an LDT, use the other slot so that other CPUs
526 * will continue to use the old LDT until install_ldt() switches
527 * them over to the new LDT.
528 */
529 error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
530 if (error) {
Thomas Gleixnera62d6982017-12-31 11:24:34 +0100531 /*
532 * This only can fail for the first LDT setup. If an LDT is
533 * already installed then the PTE page is already
534 * populated. Mop up a half populated page table.
535 */
Thomas Gleixner7f414192017-12-31 16:52:15 +0100536 if (!WARN_ON_ONCE(old_ldt))
537 free_ldt_pgtables(mm);
Thomas Gleixnera62d6982017-12-31 11:24:34 +0100538 free_ldt_struct(new_ldt);
Andy Lutomirskif55f0502017-12-12 07:56:45 -0800539 goto out_unlock;
540 }
541
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700542 install_ldt(mm, new_ldt);
Kirill A. Shutemova0e6e082018-10-26 15:28:55 +0300543 unmap_ldt_struct(mm, old_ldt);
Andy Lutomirski37868fe2015-07-30 14:31:32 -0700544 free_ldt_struct(old_ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 error = 0;
546
547out_unlock:
Peter Zijlstrac2b34962017-12-14 12:27:30 +0100548 up_write(&mm->context.ldt_usr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549out:
550 return error;
551}
552
Dave Hansenda20ab32017-10-18 10:21:07 -0700553SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
554 unsigned long , bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 int ret = -ENOSYS;
557
558 switch (func) {
559 case 0:
560 ret = read_ldt(ptr, bytecount);
561 break;
562 case 1:
563 ret = write_ldt(ptr, bytecount, 1);
564 break;
565 case 2:
566 ret = read_default_ldt(ptr, bytecount);
567 break;
568 case 0x11:
569 ret = write_ldt(ptr, bytecount, 0);
570 break;
571 }
Dave Hansenda20ab32017-10-18 10:21:07 -0700572 /*
573 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
574 * return type, but tht ABI for sys_modify_ldt() expects
575 * 'int'. This cast gives us an int-sized value in %rax
576 * for the return code. The 'unsigned' is necessary so
577 * the compiler does not try to sign-extend the negative
578 * return codes into the high half of the register when
579 * taking the value from int->long.
580 */
581 return (unsigned int)ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}