x86/tls: Disallow unusual TLS segments
[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 #ifdef CONFIG_X86_64
60         /* The L bit makes no sense for data. */
61         if (info->lm)
62                 return false;
63 #endif
64
65         return true;
66 }
67
68 static void set_tls_desc(struct task_struct *p, int idx,
69                          const struct user_desc *info, int n)
70 {
71         struct thread_struct *t = &p->thread;
72         struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
73         int cpu;
74
75         /*
76          * We must not get preempted while modifying the TLS.
77          */
78         cpu = get_cpu();
79
80         while (n-- > 0) {
81                 if (LDT_empty(info))
82                         desc->a = desc->b = 0;
83                 else
84                         fill_ldt(desc, info);
85                 ++info;
86                 ++desc;
87         }
88
89         if (t == &current->thread)
90                 load_TLS(t, cpu);
91
92         put_cpu();
93 }
94
95 /*
96  * Set a given TLS descriptor:
97  */
98 int do_set_thread_area(struct task_struct *p, int idx,
99                        struct user_desc __user *u_info,
100                        int can_allocate)
101 {
102         struct user_desc info;
103
104         if (copy_from_user(&info, u_info, sizeof(info)))
105                 return -EFAULT;
106
107         if (!tls_desc_okay(&info))
108                 return -EINVAL;
109
110         if (idx == -1)
111                 idx = info.entry_number;
112
113         /*
114          * index -1 means the kernel should try to find and
115          * allocate an empty descriptor:
116          */
117         if (idx == -1 && can_allocate) {
118                 idx = get_free_idx();
119                 if (idx < 0)
120                         return idx;
121                 if (put_user(idx, &u_info->entry_number))
122                         return -EFAULT;
123         }
124
125         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
126                 return -EINVAL;
127
128         set_tls_desc(p, idx, &info, 1);
129
130         return 0;
131 }
132
133 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
134 {
135         int ret = do_set_thread_area(current, -1, u_info, 1);
136         asmlinkage_protect(1, ret, u_info);
137         return ret;
138 }
139
140
141 /*
142  * Get the current Thread-Local Storage area:
143  */
144
145 static void fill_user_desc(struct user_desc *info, int idx,
146                            const struct desc_struct *desc)
147
148 {
149         memset(info, 0, sizeof(*info));
150         info->entry_number = idx;
151         info->base_addr = get_desc_base(desc);
152         info->limit = get_desc_limit(desc);
153         info->seg_32bit = desc->d;
154         info->contents = desc->type >> 2;
155         info->read_exec_only = !(desc->type & 2);
156         info->limit_in_pages = desc->g;
157         info->seg_not_present = !desc->p;
158         info->useable = desc->avl;
159 #ifdef CONFIG_X86_64
160         info->lm = desc->l;
161 #endif
162 }
163
164 int do_get_thread_area(struct task_struct *p, int idx,
165                        struct user_desc __user *u_info)
166 {
167         struct user_desc info;
168
169         if (idx == -1 && get_user(idx, &u_info->entry_number))
170                 return -EFAULT;
171
172         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
173                 return -EINVAL;
174
175         fill_user_desc(&info, idx,
176                        &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
177
178         if (copy_to_user(u_info, &info, sizeof(info)))
179                 return -EFAULT;
180         return 0;
181 }
182
183 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
184 {
185         int ret = do_get_thread_area(current, -1, u_info);
186         asmlinkage_protect(1, ret, u_info);
187         return ret;
188 }
189
190 int regset_tls_active(struct task_struct *target,
191                       const struct user_regset *regset)
192 {
193         struct thread_struct *t = &target->thread;
194         int n = GDT_ENTRY_TLS_ENTRIES;
195         while (n > 0 && desc_empty(&t->tls_array[n - 1]))
196                 --n;
197         return n;
198 }
199
200 int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
201                    unsigned int pos, unsigned int count,
202                    void *kbuf, void __user *ubuf)
203 {
204         const struct desc_struct *tls;
205
206         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
207             (pos % sizeof(struct user_desc)) != 0 ||
208             (count % sizeof(struct user_desc)) != 0)
209                 return -EINVAL;
210
211         pos /= sizeof(struct user_desc);
212         count /= sizeof(struct user_desc);
213
214         tls = &target->thread.tls_array[pos];
215
216         if (kbuf) {
217                 struct user_desc *info = kbuf;
218                 while (count-- > 0)
219                         fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
220                                        tls++);
221         } else {
222                 struct user_desc __user *u_info = ubuf;
223                 while (count-- > 0) {
224                         struct user_desc info;
225                         fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
226                         if (__copy_to_user(u_info++, &info, sizeof(info)))
227                                 return -EFAULT;
228                 }
229         }
230
231         return 0;
232 }
233
234 int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
235                    unsigned int pos, unsigned int count,
236                    const void *kbuf, const void __user *ubuf)
237 {
238         struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
239         const struct user_desc *info;
240         int i;
241
242         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
243             (pos % sizeof(struct user_desc)) != 0 ||
244             (count % sizeof(struct user_desc)) != 0)
245                 return -EINVAL;
246
247         if (kbuf)
248                 info = kbuf;
249         else if (__copy_from_user(infobuf, ubuf, count))
250                 return -EFAULT;
251         else
252                 info = infobuf;
253
254         for (i = 0; i < count / sizeof(struct user_desc); i++)
255                 if (!tls_desc_okay(info + i))
256                         return -EINVAL;
257
258         set_tls_desc(target,
259                      GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
260                      info, count / sizeof(struct user_desc));
261
262         return 0;
263 }