59e07af64654d87e1a46be838508a4a2cee023c3
[pandora-kernel.git] / arch / x86 / kernel / tls.c
1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/sched.h>
4 #include <linux/user.h>
5 #include <linux/regset.h>
6
7 #include <asm/uaccess.h>
8 #include <asm/desc.h>
9 #include <asm/system.h>
10 #include <asm/ldt.h>
11 #include <asm/processor.h>
12 #include <asm/proto.h>
13 #include <asm/syscalls.h>
14
15 #include "tls.h"
16
17 /*
18  * sys_alloc_thread_area: get a yet unused TLS descriptor index.
19  */
20 static int get_free_idx(void)
21 {
22         struct thread_struct *t = &current->thread;
23         int idx;
24
25         for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
26                 if (desc_empty(&t->tls_array[idx]))
27                         return idx + GDT_ENTRY_TLS_MIN;
28         return -ESRCH;
29 }
30
31 static bool tls_desc_okay(const struct user_desc *info)
32 {
33         if (LDT_empty(info))
34                 return true;
35
36         /*
37          * espfix is required for 16-bit data segments, but espfix
38          * only works for LDT segments.
39          */
40         if (!info->seg_32bit)
41                 return false;
42
43         /* Only allow data segments in the TLS array. */
44         if (info->contents > 1)
45                 return false;
46
47         /*
48          * Non-present segments with DPL 3 present an interesting attack
49          * surface.  The kernel should handle such segments correctly,
50          * but TLS is very difficult to protect in a sandbox, so prevent
51          * such segments from being created.
52          *
53          * If userspace needs to remove a TLS entry, it can still delete
54          * it outright.
55          */
56         if (info->seg_not_present)
57                 return false;
58
59         return true;
60 }
61
62 static void set_tls_desc(struct task_struct *p, int idx,
63                          const struct user_desc *info, int n)
64 {
65         struct thread_struct *t = &p->thread;
66         struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
67         int cpu;
68
69         /*
70          * We must not get preempted while modifying the TLS.
71          */
72         cpu = get_cpu();
73
74         while (n-- > 0) {
75                 if (LDT_empty(info))
76                         desc->a = desc->b = 0;
77                 else
78                         fill_ldt(desc, info);
79                 ++info;
80                 ++desc;
81         }
82
83         if (t == &current->thread)
84                 load_TLS(t, cpu);
85
86         put_cpu();
87 }
88
89 /*
90  * Set a given TLS descriptor:
91  */
92 int do_set_thread_area(struct task_struct *p, int idx,
93                        struct user_desc __user *u_info,
94                        int can_allocate)
95 {
96         struct user_desc info;
97
98         if (copy_from_user(&info, u_info, sizeof(info)))
99                 return -EFAULT;
100
101         if (!tls_desc_okay(&info))
102                 return -EINVAL;
103
104         if (idx == -1)
105                 idx = info.entry_number;
106
107         /*
108          * index -1 means the kernel should try to find and
109          * allocate an empty descriptor:
110          */
111         if (idx == -1 && can_allocate) {
112                 idx = get_free_idx();
113                 if (idx < 0)
114                         return idx;
115                 if (put_user(idx, &u_info->entry_number))
116                         return -EFAULT;
117         }
118
119         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
120                 return -EINVAL;
121
122         set_tls_desc(p, idx, &info, 1);
123
124         return 0;
125 }
126
127 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
128 {
129         int ret = do_set_thread_area(current, -1, u_info, 1);
130         asmlinkage_protect(1, ret, u_info);
131         return ret;
132 }
133
134
135 /*
136  * Get the current Thread-Local Storage area:
137  */
138
139 static void fill_user_desc(struct user_desc *info, int idx,
140                            const struct desc_struct *desc)
141
142 {
143         memset(info, 0, sizeof(*info));
144         info->entry_number = idx;
145         info->base_addr = get_desc_base(desc);
146         info->limit = get_desc_limit(desc);
147         info->seg_32bit = desc->d;
148         info->contents = desc->type >> 2;
149         info->read_exec_only = !(desc->type & 2);
150         info->limit_in_pages = desc->g;
151         info->seg_not_present = !desc->p;
152         info->useable = desc->avl;
153 #ifdef CONFIG_X86_64
154         info->lm = desc->l;
155 #endif
156 }
157
158 int do_get_thread_area(struct task_struct *p, int idx,
159                        struct user_desc __user *u_info)
160 {
161         struct user_desc info;
162
163         if (idx == -1 && get_user(idx, &u_info->entry_number))
164                 return -EFAULT;
165
166         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
167                 return -EINVAL;
168
169         fill_user_desc(&info, idx,
170                        &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
171
172         if (copy_to_user(u_info, &info, sizeof(info)))
173                 return -EFAULT;
174         return 0;
175 }
176
177 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
178 {
179         int ret = do_get_thread_area(current, -1, u_info);
180         asmlinkage_protect(1, ret, u_info);
181         return ret;
182 }
183
184 int regset_tls_active(struct task_struct *target,
185                       const struct user_regset *regset)
186 {
187         struct thread_struct *t = &target->thread;
188         int n = GDT_ENTRY_TLS_ENTRIES;
189         while (n > 0 && desc_empty(&t->tls_array[n - 1]))
190                 --n;
191         return n;
192 }
193
194 int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
195                    unsigned int pos, unsigned int count,
196                    void *kbuf, void __user *ubuf)
197 {
198         const struct desc_struct *tls;
199
200         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
201             (pos % sizeof(struct user_desc)) != 0 ||
202             (count % sizeof(struct user_desc)) != 0)
203                 return -EINVAL;
204
205         pos /= sizeof(struct user_desc);
206         count /= sizeof(struct user_desc);
207
208         tls = &target->thread.tls_array[pos];
209
210         if (kbuf) {
211                 struct user_desc *info = kbuf;
212                 while (count-- > 0)
213                         fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
214                                        tls++);
215         } else {
216                 struct user_desc __user *u_info = ubuf;
217                 while (count-- > 0) {
218                         struct user_desc info;
219                         fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
220                         if (__copy_to_user(u_info++, &info, sizeof(info)))
221                                 return -EFAULT;
222                 }
223         }
224
225         return 0;
226 }
227
228 int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
229                    unsigned int pos, unsigned int count,
230                    const void *kbuf, const void __user *ubuf)
231 {
232         struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
233         const struct user_desc *info;
234         int i;
235
236         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
237             (pos % sizeof(struct user_desc)) != 0 ||
238             (count % sizeof(struct user_desc)) != 0)
239                 return -EINVAL;
240
241         if (kbuf)
242                 info = kbuf;
243         else if (__copy_from_user(infobuf, ubuf, count))
244                 return -EFAULT;
245         else
246                 info = infobuf;
247
248         for (i = 0; i < count / sizeof(struct user_desc); i++)
249                 if (!tls_desc_okay(info + i))
250                         return -EINVAL;
251
252         set_tls_desc(target,
253                      GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
254                      info, count / sizeof(struct user_desc));
255
256         return 0;
257 }