GNU Linux-libre 4.9.292-gnu1
[releases.git] / arch / x86 / kernel / ldt.c
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/syscalls.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
19 #include <linux/kaiser.h>
20
21 #include <asm/ldt.h>
22 #include <asm/desc.h>
23 #include <asm/mmu_context.h>
24 #include <asm/syscalls.h>
25
26 /* context.lock is held for us, so we don't need any locking. */
27 static void flush_ldt(void *current_mm)
28 {
29         mm_context_t *pc;
30
31         if (current->active_mm != current_mm)
32                 return;
33
34         pc = &current->active_mm->context;
35         set_ldt(pc->ldt->entries, pc->ldt->size);
36 }
37
38 static void __free_ldt_struct(struct ldt_struct *ldt)
39 {
40         if (ldt->size * LDT_ENTRY_SIZE > PAGE_SIZE)
41                 vfree(ldt->entries);
42         else
43                 free_page((unsigned long)ldt->entries);
44         kfree(ldt);
45 }
46
47 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
48 static struct ldt_struct *alloc_ldt_struct(int size)
49 {
50         struct ldt_struct *new_ldt;
51         int alloc_size;
52         int ret;
53
54         if (size > LDT_ENTRIES)
55                 return NULL;
56
57         new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
58         if (!new_ldt)
59                 return NULL;
60
61         BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
62         alloc_size = size * LDT_ENTRY_SIZE;
63
64         /*
65          * Xen is very picky: it requires a page-aligned LDT that has no
66          * trailing nonzero bytes in any page that contains LDT descriptors.
67          * Keep it simple: zero the whole allocation and never allocate less
68          * than PAGE_SIZE.
69          */
70         if (alloc_size > PAGE_SIZE)
71                 new_ldt->entries = vzalloc(alloc_size);
72         else
73                 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
74
75         if (!new_ldt->entries) {
76                 kfree(new_ldt);
77                 return NULL;
78         }
79
80         ret = kaiser_add_mapping((unsigned long)new_ldt->entries, alloc_size,
81                                  __PAGE_KERNEL);
82         new_ldt->size = size;
83         if (ret) {
84                 __free_ldt_struct(new_ldt);
85                 return NULL;
86         }
87         return new_ldt;
88 }
89
90 /* After calling this, the LDT is immutable. */
91 static void finalize_ldt_struct(struct ldt_struct *ldt)
92 {
93         paravirt_alloc_ldt(ldt->entries, ldt->size);
94 }
95
96 /* context.lock is held */
97 static void install_ldt(struct mm_struct *current_mm,
98                         struct ldt_struct *ldt)
99 {
100         /* Synchronizes with lockless_dereference in load_mm_ldt. */
101         smp_store_release(&current_mm->context.ldt, ldt);
102
103         /* Activate the LDT for all CPUs using current_mm. */
104         on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
105 }
106
107 static void free_ldt_struct(struct ldt_struct *ldt)
108 {
109         if (likely(!ldt))
110                 return;
111
112         kaiser_remove_mapping((unsigned long)ldt->entries,
113                               ldt->size * LDT_ENTRY_SIZE);
114         paravirt_free_ldt(ldt->entries, ldt->size);
115         __free_ldt_struct(ldt);
116 }
117
118 /*
119  * we do not have to muck with descriptors here, that is
120  * done in switch_mm() as needed.
121  */
122 int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
123 {
124         struct ldt_struct *new_ldt;
125         struct mm_struct *old_mm;
126         int retval = 0;
127
128         mutex_init(&mm->context.lock);
129         old_mm = current->mm;
130         if (!old_mm) {
131                 mm->context.ldt = NULL;
132                 return 0;
133         }
134
135         mutex_lock(&old_mm->context.lock);
136         if (!old_mm->context.ldt) {
137                 mm->context.ldt = NULL;
138                 goto out_unlock;
139         }
140
141         new_ldt = alloc_ldt_struct(old_mm->context.ldt->size);
142         if (!new_ldt) {
143                 retval = -ENOMEM;
144                 goto out_unlock;
145         }
146
147         memcpy(new_ldt->entries, old_mm->context.ldt->entries,
148                new_ldt->size * LDT_ENTRY_SIZE);
149         finalize_ldt_struct(new_ldt);
150
151         mm->context.ldt = new_ldt;
152
153 out_unlock:
154         mutex_unlock(&old_mm->context.lock);
155         return retval;
156 }
157
158 /*
159  * No need to lock the MM as we are the last user
160  *
161  * 64bit: Don't touch the LDT register - we're already in the next thread.
162  */
163 void destroy_context_ldt(struct mm_struct *mm)
164 {
165         free_ldt_struct(mm->context.ldt);
166         mm->context.ldt = NULL;
167 }
168
169 static int read_ldt(void __user *ptr, unsigned long bytecount)
170 {
171         int retval;
172         unsigned long size;
173         struct mm_struct *mm = current->mm;
174
175         mutex_lock(&mm->context.lock);
176
177         if (!mm->context.ldt) {
178                 retval = 0;
179                 goto out_unlock;
180         }
181
182         if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
183                 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
184
185         size = mm->context.ldt->size * LDT_ENTRY_SIZE;
186         if (size > bytecount)
187                 size = bytecount;
188
189         if (copy_to_user(ptr, mm->context.ldt->entries, size)) {
190                 retval = -EFAULT;
191                 goto out_unlock;
192         }
193
194         if (size != bytecount) {
195                 /* Zero-fill the rest and pretend we read bytecount bytes. */
196                 if (clear_user(ptr + size, bytecount - size)) {
197                         retval = -EFAULT;
198                         goto out_unlock;
199                 }
200         }
201         retval = bytecount;
202
203 out_unlock:
204         mutex_unlock(&mm->context.lock);
205         return retval;
206 }
207
208 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
209 {
210         /* CHECKME: Can we use _one_ random number ? */
211 #ifdef CONFIG_X86_32
212         unsigned long size = 5 * sizeof(struct desc_struct);
213 #else
214         unsigned long size = 128;
215 #endif
216         if (bytecount > size)
217                 bytecount = size;
218         if (clear_user(ptr, bytecount))
219                 return -EFAULT;
220         return bytecount;
221 }
222
223 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
224 {
225         struct mm_struct *mm = current->mm;
226         struct desc_struct ldt;
227         int error;
228         struct user_desc ldt_info;
229         int oldsize, newsize;
230         struct ldt_struct *new_ldt, *old_ldt;
231
232         error = -EINVAL;
233         if (bytecount != sizeof(ldt_info))
234                 goto out;
235         error = -EFAULT;
236         if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
237                 goto out;
238
239         error = -EINVAL;
240         if (ldt_info.entry_number >= LDT_ENTRIES)
241                 goto out;
242         if (ldt_info.contents == 3) {
243                 if (oldmode)
244                         goto out;
245                 if (ldt_info.seg_not_present == 0)
246                         goto out;
247         }
248
249         if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
250             LDT_empty(&ldt_info)) {
251                 /* The user wants to clear the entry. */
252                 memset(&ldt, 0, sizeof(ldt));
253         } else {
254                 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
255                         error = -EINVAL;
256                         goto out;
257                 }
258
259                 fill_ldt(&ldt, &ldt_info);
260                 if (oldmode)
261                         ldt.avl = 0;
262         }
263
264         mutex_lock(&mm->context.lock);
265
266         old_ldt = mm->context.ldt;
267         oldsize = old_ldt ? old_ldt->size : 0;
268         newsize = max((int)(ldt_info.entry_number + 1), oldsize);
269
270         error = -ENOMEM;
271         new_ldt = alloc_ldt_struct(newsize);
272         if (!new_ldt)
273                 goto out_unlock;
274
275         if (old_ldt)
276                 memcpy(new_ldt->entries, old_ldt->entries, oldsize * LDT_ENTRY_SIZE);
277         new_ldt->entries[ldt_info.entry_number] = ldt;
278         finalize_ldt_struct(new_ldt);
279
280         install_ldt(mm, new_ldt);
281         free_ldt_struct(old_ldt);
282         error = 0;
283
284 out_unlock:
285         mutex_unlock(&mm->context.lock);
286 out:
287         return error;
288 }
289
290 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
291                 unsigned long , bytecount)
292 {
293         int ret = -ENOSYS;
294
295         switch (func) {
296         case 0:
297                 ret = read_ldt(ptr, bytecount);
298                 break;
299         case 1:
300                 ret = write_ldt(ptr, bytecount, 1);
301                 break;
302         case 2:
303                 ret = read_default_ldt(ptr, bytecount);
304                 break;
305         case 0x11:
306                 ret = write_ldt(ptr, bytecount, 0);
307                 break;
308         }
309         /*
310          * The SYSCALL_DEFINE() macros give us an 'unsigned long'
311          * return type, but tht ABI for sys_modify_ldt() expects
312          * 'int'.  This cast gives us an int-sized value in %rax
313          * for the return code.  The 'unsigned' is necessary so
314          * the compiler does not try to sign-extend the negative
315          * return codes into the high half of the register when
316          * taking the value from int->long.
317          */
318         return (unsigned int)ret;
319 }