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>
7 #include <asm/uaccess.h>
9 #include <asm/system.h>
11 #include <asm/processor.h>
12 #include <asm/proto.h>
13 #include <asm/syscalls.h>
18 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
20 static int get_free_idx(void)
22 struct thread_struct *t = ¤t->thread;
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;
31 static bool tls_desc_okay(const struct user_desc *info)
37 * espfix is required for 16-bit data segments, but espfix
38 * only works for LDT segments.
43 /* Only allow data segments in the TLS array. */
44 if (info->contents > 1)
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.
53 * If userspace needs to remove a TLS entry, it can still delete
56 if (info->seg_not_present)
62 static void set_tls_desc(struct task_struct *p, int idx,
63 const struct user_desc *info, int n)
65 struct thread_struct *t = &p->thread;
66 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
70 * We must not get preempted while modifying the TLS.
76 desc->a = desc->b = 0;
83 if (t == ¤t->thread)
90 * Set a given TLS descriptor:
92 int do_set_thread_area(struct task_struct *p, int idx,
93 struct user_desc __user *u_info,
96 struct user_desc info;
98 if (copy_from_user(&info, u_info, sizeof(info)))
101 if (!tls_desc_okay(&info))
105 idx = info.entry_number;
108 * index -1 means the kernel should try to find and
109 * allocate an empty descriptor:
111 if (idx == -1 && can_allocate) {
112 idx = get_free_idx();
115 if (put_user(idx, &u_info->entry_number))
119 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
122 set_tls_desc(p, idx, &info, 1);
127 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
129 int ret = do_set_thread_area(current, -1, u_info, 1);
130 asmlinkage_protect(1, ret, u_info);
136 * Get the current Thread-Local Storage area:
139 static void fill_user_desc(struct user_desc *info, int idx,
140 const struct desc_struct *desc)
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;
158 int do_get_thread_area(struct task_struct *p, int idx,
159 struct user_desc __user *u_info)
161 struct user_desc info;
163 if (idx == -1 && get_user(idx, &u_info->entry_number))
166 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
169 fill_user_desc(&info, idx,
170 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
172 if (copy_to_user(u_info, &info, sizeof(info)))
177 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
179 int ret = do_get_thread_area(current, -1, u_info);
180 asmlinkage_protect(1, ret, u_info);
184 int regset_tls_active(struct task_struct *target,
185 const struct user_regset *regset)
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]))
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)
198 const struct desc_struct *tls;
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)
205 pos /= sizeof(struct user_desc);
206 count /= sizeof(struct user_desc);
208 tls = &target->thread.tls_array[pos];
211 struct user_desc *info = kbuf;
213 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
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)))
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)
232 struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
233 const struct user_desc *info;
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)
243 else if (__copy_from_user(infobuf, ubuf, count))
248 for (i = 0; i < count / sizeof(struct user_desc); i++)
249 if (!tls_desc_okay(info + i))
253 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
254 info, count / sizeof(struct user_desc));