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