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)
60 /* The L bit makes no sense for data. */
68 static void set_tls_desc(struct task_struct *p, int idx,
69 const struct user_desc *info, int n)
71 struct thread_struct *t = &p->thread;
72 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
76 * We must not get preempted while modifying the TLS.
82 desc->a = desc->b = 0;
89 if (t == ¤t->thread)
96 * Set a given TLS descriptor:
98 int do_set_thread_area(struct task_struct *p, int idx,
99 struct user_desc __user *u_info,
102 struct user_desc info;
104 if (copy_from_user(&info, u_info, sizeof(info)))
107 if (!tls_desc_okay(&info))
111 idx = info.entry_number;
114 * index -1 means the kernel should try to find and
115 * allocate an empty descriptor:
117 if (idx == -1 && can_allocate) {
118 idx = get_free_idx();
121 if (put_user(idx, &u_info->entry_number))
125 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
128 set_tls_desc(p, idx, &info, 1);
133 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
135 int ret = do_set_thread_area(current, -1, u_info, 1);
136 asmlinkage_protect(1, ret, u_info);
142 * Get the current Thread-Local Storage area:
145 static void fill_user_desc(struct user_desc *info, int idx,
146 const struct desc_struct *desc)
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;
164 int do_get_thread_area(struct task_struct *p, int idx,
165 struct user_desc __user *u_info)
167 struct user_desc info;
169 if (idx == -1 && get_user(idx, &u_info->entry_number))
172 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
175 fill_user_desc(&info, idx,
176 &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
178 if (copy_to_user(u_info, &info, sizeof(info)))
183 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
185 int ret = do_get_thread_area(current, -1, u_info);
186 asmlinkage_protect(1, ret, u_info);
190 int regset_tls_active(struct task_struct *target,
191 const struct user_regset *regset)
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]))
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)
204 const struct desc_struct *tls;
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)
211 pos /= sizeof(struct user_desc);
212 count /= sizeof(struct user_desc);
214 tls = &target->thread.tls_array[pos];
217 struct user_desc *info = kbuf;
219 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
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)))
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)
238 struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
239 const struct user_desc *info;
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)
249 else if (__copy_from_user(infobuf, ubuf, count))
254 for (i = 0; i < count / sizeof(struct user_desc); i++)
255 if (!tls_desc_okay(info + i))
259 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
260 info, count / sizeof(struct user_desc));