Linux Audio

Check our new training course

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