xref: /openbmc/linux/arch/x86/kernel/tls.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
213abd0e5SRoland McGrath #include <linux/kernel.h>
313abd0e5SRoland McGrath #include <linux/errno.h>
413abd0e5SRoland McGrath #include <linux/sched.h>
513abd0e5SRoland McGrath #include <linux/user.h>
64c79a2d8SRoland McGrath #include <linux/regset.h>
72cf09666SAl Viro #include <linux/syscalls.h>
8993773d1SDianzhang Chen #include <linux/nospec.h>
913abd0e5SRoland McGrath 
107c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
1113abd0e5SRoland McGrath #include <asm/desc.h>
1213abd0e5SRoland McGrath #include <asm/ldt.h>
1313abd0e5SRoland McGrath #include <asm/processor.h>
1413abd0e5SRoland McGrath #include <asm/proto.h>
15*ae53fa18SH. Peter Anvin (Intel) #include <asm/gsseg.h>
1613abd0e5SRoland McGrath 
174c79a2d8SRoland McGrath #include "tls.h"
184c79a2d8SRoland McGrath 
1913abd0e5SRoland McGrath /*
2013abd0e5SRoland McGrath  * sys_alloc_thread_area: get a yet unused TLS descriptor index.
2113abd0e5SRoland McGrath  */
get_free_idx(void)2213abd0e5SRoland McGrath static int get_free_idx(void)
2313abd0e5SRoland McGrath {
2413abd0e5SRoland McGrath 	struct thread_struct *t = &current->thread;
2513abd0e5SRoland McGrath 	int idx;
2613abd0e5SRoland McGrath 
2713abd0e5SRoland McGrath 	for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
28efd1ca52SRoland McGrath 		if (desc_empty(&t->tls_array[idx]))
2913abd0e5SRoland McGrath 			return idx + GDT_ENTRY_TLS_MIN;
3013abd0e5SRoland McGrath 	return -ESRCH;
3113abd0e5SRoland McGrath }
3213abd0e5SRoland McGrath 
tls_desc_okay(const struct user_desc * info)3341bdc785SAndy Lutomirski static bool tls_desc_okay(const struct user_desc *info)
3441bdc785SAndy Lutomirski {
353669ef9fSAndy Lutomirski 	/*
363669ef9fSAndy Lutomirski 	 * For historical reasons (i.e. no one ever documented how any
373669ef9fSAndy Lutomirski 	 * of the segmentation APIs work), user programs can and do
383669ef9fSAndy Lutomirski 	 * assume that a struct user_desc that's all zeros except for
393669ef9fSAndy Lutomirski 	 * entry_number means "no segment at all".  This never actually
403669ef9fSAndy Lutomirski 	 * worked.  In fact, up to Linux 3.19, a struct user_desc like
413669ef9fSAndy Lutomirski 	 * this would create a 16-bit read-write segment with base and
423669ef9fSAndy Lutomirski 	 * limit both equal to zero.
433669ef9fSAndy Lutomirski 	 *
443669ef9fSAndy Lutomirski 	 * That was close enough to "no segment at all" until we
453669ef9fSAndy Lutomirski 	 * hardened this function to disallow 16-bit TLS segments.  Fix
463669ef9fSAndy Lutomirski 	 * it up by interpreting these zeroed segments the way that they
473669ef9fSAndy Lutomirski 	 * were almost certainly intended to be interpreted.
483669ef9fSAndy Lutomirski 	 *
493669ef9fSAndy Lutomirski 	 * The correct way to ask for "no segment at all" is to specify
503669ef9fSAndy Lutomirski 	 * a user_desc that satisfies LDT_empty.  To keep everything
513669ef9fSAndy Lutomirski 	 * working, we accept both.
523669ef9fSAndy Lutomirski 	 *
533669ef9fSAndy Lutomirski 	 * Note that there's a similar kludge in modify_ldt -- look at
543669ef9fSAndy Lutomirski 	 * the distinction between modes 1 and 0x11.
553669ef9fSAndy Lutomirski 	 */
563669ef9fSAndy Lutomirski 	if (LDT_empty(info) || LDT_zero(info))
5741bdc785SAndy Lutomirski 		return true;
5841bdc785SAndy Lutomirski 
5941bdc785SAndy Lutomirski 	/*
6041bdc785SAndy Lutomirski 	 * espfix is required for 16-bit data segments, but espfix
6141bdc785SAndy Lutomirski 	 * only works for LDT segments.
6241bdc785SAndy Lutomirski 	 */
6341bdc785SAndy Lutomirski 	if (!info->seg_32bit)
6441bdc785SAndy Lutomirski 		return false;
6541bdc785SAndy Lutomirski 
660e58af4eSAndy Lutomirski 	/* Only allow data segments in the TLS array. */
670e58af4eSAndy Lutomirski 	if (info->contents > 1)
680e58af4eSAndy Lutomirski 		return false;
690e58af4eSAndy Lutomirski 
700e58af4eSAndy Lutomirski 	/*
710e58af4eSAndy Lutomirski 	 * Non-present segments with DPL 3 present an interesting attack
720e58af4eSAndy Lutomirski 	 * surface.  The kernel should handle such segments correctly,
730e58af4eSAndy Lutomirski 	 * but TLS is very difficult to protect in a sandbox, so prevent
740e58af4eSAndy Lutomirski 	 * such segments from being created.
750e58af4eSAndy Lutomirski 	 *
760e58af4eSAndy Lutomirski 	 * If userspace needs to remove a TLS entry, it can still delete
770e58af4eSAndy Lutomirski 	 * it outright.
780e58af4eSAndy Lutomirski 	 */
790e58af4eSAndy Lutomirski 	if (info->seg_not_present)
800e58af4eSAndy Lutomirski 		return false;
810e58af4eSAndy Lutomirski 
8241bdc785SAndy Lutomirski 	return true;
8341bdc785SAndy Lutomirski }
8441bdc785SAndy Lutomirski 
set_tls_desc(struct task_struct * p,int idx,const struct user_desc * info,int n)851bd5718cSRoland McGrath static void set_tls_desc(struct task_struct *p, int idx,
864c79a2d8SRoland McGrath 			 const struct user_desc *info, int n)
871bd5718cSRoland McGrath {
881bd5718cSRoland McGrath 	struct thread_struct *t = &p->thread;
891bd5718cSRoland McGrath 	struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
901bd5718cSRoland McGrath 	int cpu;
911bd5718cSRoland McGrath 
921bd5718cSRoland McGrath 	/*
931bd5718cSRoland McGrath 	 * We must not get preempted while modifying the TLS.
941bd5718cSRoland McGrath 	 */
951bd5718cSRoland McGrath 	cpu = get_cpu();
961bd5718cSRoland McGrath 
974c79a2d8SRoland McGrath 	while (n-- > 0) {
989f5cb6b3SThomas Gleixner 		if (LDT_empty(info) || LDT_zero(info))
999a98e778SThomas Gleixner 			memset(desc, 0, sizeof(*desc));
1009f5cb6b3SThomas Gleixner 		else
1011bd5718cSRoland McGrath 			fill_ldt(desc, info);
1024c79a2d8SRoland McGrath 		++info;
1034c79a2d8SRoland McGrath 		++desc;
1044c79a2d8SRoland McGrath 	}
1051bd5718cSRoland McGrath 
1061bd5718cSRoland McGrath 	if (t == &current->thread)
1071bd5718cSRoland McGrath 		load_TLS(t, cpu);
1081bd5718cSRoland McGrath 
1091bd5718cSRoland McGrath 	put_cpu();
1101bd5718cSRoland McGrath }
1111bd5718cSRoland McGrath 
11213abd0e5SRoland McGrath /*
11313abd0e5SRoland McGrath  * Set a given TLS descriptor:
11413abd0e5SRoland McGrath  */
do_set_thread_area(struct task_struct * p,int idx,struct user_desc __user * u_info,int can_allocate)115efd1ca52SRoland McGrath int do_set_thread_area(struct task_struct *p, int idx,
116efd1ca52SRoland McGrath 		       struct user_desc __user *u_info,
117efd1ca52SRoland McGrath 		       int can_allocate)
11813abd0e5SRoland McGrath {
11913abd0e5SRoland McGrath 	struct user_desc info;
120c9867f86SAndy Lutomirski 	unsigned short __maybe_unused sel, modified_sel;
12113abd0e5SRoland McGrath 
12213abd0e5SRoland McGrath 	if (copy_from_user(&info, u_info, sizeof(info)))
12313abd0e5SRoland McGrath 		return -EFAULT;
12413abd0e5SRoland McGrath 
12541bdc785SAndy Lutomirski 	if (!tls_desc_okay(&info))
12641bdc785SAndy Lutomirski 		return -EINVAL;
12741bdc785SAndy Lutomirski 
128efd1ca52SRoland McGrath 	if (idx == -1)
12913abd0e5SRoland McGrath 		idx = info.entry_number;
13013abd0e5SRoland McGrath 
13113abd0e5SRoland McGrath 	/*
13213abd0e5SRoland McGrath 	 * index -1 means the kernel should try to find and
13313abd0e5SRoland McGrath 	 * allocate an empty descriptor:
13413abd0e5SRoland McGrath 	 */
135efd1ca52SRoland McGrath 	if (idx == -1 && can_allocate) {
13613abd0e5SRoland McGrath 		idx = get_free_idx();
13713abd0e5SRoland McGrath 		if (idx < 0)
13813abd0e5SRoland McGrath 			return idx;
13913abd0e5SRoland McGrath 		if (put_user(idx, &u_info->entry_number))
14013abd0e5SRoland McGrath 			return -EFAULT;
14113abd0e5SRoland McGrath 	}
14213abd0e5SRoland McGrath 
14313abd0e5SRoland McGrath 	if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
14413abd0e5SRoland McGrath 		return -EINVAL;
14513abd0e5SRoland McGrath 
1464c79a2d8SRoland McGrath 	set_tls_desc(p, idx, &info, 1);
14713abd0e5SRoland McGrath 
148c9867f86SAndy Lutomirski 	/*
149c9867f86SAndy Lutomirski 	 * If DS, ES, FS, or GS points to the modified segment, forcibly
150c9867f86SAndy Lutomirski 	 * refresh it.  Only needed on x86_64 because x86_32 reloads them
151c9867f86SAndy Lutomirski 	 * on return to user mode.
152c9867f86SAndy Lutomirski 	 */
153c9867f86SAndy Lutomirski 	modified_sel = (idx << 3) | 3;
154c9867f86SAndy Lutomirski 
155c9867f86SAndy Lutomirski 	if (p == current) {
156c9867f86SAndy Lutomirski #ifdef CONFIG_X86_64
157c9867f86SAndy Lutomirski 		savesegment(ds, sel);
158c9867f86SAndy Lutomirski 		if (sel == modified_sel)
159c9867f86SAndy Lutomirski 			loadsegment(ds, sel);
160c9867f86SAndy Lutomirski 
161c9867f86SAndy Lutomirski 		savesegment(es, sel);
162c9867f86SAndy Lutomirski 		if (sel == modified_sel)
163c9867f86SAndy Lutomirski 			loadsegment(es, sel);
164c9867f86SAndy Lutomirski 
165c9867f86SAndy Lutomirski 		savesegment(fs, sel);
166c9867f86SAndy Lutomirski 		if (sel == modified_sel)
167c9867f86SAndy Lutomirski 			loadsegment(fs, sel);
1683fb0fdb3SAndy Lutomirski #endif
169c9867f86SAndy Lutomirski 
170c9867f86SAndy Lutomirski 		savesegment(gs, sel);
171c9867f86SAndy Lutomirski 		if (sel == modified_sel)
172c9867f86SAndy Lutomirski 			load_gs_index(sel);
173c9867f86SAndy Lutomirski 	} else {
174c9867f86SAndy Lutomirski #ifdef CONFIG_X86_64
175c9867f86SAndy Lutomirski 		if (p->thread.fsindex == modified_sel)
176c9867f86SAndy Lutomirski 			p->thread.fsbase = info.base_addr;
177c9867f86SAndy Lutomirski 
178c9867f86SAndy Lutomirski 		if (p->thread.gsindex == modified_sel)
179c9867f86SAndy Lutomirski 			p->thread.gsbase = info.base_addr;
180c9867f86SAndy Lutomirski #endif
181c9867f86SAndy Lutomirski 	}
182c9867f86SAndy Lutomirski 
18313abd0e5SRoland McGrath 	return 0;
18413abd0e5SRoland McGrath }
18513abd0e5SRoland McGrath 
SYSCALL_DEFINE1(set_thread_area,struct user_desc __user *,u_info)1862cf09666SAl Viro SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
18713abd0e5SRoland McGrath {
1882cf09666SAl Viro 	return do_set_thread_area(current, -1, u_info, 1);
18913abd0e5SRoland McGrath }
19013abd0e5SRoland McGrath 
19113abd0e5SRoland McGrath 
19213abd0e5SRoland McGrath /*
19313abd0e5SRoland McGrath  * Get the current Thread-Local Storage area:
19413abd0e5SRoland McGrath  */
19513abd0e5SRoland McGrath 
fill_user_desc(struct user_desc * info,int idx,const struct desc_struct * desc)1961bd5718cSRoland McGrath static void fill_user_desc(struct user_desc *info, int idx,
1971bd5718cSRoland McGrath 			   const struct desc_struct *desc)
1981bd5718cSRoland McGrath 
1991bd5718cSRoland McGrath {
2001bd5718cSRoland McGrath 	memset(info, 0, sizeof(*info));
2011bd5718cSRoland McGrath 	info->entry_number = idx;
2021bd5718cSRoland McGrath 	info->base_addr = get_desc_base(desc);
2031bd5718cSRoland McGrath 	info->limit = get_desc_limit(desc);
2041bd5718cSRoland McGrath 	info->seg_32bit = desc->d;
2051bd5718cSRoland McGrath 	info->contents = desc->type >> 2;
2061bd5718cSRoland McGrath 	info->read_exec_only = !(desc->type & 2);
2071bd5718cSRoland McGrath 	info->limit_in_pages = desc->g;
2081bd5718cSRoland McGrath 	info->seg_not_present = !desc->p;
2091bd5718cSRoland McGrath 	info->useable = desc->avl;
2101bd5718cSRoland McGrath #ifdef CONFIG_X86_64
2111bd5718cSRoland McGrath 	info->lm = desc->l;
2121bd5718cSRoland McGrath #endif
2131bd5718cSRoland McGrath }
21413abd0e5SRoland McGrath 
do_get_thread_area(struct task_struct * p,int idx,struct user_desc __user * u_info)215efd1ca52SRoland McGrath int do_get_thread_area(struct task_struct *p, int idx,
216efd1ca52SRoland McGrath 		       struct user_desc __user *u_info)
21713abd0e5SRoland McGrath {
21813abd0e5SRoland McGrath 	struct user_desc info;
219993773d1SDianzhang Chen 	int index;
22013abd0e5SRoland McGrath 
221efd1ca52SRoland McGrath 	if (idx == -1 && get_user(idx, &u_info->entry_number))
22213abd0e5SRoland McGrath 		return -EFAULT;
2231bd5718cSRoland McGrath 
22413abd0e5SRoland McGrath 	if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
22513abd0e5SRoland McGrath 		return -EINVAL;
22613abd0e5SRoland McGrath 
227993773d1SDianzhang Chen 	index = idx - GDT_ENTRY_TLS_MIN;
228993773d1SDianzhang Chen 	index = array_index_nospec(index,
229993773d1SDianzhang Chen 			GDT_ENTRY_TLS_MAX - GDT_ENTRY_TLS_MIN + 1);
230993773d1SDianzhang Chen 
231993773d1SDianzhang Chen 	fill_user_desc(&info, idx, &p->thread.tls_array[index]);
23213abd0e5SRoland McGrath 
23313abd0e5SRoland McGrath 	if (copy_to_user(u_info, &info, sizeof(info)))
23413abd0e5SRoland McGrath 		return -EFAULT;
23513abd0e5SRoland McGrath 	return 0;
23613abd0e5SRoland McGrath }
23713abd0e5SRoland McGrath 
SYSCALL_DEFINE1(get_thread_area,struct user_desc __user *,u_info)2382cf09666SAl Viro SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
23913abd0e5SRoland McGrath {
2402cf09666SAl Viro 	return do_get_thread_area(current, -1, u_info);
24113abd0e5SRoland McGrath }
2424c79a2d8SRoland McGrath 
regset_tls_active(struct task_struct * target,const struct user_regset * regset)2434c79a2d8SRoland McGrath int regset_tls_active(struct task_struct *target,
2444c79a2d8SRoland McGrath 		      const struct user_regset *regset)
2454c79a2d8SRoland McGrath {
2464c79a2d8SRoland McGrath 	struct thread_struct *t = &target->thread;
2474c79a2d8SRoland McGrath 	int n = GDT_ENTRY_TLS_ENTRIES;
2484c79a2d8SRoland McGrath 	while (n > 0 && desc_empty(&t->tls_array[n - 1]))
2494c79a2d8SRoland McGrath 		--n;
2504c79a2d8SRoland McGrath 	return n;
2514c79a2d8SRoland McGrath }
2524c79a2d8SRoland McGrath 
regset_tls_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)2534c79a2d8SRoland McGrath int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
2540557d64dSAl Viro 		   struct membuf to)
2554c79a2d8SRoland McGrath {
2564c79a2d8SRoland McGrath 	const struct desc_struct *tls;
2570557d64dSAl Viro 	struct user_desc v;
2580557d64dSAl Viro 	int pos;
2594c79a2d8SRoland McGrath 
2600557d64dSAl Viro 	for (pos = 0, tls = target->thread.tls_array; to.left; pos++, tls++) {
2610557d64dSAl Viro 		fill_user_desc(&v, GDT_ENTRY_TLS_MIN + pos, tls);
2620557d64dSAl Viro 		membuf_write(&to, &v, sizeof(v));
2634c79a2d8SRoland McGrath 	}
2644c79a2d8SRoland McGrath 	return 0;
2654c79a2d8SRoland McGrath }
2664c79a2d8SRoland McGrath 
regset_tls_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)2674c79a2d8SRoland McGrath int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
2684c79a2d8SRoland McGrath 		   unsigned int pos, unsigned int count,
2694c79a2d8SRoland McGrath 		   const void *kbuf, const void __user *ubuf)
2704c79a2d8SRoland McGrath {
2714c79a2d8SRoland McGrath 	struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
2724c79a2d8SRoland McGrath 	const struct user_desc *info;
27341bdc785SAndy Lutomirski 	int i;
2744c79a2d8SRoland McGrath 
2758f0750f1SDan Carpenter 	if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
2764c79a2d8SRoland McGrath 	    (pos % sizeof(struct user_desc)) != 0 ||
2774c79a2d8SRoland McGrath 	    (count % sizeof(struct user_desc)) != 0)
2784c79a2d8SRoland McGrath 		return -EINVAL;
2794c79a2d8SRoland McGrath 
2804c79a2d8SRoland McGrath 	if (kbuf)
2814c79a2d8SRoland McGrath 		info = kbuf;
2824c79a2d8SRoland McGrath 	else if (__copy_from_user(infobuf, ubuf, count))
2834c79a2d8SRoland McGrath 		return -EFAULT;
2844c79a2d8SRoland McGrath 	else
2854c79a2d8SRoland McGrath 		info = infobuf;
2864c79a2d8SRoland McGrath 
28741bdc785SAndy Lutomirski 	for (i = 0; i < count / sizeof(struct user_desc); i++)
28841bdc785SAndy Lutomirski 		if (!tls_desc_okay(info + i))
28941bdc785SAndy Lutomirski 			return -EINVAL;
29041bdc785SAndy Lutomirski 
2914c79a2d8SRoland McGrath 	set_tls_desc(target,
2924c79a2d8SRoland McGrath 		     GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
2934c79a2d8SRoland McGrath 		     info, count / sizeof(struct user_desc));
2944c79a2d8SRoland McGrath 
2954c79a2d8SRoland McGrath 	return 0;
2964c79a2d8SRoland McGrath }
297