exec.c 46.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  linux/fs/exec.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

/*
 * #!-checking implemented by tytso.
 */
/*
 * Demand-loading implemented 01.12.91 - no need to read anything but
 * the header into memory. The inode of the executable is put into
 * "current->executable", and page faults do the actual loading. Clean.
 *
 * Once more I can proudly say that linux stood up to being changed: it
 * was less than 2 hours work to get demand-loading completely implemented.
 *
 * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
 * current->executable is only used by the procfs.  This allows a dispatch
 * table to check for several different types  of binary formats.  We keep
 * trying until we recognize the file or we run out of supported binary
22
 * formats.
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26
 */

#include <linux/slab.h>
#include <linux/file.h>
Al Viro's avatar
Al Viro committed
27
#include <linux/fdtable.h>
Hugh Dickins's avatar
Hugh Dickins committed
28
#include <linux/mm.h>
Davidlohr Bueso's avatar
Davidlohr Bueso committed
29
#include <linux/vmacache.h>
Linus Torvalds's avatar
Linus Torvalds committed
30 31
#include <linux/stat.h>
#include <linux/fcntl.h>
Hugh Dickins's avatar
Hugh Dickins committed
32
#include <linux/swap.h>
33
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <linux/init.h>
35
#include <linux/sched/mm.h>
36
#include <linux/sched/coredump.h>
37
#include <linux/sched/signal.h>
38
#include <linux/sched/numa_balancing.h>
39
#include <linux/sched/task.h>
40
#include <linux/pagemap.h>
41
#include <linux/perf_event.h>
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47
#include <linux/highmem.h>
#include <linux/spinlock.h>
#include <linux/key.h>
#include <linux/personality.h>
#include <linux/binfmts.h>
#include <linux/utsname.h>
48
#include <linux/pid_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51
#include <linux/module.h>
#include <linux/namei.h>
#include <linux/mount.h>
52
#include <linux/ipipe.h>
Linus Torvalds's avatar
Linus Torvalds committed
53 54
#include <linux/security.h>
#include <linux/syscalls.h>
55
#include <linux/tsacct_kern.h>
56
#include <linux/cn_proc.h>
Al Viro's avatar
Al Viro committed
57
#include <linux/audit.h>
Roland McGrath's avatar
Roland McGrath committed
58
#include <linux/tracehook.h>
Johannes Berg's avatar
Johannes Berg committed
59
#include <linux/kmod.h>
60
#include <linux/fsnotify.h>
61
#include <linux/fs_struct.h>
62
#include <linux/pipe_fs_i.h>
Ying Han's avatar
Ying Han committed
63
#include <linux/oom.h>
64
#include <linux/compat.h>
65
#include <linux/vmalloc.h>
Linus Torvalds's avatar
Linus Torvalds committed
66

67
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
68
#include <asm/mmu_context.h>
69
#include <asm/tlb.h>
70 71

#include <trace/events/task.h>
72
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed
73

74 75
#include <trace/events/sched.h>

Alan Cox's avatar
Alan Cox committed
76 77
int suid_dumpable = 0;

78
static LIST_HEAD(formats);
Linus Torvalds's avatar
Linus Torvalds committed
79 80
static DEFINE_RWLOCK(binfmt_lock);

Al Viro's avatar
Al Viro committed
81
void __register_binfmt(struct linux_binfmt * fmt, int insert)
Linus Torvalds's avatar
Linus Torvalds committed
82
{
Al Viro's avatar
Al Viro committed
83
	BUG_ON(!fmt);
84 85
	if (WARN_ON(!fmt->load_binary))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
86
	write_lock(&binfmt_lock);
Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
87 88
	insert ? list_add(&fmt->lh, &formats) :
		 list_add_tail(&fmt->lh, &formats);
Linus Torvalds's avatar
Linus Torvalds committed
89 90 91
	write_unlock(&binfmt_lock);
}

Ivan Kokshaysky's avatar
Ivan Kokshaysky committed
92
EXPORT_SYMBOL(__register_binfmt);
Linus Torvalds's avatar
Linus Torvalds committed
93

94
void unregister_binfmt(struct linux_binfmt * fmt)
Linus Torvalds's avatar
Linus Torvalds committed
95 96
{
	write_lock(&binfmt_lock);
97
	list_del(&fmt->lh);
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101 102 103 104 105 106 107
	write_unlock(&binfmt_lock);
}

EXPORT_SYMBOL(unregister_binfmt);

static inline void put_binfmt(struct linux_binfmt * fmt)
{
	module_put(fmt->module);
}

108 109 110 111 112 113
bool path_noexec(const struct path *path)
{
	return (path->mnt->mnt_flags & MNT_NOEXEC) ||
	       (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
}

114
#ifdef CONFIG_USELIB
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118 119 120
/*
 * Note that a shared library must be both readable and executable due to
 * security reasons.
 *
 * Also note that we take the address to load from from the file itself.
 */
121
SYSCALL_DEFINE1(uselib, const char __user *, library)
Linus Torvalds's avatar
Linus Torvalds committed
122
{
Al Viro's avatar
Al Viro committed
123
	struct linux_binfmt *fmt;
124
	struct file *file;
125
	struct filename *tmp = getname(library);
126
	int error = PTR_ERR(tmp);
127 128
	static const struct open_flags uselib_flags = {
		.open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
Al Viro's avatar
Al Viro committed
129
		.acc_mode = MAY_READ | MAY_EXEC,
130 131
		.intent = LOOKUP_OPEN,
		.lookup_flags = LOOKUP_FOLLOW,
132
	};
133

134 135 136
	if (IS_ERR(tmp))
		goto out;

137
	file = do_filp_open(AT_FDCWD, tmp, &uselib_flags);
138 139 140
	putname(tmp);
	error = PTR_ERR(file);
	if (IS_ERR(file))
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143
		goto out;

	error = -EINVAL;
Al Viro's avatar
Al Viro committed
144
	if (!S_ISREG(file_inode(file)->i_mode))
Linus Torvalds's avatar
Linus Torvalds committed
145 146
		goto exit;

147
	error = -EACCES;
148
	if (path_noexec(&file->f_path))
Linus Torvalds's avatar
Linus Torvalds committed
149 150
		goto exit;

151
	fsnotify_open(file);
152

Linus Torvalds's avatar
Linus Torvalds committed
153 154
	error = -ENOEXEC;

Al Viro's avatar
Al Viro committed
155 156 157 158 159 160
	read_lock(&binfmt_lock);
	list_for_each_entry(fmt, &formats, lh) {
		if (!fmt->load_shlib)
			continue;
		if (!try_module_get(fmt->module))
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
161
		read_unlock(&binfmt_lock);
Al Viro's avatar
Al Viro committed
162 163 164 165 166
		error = fmt->load_shlib(file);
		read_lock(&binfmt_lock);
		put_binfmt(fmt);
		if (error != -ENOEXEC)
			break;
Linus Torvalds's avatar
Linus Torvalds committed
167
	}
Al Viro's avatar
Al Viro committed
168
	read_unlock(&binfmt_lock);
169
exit:
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173
	fput(file);
out:
  	return error;
}
174
#endif /* #ifdef CONFIG_USELIB */
Linus Torvalds's avatar
Linus Torvalds committed
175

176
#ifdef CONFIG_MMU
Oleg Nesterov's avatar
Oleg Nesterov committed
177 178 179 180 181 182
/*
 * The nascent bprm->mm is not visible until exec_mmap() but it can
 * use a lot of memory, account these pages in current->mm temporary
 * for oom_badness()->get_mm_rss(). Once exec succeeds or fails, we
 * change the counter back via acct_arg_size(0).
 */
183
static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
184 185 186 187 188 189 190 191 192 193 194
{
	struct mm_struct *mm = current->mm;
	long diff = (long)(pages - bprm->vma_pages);

	if (!mm || !diff)
		return;

	bprm->vma_pages = pages;
	add_mm_counter(mm, MM_ANONPAGES, diff);
}

195
static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
196 197 198 199
		int write)
{
	struct page *page;
	int ret;
200
	unsigned int gup_flags = FOLL_FORCE;
201 202 203

#ifdef CONFIG_STACK_GROWSUP
	if (write) {
204
		ret = expand_downwards(bprm->vma, pos);
205 206 207 208
		if (ret < 0)
			return NULL;
	}
#endif
209 210 211 212

	if (write)
		gup_flags |= FOLL_WRITE;

213 214 215 216
	/*
	 * We are doing an exec().  'current' is the process
	 * doing the exec and bprm->mm is the new process's mm.
	 */
217
	ret = get_user_pages_remote(current, bprm->mm, pos, 1, gup_flags,
218
			&page, NULL, NULL);
219 220 221 222 223
	if (ret <= 0)
		return NULL;

	if (write) {
		unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
224
		unsigned long ptr_size, limit;
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		/*
		 * Since the stack will hold pointers to the strings, we
		 * must account for them as well.
		 *
		 * The size calculation is the entire vma while each arg page is
		 * built, so each time we get here it's calculating how far it
		 * is currently (rather than each call being just the newly
		 * added size from the arg page).  As a result, we need to
		 * always add the entire size of the pointers, so that on the
		 * last call to get_arg_page() we'll actually have the entire
		 * correct size.
		 */
		ptr_size = (bprm->argc + bprm->envc) * sizeof(void *);
		if (ptr_size > ULONG_MAX - size)
			goto fail;
		size += ptr_size;

243 244
		acct_arg_size(bprm, size / PAGE_SIZE);

245 246 247 248 249 250
		/*
		 * We've historically supported up to 32 pages (ARG_MAX)
		 * of argument strings even with small stacks
		 */
		if (size <= ARG_MAX)
			return page;
251 252

		/*
253 254
		 * Limit to 1/4 of the max stack size or 3/4 of _STK_LIM
		 * (whichever is smaller) for the argv+env strings.
255 256 257 258 259
		 * This ensures that:
		 *  - the remaining binfmt code will not run out of stack space,
		 *  - the program will have a reasonable amount of stack left
		 *    to work from.
		 */
260 261 262
		limit = _STK_LIM / 4 * 3;
		limit = min(limit, rlimit(RLIMIT_STACK) / 4);
		if (size > limit)
263
			goto fail;
264 265 266
	}

	return page;
267 268 269 270

fail:
	put_page(page);
	return NULL;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

static void put_arg_page(struct page *page)
{
	put_page(page);
}

static void free_arg_pages(struct linux_binprm *bprm)
{
}

static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
		struct page *page)
{
	flush_cache_page(bprm->vma, pos, page_to_pfn(page));
}

static int __bprm_mm_init(struct linux_binprm *bprm)
{
290
	int err;
291 292 293 294 295
	struct vm_area_struct *vma = NULL;
	struct mm_struct *mm = bprm->mm;

	bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
	if (!vma)
296
		return -ENOMEM;
297

298 299 300 301
	if (down_write_killable(&mm->mmap_sem)) {
		err = -EINTR;
		goto err_free;
	}
302 303 304 305 306 307 308 309
	vma->vm_mm = mm;

	/*
	 * Place the stack at the largest stack address the architecture
	 * supports. Later, we'll move this to an appropriate place. We don't
	 * use STACK_TOP because that can depend on attributes which aren't
	 * configured yet.
	 */
310
	BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
311 312
	vma->vm_end = STACK_TOP_MAX;
	vma->vm_start = vma->vm_end - PAGE_SIZE;
313
	vma->vm_flags = VM_SOFTDIRTY | VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
314
	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
315
	INIT_LIST_HEAD(&vma->anon_vma_chain);
316

317
	err = insert_vm_struct(mm, vma);
318
	if (err)
319 320 321
		goto err;

	mm->stack_vm = mm->total_vm = 1;
322
	arch_bprm_mm_init(mm, vma);
323 324 325 326
	up_write(&mm->mmap_sem);
	bprm->p = vma->vm_end - sizeof(void *);
	return 0;
err:
327
	up_write(&mm->mmap_sem);
328
err_free:
329 330
	bprm->vma = NULL;
	kmem_cache_free(vm_area_cachep, vma);
331 332 333 334 335 336 337 338 339 340
	return err;
}

static bool valid_arg_len(struct linux_binprm *bprm, long len)
{
	return len <= MAX_ARG_STRLEN;
}

#else

341
static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
342 343 344
{
}

345
static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		int write)
{
	struct page *page;

	page = bprm->page[pos / PAGE_SIZE];
	if (!page && write) {
		page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
		if (!page)
			return NULL;
		bprm->page[pos / PAGE_SIZE] = page;
	}

	return page;
}

static void put_arg_page(struct page *page)
{
}

static void free_arg_page(struct linux_binprm *bprm, int i)
{
	if (bprm->page[i]) {
		__free_page(bprm->page[i]);
		bprm->page[i] = NULL;
	}
}

static void free_arg_pages(struct linux_binprm *bprm)
{
	int i;

	for (i = 0; i < MAX_ARG_PAGES; i++)
		free_arg_page(bprm, i);
}

static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
		struct page *page)
{
}

static int __bprm_mm_init(struct linux_binprm *bprm)
{
	bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
	return 0;
}

static bool valid_arg_len(struct linux_binprm *bprm, long len)
{
	return len <= bprm->p;
}

#endif /* CONFIG_MMU */

/*
 * Create a new mm_struct and populate it with a temporary stack
 * vm_area_struct.  We don't have enough context at this point to set the stack
 * flags, permissions, and offset, so we use temporary values.  We'll update
 * them later in setup_arg_pages().
 */
405
static int bprm_mm_init(struct linux_binprm *bprm)
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
{
	int err;
	struct mm_struct *mm = NULL;

	bprm->mm = mm = mm_alloc();
	err = -ENOMEM;
	if (!mm)
		goto err;

	err = __bprm_mm_init(bprm);
	if (err)
		goto err;

	return 0;

err:
	if (mm) {
		bprm->mm = NULL;
		mmdrop(mm);
	}

	return err;
}

430
struct user_arg_ptr {
431 432 433 434 435 436
#ifdef CONFIG_COMPAT
	bool is_compat;
#endif
	union {
		const char __user *const __user *native;
#ifdef CONFIG_COMPAT
Al Viro's avatar
Al Viro committed
437
		const compat_uptr_t __user *compat;
438 439
#endif
	} ptr;
440 441 442
};

static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
443
{
444 445 446 447 448 449 450 451
	const char __user *native;

#ifdef CONFIG_COMPAT
	if (unlikely(argv.is_compat)) {
		compat_uptr_t compat;

		if (get_user(compat, argv.ptr.compat + nr))
			return ERR_PTR(-EFAULT);
452

453 454 455 456 457
		return compat_ptr(compat);
	}
#endif

	if (get_user(native, argv.ptr.native + nr))
458 459
		return ERR_PTR(-EFAULT);

460
	return native;
461 462
}

Linus Torvalds's avatar
Linus Torvalds committed
463 464 465
/*
 * count() counts the number of strings in array ARGV.
 */
466
static int count(struct user_arg_ptr argv, int max)
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469
{
	int i = 0;

470
	if (argv.ptr.native != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
471
		for (;;) {
472
			const char __user *p = get_user_arg_ptr(argv, i);
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475

			if (!p)
				break;
476 477 478 479

			if (IS_ERR(p))
				return -EFAULT;

480
			if (i >= max)
Linus Torvalds's avatar
Linus Torvalds committed
481
				return -E2BIG;
482
			++i;
483 484 485

			if (fatal_signal_pending(current))
				return -ERESTARTNOHAND;
Linus Torvalds's avatar
Linus Torvalds committed
486 487 488 489 490 491 492
			cond_resched();
		}
	}
	return i;
}

/*
493 494 495
 * 'copy_strings()' copies argument/environment strings from the old
 * processes's memory to the new process's stack.  The call to get_user_pages()
 * ensures the destination page is created and not swapped out.
Linus Torvalds's avatar
Linus Torvalds committed
496
 */
497
static int copy_strings(int argc, struct user_arg_ptr argv,
Adrian Bunk's avatar
Adrian Bunk committed
498
			struct linux_binprm *bprm)
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501
{
	struct page *kmapped_page = NULL;
	char *kaddr = NULL;
502
	unsigned long kpos = 0;
Linus Torvalds's avatar
Linus Torvalds committed
503 504 505
	int ret;

	while (argc-- > 0) {
506
		const char __user *str;
Linus Torvalds's avatar
Linus Torvalds committed
507 508 509
		int len;
		unsigned long pos;

510 511 512
		ret = -EFAULT;
		str = get_user_arg_ptr(argv, argc);
		if (IS_ERR(str))
Linus Torvalds's avatar
Linus Torvalds committed
513 514
			goto out;

515 516 517 518 519 520
		len = strnlen_user(str, MAX_ARG_STRLEN);
		if (!len)
			goto out;

		ret = -E2BIG;
		if (!valid_arg_len(bprm, len))
Linus Torvalds's avatar
Linus Torvalds committed
521 522
			goto out;

523
		/* We're going to work our way backwords. */
Linus Torvalds's avatar
Linus Torvalds committed
524
		pos = bprm->p;
525 526
		str += len;
		bprm->p -= len;
Linus Torvalds's avatar
Linus Torvalds committed
527 528 529 530

		while (len > 0) {
			int offset, bytes_to_copy;

531 532 533 534
			if (fatal_signal_pending(current)) {
				ret = -ERESTARTNOHAND;
				goto out;
			}
535 536
			cond_resched();

Linus Torvalds's avatar
Linus Torvalds committed
537
			offset = pos % PAGE_SIZE;
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
			if (offset == 0)
				offset = PAGE_SIZE;

			bytes_to_copy = offset;
			if (bytes_to_copy > len)
				bytes_to_copy = len;

			offset -= bytes_to_copy;
			pos -= bytes_to_copy;
			str -= bytes_to_copy;
			len -= bytes_to_copy;

			if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
				struct page *page;

				page = get_arg_page(bprm, pos, 1);
Linus Torvalds's avatar
Linus Torvalds committed
554
				if (!page) {
555
					ret = -E2BIG;
Linus Torvalds's avatar
Linus Torvalds committed
556 557 558
					goto out;
				}

559 560
				if (kmapped_page) {
					flush_kernel_dcache_page(kmapped_page);
Linus Torvalds's avatar
Linus Torvalds committed
561
					kunmap(kmapped_page);
562 563
					put_arg_page(kmapped_page);
				}
Linus Torvalds's avatar
Linus Torvalds committed
564 565
				kmapped_page = page;
				kaddr = kmap(kmapped_page);
566 567
				kpos = pos & PAGE_MASK;
				flush_arg_page(bprm, kpos, kmapped_page);
Linus Torvalds's avatar
Linus Torvalds committed
568
			}
569
			if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
Linus Torvalds's avatar
Linus Torvalds committed
570 571 572 573 574 575 576
				ret = -EFAULT;
				goto out;
			}
		}
	}
	ret = 0;
out:
577 578
	if (kmapped_page) {
		flush_kernel_dcache_page(kmapped_page);
Linus Torvalds's avatar
Linus Torvalds committed
579
		kunmap(kmapped_page);
580 581
		put_arg_page(kmapped_page);
	}
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584 585 586 587
	return ret;
}

/*
 * Like copy_strings, but get argv and its values from kernel memory.
 */
588
int copy_strings_kernel(int argc, const char *const *__argv,
589
			struct linux_binprm *bprm)
Linus Torvalds's avatar
Linus Torvalds committed
590 591 592
{
	int r;
	mm_segment_t oldfs = get_fs();
593
	struct user_arg_ptr argv = {
594
		.ptr.native = (const char __user *const  __user *)__argv,
595 596
	};

Linus Torvalds's avatar
Linus Torvalds committed
597
	set_fs(KERNEL_DS);
598
	r = copy_strings(argc, argv, bprm);
Linus Torvalds's avatar
Linus Torvalds committed
599
	set_fs(oldfs);
600

Linus Torvalds's avatar
Linus Torvalds committed
601 602 603 604 605
	return r;
}
EXPORT_SYMBOL(copy_strings_kernel);

#ifdef CONFIG_MMU
606

Linus Torvalds's avatar
Linus Torvalds committed
607
/*
608 609 610
 * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX.  Once
 * the binfmt code determines where the new stack should reside, we shift it to
 * its final location.  The process proceeds as follows:
Linus Torvalds's avatar
Linus Torvalds committed
611
 *
612 613 614 615 616 617
 * 1) Use shift to calculate the new vma endpoints.
 * 2) Extend vma to cover both the old and new ranges.  This ensures the
 *    arguments passed to subsequent functions are consistent.
 * 3) Move vma's page tables to the new range.
 * 4) Free up any cleared pgd range.
 * 5) Shrink the vma to cover only the new range.
Linus Torvalds's avatar
Linus Torvalds committed
618
 */
619
static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
Linus Torvalds's avatar
Linus Torvalds committed
620 621
{
	struct mm_struct *mm = vma->vm_mm;
622 623 624 625 626
	unsigned long old_start = vma->vm_start;
	unsigned long old_end = vma->vm_end;
	unsigned long length = old_end - old_start;
	unsigned long new_start = old_start - shift;
	unsigned long new_end = old_end - shift;
Peter Zijlstra's avatar
Peter Zijlstra committed
627
	struct mmu_gather tlb;
Linus Torvalds's avatar
Linus Torvalds committed
628

629
	BUG_ON(new_start > new_end);
Linus Torvalds's avatar
Linus Torvalds committed
630

631 632 633 634 635 636 637 638 639 640
	/*
	 * ensure there are no vmas between where we want to go
	 * and where we are
	 */
	if (vma != find_vma(mm, new_start))
		return -EFAULT;

	/*
	 * cover the whole range: [new_start, old_end)
	 */
641 642
	if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
		return -ENOMEM;
643 644 645 646 647 648

	/*
	 * move the page tables downwards, on failure we rely on
	 * process cleanup to remove whatever mess we made.
	 */
	if (length != move_page_tables(vma, old_start,
649
				       vma, new_start, length, false))
650 651 652
		return -ENOMEM;

	lru_add_drain();
653
	tlb_gather_mmu(&tlb, mm, old_start, old_end);
654 655 656 657
	if (new_end > old_start) {
		/*
		 * when the old and new regions overlap clear from new_end.
		 */
Peter Zijlstra's avatar
Peter Zijlstra committed
658
		free_pgd_range(&tlb, new_end, old_end, new_end,
659
			vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
660 661 662 663 664 665 666
	} else {
		/*
		 * otherwise, clean from old_start; this is done to not touch
		 * the address space in [new_end, old_start) some architectures
		 * have constraints on va-space that make this illegal (IA64) -
		 * for the others its just a little faster.
		 */
Peter Zijlstra's avatar
Peter Zijlstra committed
667
		free_pgd_range(&tlb, old_start, old_end, new_end,
668
			vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
Linus Torvalds's avatar
Linus Torvalds committed
669
	}
670
	tlb_finish_mmu(&tlb, old_start, old_end);
671 672

	/*
673
	 * Shrink the vma to just the new range.  Always succeeds.
674 675 676 677
	 */
	vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
678 679
}

680 681 682 683
/*
 * Finalizes the stack vm_area_struct. The flags and permissions are updated,
 * the stack is optionally relocated, and some extra space is added.
 */
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687
int setup_arg_pages(struct linux_binprm *bprm,
		    unsigned long stack_top,
		    int executable_stack)
{
688 689
	unsigned long ret;
	unsigned long stack_shift;
Linus Torvalds's avatar
Linus Torvalds committed
690
	struct mm_struct *mm = current->mm;
691 692 693 694
	struct vm_area_struct *vma = bprm->vma;
	struct vm_area_struct *prev = NULL;
	unsigned long vm_flags;
	unsigned long stack_base;
695 696 697
	unsigned long stack_size;
	unsigned long stack_expand;
	unsigned long rlim_stack;
Linus Torvalds's avatar
Linus Torvalds committed
698 699

#ifdef CONFIG_STACK_GROWSUP
700
	/* Limit stack size */
Jiri Slaby's avatar
Jiri Slaby committed
701
	stack_base = rlimit_max(RLIMIT_STACK);
702 703
	if (stack_base > STACK_SIZE_MAX)
		stack_base = STACK_SIZE_MAX;
Linus Torvalds's avatar
Linus Torvalds committed
704

705 706 707
	/* Add space for stack randomization. */
	stack_base += (STACK_RND_MASK << PAGE_SHIFT);

708 709 710
	/* Make sure we didn't let the argument array grow too large. */
	if (vma->vm_end - vma->vm_start > stack_base)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
711

712
	stack_base = PAGE_ALIGN(stack_top - stack_base);
Linus Torvalds's avatar
Linus Torvalds committed
713

714 715 716
	stack_shift = vma->vm_start - stack_base;
	mm->arg_start = bprm->p - stack_shift;
	bprm->p = vma->vm_end - stack_shift;
Linus Torvalds's avatar
Linus Torvalds committed
717
#else
718 719
	stack_top = arch_align_stack(stack_top);
	stack_top = PAGE_ALIGN(stack_top);
720 721 722 723 724

	if (unlikely(stack_top < mmap_min_addr) ||
	    unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
		return -ENOMEM;

725 726 727
	stack_shift = vma->vm_end - stack_top;

	bprm->p -= stack_shift;
Linus Torvalds's avatar
Linus Torvalds committed
728 729 730 731
	mm->arg_start = bprm->p;
#endif

	if (bprm->loader)
732 733
		bprm->loader -= stack_shift;
	bprm->exec -= stack_shift;
Linus Torvalds's avatar
Linus Torvalds committed
734

735 736 737
	if (down_write_killable(&mm->mmap_sem))
		return -EINTR;

738
	vm_flags = VM_STACK_FLAGS;
739 740 741 742 743 744 745 746 747 748 749

	/*
	 * Adjust stack execute permissions; explicitly enable for
	 * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
	 * (arch default) otherwise.
	 */
	if (unlikely(executable_stack == EXSTACK_ENABLE_X))
		vm_flags |= VM_EXEC;
	else if (executable_stack == EXSTACK_DISABLE_X)
		vm_flags &= ~VM_EXEC;
	vm_flags |= mm->def_flags;
750
	vm_flags |= VM_STACK_INCOMPLETE_SETUP;
751 752 753 754 755 756 757 758 759 760

	ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
			vm_flags);
	if (ret)
		goto out_unlock;
	BUG_ON(prev != vma);

	/* Move stack pages down in memory. */
	if (stack_shift) {
		ret = shift_arg_pages(vma, stack_shift);
761 762
		if (ret)
			goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
763 764
	}

765 766 767
	/* mprotect_fixup is overkill to remove the temporary stack flags */
	vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;

768
	stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
769 770 771 772 773 774
	stack_size = vma->vm_end - vma->vm_start;
	/*
	 * Align this down to a page boundary as expand_stack
	 * will align it up.
	 */
	rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
775
#ifdef CONFIG_STACK_GROWSUP
776 777 778 779
	if (stack_size + stack_expand > rlim_stack)
		stack_base = vma->vm_start + rlim_stack;
	else
		stack_base = vma->vm_end + stack_expand;
780
#else
781 782 783 784
	if (stack_size + stack_expand > rlim_stack)
		stack_base = vma->vm_end - rlim_stack;
	else
		stack_base = vma->vm_start - stack_expand;
785
#endif
786
	current->mm->start_stack = bprm->p;
787 788 789 790 791
	ret = expand_stack(vma, stack_base);
	if (ret)
		ret = -EFAULT;

out_unlock:
Linus Torvalds's avatar
Linus Torvalds committed
792
	up_write(&mm->mmap_sem);
793
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
794 795 796
}
EXPORT_SYMBOL(setup_arg_pages);

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
#else

/*
 * Transfer the program arguments and environment from the holding pages
 * onto the stack. The provided stack pointer is adjusted accordingly.
 */
int transfer_args_to_stack(struct linux_binprm *bprm,
			   unsigned long *sp_location)
{
	unsigned long index, stop, sp;
	int ret = 0;

	stop = bprm->p >> PAGE_SHIFT;
	sp = *sp_location;

	for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
		unsigned int offset = index == stop ? bprm->p & ~PAGE_MASK : 0;
		char *src = kmap(bprm->page[index]) + offset;
		sp -= PAGE_SIZE - offset;
		if (copy_to_user((void *) sp, src, PAGE_SIZE - offset) != 0)
			ret = -EFAULT;
		kunmap(bprm->page[index]);
		if (ret)
			goto out;
	}

	*sp_location = sp;

out:
	return ret;
}
EXPORT_SYMBOL(transfer_args_to_stack);

Linus Torvalds's avatar
Linus Torvalds committed
830 831
#endif /* CONFIG_MMU */

832
static struct file *do_open_execat(int fd, struct filename *name, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
833 834
{
	struct file *file;
835
	int err;
836
	struct open_flags open_exec_flags = {
837
		.open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
Al Viro's avatar
Al Viro committed
838
		.acc_mode = MAY_EXEC,
839 840
		.intent = LOOKUP_OPEN,
		.lookup_flags = LOOKUP_FOLLOW,
841
	};
Linus Torvalds's avatar
Linus Torvalds committed
842

843 844 845 846 847 848 849 850
	if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
		return ERR_PTR(-EINVAL);
	if (flags & AT_SYMLINK_NOFOLLOW)
		open_exec_flags.lookup_flags &= ~LOOKUP_FOLLOW;
	if (flags & AT_EMPTY_PATH)
		open_exec_flags.lookup_flags |= LOOKUP_EMPTY;

	file = do_filp_open(fd, name, &open_exec_flags);
851
	if (IS_ERR(file))
852 853 854
		goto out;

	err = -EACCES;
Al Viro's avatar
Al Viro committed
855
	if (!S_ISREG(file_inode(file)->i_mode))
856
		goto exit;
857

858
	if (path_noexec(&file->f_path))
859
		goto exit;
860 861

	err = deny_write_access(file);
862 863
	if (err)
		goto exit;
Linus Torvalds's avatar
Linus Torvalds committed
864

865 866 867
	if (name->name[0] != '\0')
		fsnotify_open(file);

868
out:
869 870
	return file;

871 872
exit:
	fput(file);
873 874
	return ERR_PTR(err);
}
875 876 877

struct file *open_exec(const char *name)
{
878 879 880 881 882 883 884 885
	struct filename *filename = getname_kernel(name);
	struct file *f = ERR_CAST(filename);

	if (!IS_ERR(filename)) {
		f = do_open_execat(AT_FDCWD, filename, 0);
		putname(filename);
	}
	return f;
886
}
Linus Torvalds's avatar
Linus Torvalds committed
887 888
EXPORT_SYMBOL(open_exec);

889
int kernel_read_file(struct file *file, void **buf, loff_t *size,
890
		     loff_t max_size, enum kernel_read_file_id id)
891 892 893 894 895 896 897 898
{
	loff_t i_size, pos;
	ssize_t bytes = 0;
	int ret;

	if (!S_ISREG(file_inode(file)->i_mode) || max_size < 0)
		return -EINVAL;

899 900 901 902
	ret = security_kernel_read_file(file, id);
	if (ret)
		return ret;

903 904 905 906
	ret = deny_write_access(file);
	if (ret)
		return ret;

907
	i_size = i_size_read(file_inode(file));
908 909 910 911 912 913 914 915
	if (max_size > 0 && i_size > max_size) {
		ret = -EFBIG;
		goto out;
	}
	if (i_size <= 0) {
		ret = -EINVAL;
		goto out;
	}
916

917 918
	if (id != READING_FIRMWARE_PREALLOC_BUFFER)
		*buf = vmalloc(i_size);
919 920 921 922
	if (!*buf) {
		ret = -ENOMEM;
		goto out;
	}
923 924 925

	pos = 0;
	while (pos < i_size) {
926
		bytes = kernel_read(file, *buf + pos, i_size - pos, &pos);
927 928
		if (bytes < 0) {
			ret = bytes;
929
			goto out_free;
930 931 932 933 934 935 936 937
		}

		if (bytes == 0)
			break;
	}

	if (pos != i_size) {
		ret = -EIO;
938
		goto out_free;
939 940
	}

941
	ret = security_kernel_post_read_file(file, *buf, i_size, id);
942 943 944
	if (!ret)
		*size = pos;

945
out_free:
946
	if (ret < 0) {
947 948 949 950
		if (id != READING_FIRMWARE_PREALLOC_BUFFER) {
			vfree(*buf);
			*buf = NULL;
		}
951
	}
952 953 954

out:
	allow_write_access(file);
955 956 957 958
	return ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file);

959
int kernel_read_file_from_path(const char *path, void **buf, loff_t *size,
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
			       loff_t max_size, enum kernel_read_file_id id)
{
	struct file *file;
	int ret;

	if (!path || !*path)
		return -EINVAL;

	file = filp_open(path, O_RDONLY, 0);
	if (IS_ERR(file))
		return PTR_ERR(file);

	ret = kernel_read_file(file, buf, size, max_size, id);
	fput(file);
	return ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file_from_path);

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size,
			     enum kernel_read_file_id id)
{
	struct fd f = fdget(fd);
	int ret = -EBADF;

	if (!f.file)
		goto out;

	ret = kernel_read_file(f.file, buf, size, max_size, id);
out:
	fdput(f);
	return ret;
}
EXPORT_SYMBOL_GPL(kernel_read_file_from_fd);

Al Viro's avatar
Al Viro committed
994 995
ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
{
996
	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
Al Viro's avatar
Al Viro committed
997 998 999 1000 1001 1002
	if (res > 0)
		flush_icache_range(addr, addr + len);
	return res;
}
EXPORT_SYMBOL(read_code);

Linus Torvalds's avatar
Linus Torvalds committed
1003 1004 1005
static int exec_mmap(struct mm_struct *mm)
{
	struct task_struct *tsk;
Davidlohr Bueso's avatar
Davidlohr Bueso committed
1006
	struct mm_struct *old_mm, *active_mm;
1007
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009 1010 1011 1012 1013 1014

	/* Notify parent that we're no longer interested in the old VM */
	tsk = current;
	old_mm = current->mm;
	mm_release(tsk, old_mm);

	if (old_mm) {
1015
		sync_mm_rss(old_mm);
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017 1018 1019
		/*
		 * Make sure that if there is a core dump in progress
		 * for the old mm, we get out and die instead of going
		 * through with the exec.  We must hold mmap_sem around
1020
		 * checking core_state and changing tsk->mm.
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022
		 */
		down_read(&old_mm->mmap_sem);
1023
		if (unlikely(old_mm->core_state)) {
Linus Torvalds's avatar
Linus Torvalds committed
1024 1025 1026 1027 1028 1029 1030
			up_read(&old_mm->mmap_sem);
			return -EINTR;
		}
	}
	task_lock(tsk);
	active_mm = tsk->active_mm;
	tsk->mm = mm;
1031
	ipipe_mm_switch_protect(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033
	tsk->active_mm = mm;
	activate_mm(active_mm, mm);
1034
	ipipe_mm_switch_unprotect(flags);
Davidlohr Bueso's avatar
Davidlohr Bueso committed
1035 1036
	tsk->mm->vmacache_seqnum = 0;
	vmacache_flush(tsk);
Linus Torvalds's avatar
Linus Torvalds committed
1037 1038 1039
	task_unlock(tsk);
	if (old_mm) {
		up_read(&old_mm->mmap_sem);
1040
		BUG_ON(active_mm != old_mm);
1041
		setmax_mm_hiwater_rss(&tsk->signal->maxrss, old_mm);
1042
		mm_update_next_owner(old_mm);
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
		mmput(old_mm);
		return 0;
	}
	mmdrop(active_mm);
	return 0;
}

/*
 * This function makes sure the current process has its own signal table,
 * so that flush_signal_handlers can later reset the handlers without
 * disturbing other processes.  (Other processes might share the signal
 * table via the CLONE_SIGHAND option to clone().)
 */
1056
static int de_thread(struct task_struct *tsk)
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058
{
	struct signal_struct *sig = tsk->signal;
1059
	struct sighand_struct *oldsighand = tsk->sighand;
Linus Torvalds's avatar
Linus Torvalds committed
1060 1061
	spinlock_t *lock = &oldsighand->siglock;

1062
	if (thread_group_empty(tsk))
Linus Torvalds's avatar
Linus Torvalds committed
1063 1064 1065 1066 1067 1068
		goto no_thread_group;

	/*
	 * Kill all other threads in the thread group.
	 */
	spin_lock_irq(lock);
1069
	if (signal_group_exit(sig)) {
Linus Torvalds's avatar
Linus Torvalds committed
1070 1071 1072 1073 1074 1075 1076
		/*
		 * Another group action in progress, just
		 * return so that the signal is processed.
		 */
		spin_unlock_irq(lock);
		return -EAGAIN;
	}
1077

1078
	sig->group_exit_task = tsk;
1079 1080 1081
	sig->notify_count = zap_other_threads(tsk);
	if (!thread_group_leader(tsk))
		sig->notify_count--;
Linus Torvalds's avatar
Linus Torvalds committed
1082

1083
	while (sig->notify_count) {
1084
		__set_current_state(TASK_KILLABLE);
Linus Torvalds's avatar
Linus Torvalds committed
1085 1086
		spin_unlock_irq(lock);
		schedule();
1087 1088
		if (unlikely(__fatal_signal_pending(tsk)))
			goto killed;
Linus Torvalds's avatar
Linus Torvalds committed
1089 1090 1091 1092 1093 1094 1095 1096 1097
		spin_lock_irq(lock);
	}
	spin_unlock_irq(lock);

	/*
	 * At this point all other threads have exited, all we have to
	 * do is to wait for the thread group leader to become inactive,
	 * and to assume its PID:
	 */
1098
	if (!thread_group_leader(tsk)) {
1099
		struct task_struct *leader = tsk->group_leader;
1100 1101

		for (;;) {
1102
			cgroup_threadgroup_change_begin(tsk);
1103
			write_lock_irq(&tasklist_lock);
1104 1105 1106 1107 1108
			/*
			 * Do this under tasklist_lock to ensure that
			 * exit_notify() can't miss ->group_exit_task
			 */
			sig->notify_count = -1;
1109 1110
			if (likely(leader->exit_state))
				break;
1111
			__set_current_state(TASK_KILLABLE);
1112
			write_unlock_irq(&tasklist_lock);
1113
			cgroup_threadgroup_change_end(tsk);
1114
			schedule();
1115 1116
			if (unlikely(__fatal_signal_pending(tsk)))
				goto killed;
1117
		}
Linus Torvalds's avatar
Linus Torvalds committed
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		/*
		 * The only record we have of the real-time age of a
		 * process, regardless of execs it's done, is start_time.
		 * All the past CPU time is accumulated in signal_struct
		 * from sister threads now dead.  But in this non-leader
		 * exec, nothing survives from the original leader thread,
		 * whose birth marks the true age of this process now.
		 * When we take on its identity by switching to its PID, we
		 * also take its birthdate (always earlier than our own).
		 */
1129
		tsk->start_time = leader->start_time;
1130
		tsk->real_start_time = leader->real_start_time;
1131

1132 1133
		BUG_ON(!same_thread_group(leader, tsk));
		BUG_ON(has_group_leader_pid(tsk));
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135 1136 1137 1138 1139
		/*
		 * An exec() starts a new thread group with the
		 * TGID of the previous thread group. Rehash the
		 * two threads with a switched PID, and release
		 * the former thread group leader:
		 */
1140 1141

		/* Become a process group leader with the old leader's pid.
1142