ldt.c 13.7 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

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

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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 已提交
51
/* context.lock is held by the task which issued the smp function call */
52
static void flush_ldt(void *__mm)
L
Linus Torvalds 已提交
53
{
54
	struct mm_struct *mm = __mm;
55

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

59
	load_mm_ldt(mm);
60 61

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

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

70
	if (num_entries > LDT_ENTRIES)
71 72 73 74 75 76 77
		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));
78
	alloc_size = num_entries * LDT_ENTRY_SIZE;
79 80 81 82 83 84 85 86 87

	/*
	 * 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 已提交
88
	else
89
		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
L
Linus Torvalds 已提交
90

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

96 97 98
	/* The new LDT isn't aliased for PTI yet. */
	new_ldt->slot = -1;

99
	new_ldt->nr_entries = num_entries;
100 101
	return new_ldt;
}
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#ifdef CONFIG_PAGE_TABLE_ISOLATION

static void do_sanity_check(struct mm_struct *mm,
			    bool had_kernel_mapping,
			    bool had_user_mapping)
{
	if (mm->context.ldt) {
		/*
		 * We already had an LDT.  The top-level entry should already
		 * have been allocated and synchronized with the usermode
		 * tables.
		 */
		WARN_ON(!had_kernel_mapping);
		if (static_cpu_has(X86_FEATURE_PTI))
			WARN_ON(!had_user_mapping);
	} else {
		/*
		 * This is the first time we're mapping an LDT for this process.
		 * Sync the pgd to the usermode tables.
		 */
		WARN_ON(had_kernel_mapping);
		if (static_cpu_has(X86_FEATURE_PTI))
			WARN_ON(had_user_mapping);
	}
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#ifdef CONFIG_X86_PAE

static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
{
	p4d_t *p4d;
	pud_t *pud;

	if (pgd->pgd == 0)
		return NULL;

	p4d = p4d_offset(pgd, va);
	if (p4d_none(*p4d))
		return NULL;

	pud = pud_offset(p4d, va);
	if (pud_none(*pud))
		return NULL;

	return pmd_offset(pud, va);
}

static void map_ldt_struct_to_user(struct mm_struct *mm)
{
	pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
	pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
	pmd_t *k_pmd, *u_pmd;

	k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
	u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);

	if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
		set_pmd(u_pmd, *k_pmd);
}

static void sanity_check_ldt_mapping(struct mm_struct *mm)
{
	pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
	pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
	bool had_kernel, had_user;
	pmd_t *k_pmd, *u_pmd;

	k_pmd      = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
	u_pmd      = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
	had_kernel = (k_pmd->pmd != 0);
	had_user   = (u_pmd->pmd != 0);

	do_sanity_check(mm, had_kernel, had_user);
}

#else /* !CONFIG_X86_PAE */

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static void map_ldt_struct_to_user(struct mm_struct *mm)
{
	pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);

	if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
		set_pgd(kernel_to_user_pgdp(pgd), *pgd);
}

static void sanity_check_ldt_mapping(struct mm_struct *mm)
{
	pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
	bool had_kernel = (pgd->pgd != 0);
	bool had_user   = (kernel_to_user_pgdp(pgd)->pgd != 0);

	do_sanity_check(mm, had_kernel, had_user);
}

197 198
#endif /* CONFIG_X86_PAE */

199 200 201 202 203 204 205 206
/*
 * If PTI is enabled, this maps the LDT into the kernelmode and
 * usermode tables for the given mm.
 */
static int
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
{
	unsigned long va;
207
	bool is_vmalloc;
208
	spinlock_t *ptl;
209
	int i, nr_pages;
210 211 212 213 214 215 216 217 218 219 220
	pgd_t *pgd;

	if (!static_cpu_has(X86_FEATURE_PTI))
		return 0;

	/*
	 * Any given ldt_struct should have map_ldt_struct() called at most
	 * once.
	 */
	WARN_ON(ldt->slot != -1);

221 222 223
	/* Check if the current mappings are sane */
	sanity_check_ldt_mapping(mm);

224 225 226 227 228 229 230 231 232
	/*
	 * Did we already have the top level entry allocated?  We can't
	 * use pgd_none() for this because it doens't do anything on
	 * 4-level page table kernels.
	 */
	pgd = pgd_offset(mm, LDT_BASE_ADDR);

	is_vmalloc = is_vmalloc_addr(ldt->entries);

233 234 235
	nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);

	for (i = 0; i < nr_pages; i++) {
236 237 238
		unsigned long offset = i << PAGE_SHIFT;
		const void *src = (char *)ldt->entries + offset;
		unsigned long pfn;
239
		pgprot_t pte_prot;
240 241 242 243 244 245 246 247 248 249 250 251 252
		pte_t pte, *ptep;

		va = (unsigned long)ldt_slot_va(slot) + offset;
		pfn = is_vmalloc ? vmalloc_to_pfn(src) :
			page_to_pfn(virt_to_page(src));
		/*
		 * Treat the PTI LDT range as a *userspace* range.
		 * get_locked_pte() will allocate all needed pagetables
		 * and account for them in this mm.
		 */
		ptep = get_locked_pte(mm, va, &ptl);
		if (!ptep)
			return -ENOMEM;
T
Thomas Gleixner 已提交
253 254 255 256 257
		/*
		 * Map it RO so the easy to find address is not a primary
		 * target via some kernel interface which misses a
		 * permission check.
		 */
258 259
		pte_prot = __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL);
		/* Filter out unsuppored __PAGE_KERNEL* bits: */
260
		pgprot_val(pte_prot) &= __supported_pte_mask;
261
		pte = pfn_pte(pfn, pte_prot);
262 263 264 265
		set_pte_at(mm, va, ptep, pte);
		pte_unmap_unlock(ptep, ptl);
	}

266 267
	/* Propagate LDT mapping to the user page-table */
	map_ldt_struct_to_user(mm);
268 269 270 271 272

	ldt->slot = slot;
	return 0;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
{
	unsigned long va;
	int i, nr_pages;

	if (!ldt)
		return;

	/* LDT map/unmap is only required for PTI */
	if (!static_cpu_has(X86_FEATURE_PTI))
		return;

	nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);

	for (i = 0; i < nr_pages; i++) {
		unsigned long offset = i << PAGE_SHIFT;
		spinlock_t *ptl;
		pte_t *ptep;

		va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
		ptep = get_locked_pte(mm, va, &ptl);
		pte_clear(mm, va, ptep);
		pte_unmap_unlock(ptep, ptl);
	}

	va = (unsigned long)ldt_slot_va(ldt->slot);
	flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, 0);
}

302 303 304 305 306 307 308
#else /* !CONFIG_PAGE_TABLE_ISOLATION */

static int
map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
{
	return 0;
}
309 310 311 312

static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
{
}
313 314
#endif /* CONFIG_PAGE_TABLE_ISOLATION */

315 316 317 318 319
static void free_ldt_pgtables(struct mm_struct *mm)
{
#ifdef CONFIG_PAGE_TABLE_ISOLATION
	struct mmu_gather tlb;
	unsigned long start = LDT_BASE_ADDR;
J
Joerg Roedel 已提交
320
	unsigned long end = LDT_END_ADDR;
321 322 323 324 325 326 327 328 329 330

	if (!static_cpu_has(X86_FEATURE_PTI))
		return;

	tlb_gather_mmu(&tlb, mm, start, end);
	free_pgd_range(&tlb, start, end, start, end);
	tlb_finish_mmu(&tlb, start, end);
#endif
}

331 332 333
/* After calling this, the LDT is immutable. */
static void finalize_ldt_struct(struct ldt_struct *ldt)
{
334
	paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
L
Linus Torvalds 已提交
335 336
}

P
Peter Zijlstra 已提交
337
static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
L
Linus Torvalds 已提交
338
{
P
Peter Zijlstra 已提交
339 340
	mutex_lock(&mm->context.lock);

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

P
Peter Zijlstra 已提交
344 345 346 347
	/* 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);
348
}
349

350 351 352 353
static void free_ldt_struct(struct ldt_struct *ldt)
{
	if (likely(!ldt))
		return;
354

355 356
	paravirt_free_ldt(ldt->entries, ldt->nr_entries);
	if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
357
		vfree_atomic(ldt->entries);
358
	else
359
		free_page((unsigned long)ldt->entries);
360
	kfree(ldt);
L
Linus Torvalds 已提交
361 362 363
}

/*
364 365
 * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
 * the new task is not running, so nothing can be installed.
L
Linus Torvalds 已提交
366
 */
367
int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
L
Linus Torvalds 已提交
368
{
369
	struct ldt_struct *new_ldt;
L
Linus Torvalds 已提交
370 371
	int retval = 0;

372
	if (!old_mm)
373 374 375
		return 0;

	mutex_lock(&old_mm->context.lock);
376
	if (!old_mm->context.ldt)
377 378
		goto out_unlock;

379
	new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
380 381 382 383 384 385
	if (!new_ldt) {
		retval = -ENOMEM;
		goto out_unlock;
	}

	memcpy(new_ldt->entries, old_mm->context.ldt->entries,
386
	       new_ldt->nr_entries * LDT_ENTRY_SIZE);
387 388
	finalize_ldt_struct(new_ldt);

389 390 391 392 393 394
	retval = map_ldt_struct(mm, new_ldt, 0);
	if (retval) {
		free_ldt_pgtables(mm);
		free_ldt_struct(new_ldt);
		goto out_unlock;
	}
395 396 397 398
	mm->context.ldt = new_ldt;

out_unlock:
	mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
399 400 401 402
	return retval;
}

/*
403 404 405
 * 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 已提交
406
 */
407
void destroy_context_ldt(struct mm_struct *mm)
L
Linus Torvalds 已提交
408
{
409 410
	free_ldt_struct(mm->context.ldt);
	mm->context.ldt = NULL;
L
Linus Torvalds 已提交
411 412
}

413 414 415 416 417
void ldt_arch_exit_mmap(struct mm_struct *mm)
{
	free_ldt_pgtables(mm);
}

418
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
419
{
420
	struct mm_struct *mm = current->mm;
421 422
	unsigned long entries_size;
	int retval;
L
Linus Torvalds 已提交
423

P
Peter Zijlstra 已提交
424
	down_read(&mm->context.ldt_usr_sem);
425 426 427 428 429 430

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

431 432
	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
L
Linus Torvalds 已提交
433

434 435 436
	entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
	if (entries_size > bytecount)
		entries_size = bytecount;
L
Linus Torvalds 已提交
437

438
	if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
439 440 441 442
		retval = -EFAULT;
		goto out_unlock;
	}

443
	if (entries_size != bytecount) {
444
		/* Zero-fill the rest and pretend we read bytecount bytes. */
445
		if (clear_user(ptr + entries_size, bytecount - entries_size)) {
446 447
			retval = -EFAULT;
			goto out_unlock;
L
Linus Torvalds 已提交
448 449
		}
	}
450 451 452
	retval = bytecount;

out_unlock:
P
Peter Zijlstra 已提交
453
	up_read(&mm->context.ldt_usr_sem);
454
	return retval;
L
Linus Torvalds 已提交
455 456
}

457
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
458
{
459 460 461 462 463 464 465 466
	/* 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 已提交
467 468
	if (clear_user(ptr, bytecount))
		return -EFAULT;
469
	return bytecount;
L
Linus Torvalds 已提交
470 471
}

472
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
473
{
474
	struct mm_struct *mm = current->mm;
475
	struct ldt_struct *new_ldt, *old_ldt;
476
	unsigned int old_nr_entries, new_nr_entries;
477
	struct user_desc ldt_info;
478
	struct desc_struct ldt;
L
Linus Torvalds 已提交
479 480 481 482 483
	int error;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
484
	error = -EFAULT;
485
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496 497
		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;
	}

498 499 500 501 502 503 504 505
	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 已提交
506
		}
507 508 509 510

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

P
Peter Zijlstra 已提交
513 514
	if (down_write_killable(&mm->context.ldt_usr_sem))
		return -EINTR;
515

516 517 518
	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);
519 520

	error = -ENOMEM;
521
	new_ldt = alloc_ldt_struct(new_nr_entries);
522
	if (!new_ldt)
523 524
		goto out_unlock;

525
	if (old_ldt)
526 527
		memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);

528 529
	new_ldt->entries[ldt_info.entry_number] = ldt;
	finalize_ldt_struct(new_ldt);
L
Linus Torvalds 已提交
530

531 532 533 534 535 536 537 538
	/*
	 * If we are using PTI, map the new LDT into the userspace pagetables.
	 * If there is already an LDT, use the other slot so that other CPUs
	 * will continue to use the old LDT until install_ldt() switches
	 * them over to the new LDT.
	 */
	error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
	if (error) {
539 540 541 542 543
		/*
		 * This only can fail for the first LDT setup. If an LDT is
		 * already installed then the PTE page is already
		 * populated. Mop up a half populated page table.
		 */
544 545
		if (!WARN_ON_ONCE(old_ldt))
			free_ldt_pgtables(mm);
546
		free_ldt_struct(new_ldt);
547 548 549
		goto out_unlock;
	}

550
	install_ldt(mm, new_ldt);
551
	unmap_ldt_struct(mm, old_ldt);
552
	free_ldt_struct(old_ldt);
L
Linus Torvalds 已提交
553 554 555
	error = 0;

out_unlock:
P
Peter Zijlstra 已提交
556
	up_write(&mm->context.ldt_usr_sem);
L
Linus Torvalds 已提交
557 558 559 560
out:
	return error;
}

561 562
SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
		unsigned long , bytecount)
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
{
	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;
	}
580 581 582 583 584 585 586 587 588 589
	/*
	 * 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 已提交
590
}