ldt.c 7.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5
/*
 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
 * Copyright (C) 2002 Andi Kleen
6
 *
L
Linus Torvalds 已提交
7
 * This handles calls from both 32bit and 64bit mode.
P
Peter Zijlstra 已提交
8 9 10 11 12
 *
 * Lock order:
 *	contex.ldt_usr_sem
 *	  mmap_sem
 *	    context.lock
L
Linus Torvalds 已提交
13 14 15
 */

#include <linux/errno.h>
16
#include <linux/gfp.h>
L
Linus Torvalds 已提交
17 18 19 20
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
21
#include <linux/syscalls.h>
22
#include <linux/slab.h>
L
Linus Torvalds 已提交
23
#include <linux/vmalloc.h>
24
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
25 26 27

#include <asm/ldt.h>
#include <asm/desc.h>
28
#include <asm/mmu_context.h>
29
#include <asm/syscalls.h>
L
Linus Torvalds 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
static void refresh_ldt_segments(void)
{
#ifdef CONFIG_X86_64
	unsigned short sel;

	/*
	 * Make sure that the cached DS and ES descriptors match the updated
	 * LDT.
	 */
	savesegment(ds, sel);
	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
		loadsegment(ds, sel);

	savesegment(es, sel);
	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
		loadsegment(es, sel);
#endif
}

P
Peter Zijlstra 已提交
50
/* context.lock is held by the task which issued the smp function call */
51
static void flush_ldt(void *__mm)
L
Linus Torvalds 已提交
52
{
53
	struct mm_struct *mm = __mm;
54 55
	mm_context_t *pc;

56
	if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
57 58
		return;

59
	pc = &mm->context;
60
	set_ldt(pc->ldt->entries, pc->ldt->nr_entries);
61 62

	refresh_ldt_segments();
L
Linus Torvalds 已提交
63 64
}

65
/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
66
static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
L
Linus Torvalds 已提交
67
{
68
	struct ldt_struct *new_ldt;
69
	unsigned int alloc_size;
70

71
	if (num_entries > LDT_ENTRIES)
72 73 74 75 76 77 78
		return NULL;

	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
	if (!new_ldt)
		return NULL;

	BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
79
	alloc_size = num_entries * LDT_ENTRY_SIZE;
80 81 82 83 84 85 86 87 88

	/*
	 * Xen is very picky: it requires a page-aligned LDT that has no
	 * trailing nonzero bytes in any page that contains LDT descriptors.
	 * Keep it simple: zero the whole allocation and never allocate less
	 * than PAGE_SIZE.
	 */
	if (alloc_size > PAGE_SIZE)
		new_ldt->entries = vzalloc(alloc_size);
L
Linus Torvalds 已提交
89
	else
90
		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
L
Linus Torvalds 已提交
91

92 93 94 95
	if (!new_ldt->entries) {
		kfree(new_ldt);
		return NULL;
	}
96

97
	new_ldt->nr_entries = num_entries;
98 99
	return new_ldt;
}
100

101 102 103
/* After calling this, the LDT is immutable. */
static void finalize_ldt_struct(struct ldt_struct *ldt)
{
104
	paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
L
Linus Torvalds 已提交
105 106
}

P
Peter Zijlstra 已提交
107
static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
L
Linus Torvalds 已提交
108
{
P
Peter Zijlstra 已提交
109 110
	mutex_lock(&mm->context.lock);

111
	/* Synchronizes with READ_ONCE in load_mm_ldt. */
P
Peter Zijlstra 已提交
112
	smp_store_release(&mm->context.ldt, ldt);
113

P
Peter Zijlstra 已提交
114 115 116 117
	/* Activate the LDT for all CPUs using currents mm. */
	on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);

	mutex_unlock(&mm->context.lock);
118
}
119

120 121 122 123
static void free_ldt_struct(struct ldt_struct *ldt)
{
	if (likely(!ldt))
		return;
124

125 126
	paravirt_free_ldt(ldt->entries, ldt->nr_entries);
	if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
127
		vfree_atomic(ldt->entries);
128
	else
129
		free_page((unsigned long)ldt->entries);
130
	kfree(ldt);
L
Linus Torvalds 已提交
131 132 133 134 135 136
}

/*
 * we do not have to muck with descriptors here, that is
 * done in switch_mm() as needed.
 */
137
int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
L
Linus Torvalds 已提交
138
{
139
	struct ldt_struct *new_ldt;
140
	struct mm_struct *old_mm;
L
Linus Torvalds 已提交
141 142
	int retval = 0;

P
Peter Zijlstra 已提交
143 144
	init_rwsem(&mm->context.ldt_usr_sem);

L
Linus Torvalds 已提交
145
	old_mm = current->mm;
146 147 148
	if (!old_mm) {
		mm->context.ldt = NULL;
		return 0;
L
Linus Torvalds 已提交
149
	}
150 151 152 153 154 155 156

	mutex_lock(&old_mm->context.lock);
	if (!old_mm->context.ldt) {
		mm->context.ldt = NULL;
		goto out_unlock;
	}

157
	new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
158 159 160 161 162 163
	if (!new_ldt) {
		retval = -ENOMEM;
		goto out_unlock;
	}

	memcpy(new_ldt->entries, old_mm->context.ldt->entries,
164
	       new_ldt->nr_entries * LDT_ENTRY_SIZE);
165 166 167 168 169 170
	finalize_ldt_struct(new_ldt);

	mm->context.ldt = new_ldt;

out_unlock:
	mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
171 172 173 174
	return retval;
}

/*
175 176 177
 * No need to lock the MM as we are the last user
 *
 * 64bit: Don't touch the LDT register - we're already in the next thread.
L
Linus Torvalds 已提交
178
 */
179
void destroy_context_ldt(struct mm_struct *mm)
L
Linus Torvalds 已提交
180
{
181 182
	free_ldt_struct(mm->context.ldt);
	mm->context.ldt = NULL;
L
Linus Torvalds 已提交
183 184
}

185
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
186
{
187
	struct mm_struct *mm = current->mm;
188 189
	unsigned long entries_size;
	int retval;
L
Linus Torvalds 已提交
190

P
Peter Zijlstra 已提交
191
	down_read(&mm->context.ldt_usr_sem);
192 193 194 195 196 197

	if (!mm->context.ldt) {
		retval = 0;
		goto out_unlock;
	}

198 199
	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
L
Linus Torvalds 已提交
200

201 202 203
	entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
	if (entries_size > bytecount)
		entries_size = bytecount;
L
Linus Torvalds 已提交
204

205
	if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
206 207 208 209
		retval = -EFAULT;
		goto out_unlock;
	}

210
	if (entries_size != bytecount) {
211
		/* Zero-fill the rest and pretend we read bytecount bytes. */
212
		if (clear_user(ptr + entries_size, bytecount - entries_size)) {
213 214
			retval = -EFAULT;
			goto out_unlock;
L
Linus Torvalds 已提交
215 216
		}
	}
217 218 219
	retval = bytecount;

out_unlock:
P
Peter Zijlstra 已提交
220
	up_read(&mm->context.ldt_usr_sem);
221
	return retval;
L
Linus Torvalds 已提交
222 223
}

224
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
225
{
226 227 228 229 230 231 232 233
	/* CHECKME: Can we use _one_ random number ? */
#ifdef CONFIG_X86_32
	unsigned long size = 5 * sizeof(struct desc_struct);
#else
	unsigned long size = 128;
#endif
	if (bytecount > size)
		bytecount = size;
L
Linus Torvalds 已提交
234 235
	if (clear_user(ptr, bytecount))
		return -EFAULT;
236
	return bytecount;
L
Linus Torvalds 已提交
237 238
}

239
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
240
{
241
	struct mm_struct *mm = current->mm;
242
	struct ldt_struct *new_ldt, *old_ldt;
243
	unsigned int old_nr_entries, new_nr_entries;
244
	struct user_desc ldt_info;
245
	struct desc_struct ldt;
L
Linus Torvalds 已提交
246 247 248 249 250
	int error;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
251
	error = -EFAULT;
252
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260 261 262 263 264
		goto out;

	error = -EINVAL;
	if (ldt_info.entry_number >= LDT_ENTRIES)
		goto out;
	if (ldt_info.contents == 3) {
		if (oldmode)
			goto out;
		if (ldt_info.seg_not_present == 0)
			goto out;
	}

265 266 267 268 269 270 271 272
	if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
	    LDT_empty(&ldt_info)) {
		/* The user wants to clear the entry. */
		memset(&ldt, 0, sizeof(ldt));
	} else {
		if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
			error = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
273
		}
274 275 276 277

		fill_ldt(&ldt, &ldt_info);
		if (oldmode)
			ldt.avl = 0;
L
Linus Torvalds 已提交
278 279
	}

P
Peter Zijlstra 已提交
280 281
	if (down_write_killable(&mm->context.ldt_usr_sem))
		return -EINTR;
282

283 284 285
	old_ldt       = mm->context.ldt;
	old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
	new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
286 287

	error = -ENOMEM;
288
	new_ldt = alloc_ldt_struct(new_nr_entries);
289
	if (!new_ldt)
290 291
		goto out_unlock;

292
	if (old_ldt)
293 294
		memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);

295 296
	new_ldt->entries[ldt_info.entry_number] = ldt;
	finalize_ldt_struct(new_ldt);
L
Linus Torvalds 已提交
297

298 299
	install_ldt(mm, new_ldt);
	free_ldt_struct(old_ldt);
L
Linus Torvalds 已提交
300 301 302
	error = 0;

out_unlock:
P
Peter Zijlstra 已提交
303
	up_write(&mm->context.ldt_usr_sem);
L
Linus Torvalds 已提交
304 305 306 307
out:
	return error;
}

308 309
SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
		unsigned long , bytecount)
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
	int ret = -ENOSYS;

	switch (func) {
	case 0:
		ret = read_ldt(ptr, bytecount);
		break;
	case 1:
		ret = write_ldt(ptr, bytecount, 1);
		break;
	case 2:
		ret = read_default_ldt(ptr, bytecount);
		break;
	case 0x11:
		ret = write_ldt(ptr, bytecount, 0);
		break;
	}
327 328 329 330 331 332 333 334 335 336
	/*
	 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
	 * return type, but tht ABI for sys_modify_ldt() expects
	 * 'int'.  This cast gives us an int-sized value in %rax
	 * for the return code.  The 'unsigned' is necessary so
	 * the compiler does not try to sign-extend the negative
	 * return codes into the high half of the register when
	 * taking the value from int->long.
	 */
	return (unsigned int)ret;
L
Linus Torvalds 已提交
337
}